#!%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::Object;

use DBI;
use POSIX qw(locale_h);
use HTML::Template;
use locale;
use Locale::gettext;
use CGI;
use CGI::Session;
use Vhffs::User;
use Vhffs::Main;
use Vhffs::Object;
use Vhffs::Constants;

sub search
{
    my ($main, $name) = @_;
    my $sql;
    my @params;
    my $objs = [];

    if( defined $name ) {
        $sql = 'SELECT o.object_id AS oid, u.username, g.groupname, o.type, o.state FROM vhffs_object o INNER JOIN vhffs_users u ON o.owner_uid = u.uid INNER JOIN vhffs_groups g ON o.owner_gid = g.gid WHERE ( o.description ILIKE ? ) OR ( o.object_id = ? ) OR ( o.owner_uid = ? ) OR ( o.owner_gid = ? ) OR ( state = ? ) OR ( u.username LIKE ? ) OR ( g.groupname LIKE ? ) OR ( o.type = ? ) ORDER BY o.object_id';
        push(@params, '%'.$name.'%', $name, $name, $name, $name, '%'.lc($name).'%', '%'.lc($name).'%', $name );
    } else {
        $sql = 'SELECT o.object_id AS oid, u.username, g.groupname, o.type, o.state FROM vhffs_object o INNER JOIN vhffs_users u ON o.owner_uid = u.uid INNER JOIN vhffs_groups g ON o.owner_gid = g.gid ORDER BY o.object_id';
    }
    my $dbh = $main->get_db_object();
    my $sth = $dbh->prepare($sql) or return undef;

    return undef unless($sth->execute(@params));

    while(my $o = $sth->fetchrow_hashref('NAME_lc')) {
        $o->{state} = Vhffs::Functions::status_string_from_status_id($o->{state});
        $o->{type} = Vhffs::Functions::type_string_from_type_id($o->{type});
        push(@$objs, $o);
    }
    return $objs;
}


