<?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");
extract($_GET);
extract($_POST);
$page = $_GET['page'];
$limit = $_GET['limit'];
if (!($limit)){
$limit =30;} // Default results per-page.
if (!($page)){
$page = 0;} // Default page value.
$numresults = $db->query("select * from subcat_gallery  where cid='".$_GET['cid']."'"); // the query.
$numrows = mysql_num_rows($numresults); // Number of rows returned from above query.


$pages = intval($numrows/$limit); // Number of results pages.

// $pages now contains int of pages, unless there is a remainder from division.

if ($numrows%$limit) {
$pages++;} // has remainder so add one page

$current = ($page/$limit) + 1; // Current page number.

if (($pages < 1) || ($pages == 0)) {
$total = 1;} // If $pages is less than one or equal to 0, total pages is 1.

else {
$total = $pages;} // Else total pages is $pages value.

$first = $page + 1; // The first result.

if (!((($page + $limit) / $limit) >= $pages) && $pages != 1) {
$last = $page + $limit;} //If not last results page, last result equals $page plus $limit.
 
else{
$last = $numrows;} // If last results page, last result equals total number of results.


//escape from PHP mode.
?>
<!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> سیستم جامع مدیریت W200 </title><link href="css/dropdown/dropdown.vertical.rtl.css" media="all" rel="stylesheet" type="text/css" />
<link href="css/dropdown/themes/default/default.css" media="all" rel="stylesheet" type="text/css" />
<!--[if lt IE 7]>
<script type="text/javascript" src="js/jquery/jquery.js"></script>
<script type="text/javascript" src="js/jquery/jquery.dropdown.js"></script>
<![endif]-->



<script type="text/javascript" src="js/js_body.js"></script>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<!--[if IE 5]>
<style>body {text-align: center;}#wrapper {text-align: left;}.menulist a {float: left; clear: both;}</style>
<![endif]-->
<!--[if lte IE 7]>
<style>#wrapper, #footer, #masthead, #column_wrapper {zoom: 1;}</style>
<![endif]-->
</head>
<body id="p7bod" onload="P7_Uberlink('p7uberlink','p7bod')">

<div id="wrapper">

  <div id="masthead">
    <div class="content" align="center"> سیستم جامع مدیریت W200 </div>
  </div>
  
<div id="column_wrapper">
    <div id="left">
      <div class="content">  






<?php
function dell_banner($catname){
$filename = "SELECT file FROM admin_banner WHERE b_id = '".mysql_real_escape_string($catname)."'";
$picname = mysql_query($filename) or die ("Bad Query<br>".$filename);
$array = mysql_fetch_array($picname);
unlink('../upload/up_gallery/'.$array['file']);
$dell="delete from admin_banner where b_id='".mysql_real_escape_string($catname)."'";    
mysql_query($dell) or die ("Bad Query<br>".$dell);
		echo debug_message('تصویر با موفقیت حذف شد ...','2','');		
		echo '<form action="admin_banner.php" method="GET">
				<p align="center"><input type="submit" id="button" value="OK"></p>
		</form>';	
}
if ($_GET[delid]){
$dcat = ($_GET[delid]);
dell_banner($dcat);
}
if($_POST['edit']){
	
		if ($_FILES['myfile']['name'] =='') {
			$file=$_POST['recent'];
			} else {
				$acceptedSuffix = array('jpeg', 'gif', 'png');
				$suffix = explode('/', $_FILES['myfile']['type']);
				if(in_array($suffix[1], $acceptedSuffix)){
		 			$file=time().'.'.$suffix[1];
					
					move_uploaded_file($_FILES['myfile']['tmp_name'],'../upload/up_gallery/'.$file) or die ("NOT MOVED");
				}
					
	        }		
	
	$sql="update admin_banner set
	bannername='".$_POST['bannername']."',
	link='".$_POST['link']."',
	file='".$file."'
	where b_id='".$_POST['b_id']."' ";
    $query=$db->query($sql);

		echo debug_message('تصویر با موفقیت ویرایش شد ...','2','');
		
		echo '<form action="admin_banner.php" method="POST">
				<p align="center"><input type="submit" id="button" value="OK"></p>
		</form>'; 
}
		
