<?php

// -----------------------------------------------------------------------
// This file is part of aroundme
// 
// Copyright (C) 2003, 2005 Barnraiser
// http://www.barnraiser.org/
// info@barnraiser.org
// 
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2, or (at your option) any
// later version.
// 
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with program; see the file COPYING. If not, write to the Free
// Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.
// -----------------------------------------------------------------------

include "inc/header_group.inc.php";

//ACS check
if (!$user_acs->checkPermission($c_acs['arr_acs']['resource']['group'], $c_acs['arr_acs']['privilege']['allow_create'])) { //resourece_id, Priv_id
	header("Location: " . $_SESSION['s_homepage'] . "?" . SID);
	exit;
}

//append language setup
require_once($languageTextPath . 'group_setup_rules.lang.php');
require_once($languageArrayPath . 'arr_language_name.lang.php');
require_once($languageArrayPath . 'arr_genre.lang.php');
require_once($languageArrayPath . 'arr_group.lang.php');
require_once($languageArrayPath . 'arr_acs.lang.php');


if (isset($_POST['create_group'])) {
	$output_group = array();
	$output_group['group_visibility'] = $data['group_visibility'];

	$display = "group_setup";
}
elseif (isset($_POST['insert_group'])) {
	if(formCheck($data)) {
		//setup Acs
		$group_acs = new Acs(null, $db, $c_acs['arr_section']['group']);
	
		//append data
		if ($data['group_visibility'] != 1) {//hidden group
		    $data['group_rules'] = $lang['txt_group_hidden_rules'];
		}
		else {
			$data['group_rules'] = $lang['txt_group_visible_rules'];
		}
		
		$user->user_id = $me['user_id'];
		$output_profile = $user->getProfile();
		
		//setup group
		require_once($c_node['dir']['class_path'] . 'GroupAdmin.class.php');
		$group = new GroupAdmin($db, $me);
		$group->lang = $lang;
		$group->im = $im;
		$group->im->user_id = $me['user_id'];
		$group->default_acs_ids = $c_acs;
		$group->default_acs_role_names = $lang['arr_acs']['role'];
		$group->group_file_allocation = $c_node['file']['default_max_group_space'];
		$group->acs = $group_acs;
		$group->status_ids = $c_node['group']['status'];
		$group->member_status_ids = $c_node['group']['member_status'];
		$group->setup = $c_node['group_setup'];
		$group->default_library_title = $lang['txt_default_library_title'];
		$group->default_library_synopsis = $lang['txt_default_library_synopsis'];
		$group->default_library_body = $lang['txt_default_library_body'];
		$group->default_section_title = $lang['txt_default_section_title'];
		$group->admin_message_text = readTxtFile($languageTextPath . "admin_group_create.txt.php");
		$group->node_id = $c_node['node']['node_id'];
		$group->data = $data;
		$group->profile_id = $output_profile['profile_id'];
		$make_group = $group->createGroup();
		
		if (isset($make_group) && $make_group == 1) {
		    $display = "completed_pending";
		}
		elseif (isset($make_group) && $make_group == 2) {
			$body->set('group_id', $group->group_id);
		    $display = "completed";
		}
		else {
			$display = "error";
		}
		
	}
	else { //formCheck failed
		$output_group = $data;
		$display = "group_setup";
		
	}
	
}
else {
	$display = "group_select";
}


//move to template
if (isset($display)) {
	$body->set('display', $display);
}

if (!empty($output_group)) {
	$body->set('group', $output_group);
}

$body->set('group_registration_type', $c_node['group']['registration_type']);
$body->set('group_setup', $c_node['group_setup']);
$body->set('language_id', $me['language_id']);

include "inc/footer.inc.php";



// formCheck --------------------------------------------------------------------
// checks the form against given fields
// tom calthrop - 9th December 2004
//
function formCheck($data) {
	
	if (empty($data['group_title'])) {
	    $GLOBALS['am_error_log'][] = array(11, null, null);
	}
	
	if (empty($data['group_synopsis'])) {
	    $GLOBALS['am_error_log'][] = array(30, null, null);
	}
	
	if (empty($GLOBALS['am_error_log'])) {
	    return true;
	}
}//EO formCheck

?>
