#!%PERL%
# Copyright (c) vhffs project and its contributors
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without 
# modification, are permitted provided that the following conditions 
# are met:
#
# 1. Redistributions of source code must retain the above copyright 
#   notice, this list of conditions and the following disclaimer.
#2. Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in 
#   the documentation and/or other materials provided with the 
#   distribution.
#3. Neither the name of vhffs nor the names of its contributors 
#   may be used to endorse or promote products derived from this 
#   software without specific prior written permission.
#
#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
#"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
#LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
#FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
#COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
#INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
#BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
#LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
#CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
# POSSIBILITY OF SUCH DAMAGE.

use strict;
use utf8;

package Vhffs::Panel::Public;

use base qw(Vhffs::Panel::Main);

use locale;
use Locale::gettext;
use POSIX qw(locale_h);

use Template;

use lib '%VHFFS_LIB_DIR%';
use Vhffs::Tag;
use Vhffs::Functions;

sub new {
	my $class = shift;
	my $panel = $class->SUPER::new(@_);
	$panel->check_public();
	return $panel;
}

sub render {
	my ($self, $file, $vars) = @_;
	my $conf = $self->{vhffs}->get_config;
	
	# TODO Should be in parent class when Template::Toolkit switch is over
	my $create_vars = {
		INCLUDE_PATH => $self->{templatedir}.'/public/',
		FILTERS => {
			i18n => \&gettext,
			mail => sub {
				return Vhffs::Functions::obfuscate_email($self->{vhffs}, $_[0]);
			},
			# Format filter accept only one argument
			# pretty_print can 'sprintf' anything, use it as
			# [% '%s is $%d' | pretty_print(article, price)]
			pretty_print => [sub {
				my $context = shift;
				my @args = @_;
				return sub {
					my $format = shift;
					return sprintf($format, @args);
				}
			}, 1]
		}
	};
	
	
	$vars = {} unless(defined $vars);
	
	$vars->{theme} = $conf->get_panel->{default_theme} unless(defined $vars->{theme});
	$vars->{left} = 'parts/left-menu.tt' unless(defined $vars->{left});
	$vars->{right} = 'parts/tags-cloud.tt' unless(defined $vars->{right});
	$vars->{top} = 'parts/top-menu.tt' unless(defined $vars->{top});
	$vars->{panel_url} = $conf->get_panel->{url};
	$vars->{help_url} = $conf->get_panel->{url_help};
	$vars->{users_avatar} = Vhffs::Functions::strtobool( $conf->get_panel->{'users_avatars'} );
	$vars->{groups_avatar} = Vhffs::Functions::strtobool( $conf->get_panel->{'groups_avatars'} );
	
	# Handling ajax stuff
	if($self->{is_ajax_request}) {
		
	} else {
		$create_vars->{PROCESS} = 'layouts/public.tt';
		$vars->{popular_tags} = Vhffs::Tag::get_most_popular_tags($self->{vhffs});
		$vars->{random_tags} = Vhffs::Tag::get_random_tags($self->{vhffs});
	}
	
	my $template = new Template($create_vars);

	binmode STDOUT , ':utf8';	
	
	print CGI->header( -cookie=>[ @{$self->{cookies}} ], -type=>'text/html', -charset=>'utf-8' );

	$template->process($file, $vars) 
		|| die('Error while processing template: '.$template->error());
	
}

1;