elseif($_POST['send']){
	
$sql="select * from admin_banner where type=1 and '".$_POST['type']."'=1";
$query = mysql_query($sql) or die ("Bad Query<br>".$sql);
$numrow=mysql_num_rows($query);
	if($numrow>1){$banner=0;}elseif($numrow==0){$banner=1;}
	
	if($banner==1){
	if($_POST['bannername']!="" ){

		if ($_FILES['myfile']['name'] =='') {
			$file=$_POST['recent'];
			} else {
				$acceptedSuffix = array('jpeg', 'gif', 'png');
				$suffix = explode('/', $_FILES['myfile']['type']);
				if(in_array($suffix[1], $acceptedSuffix)){
		 			$file=time().'.'.$suffix[1];
					move_uploaded_file($_FILES['myfile']['tmp_name'],'../upload/up_gallery/'.$file) or die ("NOT MOVED");
				}
	        }	
		$query="insert into admin_banner values('',
											  '".mysql_real_escape_string(trim($_POST['bannername']))."',
											  '".$_POST['link']."',
											  '',
											  '".$file."')";
		mysql_query($query) or die ("Bad Query<br>".$query);

		echo debug_message('تصویر با موفقیت افزوده شد ...','2','');
		
		echo '<form action="admin_banner.php" method="GET">
				<p align="center"><input type="submit" id="button" value="OK"></p>
		</form>';	
  } else{
		echo debug_message('نوع تصویر یا نام تصویر  وارد نشده است...','1','');
		
		echo '<form action="admin_banner.php" method="POST">
				<p align="center"><input type="submit" id="button" value="OK"></p>
		</form>';   
  }
}else{
		echo debug_message('آپلود تصویر بنر بیش از یکی مجاز نمی باشد ...','1','');
		
		echo '<form action="admin_banner.php" method="POST">
				<p align="center"><input type="submit" id="button" value="OK"></p>
		</form>'; 	
	}
}
$sql="select * from admin_banner where b_id='".$_GET['b_id']."'";
$query = mysql_query($sql) or die ("Bad Query<br>".$sql);
$numrow_e=mysql_num_rows($query);
$row=mysql_fetch_array($query);
	if($row['type']==1){$chech1='checked="checked"';}elseif($row['type']==2){$chech2='checked="checked"';}

?>
<form  action="admin_banner.php" method="post" enctype="multipart/form-data" name="form1" id="form1">

  <table width="90%" border="0" align="center" class="table_insert" cellpadding="3" cellspacing="3"  dir="rtl">
          
    <tr>
      <td align="right"   dir="rtl">نام تصویر  :</td>
      <td >
        <input name="bannername" type="text" size="43"  value="<?php echo $row['bannername']; ?>" dir="rtl"  /></td>
    </tr>
    
    
    <tr>
      <td  dir="rtl" align="right">لینک :(http://www.Domain Name.com)</td>
      <td >
        <input name="link" type="text" size="43"  value="<?php echo $row['link']; ?>" dir="rtl" /></td>
    </tr>
    
      
	 <tr>
       <td colspan="2"  dir="rtl" align="right"><p dir="rtl"> پهنای بنر بيش از 180 پيكسل نباشد.</p></td>
     </tr>
     
     <tr>
         <td>
          تصویر :         
         </td>
       <td width="260" ><input name="myfile" id="myfile"  type="file" size="31"  dir="rtl" /><input type="hidden" value="<?php echo $row['file'];?>" name="recent" /></td>
    </tr>

    <tr>
      <td  colspan="2" align="center" >
       <?php if($numrow_e){
			  echo ' <input type="hidden" value="'.$_GET['b_id'].'"  name="b_id" />
      		       <input type="submit" id="button" name="edit" value="edit"  />';}
			 else {  
      		  echo'<input type="submit" id="button" name="send"  value="Send"  />';}
	 ?>
        </td>
    </tr>
  </table>
  
 <table width="90%"  border="0" align="center" cellpadding="0" cellspacing="0"  dir="rtl"  class="table1" >
        <tr>
          <th class="td_header"> تصویر</th>
          <th class="td_header">نام تصویر</th>
          <th class="td_header">لینک</th>
          <th class="td_header">ویرایش</th>
          <th class="td_header">حذف</th>
          
        </tr>
<?php
$sql="select * from admin_banner order by b_id";
$query = mysql_query($sql) or die ("Bad Query<br>".$sql);
while($catarray = mysql_fetch_array($query))
{ 
print "<tr align=center dir=rtl>
<td class=\"tablebattom1\"><img src=\"../upload/up_gallery/".$catarray['file']. "\" width=\"100px\" /> </td> 
<td class=\"tablebattom1\">".$catarray['bannername']. " </td>
<td class=\"tablebattom1\">".$catarray['link']. " </td>
<td class=\"tablebattom1\"><a href=\"admin_banner.php?b_id=".$catarray['b_id']."\"><img border=\"0\" src=\"images/edit.gif  \"></a></td>
<td class=\"tablebattom1\"><a href=\"admin_banner.php?delid=".$catarray['b_id']."\"><img border=\"0\" src=\"images/dell.gif  \"></a></td></tr> \n ";
}




?>

</table>
</form>



      </div>
    </div>
    
    
    <div id="right">
      <div class="content">
        <h4>منو</h4>
<?php include("include/header.php");?>

      </div>
    </div>
    
    <br class="clearfloat" />
  </div>
  
  <div id="footer"><?php include("include/footer.php");?></div>
  
</div>
</body>
</html>
<?php } ?>