<?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['lang3']	= 'لايوجد أي اعضاء بالقاعدة ؟ حتماً هنالك خطأ كبير !';
$language['lang4']	= 'هل أنت متأكد من أنك تريد حذف الأعضاء المُحددون ? ##################### ملاحظة : لايمكنك التراجع !';
$language['lang5']	= 'حذف الأعضاء المُحددون';
$language['lang6']	= 'لا يوجد تحديد !';
$language['lang7']	= 'الرجوع ';
$language['lang9']	= 'المعذرة ... العضو المطلوب غير موجودة في قاعدة البيانات';
$language['lang10']	= 'اسم المستخدم يجب ان لايحتوي على رموز وأن لايكون فارغاً';
$language['lang11']	= 'البريد الإلكتروني غير صحيح';
$language['lang12']	= 'المعذرة , ليس لديك صلاحية لتعديل هذا الشخص';
$language['lang13']	= 'يجب عليك كتابة كلمة المرور';
## لغة قوائم الدول و الشهور و الأيام
## 1 يعني عربي , و اثنان يعني انجليزي
$language['l']		= 1;

if($permission['allow_members'] == 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'] == 'control'){
	$all			= mysql_fetch_array(mysql_query("SELECT COUNT(`id`) AS `num` FROM `members`"));
	if($all == 0){
		echo '<div class="error">'.$language['lang3'].'</div>';
		down();
		die();
	}

	
	switch($_GET['do']){
	case 'group':
		$group_id	= $_GET['group_id'];
		if(! id_check($group_id,'groups')){
			echo '<div class="error">'.$language['lang1'].'</div>';
			down();
			die();
		}
		$GIVEN_Q	= "WHERE `group_id`='".$group_id."'";
		$GIVEN_T	= '&do=group&group_id='.$group_id;
	break;
	default:
	break;
	}
	
	

	$page = intval($_GET['page']);
	if($page == 0 OR $page == '' OR !is_numeric($page)){ $page = 1; $start=0; }
	$limit = 20;
	if($page) $start = ($page - 1) * $limit; else
	$start = 0;	
	$targetpage = 'members.php?step=control'.$GIVEN_T;
	$adjacents = 10;		
	$xquery = "SELECT COUNT(`id`) as num FROM `members` ".$GIVEN_Q."";
	$total_pages = mysql_fetch_array(mysql_query($xquery));
	$total_pages = $total_pages[num];
	$prev = $page - 1;
	$next = $page + 1;
	$lastpage = ceil($total_pages/$limit);
	$lpm1 = $lastpage - 1;
	
		$groups_query	= mysql_query("SELECT `id`,`name` FROM `groups`");

	switch($_GET['do']){
	case 'group':
		$query		= mysql_query("SELECT `id`,`username`,`reg_date`,`group_id`  FROM `members` WHERE `group_id`='".$group_id."' ORDER BY `id` DESC LIMIT $start,$limit");
		$num		= mysql_num_rows(mysql_query("SELECT `id` FROM `members` WHERE `group_id`='".$group_id."' ".$ADD_SQL.""));
	break;
	default:
		$query	= mysql_query("SELECT `id`,`username`,`reg_date`,`group_id` FROM `members` ORDER BY `id` DESC LIMIT $start,$limit");
		$num	= mysql_num_rows(mysql_query("SELECT `id` FROM `members`"));
	break;
	}


	show('members_control.php');

	$pagination = "";
	if($lastpage > 1)
	{	
		$pagination .= "<div class=\"pagination\">";
		//previous button
		if ($page > 1) 
			$pagination.= "<a href=\"$targetpage&page=$prev\">« السابق</a>";
		else
			$pagination.= "<span class=\"disabled\">« السابق</span>";	
		
		//pages	
		if ($lastpage < 7 + ($adjacents * 2))	//not enough pages to bother breaking it up
		{	
			for ($counter = 1; $counter <= $lastpage; $counter++)
			{
				if ($counter == $page)
					$pagination.= "<span class=\"current\">$counter</span>";
				else
					$pagination.= "<a href=\"$targetpage&page=$counter\">$counter</a>";					
			}
		}
		elseif($lastpage > 5 + ($adjacents * 2))	//enough pages to hide some
		{
			//close to beginning; only hide later pages
			if($page < 1 + ($adjacents * 2))		
			{
				for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
				{
					if ($counter == $page)
						$pagination.= "<span class=\"current\">$counter</span>";
					else
						$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";					
				}
				$pagination.= "...";
				$pagination.= "<a href=\"$targetpage&page=$lpm1\">$lpm1</a>";
				$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";		
			}
			//in middle; hide some front and some back
			elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
			{
				$pagination.= "<a href=\"$targetpage&page=1\">1</a>";
				$pagination.= "<a href=\"$targetpage&page=2\">2</a>";
				$pagination.= "...";
				for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
				{
					if ($counter == $page)
						$pagination.= "<span class=\"current\">$counter</span>";
					else
						$pagination.= "<a href=\"$targetpage&page=$counter\">$counter</a>";					
				}
				$pagination.= "...";
				$pagination.= "<a href=\"$targetpage&page=$lpm1\">$lpm1</a>";
				$pagination.= "<a href=\"$targetpage&page=$lastpage\">$lastpage</a>";		
			}
			//close to end; only hide early pages
			else
			{
				$pagination.= "<a href=\"$targetpage&page=1\">1</a>";
				$pagination.= "<a href=\"$targetpage&page=2\">2</a>";
				$pagination.= "...";
				for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
				{
					if ($counter == $page)
						$pagination.= "<span class=\"current\">$counter</span>";
					else
						$pagination.= "<a href=\"$targetpage&page=$counter\">$counter</a>";					
				}
			}
		}
		
		//next button
		if ($page < $counter - 1) 
			$pagination.= "&nbsp;&nbsp;<a href=\"$targetpage&page=$next\">التالي »</a>";
		else
			$pagination.= "&nbsp;&nbsp;<span class=\"disabled\">التالي »</span>";
		$pagination.= "</div>\n";		
	}

	if($all > 20){
	echo '<br />',$pagination,'<br />';
	}
	if($num > 0){
	?>
	<br />
	<input  onclick="javascript:return confirm('<?= $language['lang4']; ?>')" type="submit" 
	name="submit" value="<?= $language['lang5']; ?>" /> </form>
<?php }
	
	
}







