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

use lib '%VHFFS_LIB_DIR%';
use Vhffs::Main;
use Vhffs::Panel::Main;
use Vhffs::Panel::Group;
use Vhffs::Panel::Template;
use Vhffs::Tag;
use Vhffs::Tag::Category;
use Vhffs::User;

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 = $vhffs->get_config->get_templatedir;
my $submitted = defined($cgi->param('project_submit'));
my $groupname = $cgi->param('project_name');
my $realname = Encode::decode_utf8( $cgi->param('realname') );
my $description = Encode::decode_utf8( $cgi->param('description') );

if($submitted) {
    # User posted the form, let's check it
    if( $groupname !~ /^[a-z0-9]{3,12}$/ ) {
        $panel->add_error( gettext('Groupname must contain between 3 and 12 characters, only letters or numbers in lower case') );
    } elsif( $description =~ /^\s*$/ ) {
        $panel->add_error( gettext('You must enter a description') );
    } elsif( $realname =~ /^\s*$/ ) {
        $panel->add_error( gettext('You must enter a full name') );
    } elsif( substr($groupname,0,1) ne lc substr($realname,0,1) ) {
        $panel->add_error( gettext('The first letter of groupname and full name must be the same') );
    } else {
    	my $group;
   	    if( ! defined($group = Vhffs::Panel::Group::create_group( $groupname , $realname, $user , $vhffs, $description ) ) ) {
        	$panel->add_error( gettext('Error creating group (maybe a group with the same name already exists)') );
    	} else {
    		# Creation succeeded. Since we don't care about the correctness
    		# of the tags we were passed, we don't do display any error messages
    		my @tags = $cgi->param('tags');
    		foreach my $tag_id (@tags) {
    			my $tag = Vhffs::Tag::get_by_tag_id($vhffs, $tag_id);
    			if(defined $tag && $tag->get_category()->{visibility} == Vhffs::Constants::TAG_VISIBILITY_GROUP_CREATION) {
    				$group->add_tag($tag, $user);
    			}
    		}
        	my $url = '/group/index.pl?msg='.gettext('Project Successfully created !');
        	$panel->redirect($url);
    	}
    }
}

if( ! $submitted || $panel->has_errors() ) {
    my $template = new Vhffs::Panel::Template( filename => $templatedir.'/panel/group/create.tmpl', die_on_bad_params => 0, loop_context_vars => 1 );

    $panel->set_title( gettext('Create a Project') );
    $template->param( PROJECT_NAME => gettext('Project Name') );
    $template->param( PROJECT_NAME_EXPLAIN => gettext('with only lowercase alphanumerical letters, no space, from 3 to 12 characters') );
    $template->param( REALNAME_TEXT => gettext('Project full name') );
    $template->param( TEXT_OWNER => gettext('Owner') );
    $template->param( OWNER => $user->get_username );
    $template->param( SEND => gettext('Send') );
    $template->param( DESCRIPTION => gettext('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;
    $template->param( PROJECT_VALUE => $groupname );
    $template->param( REALNAME_VALUE => $realname );
    $template->param( DESCRIPTION_VALUE => $description );
    
    my $categories =
		Vhffs::Tag::Category::get_all($vhffs, Vhffs::Constants::TAG_VISIBILITY_GROUP_CREATION);
	foreach my $c (@{$categories}) {
		$c->{tags} = Vhffs::Tag::get_by_category_id($vhffs, $c->{category_id});
	}
    
    $template->param( TAG_CATEGORIES => $categories );

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