<?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="pages.php?step=add">إضافة صفحة </a></strong> ?';
if($permission['allow_pages'] == 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['languages_ids']		= array();
	show('page_add.php');
}




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




if($_GET['step'] == 'insert'){
	if(empty($_POST)){ die(); }
	$post['title']			= $_POST['title'];
	$post['desc']			= $_POST['desc'];
	$post['content']		= $_POST['content'];
	$post['languages_ids']	= @implode(',',$_POST['languages_ids']);
	$post['group_id']		= $_POST['group_id'];
	$post['meta_open']		= $_POST['meta_open'];
	$post['meta_desc']		= $_POST['meta_desc'];
	$post['date']			= date('d-m-Y');	
	
	if(empty($post['title']) OR empty($_POST['content'])){
		$rows						= $post;
		$what						= 'add';
		querys();
		$rows['languages_ids']		= explode(',',$rows['languages_ids']);
		show('page_add.php');
		down();
		die();
	}
	
	
	$insert	= mysql_query("INSERT INTO `pages` (`title`,`desc`,`content`,`group_id`,`languages_ids`,`meta_open`,`meta_desc`,`date`)
	VALUES ('".$post['title']."','".$post['desc']."','".$post['content']."',
	'".$post['group_id']."','".$post['languages_ids']."','".$post['meta_ioen']."','".$post['meta_desc']."','".$post['date']."')");
	
	if($insert){
		ok('pages.php?step=control',2);
	}else{
		echo mysql_error();
		die();
	}
}






if($_GET['step'] == 'update'){
	if(! id_check($_GET['id'],'pages')){
		echo '<div class="error">'.$language['lang1'].'</div>';
		down();
		die();
	}
	$id					= $_GET['id'];
	$post['title']			= $_POST['title'];
	$post['desc']			= $_POST['desc'];
	$post['content']		= $_POST['content'];
	$post['languages_ids']	= @implode(',',$_POST['languages_ids']);
	$post['group_id']		= $_POST['group_id'];
	$post['meta_open']		= $_POST['meta_open'];
	$post['meta_desc']		= $_POST['meta_desc'];
	
	if(empty($post['title']) OR empty($_POST['group_id'])){
		$post['only_for_members']	= $post['ofm'];
		$rows						= $post;
		$what						= 'edit';
		querys();
		$rows['languages_ids']		= explode(',',$rows['languages_ids']);
		show('page_add.php');
		down();
		die();
	}
	
	
	$update	= mysql_query("UPDATE `pages` SET `title`='".$post['title']."',`desc`='".$post['desc']."',
	`content`='".$post['content']."',`group_id`='".$post['group_id']."',`languages_ids`='".$post['languages_ids']."',
	`meta_open`='".$post['meta_open']."',`meta_desc`='".$post['meta_desc']."'
	 WHERE `id`='".$id."'");
	 
	if(empty($to)){
		$to	= 'pages.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 `pages` ORDER BY `id` ASC");
	$num	= mysql_num_rows($query);
	
	if($num == 0){
		echo '<div class="error">',$language['lang2'],'</div>';
		down();
		die();
	}
	$to	= 'pages.php?step=control';
	show('page_control.php');
	
}
		
		
		
		
		
if($_GET['step'] == 'delete'){
	if(! id_check($_GET['id'],'pages')){
		echo '<div class="error">'.$language['lang1'].'</div>';
		down();
		die();
	}
	
	$delete	= mysql_query("DELETE FROM `pages` WHERE `id`='".$_GET['id']."'");
	if($delete){
		ok('pages.php?step=control');
	}else{
		die('error !'.mysql_error());
	}
}





down();
?>