#!%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.

# details.pl - Displays details about a tag request and
#              allows moderator to accept or reject it. 

use utf8;
use POSIX qw(locale_h);
use DateTime;
use DateTime::Locale;
use locale;
use Locale::gettext;
use CGI;
use CGI::Session;
use Encode;
use strict;

use lib '%VHFFS_LIB_DIR%';
use Vhffs::Main;
use Vhffs::ObjectFactory;
use Vhffs::Panel::Main;
use Vhffs::Panel::Template;
use Vhffs::Tag;
use Vhffs::Tag::Category;
use Vhffs::Tag::Request;
   
my $panel = new Vhffs::Panel::Main();
exit 0 unless $panel;
my $session = $panel->get_session;
exit 0 unless $session;
my $templatedir = $panel->{'templatedir'};
my $cgi = $panel->{cgi};
my $vhffs = $panel->{vhffs};

$panel->check_modo;

my $request_id;

unless(defined ($request_id = $cgi->param('request_id'))) {
	my $url = '/admin/tag/request/list.pl?error='.gettext('CGI Error');
	$panel->redirect($url);
}

my $request;

unless(defined ($request = Vhffs::Tag::Request::get_by_request_id($vhffs, $request_id))) {
	my $url = '/admin/tag/request/list.pl?error='.gettext('Tag request not found');
	$panel->redirect($url);
}

my $template = new Vhffs::Panel::Template( filename => $templatedir.'/panel/admin/tag/request/details.tmpl', die_on_bad_params => 0 );

if(defined $cgi->param('accept_request_submit')) {
	if(accept_request()) {
		my $url = '/admin/tag/request/list.pl?msg='.gettext('Tag request accepted');
		$panel->redirect($url);
	} else {
		$template->param('CATEGORY' => $cgi->param('category_label'));
		$template->param('CATEGORY_DESCRIPTION' => $cgi->param('category_description'));
		$template->param('TAG' => $cgi->param('tag_label'));
		$template->param('TAG_DESCRIPTION' => $cgi->param('tag_description'));
	}
} elsif(defined $cgi->param('discard_request_submit')) {
	$request->delete();
	my $url = '/admin/tag/request/list.pl?msg='.gettext('Tag request deleted');
	$panel->redirect($url);
} else {
	$template->param('CATEGORY' => $request->{category_label});
	$template->param('TAG' => $request->{tag_label});
}

my $user = $panel->{user};
$panel->set_title( gettext('Tag request details') );
my $loc = DateTime::Locale->load($user->get_lang);

my $dt = DateTime->from_epoch( epoch => $request->{created}, locale => $user->get_lang);

my $tag_requester   = $request->get_requester;
my $requester_param = defined($tag_requester) ? $tag_requester->get_username : gettext('DELETED');

my $tagged_object   = $request->get_tagged;
my $tagged_param    = defined($tagged_object) ? $tagged_object->get_label    : gettext('DELETED');

$template->param('REQUEST_ID' => $request->{request_id});
$template->param('REQUESTER' => $requester_param);
$template->param('TAGGED' => $tagged_param);
$template->param('CREATED' =>  $dt->strftime($loc->medium_date_format().' '.$loc->long_time_format()));
$template->param('CATEGORIES' => Vhffs::Tag::Category::get_all($vhffs));

my $category_id = $cgi->param('category');

if(defined $category_id) {
	$template->param('TAGS' => Vhffs::Tag::get_by_category_id($vhffs, $category_id));
	$template->param('SELECTED_CATEGORY_ID' => $category_id);
} else {
	# Avoid warning in tmpl_expr
	$template->param('SELECTED_CATEGORY_ID' => -1);
}


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


sub accept_request {
	my $category_id = $cgi->param('category');
	my $category_label = $cgi->param('category_label');
	my $tag_id = $cgi->param('tag');
	my $tag_label = $cgi->param('tag_label');
	my $category_description = $cgi->param('category_description');
	my $tag_description = $cgi->param('tag_description');
	
	unless(defined $category_id && defined $category_label && defined $category_description
			&& defined $tag_id && defined $tag_label && defined $tag_description) {
		$panel->add_error(gettext('CGI Error'));
		return 0;
	}
	
	my $category;
	my $tag;
	
	if($category_id < 0) {
		if($tag_id > 0) {
			$panel->add_error( gettext('If you want to create a new category, you have to create a new tag too') );
			return 0;
		}
		
		if($category_label =~ /^\s*$/ || $category_description =~ /^\s*$/) {
			$panel->add_error( gettext('You have to enter a label and a description for the category') );
			return 0;
		}
		
		if($tag_label =~ /^\s*$/ || $tag_description =~ /^\s*$/) {
			$panel->add_error( gettext('You have to enter a label and a description for the tag') );
			return 0;
		}
		
		unless(defined ($category = Vhffs::Tag::Category::create($vhffs, $category_label, $category_description, Vhffs::Constants::TAG_VISIBILITY_PUBLIC, $panel->{user}) ) ) {
			$panel->add_error( gettext('Unable to create category') );
			return 0;
		}
		
		unless(defined ($tag = Vhffs::Tag::create($vhffs, $tag_label, $tag_description, $panel->{user}, $category) ) ) {
			$panel->add_error( gettext('Unable to create tag') );
			$category->delete();
			return 0;
		}
	} else {
		$category = Vhffs::Tag::Category::get_by_category_id($vhffs, $category_id);
		unless(defined $category) {
			$panel->add_error( gettext('Category not found') );
			return 0;
		}
		
		if($tag_id > 0) {
			$tag = Vhffs::Tag::get_by_tag_id($vhffs, $tag_id);
			unless( defined($tag) ) {
				$panel->add_error( gettext('Tag not found') );
				return 0;
			}
		} else {
			if($tag_label =~ /^\s*$/ || $tag_description =~ /^\s*$/) {
				$panel->add_error( gettext('You have to enter a label and a description for the tag') );
				return 0;
			}
			
			$tag = Vhffs::Tag::create($vhffs, $tag_label, $tag_description, $panel->{user}, $category);
			
			unless(defined $tag) {
				$panel->add_error( gettext('Unable to create tag') );
				return 0;
			}
		}
	}
	
	# Adds the tag to the object for which it has
	# been requested.
	
	my $object = $request->get_tagged();
	if(defined $object) {
		$user = $request->get_requester();
		$user = $panel->{user} unless(defined $user);
		$object->add_tag($tag, $user);
	}
	
	$request->delete();
	
	return 1;
}
