<?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="services.php?step=add">إضافة خدمات </a></strong> ?';
if($permission['allow_services'] == 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';
	show('service_add.php');
}




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




if($_GET['step'] == 'insert'){
	if(empty($_POST)){ die(); }
	$post['title']			= $_POST['title'];
	$post['desc']			= $_POST['desc'];
	$post['text']			= nl2br($_POST['text']);
	$post['text']			= str_replace(array('<br>','<br />'),'
',$post['text']);
	
	if(empty($post['title'])){
		$rows						= $post;
		$what						= 'add';
		show('service_add.php');
		down();
		die();
	}
	
	
	$insert	= mysql_query("INSERT INTO `services` (`title`,`desc`,`text`)
	VALUES ('".$post['title']."','".$post['desc']."','".$post['text']."')");
	
	if($insert){
		ok('services.php?step=control',2);
	}else{
		echo mysql_error();
		die();
	}
}






if($_GET['step'] == 'update'){
	if(! id_check($_GET['id'],'services')){
		echo '<div class="error">'.$language['lang1'].'</div>';
		down();
		die();
	}
	$id					= intval($_GET['id']);
	$post['title']		= $_POST['title'];
	$post['desc']		= $_POST['desc'];
	$post['text']		= nl2br($_POST['text']);
	$post['text']		= str_replace(array('<br>','<br />'),'
',$post['text']);
	
	if(empty($post['title'])){
		$rows						= $post;
		$what						= 'edit';
		show('service_add.php');
		down();
		die();
	}
	
	
	$update	= $db->update('services',array('title','desc','text'),array($post['title'],$post['desc'],$post['text']),$id);
	 
	if(empty($to)){
		$to	= 'services.php?step=control';
	}else{
		$to	= replace_to($to);
	}
	
	if($update){
		ok($to,2);
	}else{
		echo mysql_error();
		die();
	}
}





if($_GET['step'] == 'control'){
	$query	= $db->query("SELECT * FROM `services` ORDER BY `id` ASC");
	$num	= $db->count('services','');
	
	if($num == 0){
		echo '<div class="error">',$language['lang2'],'</div>';
		down();
		die();
	}
	$to	= 'services.php?step=control';
	show('service_control.php');
	
}
		
		
		
		
		
if($_GET['step'] == 'delete'){
	if(! id_check($_GET['id'],'services')){
		echo '<div class="error">'.$language['lang1'].'</div>';
		down();
		die();
	}
	
	$delete	= $db->query("DELETE FROM `services` WHERE `id`='".intval($_GET['id'])."'");
	$delete	= $db->query("DELETE FROM `linked_services` WHERE `service_id`='".intval($_GET['id'])."'");
	
	if($delete){
		ok('services.php?step=control');
	}else{
		die('error !'.mysql_error());
	}
}


if($_GET['step'] == 'link'){
	if(! id_check($_GET['user_id'],'members')){
		echo '<div class="error">'.$language['lang1'].'</div>';
		down();
		die();
	}
	
	$post['service_id']	= $_POST['service_id'];
	$post['user_id']	= intval($_GET['user_id']);
	$post['text']		= nl2br($_POST['text']);
	
	$insert	= $db->insert('linked_services',array('user_id','service_id','text'),array($post['service_id'],$post['user_id'],$post['text']));
	if($insert){
	$to	= addslashes($_GET['to']);
	if(empty($to)){
		$to	= 'services.php?step=control';
	}else{
		$to	= replace_to($to);
	}
		ok($to,2);
	}else{
		echo mysql_error();
		die();
	}
}






if($_GET['step'] == 'unlink'){
	if(! id_check($_GET['id'],'linked_services')){
		echo '<div class="error">'.$language['lang1'].'</div>';
		down();
		die();
	}
	
	$linked_services	= $_GET['id'];
	
	$delete	= $db->query("DELETE FROM `linked_services` WHERE `id`='".$linked_services."'");
	if($delete){
	$to	= addslashes($_GET['to']);
	if(empty($to)){
		$to	= 'services.php?step=control';
	}else{
		$to	= replace_to($to);
	}
		ok($to,2);
	}else{
		echo mysql_error();
		die();
	}
}
	

down();
?>