sub admin_part
{
    use DateTime;
    use DateTime::Locale;
	my $panel = shift;
	my $object = shift;

	return undef unless defined $object;

	my $vhffs = $panel->{vhffs};
	my $templatedir = $panel->{templatedir};
	my $user = $panel->{user};
	
    return undef if( $user->is_admin != 1 );

    my $loc = DateTime::Locale->load($user->get_lang);

	my $template = new HTML::Template( filename => $templatedir.'/panel/admin/object/editembedded.tmpl' );

	$template->param( TITLE => gettext("Admin part") );

	$template->param( TEXT_OID => gettext("Object id") );
	$template->param( TEXT_UID => gettext("UID of owner") );
	$template->param( TEXT_DATE => gettext("Date of creation") );
	$template->param( TEXT_STATUS => gettext("Status") );
	$template->param( TEXT_DESCRIPTION => gettext("Description") );
	$template->param( SEND => gettext("Send") );
	

	$template->param( VALUE_DESCRIPTION => $object->get_description );
	$template->param( VALUE_OID => $object->get_oid );
	$template->param( VALUE_UID => $object->get_owner_uid );
	$template->param( VALUE_USERNAME => Vhffs::User::get_name_by_uid( $vhffs , $object->get_owner_uid ));

	if( defined $object->get_date )
	{
        my $dt = DateTime->from_epoch( epoch => $object->get_date, locale => $user->get_lang);
        $template->param( VALUE_DATE => $dt->strftime($loc->full_date_format.' '.$loc->full_time_format) );
	}
	else
	{
		$template->param( VALUE_DATE => gettext("no date") );
	}

	$template->param( TEXT_STATUS_WAITING_FOR_VALIDATION => gettext("Waiting for validation") );
	$template->param( TEXT_STATUS_VALIDATION_REFUSED => gettext("Validation refused") );
	$template->param( TEXT_STATUS_WAITING_FOR_CREATION => gettext("Waiting for creation") );
	$template->param( TEXT_STATUS_CREATING_ERROR => gettext("Creating error") );
	$template->param( TEXT_STATUS_ACTIVATED => gettext("Activated") );
	$template->param( TEXT_STATUS_CREATED => gettext("Created") );
	$template->param( TEXT_STATUS_SUSPEND_PENDING => gettext("Waiting for suspension") );
	$template->param( TEXT_STATUS_SUSPENDED => gettext("Suspended") );
	$template->param( TEXT_STATUS_WAITING_FOR_MODIFICATION => gettext("Waiting for modification") );
	$template->param( TEXT_STATUS_MODIFICATION_ERROR => gettext("Modification error") );
	$template->param( TEXT_STATUS_MODIFICATION_APPLIED => gettext("Modification applied") );
	$template->param( TEXT_STATUS_TO_DELETE => gettext("Will be deleted") );

	$template->param( VALUE_STATUS_WAITING_FOR_VALIDATION => Vhffs::Constants::WAITING_FOR_VALIDATION );
	$template->param( VALUE_STATUS_VALIDATION_REFUSED => Vhffs::Constants::VALIDATION_REFUSED );
	$template->param( VALUE_STATUS_WAITING_FOR_CREATION => Vhffs::Constants::WAITING_FOR_CREATION );
	$template->param( VALUE_STATUS_CREATING_ERROR => Vhffs::Constants::CREATING_ERROR );
	$template->param( VALUE_STATUS_ACTIVATED => Vhffs::Constants::ACTIVATED );
	$template->param( VALUE_STATUS_CREATED => Vhffs::Constants::CREATED );
	$template->param( VALUE_STATUS_SUSPEND_PENDING => Vhffs::Constants::SUSPEND_PENDING );
	$template->param( VALUE_STATUS_SUSPENDED => Vhffs::Constants::SUSPENDED );
	$template->param( VALUE_STATUS_WAITING_FOR_MODIFICATION => Vhffs::Constants::WAITING_FOR_MODIFICATION );
	$template->param( VALUE_STATUS_MODIFICATION_ERROR => Vhffs::Constants::MODIFICATION_ERROR );
	$template->param( VALUE_STATUS_MODIFICATION_APPLIED => Vhffs::Constants::MODIFICATION_APPLIED );
	$template->param( VALUE_STATUS_TO_DELETE => Vhffs::Constants::TO_DELETE );

	my $status = $object->get_status;

	if( $status == Vhffs::Constants::WAITING_FOR_VALIDATION )
	{
		$template->param( SELECTED_STATUS_WAITING_FOR_VALIDATION => "selected" );
	}
	elsif( $status == Vhffs::Constants::VALIDATION_REFUSED )
	{
		$template->param( SELECTED_STATUS_VALIDATION_REFUSED => "selected" );
	}
	elsif( $status == Vhffs::Constants::WAITING_FOR_CREATION )
	{
		$template->param( SELECTED_STATUS_WAITING_FOR_CREATION => "selected" );
	}
	elsif( $status == Vhffs::Constants::CREATING_ERROR )
	{
		$template->param( SELECTED_STATUS_CREATING_ERROR => "selected" );
	}
	elsif( $status == Vhffs::Constants::CREATED )
	{
		$template->param( SELECTED_STATUS_CREATED => "selected" );
	}
	elsif( $status == Vhffs::Constants::ACTIVATED )
	{
		$template->param( SELECTED_STATUS_ACTIVATED => "selected" );
	}
	elsif( $status == Vhffs::Constants::SUSPEND_PENDING )
	{
		$template->param( SELECTED_STATUS_SUSPEND_PENDING => "selected" );
	}
	elsif( $status == Vhffs::Constants::SUSPENDED )
	{
		$template->param( SELECTED_STATUS_SUSPENDED => "selected" );
	}
	elsif( $status == Vhffs::Constants::WAITING_FOR_MODIFICATION )
	{
		$template->param( SELECTED_STATUS_WAITING_FOR_MODIFICATION => "selected" );
	}
	elsif( $status == Vhffs::Constants::MODIFICATION_ERROR )
	{
		$template->param( SELECTED_STATUS_MODIFICATION_ERROR => "selected" );
	}
	elsif( $status == Vhffs::Constants::MODIFICATION_APPLIED )
	{
		$template->param( SELECTED_STATUS_MODIFICATION_APPLIED => "selected" );
	}
	elsif( $status == Vhffs::Constants::TO_DELETE )
	{
		$template->param( SELECTED_STATUS_TO_DELETE => "selected" );
	}


	$template->param( AVATAR_TITLE => gettext( "Avatar management" ) );
	$template->param( DELETE_AVATAR => gettext( "Delete avatar for this object" ) );

	# Print history
	$template->param( HISTORY_TITLE => gettext("History of this object") );
	my $string;
	my $history = $object->get_history;

    unless( @{$history} ) {
        $template->param( HISTORY_PART => gettext('No event about this object') );
    } else {
        my $output = '';
        my $history_tmpl = new HTML::Template( filename => $templatedir.'/panel/misc/history_part.tmpl' );
        foreach (@{$history}) {
            my $dt = DateTime->from_epoch( epoch => $_->{date}, locale => $user->get_lang);
            $history_tmpl->param( DATE => $dt->strftime($loc->medium_date_format().' '.$loc->long_time_format() ) );
            $history_tmpl->param( EVENT => $_->{message} );
            $history_tmpl->param( SOURCE => $_->{source} );
            $history_tmpl->param( MODERATOR => $user->is_admin || $user->is_moderator );
            $output .= $history_tmpl->output;
        }
        $template->param( HISTORY_PART => $output );
    }

	return $template;
}


1;
