<?php
/* MyTickets [   by = www.phpx3.com   ]
* Faha1ad2@hotmail.com
* Writed By : Fahad S. Al-ghudaier
* All Right Reseved (C) 2010
*/
session_start();
include("Admin_Function.php");
head('rtl','','');
includes();
check_session();

$language			= array();
$language['lang1']	= 'المعذرة ... يوجد خطأ بالمعطيات !!!';
$language['lang2']	= 'لايوجد أي أقسام مضافة ... هل تود <strong><a href="sections.php?step=add">إضافة قسم </a></strong> ?';
$language['lang4']	= 'يجب عليك كتابة اسم للمجموعة';
$language['lang5']	= 'إذا أتحت صلاحية التحكم بالتذاكر , فلا بد من اختيار الأقسام التي يستطيع التحكم بها';

if( $permission['top_man'] == 1){ }else{
	show("no_permission.php");
	down();
	die();
}
if(empty($_GET['step'])){
	redirect('admin.php',0);
	die();
}





if($_GET['step'] == 'add'){
	$what						= 'add';
	$rows['allowed_sections']	= array();
	show('group_add.php');
}


if($_GET['step'] == 'insert'){
	$what	= 'add';
	
	$posted['name']		= $_POST['name'];
	$posted['top_man']	= $_POST['top_man'];
	$posted['allow_cp']	= $_POST['allow_cp'];
	$posted['allow_add']	= $_POST['allow_add'];
	$posted['allow_control']	= $_POST['allow_control'];
	$posted['allow_options']	= $_POST['allow_options'];
	$posted['allow_members']	= $_POST['allow_members'];
	$posted['allow_sections']	= $_POST['allow_sections'];
	$posted['allowed_sections']	=  @implode(',',$_POST['allowed_sections']);
	

	if(empty($posted['name'])){
		echo '<div class="error">'.$language['lang4'].'</div>';
		$rows	= $posted;
		$rows['allowed_sections']			= explode(',',$rows['allowed_sections']);
		show('group_add.php');
		down();
		die();
	}
	if($posted['allow_control'] == 1 AND empty($posted['allowed_sections'])){
		echo '<div class="error">'.$language['lang5'].'</div>';
		$rows	= $posted;
		$rows['allowed_sections']			= explode(',',$rows['allowed_sections']);
		show('group_add.php');
		down();
		die();
	}
	
	$insert	= mysql_query("INSERT INTO `groups` (`name`,`create_date`,`top_man`,`allow_cp`,`allow_add`,
	`allow_control`,`allow_options`,`allow_members`,`allow_sections`,`allowed_sections`) VALUES (
	'".$posted['name']."','".date('l jS \of F Y h:i:s A')."','".$posted['top_man']."',
	'".$posted['allow_cp']."','".$posted['allow_add']."','".$posted['allow_control']."','".$posted['allow_options']."',
	'".$posted['allow_members']."','".$posted['allow_sections']."','".$posted['allowed_sections']."')");
	
	if($insert){
		ok('groups.php?step=control',2);
	}else{
		echo mysql_error();
	}
}


	
if($_GET['step'] == 'edit'){
	$id	= $_GET['id'];
	$what	= 'edit';
	if($id == 1){ 
		echo '<div class="error">هذه المجموعة لايمكن تعديلها أو حذفها !<br />
		This Group is uneditable , you can not edit it or delete it.</div>';
		down();
		die();
	}
	
	if(! id_check($id,'groups')){
		echo '<div class="error">'.$language['lang1'].'</div>';
		down();
		die();
	}
	
	$query						= mysql_query("SELECT * FROM `groups` WHERE `id`='".$id."'");
	$rows						= mysql_fetch_array($query);
	$rows['allowed_sections']	= explode(',',$rows['allowed_sections']);
	
	show('group_add.php');
}





