<?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> ?';
if($permission['allow_sections'] == 1 OR $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';
	querys();
	$rows['groups_ids']		= array();
	show('section_add.php');
}




if($_GET['step'] == 'edit'){
	if(! id_check($_GET['id'],'section')){
		echo '<div class="error">'.$language['lang1'].'</div>';
		down();
		die();
	}
	
	$id			= $_GET['id'];
	$what		= 'edit';
	querys();
	$query		= mysql_query("SELECT * FROM `section` WHERE `id`='".$id."'");
	$rows		= mysql_fetch_array($query);
	$rows['groups_ids']		= explode(',',$rows['groups_ids']);
	show('section_add.php');
}




if($_GET['step'] == 'insert'){
	if(empty($_POST)){ die(); }
	$post['title']			= $_POST['title'];
	$post['desc']			= $_POST['desc'];
	$post['ofm']			= $_POST['only_for_members'];
	$post['groups_ids']		= @implode(',',$_POST['groups_ids']);
	$post['language_id']	= $_POST['language_id'];
	$post['level']			= intval($_POST['level']);
	
	if(empty($post['title']) OR empty($_POST['groups_ids'])){
		$post['only_for_members']	= $post['ofm'];
		$rows						= $post;
		$what						= 'add';
		querys();
		$rows['groups_ids']			= explode(',',$rows['groups_ids']);
		show('section_add.php');
		down();
		die();
	}
	
	
	$insert	= mysql_query("INSERT INTO `section` (`title`,`desc`,`only_for_members`,`groups_ids`,`language_id`,`user_id`,`level`)
	VALUES ('".$post['title']."','".$post['desc']."','".$post['ofm']."',
	'".$post['groups_ids']."','".$post['language_id']."','".$_SESSION['member_id']."','".$post['level']."')");
	
	if($insert){
		ok('sections.php?step=control',2);
	}else{
		echo mysql_error();
		die();
	}
}






if($_GET['step'] == 'update'){
	if(! id_check($_GET['id'],'section')){
		echo '<div class="error">'.$language['lang1'].'</div>';
		down();
		die();
	}
	$id					= $_GET['id'];
	$post['title']		= $_POST['title'];
	$post['desc']		= $_POST['desc'];
	$post['ofm']		= $_POST['only_for_members'];
	$post['groups_ids']	= @implode(',',$_POST['groups_ids']);
	$post['language_id']	= $_POST['language_id'];
	$post['level']		= intval($_POST['level']);
	
	if(empty($post['title']) OR empty($_POST['groups_ids'])){
		$post['only_for_members']	= $post['ofm'];
		$rows						= $post;
		$what						= 'edit';
		querys();
		$rows['groups_ids']			= explode(',',$rows['groups_ids']);
		show('section_add.php');
		down();
		die();
	}
	
	
	$update	= mysql_query("UPDATE `section` SET `title`='".$post['title']."',`desc`='".$post['desc']."',
	`only_for_members`='".$post['ofm']."',`groups_ids`='".$post['groups_ids']."',`language_id`='".$post['language_id']."',`level`='".$post['level']."',`user_id`='".$_SESSION['member_id']."'
	 WHERE `id`='".$id."'");
	 
	if(empty($to)){
		$to	= 'sections.php?step=control';
	}else{
		$to	= replace_to($to);
	}
	
	if($update){
		ok($to,2);
	}else{
		echo mysql_error();
		die();
	}
}





if($_GET['step'] == 'control'){
	$query	= mysql_query("SELECT * FROM `section` ORDER BY `level` ASC");
	$num	= mysql_num_rows($query);
	
	if($num == 0){
		echo '<div class="error">',$language['lang2'],'</div>';
		down();
		die();
	}
	$to	= 'sections.php?step=control';
	show('section_control.php');
	
}
		
		
		
		
		
if($_GET['step'] == 'delete'){
	if(! id_check($_GET['id'],'section')){
		echo '<div class="error">'.$language['lang1'].'</div>';
		down();
		die();
	}
	
	if(mysql_num_rows(mysql_query("SELECT `id` FROM `tickets` WHERE `section_id`='".$_GET['id']."'")) > 0){
		$query			= mysql_query("SELECT `id`,`title` FROM `section` WHERE `id`='".$_GET['id']."'");
		$rows			= mysql_fetch_array($query);

		$othet_sections	= mysql_query("SELECT `id`,`title` FROM `section` WHERE `id`!='".$_GET['id']."'");
		show('section_do.php');
	}else{
		redirect('sections.php?step=remove&remove_type=2&id='.$_GET['id'].'&to='.$_GET['to'].'',0);
		die();
	}
}




if($_GET['step'] == 'remove'){
	$remove_type	= $_GET['remove_type'];
	$id				= addslashes($_GET['id']);
	if(! id_check($_GET['id'],'section')){
		echo '<div class="error">'.$language['lang1'].'</div>';
		down();
		die();
	}
	if(empty($remove_type)){
		$remove_type	= 1;
	}
	switch($remove_type){
	case 1:
		$new_section	= $_GET['new_section'];
		if(! id_check($new_section,'section')){
			echo '<div class="error">'.$language['lang1'].'</div>';
			down();
			die();
		}
		$ok	= 0;
		$query1	= mysql_query("SELECT `id` FROM `tickets` WHERE `section_id`='".$id."'");
		while($rows	= mysql_fetch_array($query1)){
			$ok	= $ok+1;
			if($ok == '25'){ $ok = 0; sleep(3); }
			$update	= mysql_query("UPDATE `tickets` SET `section_id`='".$new_section."' WHERE `id`='".$rows['id']."'");
		}
		$remove	= mysql_query("DELETE FROM `section` WHERE `id`='".$id."'");
		
		
	break;
	case 2:
		$remove		= mysql_query("DELETE FROM `section` WHERE `id`='".$id."'");
		$remove2	= mysql_query("DELETE FROM `tickets` WHERE `section_id`='".$id."'");
	break;
	}
	
	if($remove){
		ok('sections.php?step=control',2);
	}else{
		echo mysql_error();
	}
}

down();
?>