if($_GET['step'] == 'delete_group'){

	$to = $_GET['to'];
	if(!isset($to)){
		$to = 'members.php?step=control';
	}else{
		$to	= replace_to($to);
	}
	$Array = $_POST['id'];
	if(! is_array($Array)){
		echo '<div class="error">'.$language['lang6'].'<br />&lt;&lt; <a href="'.$to.'">'.$language['lang7'].'</a></div>';
		down();
		die();
	}else{
		$Array		= mysql_real_escape_string(@implode(',',$Array));
		$DELETE		= mysql_query("DELETE FROM `members` WHERE `id` IN ($Array)");
		$DELETE2	= mysql_query("DELETE FROM `tickets` WHERE `user_id` IN ($Array)");
		$DELETE3	= mysql_query("DELETE FROM `reply` WHERE `user_id` IN ($Array)");
			if($DELETE){
				ok($to);
			}else{
				echo mysql_error();
			}
	}

}





if($_GET['step'] == 'edit'){
	$id	= $_GET['id'];
	if(! id_check($id,'members')){
		echo '<div class="error">'.$language['lang1'].'</div>';
		down();
		die();
	}
	
	include('../include/system/arrays.php');
	$what				= 'edit';
	$query	= mysql_query("SELECT * FROM `members` WHERE `id`='".$id."'");
	$rows	= mysql_fetch_array($query);
	$tickets= mysql_num_rows(mysql_query("SELECT `id` FROM `tickets` WHERE `user_id`='".$id."'"));
	$replys	= mysql_num_rows(mysql_query("SELECT `id` FROM `reply` WHERE `user_id`='".$id."'"));
	querys();
	show('member_edit.php');
}