if($_GET['step'] == 'update'){
	$what	= 'edit';
	
	$posted['name']				= $_POST['name'];
	$posted['top_man']			= $_POST['top_man'];
	$posted['allow_cp']			= $_POST['allow_cp'];
	$posted['allow_add']		= $_POST['allow_add'];
	$posted['allow_control']	= $_POST['allow_control'];
	$posted['allow_options']	= $_POST['allow_options'];
	$posted['allow_members']	= $_POST['allow_members'];
	$posted['allow_sections']	= $_POST['allow_sections'];
	$posted['allowed_sections']	=  @implode(',',$_POST['allowed_sections']);

	if(empty($posted['name'])){
		echo '<div class="error">'.$language['lang4'].'</div>';
		$rows	= $posted;
		$rows['allowed_sections']			= explode(',',$rows['allowed_sections']);
		show('group_add.php');
		down();
		die();
	}
	if($posted['allow_control'] == 1 AND empty($posted['allowed_sections'])){
		echo '<div class="error">'.$language['lang5'].'</div>';
		$posted['allowed_sections']	= array();
		$rows	= $posted;
		$rows['allowed_sections']			= explode(',',$rows['allowed_sections']);
		show('group_add.php');
		down();
		die();
	}
	
	$update	= mysql_query("UPDATE `groups` SET
	`name`='".$posted['name']."',`top_man`='".$posted['top_man']."',
	`allow_cp`='".$posted['allow_cp']."',`allow_add`='".$posted['allow_add']."',`allow_control`='".$posted['allow_control']."',
	`allow_options`='".$posted['allow_options']."',
	`allow_members`='".$posted['allow_members']."',`allow_sections`='".$posted['allow_sections']."',`allowed_sections`='".$posted['allowed_sections']."' WHERE `id`='".$id."'");
	
	$to = $_GET['to'];
	if(!isset($to)){
		$to = 'groups.php?step=control';
	}else{
		$to	= replace_to($to);
	}
	
	if($update){
		ok('groups.php?step=control',2);
	}else{
		echo mysql_error();
	}
}






if($_GET['step'] == 'control'){
	$query	= mysql_query("SELECT `id`,`name`,`create_date` FROM `groups` WHERE `id`!='1' ORDER BY `id` DESC");
	show('group_control.php');
}







if($_GET['step'] == 'delete'){
	if($_GET['id'] == 1){ 
		echo '<div class="error">هذه المجموعة لايمكن تعديلها أو حذفها !<br />
		This Group is uneditable , you can not edit it or delete it.</div>';
		down();
		die();
	}
	if(! id_check($_GET['id'],'groups')){
		echo '<div class="error">'.$language['lang1'].'</div>';
		down();
		die();
	}
	
	if(mysql_num_rows(mysql_query("SELECT `id` FROM `members` WHERE `group_id`='".$_GET['id']."'")) > 0){
		$query			= mysql_query("SELECT `id`,`name` FROM `groups` WHERE `id`='".$_GET['id']."'");
		$rows			= mysql_fetch_array($query);

		$othet_groups	= mysql_query("SELECT `id`,`name` FROM `groups` WHERE `id`!='".$_GET['id']."'");
		show('group_do.php');
	}else{
		redirect('groups.php?step=remove&remove_type=2&id='.$_GET['id'].'&to='.$_GET['to'].'',0);
		die();
	}
}





if($_GET['step'] == 'remove'){
	if($_GET['id'] == 1){ 
		echo '<div class="error">هذه المجموعة لايمكن تعديلها أو حذفها !<br />
		This Group is uneditable , you can not edit it or delete it.</div>';
		down();
		die();
	}
	$remove_type	= $_GET['remove_type'];
	$id				= addslashes($_GET['id']);
	if(! id_check($_GET['id'],'groups')){
		echo '<div class="error">'.$language['lang1'].'</div>';
		down();
		die();
	}
	if(empty($remove_type)){
		$remove_type	= 1;
	}
	switch($remove_type){
	case 1:
		$new_group	= $_GET['new_group'];
		if(! id_check($new_group,'groups')){
			echo '<div class="error">'.$language['lang1'].'</div>';
			down();
			die();
		}
		$ok	= 0;
		$query1	= mysql_query("SELECT `id` FROM `members` WHERE `group_id`='".$id."'");
		while($rows	= mysql_fetch_array($query1)){
			$ok	= $ok+1;
			if($ok == '25'){ $ok = 0; sleep(3); }
			$update	= mysql_query("UPDATE `members` SET `group_id`='".$new_group."' WHERE `id`='".$rows['id']."'");
		}
		$remove	= mysql_query("DELETE FROM `groups` WHERE `id`='".$id."'");
		
		
	break;
	case 2:
		$remove		= mysql_query("DELETE FROM `groups` WHERE `id`='".$id."'");
		$remove2	= mysql_query("DELETE FROM `members` WHERE `group_id`='".$id."'");
	break;
	}
	
	if($remove){
		ok('groups.php?step=control',2);
	}else{
		echo mysql_error();
	}
}




down();
?>