#!%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::Main;
use Vhffs::Panel::Main;
use Vhffs::Panel::Menu;
use Vhffs::Panel::Group;
use Vhffs::Constants;
use Vhffs::Object;
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 $templatedir = $panel->{templatedir};
my $user = $panel->{user};
my $cgi = $panel->{cgi};

my $oid = $cgi->param('oid');
$panel->add_error( gettext('CGI Error !') ) unless defined $oid;

my $object;
if( defined $oid )  {
	$object = Vhffs::ObjectFactory::fetch_object( $vhffs , $oid );
	$panel->add_error( gettext('This object does not exist') ) unless defined $object;
}

if( defined $object  &&  $object->get_status != Vhffs::Constants::VALIDATION_REFUSED )  {
	$panel->add_error( gettext('This object is not in refused state') );
	undef $object;
}

if( defined $object  &&  $object->get_owner_uid != $user->get_uid )  {
	$panel->add_error( gettext('You are not allowed to do it, you don\'t own this object') );
	undef $object;
}

my $submitted = $cgi->param('submitted');

if( $submitted && $object ) {

	my $description = Encode::decode_utf8( $cgi->param('description') );
	unless( defined $description ) {
		$panel->add_error( gettext('CGI Error !') );
	}
	elsif( $description =~ /^\s*$/ ) {
		$panel->add_error( gettext('You must enter a description') );
	}
	elsif ( $object->resubmit_for_moderation( $description ) )  {
		my $url;
		if( $object->get_type == Vhffs::Constants::TYPE_GROUP ) {
			$url = '/group/index.pl?msg='.gettext('The new description has been submitted');
		}
		else {
			my $group = Vhffs::Group::get_by_gid( $vhffs , $object->get_owner_gid );
			$url = '/group/view.pl?group='.$group->get_groupname.'&msg='.gettext('The new description has been submitted');
		}
		$panel->redirect( $url );
	}
	else {
		$panel->add_error( gettext('An error occured while updating this object.') );
	}
}


$panel->set_title( gettext('Propose a new description') );

if( $panel->has_errors ) {

	$panel->build;
	$panel->display;
}
elsif( !$submitted ) {

	my $template = new HTML::Template( filename => $templatedir.'/panel/misc/resubmit.tmpl' );

	$template->param( OBJECTTYPE_TEXT => gettext('Object type') );
	$template->param( OBJECTTYPE => Vhffs::Functions::type_string_from_type_id( $object->get_type ) );

	$template->param( OBJECTNAME_TEXT => gettext('Object name') );
	$template->param( OBJECTNAME => $object->get_label );

	$template->param( REFUSEREASON_TEXT => gettext('Reason of refusal') );
	$template->param( REFUSEREASON => $object->get_refuse_reason );

	$template->param( DESCRIPTION_TEXT => gettext('Description') );
	$template->param( DESCRIPTION => $object->get_description );

	$template->param( WARNING_PUBLIC => gettext("Please consider that this description is going to be displayed in the public area. So you have to write it in impersonal form. You should take care to write it with correct grammar and tenses. Take all the time you need to fill it with the best content you are able to do.") ) if( $panel->is_public  &&  ( $object->get_type == Vhffs::Constants::TYPE_GROUP  ||  $object->get_type == Vhffs::Constants::TYPE_WEB ) );

	$template->param( OID => $object->get_oid );
	$template->param( SEND => gettext('Submit') );

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