#!%PERL% -w
# 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 utf8;
use POSIX qw(locale_h);
use HTML::Template;
use locale;
use Locale::gettext;
use CGI;
use CGI::Session;
use strict;

use lib '%VHFFS_LIB_DIR%';
use Vhffs::User;
use Vhffs::Group;
use Vhffs::Main;
use Vhffs::Panel::Main;
use Vhffs::Panel::Group;
use Vhffs::Functions;
use Vhffs::ObjectFactory;

my $panel = new Vhffs::Panel::Main();
exit 0 unless $panel;
my $session = $panel->get_session;
exit 0 unless $session;

my $vhffs = $panel->{'vhffs'};
my $user = $panel->{'user'};
my $cgi = $panel->{'cgi'};
my $templatedir = $panel->{'templatedir'};
my $template;

my $group = Vhffs::Group::get_by_groupname( $vhffs , $cgi->param('group') );

unless( defined $group ) {
	$template = new HTML::Template( filename => $templatedir.'/panel/misc/simplemsg.tmpl' );
	$template->param( MESSAGE => gettext( 'Error. This group doesn\'t exists') );
} elsif( $group->get_status != Vhffs::Constants::ACTIVATED ) {
	$template = new HTML::Template( filename => $templatedir.'/panel/misc/simplemsg.tmpl' );
	$template->param( MESSAGE => gettext( 'This object is not functional yet. Please wait creation or moderation.') );
} elsif( ! $user->can_view( $group ) ) {
	$template = new HTML::Template( filename => $templatedir.'/panel/misc/simplemsg.tmpl' );
	$template->param( MESSAGE => gettext( 'You\'re not allowed to do this (ACL rights)' ) );
} else {
	my $history = $group->get_full_history;

	$panel->set_title( gettext('Project History') );
	$template = new HTML::Template( filename => $templatedir.'/panel/group/history.tmpl', global_vars => 1 );
	$template->param( HDATE => gettext('Date') );
	$template->param( HTYPE => gettext('Type') );
	$template->param( HNAME => gettext('Name') );
	$template->param( HEVENT => gettext('Event') );
        $template->param( MODERATOR => $user->is_moderator || $user->is_admin );

	unless(@{$history}) {
		$template->param( HISTORY_PART => gettext('No event about this group') );
	} else {
        my $output = '';
        my $history_tmpl = new HTML::Template( filename => $templatedir.'/panel/group/history_part.tmpl' );
        use DateTime;
        use DateTime::Locale;
        my $loc = DateTime::Locale->load($user->get_lang);
        foreach (@{$history}) {
            my $dt = DateTime->from_epoch( epoch => $_->{date}, locale => $user->get_lang);
            $history_tmpl->param( DATE => $dt->strftime($loc->medium_date_format() ) );
            $history_tmpl->param( TIME => $dt->strftime($loc->long_time_format() )  );
            $history_tmpl->param( TYPE => Vhffs::Functions::type_string_from_type_id( $_->{type} ) );
            my $object = Vhffs::ObjectFactory::fetch_object( $vhffs , $_->{object_id} );
            $history_tmpl->param( NAME => $object->get_label );
            $history_tmpl->param( EVENT => $_->{message} );
            $history_tmpl->param( MODERATOR => $user->is_moderator || $user->is_admin );
            $history_tmpl->param( SOURCE => $_->{source} );

            $output .= $history_tmpl->output;
        }
		$template->param( HISTORY_PART => $output );
	}
}

$panel->build( $template );
$panel->display;
