<?php
include("include/config.php");
include("include/protect.class.php");
include("include/jdf.php");
include('include/functions.php');
$prot=new protect();
if ($prot->showPage) {
$db=new DB($globalConfig['dbase'], $globalConfig['dbhost'],$globalConfig['dbuser'], $globalConfig['dbpass']);
mysql_query("SET NAMES utf8");
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>پست ها</title>
<link rel="stylesheet" type="text/css" href="fancybox/jquery.fancybox-1.3.1.css" media="screen" />
<script src="js/jquery-1.4.2.min.js" type="text/javascript"></script>
<script type="text/javascript" src="fancybox/jquery.fancybox-1.3.1.js"></script>
<?php include("maintemplate/mainheader.php");?>
<script type="text/javascript" src="ckeditor/ckeditor.js"></script>
<script language="JavaScript" src="maintemplate/js/gen_validatorv4.js" type="text/javascript" xml:space="preserve"></script>



<?php 
				$ead = $db->query("SELECT * FROM ads WHERE id = '".$db->safe_string($_GET['AID'])."'");
				$ad = $db->fetchNextArray($ead);
				?>

</head>
<body onload="initialize()">
<div id="wpwrap">
<div id="wpcontent">
	<?php include("include/menutop.php");?> 
<div id="wpbody">
	<?php include("include/header.php");?>
    <div style="overflow:hidden; padding-bottom:50px;" id="wpbody-content">
        <div id="screen-meta">
            <div class="wrap">
                <div id="icon-index" class="icon32"><b4r></div>
                <h2>آگهی ها</h2>
               
                <div id="dashboard-widgets-wrap">
                    <div id="dashboard-widgets" class="metabox-holder">
                    <div class="postbox-container" style="width:98%;">
                                <div id="normal-sortables" class="meta-box-sortables ui-sortable"><div id="dashboard_recent_comments" class="postbox ">
                                   <div class="handlediv" title="برای جانشینی کلیک کنید"><br></div><h3 class="hndle"><span>  جست و جو</span></h3>
                             <div align="center" style="padding:5px;">
                                               
<form action="adsportal.php?search" method="post" name="form2" enctype="multipart/form-data">
<div style="float:right">
<tr class="even" valign="top">
<th scope="row"><label for="name">نام آگهی</label></th>
<td><input name="name"  id="name"  size="45" value="<?php echo $_POST['name']?>" dir="ltr" class="regular-text" type="text">

</td>
</tr>
</div>
<input name="search" class="button-primary" value="جست و جو" type="submit"> 
</form>    
                                                </div>

                              </div>
                            </div>
                        </div>
                        <?php if ($_POST['search']){?>
                        <div class="postbox-container" style="width:98%;">
                                <div id="normal-sortables" class="meta-box-sortables ui-sortable"><div id="dashboard_recent_comments" class="postbox ">
                                   <div class="handlediv" title="برای جانشینی کلیک کنید"><br></div><h3 class="hndle"><span> لیست نتایج جست و جو</span></h3>
                             <div align="center" style="padding:5px;">
                                               
<table id="rounded-corner"dir="rtl">

  <thead>
			<tr>
            	<th scope="col" id="rounded-q5"></th>
                <th scope="col" id="rounded-q5">نام</th>
                <th scope="col" id="rounded-q5">بخش</th>
				<th scope="col" id="rounded-q5">زمان افزودن</th>
                <th scope="col" id="rounded-q5">بازدید</th>
                <th scope="col" id="rounded-q5">ویرایش</th>
                <th scope="col" id="rounded-q5"> حذف</th>
			</tr>
		</thead>
		<tbody>
<?php	

if($_POST['name'] !='' and isset($_POST['name'])){
$name = $_POST['name'];
}else{
	$name = 'asdkalsdkasdalds;asda;sda;sd;';
	}
if($_POST['code'] !='' and isset($_POST['code'])){
$code = $_POST['code']; }

	    $e_product = $db->query("select * from ads where name like '%".trim($name)."%' limit 50");
		while($row= $db->fetchNextArray($e_product))
		{
				if($data['visited'] == 0){
			$sytle = 'style="color:#390"';
		}else{
			$style = '';
		}
		if(($i % 2) == 0) {
			$class = 'tableRow2';
		}else{
			$class = 'tableRow1';
		}
			echo '<tr class="'.$class.'" onmouseover="this.className=\'tableRowOver\'" onmouseout="this.className=\''.$class.'\'">';
			
?>
					
<form action="convert"> 
		<td ><?php echo $row['id'];?></td>
        <td ><?php echo $row['name'];?></td> 
        <td ><?php $sql1 = $db->query("select * from categories where id = '".(int)$row['category']."'");
			$res1 = $db->fetchNextArray($sql1);
			echo $res1['name'];
			?></td> 
        <td ><?php echo tep_hejri11($row['date']);?></td> 
        <td ><?php echo $row['count'];?></td>   
        <td ><a href="adsportal.php?cat=<?php echo $row['category'];?>&AID=<?php echo $row['id'];?>"><img src="images/pdo.png"></a></td> 
        <td ><a href="adsportal.php?cat=<?php echo $row['category'];?>&del=true&AID=<?php echo $row['id'];?>"><img src="images/cros.gif"></a></td>
			<?php }
	?>
</form>
    		
		</tbody>
  </table>
                   
                                                </div>

                              </div>
                            </div>
                        </div>
                        <?php } ?>
              		<?php if(isset($_GET['cat'])){?>
                    <?php
	if(isset($_GET['del'])){
		if(isset($_GET['AID']) && ($_GET['AID'] != '')){
								
		$db->execute("delete from ads where id = '".(int)$_GET['AID']."'");

		echo debug_message('آگهی با موفقیت حذف شد','2','98');
		
		echo '<form action="adsportal.php?cat='.$_GET['cat'].'" method="POST">
		<p align="center"><input type="submit" id="button" value="بازگشت"></p>
		</form>';	
		}}

                if(isset($_GET['insert'])){
					if($_GET['form']=="main"){
						
		$slug = str_replace(" ", "-", $_POST['name']);				
		/////////////////////////////////////////////insert codes///////////////////////////			
		$db->execute("insert ads set
		name='".$_POST['name']."',
		category='".$_GET['cat']."',
		short_des='".$_POST['short_des']."',
		description='".addslashes($_POST['description'])."',
		years='".$_POST['years']."',
		slug = '".$db->safe_string($slug)."',
		percent='".$_POST['percent']."',
		tech='".$_POST['techs']."',
		url='".$_POST['url']."',
		userID='".$_POST['userID']."',
		date = NOW()
		 ");
		  $e_categpry = $db->query("select * from ads order by id DESC limit 1");

		 $lastid =$db->lastInsertedId();
			  //while loop
		 $sql = "SELECT * FROM morefields where cat=".$_GET['cat']."";
		 $query = $db->query($sql) ;
			  while($array=$db->fetchNextArray($query)){
				  if($array['kind']=='1'){
					  //insert text fields
					   $db->execute("update ads set
						 ".$array['latin']." = '".$db->safe_string($_POST[$array['latin']])."'
						 where id=".$lastid."");
					  }
				  if($array['kind']=='2'){
					  //upload pictures and files
					  			
								//  5MB maximum file size
$MAXIMUM_FILESIZE = 1 * 1024 * 1024;
//  Valid file extensions (images, word, excel, powerpoint)
$rEFileTypes =
  "/^\.(jpg|jpeg|gif|png|doc|docx|txt|rtf|pdf|xls|xlsx|
        ppt|pptx){1}$/i";
$dir_base = "../upload/files/";

$isFile = is_uploaded_file($_FILES[$array['latin']]['tmp_name']);
if ($isFile)    //  do we have a file?
   {//  sanatize file name
    //     - remove extra spaces/convert to _,
    //     - remove non 0-9a-Z._- characters,
    //     - remove leading/trailing spaces
    //  check if under 5MB,
    //  check file extension for legal file types
    $safe_filename1 = preg_replace(
                     array("/\s+/", "/[^-\.\w]+/"),
                     array("_", ""),
                     trim($_FILES[$array['latin']]['name']));
	$safe_filename = time().'-'.$safe_filename1;
    if ($_FILES[$array['latin']]['size'] <= $MAXIMUM_FILESIZE &&
        preg_match($rEFileTypes, strrchr($safe_filename, '.')))
      {$isMove = move_uploaded_file (
                 $_FILES[$array['latin']]['tmp_name'],
                 $dir_base.$safe_filename);}
      } 
		$path='upload/files/'.$safe_filename.'';
			$db->execute("update ads set
						 ".$array['latin']." = '".$path."'
						 where id=".$lastid."");		  
					  }
				  if($array['kind']=='3'){
					  //insert text fields
					   $db->execute("update ads set
						 ".$array['latin']." = '".$db->safe_string($_POST[$array['latin']])."'
						 where id=".$lastid."");
					  }
				  }
				
		echo debug_message('آگهی جدید با موفقیت اضافه شد','2','98');
		
		echo '<form action="adsportal.php?cat='.$_GET['cat'].'" method="POST">
		<p align="center"><input type="submit" id="button" value="بازگشت"></p>
		</form>';	
		
		}
					if($_GET['form']=='picture'){
					switch($_GET['act']){
				case 'profile':
					$db->execute("update pictures set
														profile = 0
														where adid='".$_GET['AID']."'
														");
					$db->execute("update pictures set
														profile = 1
														where id='".$_GET['picid']."'
														");
					echo '<div class="congratulation" style="width:88%; margin:0px auto; text-align:center;">اطلاعات تصاویر با موفقیت بروزرسانی شد</div>';	
				break;
				case 'del':
						 
						$epic = $db->query("SELECT * FROM pictures WHERE id = '".$db->safe_string($_GET['picid'])."'");
						$pic = $db->fetchNextArray($epic);
						$pathkh2 = "../".$pic['file'];
						unlink("$pathkh2");  
						$db->execute("delete from pictures where id='".$_GET['picid']."'");
						echo '<div class="congratulation" style="width:88%; margin:0px auto; text-align:center;">تصویر با موفقیت حذف شد</div>';
				break;
				default:
					
					  //upload pictures and files
						
								//  5MB maximum file size
$MAXIMUM_FILESIZE = 1 * 1024 * 1024;
//  Valid file extensions (images, word, excel, powerpoint)
$rEFileTypes =
  "/^\.(jpg|jpeg|gif|png){1}$/i";
$dir_base = "../upload/adspics/";

$isFile = is_uploaded_file($_FILES['myfile']['tmp_name']);
if ($isFile)    //  do we have a file?
   {//  sanatize file name
    //     - remove extra spaces/convert to _,
    //     - remove non 0-9a-Z._- characters,
    //     - remove leading/trailing spaces
    //  check if under 5MB,
    //  check file extension for legal file types
    $safe_filename1 = preg_replace(
                     array("/\s+/", "/[^-\.\w]+/"),
                     array("_", ""),
                     trim($_FILES['myfile']['name']));
	$safe_filename = time().'-'.$safe_filename1;
    if ($_FILES['myfile']['size'] <= $MAXIMUM_FILESIZE &&
        preg_match($rEFileTypes, strrchr($safe_filename, '.')))
      {$isMove = move_uploaded_file (
                 $_FILES['myfile']['tmp_name'],
                 $dir_base.$safe_filename);}
      } else{$nn=1;}
	  
	  if($nn == 1){$path=0;}else{
		$path='upload/adspics/'.$safe_filename.'';}

		$db->execute("insert into pictures set
														name = '".$db->safe_string($_POST['name'])."',
														adid = '".$_GET['AID']."',
														file = '".$path."',
														date = NOW(),
														profile = 0
														");
				echo '<div class="congratulation" style="width:88%; margin:0px auto; text-align:center;">تصویر با موفقیت اضافه شد</div>';											
					
				break;
					}
					
					}
				
				} 
				/////////////////////////////////pictures////////////////////////////
				
				//////////////////////////////////////////////////////////////
				if(isset($_GET['update'])){
			if($_GET['form']=="main"){
			$slug = str_replace(" ", "-", $_POST['name']);
			 $db->execute("update ads set
		name='".$_POST['name']."',
		category='".$_GET['cat']."',
		short_des='".$_POST['short_des']."',
		description='".addslashes($_POST['description'])."',
		years='".$_POST['years']."',
		slug = '".$db->safe_string($slug)."',
		percent='".$_POST['percent']."',
		tech='".$_POST['techs']."',
		url='".$_POST['url']."',
		userID='".$_POST['userID']."'
		where id='".$_GET['AID']."'
		 ");
         $lastid =$_GET['AID'];
				
			  //while loop
		 $sql = "SELECT * FROM morefields where cat=".$_GET['cat']."";
		 $query = $db->query($sql) ;
			  while($array=$db->fetchNextArray($query)){
				 
					  //insert text fields
					   $db->execute("update ads set
						 ".$array['latin']." = '".$db->safe_string($_POST[$array['latin']])."'
						 where id=".$lastid."");
					 
				
			 $e_product = $db->query("select * from ads where id='".$_GET['AID']."'");
			 $product= $db->fetchNextArray($e_product); 
			 //languages input
			
			
			//check for the new pic. is not set any new pic
			$isFile = is_uploaded_file($_FILES[$array['latin']]['tmp_name']);
			if (!$isFile){
					$path = $product[$array['latin']];
					}else {
						//unlink first
					
							$path2 = "../".$product[$array['latin']];
							if($product[$array['latin']] != "upload/files/"){
						unlink("$path2") or die ('اپراتور قابل حذف نیست');
							}
					  
					  
					  
					  
					  //upload pictures and files
					  			
								//  5MB maximum file size
$MAXIMUM_FILESIZE = 1 * 1024 * 1024;
//  Valid file extensions (images, word, excel, powerpoint)
$rEFileTypes =
  "/^\.(jpg|jpeg|gif|png|doc|docx|txt|rtf|pdf|xls|xlsx|
        ppt|pptx){1}$/i";
$dir_base = "../upload/files/";

$isFile = is_uploaded_file($_FILES[$array['latin']]['tmp_name']);
if ($isFile)    //  do we have a file?
   {//  sanatize file name
    //     - remove extra spaces/convert to _,
    //     - remove non 0-9a-Z._- characters,
    //     - remove leading/trailing spaces
    //  check if under 5MB,
    //  check file extension for legal file types
    $safe_filename1 = preg_replace(
                     array("/\s+/", "/[^-\.\w]+/"),
                     array("_", ""),
                     trim($_FILES[$array['latin']]['name']));
	$safe_filename = time().'-'.$safe_filename1;
    if ($_FILES[$array['latin']]['size'] <= $MAXIMUM_FILESIZE &&
        preg_match($rEFileTypes, strrchr($safe_filename, '.')))
      {$isMove = move_uploaded_file (
                 $_FILES[$array['latin']]['tmp_name'],
                 $dir_base.$safe_filename);}
      } 
		$path='upload/files/'.$safe_filename.'';
			$db->execute("update ads set
						 ".$array['latin']." = '".$path."'
						 where id=".$lastid."");		  
					  
				  }
			
					  
				
					  //insert text fields
					   $db->execute("update ads set
						 ".$array['latin']." = '".$db->safe_string($_POST[$array['latin']])."'
						 where id=".$lastid."");
					  
				  }	
			echo debug_message('آگهی مورد نظر با موفقیت ویرایش شد','2','98');
		
		echo '<form action="adsportal.php?cat='.$_GET['cat'].'" method="POST">
		<p align="center"><input type="submit" id="button" value="بازگشت"></p>
		</form>';
		
		
			}
			
		
		}else{?>
                         <div class="postbox-container" style="width:68%;">

                                <div id="normal-sortables" class="meta-box-sortables ui-sortable"><div id="dashboard_recent_comments" class="postbox ">
                                   <div class="handlediv" title="برای جانشینی کلیک کنید"><br></div><h3 class="hndle"><span>اضافه کردن آگهی جدید</span></h3>
                                <div style="padding:10px; line-height:24px">

<?php
	

	
			$form_action = 'insert';
		if(isset($_GET['AID'])){
			$sql = $db->query("select * from ads where id = '".(int)$_GET['AID']."'");
			$res = $db->fetchNextArray($sql);
			$form_action = 'update&AID='.$_GET['AID'];
		}
?>
<div class="inside" style="">
<form action="adsportal.php?cat=<?php echo $_GET['cat'];?>&do=add_kind&<?php echo $form_action; ?>&form=main" method="post" name="form1" id="form1" enctype="multipart/form-data">
<table class="form-table">
<tbody>
<tr class="even" valign="top">
<th scope="row"><label for="name">نام</label></th>
<td><input name="name"  id="name" size="16" value="<?php echo $res['name']; ?>" dir="ltr" class="regular-text" type="text">
<div id='form1_name_errorloc' class="error_strings"></div>
</td>
</tr>
<tr class="odd" valign="top">
<th scope="row"><label for="short_des">توضیحات مختصر</label></th>
<td><textarea name="short_des" id="short_des" cols="50" rows="10"><?php echo $res['short_des']; ?></textarea>
</td>
</tr>
<tr class="even" valign="top">
<th scope="row"><label for="description">توضیحات اصلی</label></th>
<td>
   <textarea cols="80" id="description" name="description" rows="10"><?php echo stripslashes($res['description']);?></textarea>
			<script type="text/javascript">
			//<![CDATA[

				CKEDITOR.replace( 'description',
					{
						fullPage : false,
						extraPlugins : 'docprops'
					});

			//]]>
			</script>                      
</td>
</tr>
<tr class="odd" valign="top">
<th scope="row"><label for="years">سال راه اندازی</label></th>
<td><input name="years"  id="years" size="16" value="<?php echo $res['years']; ?>" dir="ltr" class="regular-text" type="text">
</td>
</tr>
<tr class="even" valign="top">
<th scope="row"><label for="techs">تکنولوژیها</label></th>
<td><input name="techs"  id="techs" size="16" value="<?php echo $res['tech']; ?>" dir="ltr" class="regular-text" type="text">
</td>
</tr>
<tr class="odd" valign="top">
<th scope="row"><label for="percent">میزان پیشرفت</label></th>
<td><input name="percent"  id="percent" size="16" value="<?php echo $res['percent']; ?>" dir="ltr" class="regular-text" type="text">
</td>
</tr>
<tr class="even" valign="top">
<th scope="row"><label for="userID">شماره کاربر</label></th>
<td><input name="userID"  id="userID" size="16" value="<?php echo $res['userID']; ?>" dir="ltr" class="regular-text" type="text">
</td>
</tr>
<tr class="odd" valign="top">
<th scope="row"><label for="url">آدرس سایت</label></th>
<td><input name="url"  id="url" size="16" value="<?php echo $res['url']; ?>" dir="ltr" class="regular-text" type="text">
</td>
</tr>
<?php 
		  $sql = "SELECT * FROM morefields where cat = '".$_GET['cat']."'";
			  $query = $db->query($sql) ;
			  while($array=$db->fetchNextArray($query)){?>
          <?php if ($array['kind']=='1'){?>
           <tr>
            <td class="td_d_lable"><?php echo $array['name'];?></td>
            <td class="td_d">
            <input type="text" lang="fa" class="validate[required] inp_text" name="<?php echo $array['latin'];?>" id="<?php echo $array['latin'];?>" value="<?php echo $res[$array['latin']];?>"  /></td>
        </tr>
       <?php } if($array['kind']=='2'){?>
        <tr>
            <td class="td_d_lable"><?php echo $array['name'];?></td>
            <td class="td_d">
            <?php if($_GET['AID']){ ?>
<div class="error_strings">در صورتیکه قصد تغییر این بخش را ندارید آن را به همین صورت رها کنید</div>
<?php } ?>
            <input name="<?php echo $array['latin'];?>" id="<?php echo $array['latin'];?>"  type="file" size="31"  dir="rtl" accept="image/gif, image/jpeg"  /></td>
        </tr>
       <?php }?>
       <?php if($array['kind']=='3'){?>
        <tr>
            <td class="td_d_lable"><?php echo $array['name'];?></td>
            <td class="td_d">
           <input type="checkbox" value="1" name="<?php echo $array['latin'];?>" <?php if($res[$array['latin']]==1){ echo 'checked="checked"';}?> /></td>
        </tr>
       <?php }?>
        <?php } ?>
        
</tbody></table>


<p class="submit">
<input name="process" class="button-primary" value="ذخیره" type="submit">
</p>
</form>
<script language="JavaScript" type="text/javascript"
    xml:space="preserve">//<![CDATA[
//You should create the validator only after the definition of the HTML form
  var frmvalidator  = new Validator("form1");
    frmvalidator.EnableOnPageErrorDisplay();
    frmvalidator.EnableMsgsTogether();


	frmvalidator.addValidation("name","req","وارد کردن نام  آگهی ضروری است");
	
	
	
//]]></script>
</div>
                                
                                </div>

                              </div>
                            </div>
                        </div>  
                        <div style="width:29%;" class="postbox-container">
<div class="postbox" id="dashboard_recent_drafts">
<div title="برای جانشینی کلیک کنید" class="handlediv"><br></div><h3 class="hndle"><span></span>راهنما</h3>
<div class="inside">
این بخش برای اضافه کردن آگهی جدید است<br/>
<a href="adsportal.php?cat=<?php echo $_GET['cat'];?>"><div class="mybutton" style="width:310px;">اضافه کردن آگهی جدید</div></a>
</div>

</div>

</div>
						<?php if (isset($_GET['AID'])){?>
						
					
						<div style="width:29%;" class="postbox-container">
<div class="postbox" id="dashboard_recent_drafts">
<div title="برای جانشینی کلیک کنید" class="handlediv"><br></div><h3 class="hndle"><span></span>تصاویر</h3>
<div class="inside">
<div>
            <form action="adsportal.php?cat=<?php echo $_GET['cat'];?>&do=add_kind&insert&form=picture&AID=<?php echo $_GET['AID'];?>" method="post" name="formIDpic" id="formIDpic" enctype="multipart/form-data">
  	 <table  class="table_insert" cellpadding="3" cellspacing="3" width="100%" align="center" >  
      
           <tr>
            <td class="td_d_lable">نام :</td>
            <td class="td_d">
            
            <input type="text" size="40" name="name" id="name" value="" class="validate[required] text-input inp_text" /></td>
        </tr>
        <tr>
            <td class="td_d_lable">تصویر :</td>
            <td class="td_d">
            <input name="myfile" id="myfile"  type="file" size="31"  dir="rtl" accept="image/gif, image/jpeg"  /></td>
        </tr>
     

         <tr>
            <td colspan="2" align="center">
            <?php
              echo '<input type="submit" name="Insert" value="Add picture" />';
			?>
              </td>
        </tr>
        </table>        
   </form>
           <div align="center">
                                               
<table id="rounded-corner"dir="rtl">

  <thead>
			<tr>
				<th scope="col" id="rounded-q5"></th>
				<th scope="col" id="rounded-q5">نام</th>
				<th scope="col" id="rounded-q5">تصویر پروفایل</th>
                <th scope="col" id="rounded-q5">حذف</th>
			</tr>
		</thead>
		<tbody>
<?php
	/*
		Place code to connect to your DB here.
	*/
	// include your code to connect to DB.

	$tbl_name="pictures";		//your table name
	// How many adjacent pages should be shown on each side?
	$adjacents = 3;
	
	/* 
	   First get total number of rows in data table. 
	   If you have a WHERE clause in your query, make sure you mirror it here.
	*/
	$query = "SELECT COUNT(*) as num FROM $tbl_name WHERE adid =".$_GET['AID']."";	
	$total_pages = mysql_fetch_array(mysql_query($query));
	$total_pages = $total_pages[num];
	
	/* Setup vars for query. */
	$targetpage = "adsportal.php"; 	//your file name  (the name of this file)
	$limit = 10; 								//how many items to show per page
	$page = $_GET['page'];
	if($page) 
		$start = ($page - 1) * $limit; 			//first item to display on this page
	else
		$start = 0;								//if no page var is given, set start to 0
	
	/* Get data. */
	$sql = "SELECT * FROM $tbl_name WHERE adid =".$_GET['AID']." ORDER BY id DESC LIMIT $start, $limit";
	$result = mysql_query($sql);
	
	/* Setup page vars for display. */
	if ($page == 0) $page = 1;					//if no page var is given, default to 1.
	$prev = $page - 1;							//Previous page is page - 1
	$next = $page + 1;							//Next page is page + 1
	$lastpage = ceil($total_pages/$limit);		//lastpage is = total pages / items per page, rounded up.
	$lpm1 = $lastpage - 1;						//last page minus 1
	
	/* 
		Now we apply our rules and draw the pagination object. 
		We're actually saving the code to a variable in case we want to draw it more than once.
	*/
	$pagination = "";
	if($lastpage > 1)
	{	
		$pagination .= "<div class=\"pagination\">";
		//Previous button
		if ($page > 1) 
			$pagination.= "<a href=\"$targetpage?page=$prev\">« Previous</a>";
		else
			$pagination.= "<span class=\"disabled\">« Previous</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.= "<a href=\"$targetpage?page=$next\">Next »</a>";
		else
			$pagination.= "<span class=\"disabled\">Next »</span>";
		$pagination.= "</div>\n";		
	}
?>

	<?php
	$i=1;
		while($row = mysql_fetch_array($result))
		{
				if($data['visited'] == 0){
			$sytle = 'style="color:#390"';
		}else{
			$style = '';
		}
		if(($i % 2) == 0) {
			$class = 'tableRow2';
		}else{
			$class = 'tableRow1';
		}
			echo '<tr class="'.$class.'" onmouseover="this.className=\'tableRowOver\'" onmouseout="this.className=\''.$class.'\'">';
			
			
?>
					
<form action="convert">
        <td width="40"> <img width="80" height="60" src="include/image.php/name.jpg?parameters=@width=140@height=120@cropratio=1:1@quality=100@image=<?php echo $globalConfig['baseurl'].'/'.$row['file']; ?>" /></td> 
        <td><?php echo $row['name'];?></td>
        <td width="100"> 
        <?php if($row['profile']=='0'){?>
        <a href="adsportal.php?cat=<?php echo $_GET['cat'];?>&do=add_kind&insert&form=picture&AID=<?php echo $_GET['AID'];?>&act=profile&picid=<?php echo $row['id'];?>"><div class="plus" style="margin-left:38px;"></div></a>
        <?php }else { ?>
        <a href="adsportal.php?cat=<?php echo $_GET['cat'];?>&do=add_kind&insert&form=picture&AID=<?php echo $_GET['AID'];?>&act=profile&picid=<?php echo $row['id'];?>"><div class="mult" style="margin-left:38px;"></div></a>
        <?php } ?>
        </td> 
        <td width="40"> <a href="adsportal.php?cat=<?php echo $_GET['cat'];?>&do=add_kind&insert&form=picture&AID=<?php echo $_GET['AID'];?>&act=del&picid=<?php echo $row['id'];?>"><div class="mult" style="margin-left:05px;"></div></a></td>
			<?php $i++;}
	?>
<?php echo $row['id'];?>
</form>
    		
		</tbody>
  </table>
  <div style="direction:ltr">
<?=$pagination?>
                             </div>                 
                                                </div>

        </div>
</div>

</div>

</div>
						<?php } ?>
<?php
		}?>
        <div class="postbox-container" style="width:98%;">
                                <div id="normal-sortables" class="meta-box-sortables ui-sortable"><div id="dashboard_recent_comments" class="postbox ">
                                   <div class="handlediv" title="برای جانشینی کلیک کنید"><br></div><h3 class="hndle"><span> لیست  آگهی ها</span></h3>
                             <div align="center" style="padding:5px;">
                                               
<table id="rounded-corner"dir="rtl">

  <thead>
			<tr>
            	<th scope="col" id="rounded-q5"></th>
                <th scope="col" id="rounded-q5">نام</th>
                <th scope="col" id="rounded-q5">بخش</th>
				<th scope="col" id="rounded-q5">زمان افزودن</th>
                <th scope="col" id="rounded-q5">بازدید</th>
                <th scope="col" id="rounded-q5">ویرایش</th>
                <th scope="col" id="rounded-q5"> حذف</th>
			</tr>
		</thead>
		<tbody>
<?php
	/*
		Place code to connect to your DB here.
	*/
	// include your code to connect to DB.

	$tbl_name="ads";		//your table name
	// How many adjacent pages should be shown on each side?
	$adjacents = 3;
	
	/* 
	   First get total number of rows in data table. 
	   If you have a WHERE clause in your query, make sure you mirror it here.
	*/

	$query = "SELECT COUNT(*) as num FROM $tbl_name where category=".$_GET['cat']."";
	$total_pages = mysql_fetch_array(mysql_query($query));
	$total_pages = $total_pages[num];
	
	/* Setup vars for query. */
	$targetpage = "adsportal.php?cat=".$_GET['cat'].""; 	//your file name  (the name of this file)
	$limit = 15; 								//how many items to show per page
	$page = $_GET['page'];
	if($page) 
		$start = ($page - 1) * $limit; 			//first item to display on this page
	else
		$start = 0;								//if no page var is given, set start to 0
	
	/* Get data. */
	
	$sql = "SELECT * FROM $tbl_name  where category=".$_GET['cat']." ORDER BY id DESC LIMIT $start, $limit";	
	$result = mysql_query($sql);
	
	/* Setup page vars for display. */
	if ($page == 0) $page = 1;					//if no page var is given, default to 1.
	$prev = $page - 1;							//قبلی page is page - 1
	$next = $page + 1;							//بعدی page is page + 1
	$lastpage = ceil($total_pages/$limit);		//lastpage is = total pages / items per page, rounded up.
	$lpm1 = $lastpage - 1;						//last page minus 1
	
	/* 
		Now we apply our rules and draw the pagination object. 
		We're actually saving the code to a variable in case we want to draw it more than once.
	*/
	$pagination = "";
	if($lastpage > 1)
	{	
		$pagination .= "<div class=\"pagination\">";
		//قبلی 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>";					
				}
			}
		}
		
		//بعدی button
		if ($page < $counter - 1) 
			$pagination.= "<a href=\"$targetpage&page=$next\">بعدی »</a>";
		else
			$pagination.= "<span class=\"disabled\">بعدی »</span>";
		$pagination.= "</div>\n";		
	}
?>

	<?php
		while($row = mysql_fetch_array($result))
		{
				if($data['visited'] == 0){
			$sytle = 'style="color:#390"';
		}else{
			$style = '';
		}
		if(($i % 2) == 0) {
			$class = 'tableRow2';
		}else{
			$class = 'tableRow1';
		}
			echo '<tr class="'.$class.'" onmouseover="this.className=\'tableRowOver\'" onmouseout="this.className=\''.$class.'\'">';
			
?>
					
<form action="convert"> 
		<td ><?php echo $row['id'];?></td>
        <td ><?php echo $row['name'];?></td> 
        <td ><?php $sql1 = $db->query("select * from categories where id = '".(int)$row['category']."'");
			$res1 = $db->fetchNextArray($sql1);
			echo $res1['name'];
			?></td> 
        <td ><?php echo tep_hejri11($row['date']);?></td> 
        
        
         
        <td ><?php echo $row['count'];?></td>   
        <td ><a href="adsportal.php?cat=<?php echo $_GET['cat'];?>&AID=<?php echo $row['id'];?>"><img src="images/pdo.png"></a></td> 
        <td ><a href="adsportal.php?cat=<?php echo $_GET['cat'];?>&del=true&AID=<?php echo $row['id'];?>"><img src="images/cros.gif"></a></td>
			<?php }
	?>
</form>
    		
		</tbody>
  </table>
  <div style="direction:ltr">
<?=$pagination?>
                             </div>                 
                                                </div>

                              </div>
                            </div>
                        </div>

					<?php }else{ ?>
					<div class="postbox-container" style="width:98%;">
                                <div id="normal-sortables" class="meta-box-sortables ui-sortable"><div id="dashboard_recent_comments" class="postbox ">
                                   <div class="handlediv" title="برای جانشینی کلیک کنید"><br></div><h3 class="hndle"><span>لیست دسته ها</span></h3>
                             <div align="center" style="padding:5px;">
                                               
<table id="rounded-corner"dir="rtl">

  <thead>
			<tr>
            	<th scope="col" id="rounded-q5"></th>
                <th scope="col" id="rounded-q5">نام دسته</th>
                <th scope="col" id="rounded-q5"> اضافه کردن نمونه کار</th>
			</tr>
		</thead>
		<tbody>
<?php
	/*
		Place code to connect to your DB here.
	*/
	// include your code to connect to DB.

	$tbl_name="categories";		//your table name
	// How many adjacent pages should be shown on each side?
	$adjacents = 3;
	
	/* 
	   First get total number of rows in data table. 
	   If you have a WHERE clause in your query, make sure you mirror it here.
	*/

	$query = "SELECT COUNT(*) as num FROM $tbl_name";
	$total_pages = mysql_fetch_array(mysql_query($query));
	$total_pages = $total_pages[num];
	
	/* Setup vars for query. */
	$targetpage = "adsportal.php"; 	//your file name  (the name of this file)
	$limit = 10; 								//how many items to show per page
	$page = $_GET['page'];
	if($page) 
		$start = ($page - 1) * $limit; 			//first item to display on this page
	else
		$start = 0;								//if no page var is given, set start to 0
	
	/* Get data. */
	
	$sql = "SELECT * FROM $tbl_name ORDER BY id DESC LIMIT $start, $limit";	
	$result = mysql_query($sql);
	
	/* Setup page vars for display. */
	if ($page == 0) $page = 1;					//if no page var is given, default to 1.
	$prev = $page - 1;							//قبلی page is page - 1
	$next = $page + 1;							//بعدی page is page + 1
	$lastpage = ceil($total_pages/$limit);		//lastpage is = total pages / items per page, rounded up.
	$lpm1 = $lastpage - 1;						//last page minus 1
	
	/* 
		Now we apply our rules and draw the pagination object. 
		We're actually saving the code to a variable in case we want to draw it more than once.
	*/
	$pagination = "";
	if($lastpage > 1)
	{	
		$pagination .= "<div class=\"pagination\">";
		//قبلی 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>";					
				}
			}
		}
		

		//بعدی button
		if ($page < $counter - 1) 
			$pagination.= "<a href=\"$targetpage?page=$next\">بعدی »</a>";
		else
			$pagination.= "<span class=\"disabled\">بعدی »</span>";
		$pagination.= "</div>\n";		
	}
?>

	<?php
		while($row = mysql_fetch_array($result))
		{
				if($data['visited'] == 0){
			$sytle = 'style="color:#390"';
		}else{
			$style = '';
		}
		if(($i % 2) == 0) {
			$class = 'tableRow2';
		}else{
			$class = 'tableRow1';
		}
			echo '<tr class="'.$class.'" onmouseover="this.className=\'tableRowOver\'" onmouseout="this.className=\''.$class.'\'">';

			
?>
					
<form action="convert"> 
		<td ><?php echo $row['id'];?></td>
        <td ><?php echo $row['name'];?></td>
        <td ><a href="adsportal.php?cat=<?php echo $row['id'];?>"><img src="images/add.png"></a></td>
			<?php }
	?>
</form>
    		
		</tbody>
  </table>
  <div style="direction:ltr">
<?=$pagination?>
                             </div>                 
                                                </div>

                              </div>
                            </div>
                        </div>
                    <?php } ?>
                    </div>
                    <div class="clear"></div>
                </div><!-- dashboard-widgets-wrap -->
            </div><!-- wrap -->
			<div class="clear"></div></div><!-- wpbody-content -->
			<div class="clear"></div></div><!-- wpbody -->
			<div class="clear"></div></div><!-- wpcontent -->
        </div>
    </div><!-- wpcontent -->
</div><!-- wpwrap -->
<?php include("include/footer.php");?>
</div>
</div>
</body>
</html><?php }?>