if($_GET['step'] == 'update'){
	$id	= $_GET['id'];
	if(! id_check($id,'members')){
		echo '<div class="error">'.$language['lang1'].'</div>';
		down();
		die();
	}
	
	include('../include/system/arrays.php');


	get_userinfo($id,'`id`,`username`,`group_id`');
	$Gquery		= mysql_query("SELECT `id`,`top_man` FROM `groups` WHERE `id`='".$minfo['group_id']."'");
	$GR			= mysql_fetch_array($Gquery);
	if(! $permission['top_man'] == 1 AND $GR['top_man'] == 1){
		echo '<div class="error">'.$language['lang12'].'</div>';
		down();
		die();
	}
	
	$what				= 'edit';
	$posted['username']	= input_handler($_POST['username']);
	$posted['password']	= trim($_POST['password']);
	$posted['country']	= $_POST['country'];
	$posted['email']	= $_POST['email'];
	$posted['website']	= $_POST['website'];
	$posted['group_id']	= $_POST['group_id'];
	$posted['language']	= $_POST['language'];
	
	if(empty($posted['username'])){
		echo '<div class="error">'.$language['lang10'].'</div>';
		$rows	= $posted;
		querys();
		show('member_edit.php');
		down();
		die();
	}
	
	if(! empty($posted['password'])){
		$Npassword	= md5($posted['password']);
		$ADD_SQL	= ",`password`='".$Npassword."'";
	}
	
	if(empty($posted['email']) OR check($posted['email'],'email') == false){
		echo '<div class="error">'.$language['lang11'].'</div>';
		$rows	= $posted;
		querys();
		show('member_edit.php');
		down();
		die();
	}

	$update	= mysql_query("UPDATE `members` SET `username`='".$posted['username']."',
	`country`='".$posted['country']."',`email`='".$posted['email']."',
	`website`='".$posted['website']."',`language`='".$posted['language']."',`group_id`='".$posted['group_id']."'".$ADD_SQL."
	 WHERE `id`='".$id."'");
	 
	$to = $_GET['to'];
	if(empty($to)){
		$to = 'members.php?step=control';
	}else{
		$to	= replace_to($to);
	}
	 
	 if($update){
		ok($to,2);
	}else{
		echo mysql_error();
	}
}

	
	
if($_GET['step'] == 'add'){
	include('../include/system/arrays.php');
	$query1	= mysql_fetch_array(mysql_query("SELECT `main_group` FROM `option` WHERE `id`='1'"));
	$VGroup	= $query1['main_group'];
	$what	= 'add';
	querys();
	show('member_edit.php');
}

if($_GET['step'] == 'insert'){
	include('../include/system/arrays.php');
	$what				= 'add';
	$posted['username']	= input_handler($_POST['username']);
	$posted['password']	= trim($_POST['password']);
	$posted['country']	= $_POST['country'];
	$posted['email']	= $_POST['email'];
	$posted['website']	= $_POST['website'];
	$posted['group_id']	= $_POST['group_id'];
	$posted['language']	= $_POST['language'];
	
	if(empty($posted['username'])){
		echo '<div class="error">'.$language['lang10'].'</div>';
		$rows	= $posted;
		querys();
		show('member_edit.php');
		down();
		die();
	}
	
	if(empty($posted['password'])){
		echo '<div class="error">'.$language['lang13'].'</div>';
		$rows	= $posted;
		querys();
		show('member_edit.php');
		down();
		die();
	}
	$posted['password']	= md5($posted['password']);
	
	if(empty($posted['email']) OR check($posted['email'],'email') == false){
		echo '<div class="error">'.$language['lang11'].'</div>';
		$rows	= $posted;
		querys();
		show('member_edit.php');
		down();
		die();
	}

	$insert	= mysql_query("INSERT INTO `members` (`username`,`password`,`country`,`email`,`website`,`last_login`,`reg_date`,`language`,`group_id`)
	VALUES ('".$posted['username']."','".$posted['password']."','".$posted['country']."',
	'".$posted['email']."','".$posted['website']."','',
	'".date('d/m/Y')."','".$posted['language']."','".$posted['group_id']."'
	)");
	
	if($insert){
		ok('members.php?step=control',2);
	}else{
		echo mysql_error();
	}
}



down();
?>