<?php
include_once("../include/main.inc.php");
check_session('MyCpanel','index.php');

################ Paging Starts From Here ##################
@extract($_REQUEST);
$start = (intval($start)==0 or $start=="")?$start=0:$start;
$pagesize = intval($pagesize)==0?$pagesize=20:$pagesize;

$record_type="Banner";
$script_name="manage_banner_advertisement.php";
$table_name="banner_tbl";
######################################################
if($_POST[action]=="Delete"){
	if(sizeof($_POST[cid])==0) {
		$_SESSION[session_message]='Please select a record first.';
		header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
		exit;
	}
	for ($x=0; $x < sizeof($_POST[cid]); $x++){
		$arr=$_POST[cid];
		updateTable($table_name," status='D' where slno='$arr[$x]'");		
	}
	$_SESSION[session_message]='Record has been deleted from the admin panel.';
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize."&status=".$status);
	exit;
}

if($_POST[action]=="Activate"){
	if(sizeof($_POST[cid])==0) {
		$_SESSION[session_message]='Please select a record first.';
		header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize."&status=".$status);
		exit;
	}
	for ($x=0; $x < sizeof($_POST[cid]); $x++){
		$arr=$_POST[cid];
		$res=getResult("banner_tbl"," where slno='$arr[$x]'");
		$dur=$res[duration];
		$exp_dt=date("Y-m-d", mktime(0,0,0,date("m"),date("d")+$dur,date("Y")));
		updateTable($table_name," status='Y', activationDate='$wdt', expiryDate='$exp_dt' where slno='$arr[$x]'");
	}
	$_SESSION[session_message]='Selected record(s) activated successfully.';
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize."&status=".$status);
	exit;
}

if($_POST[action]=="Deactivate"){
	if(sizeof($_POST[cid])==0) {
		$_SESSION[session_message]='Please select a record first.';
		header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize."&status=".$status);
		exit;
	}
	for ($x=0; $x < sizeof($_POST[cid]); $x++){
		$arr=$_POST[cid];
		updateTable($table_name," status='N', expiryDate='$wdt' where slno='$arr[$x]'");		
	}
	$_SESSION[session_message]='Selected record(s) deactivated successfully.';
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize."&status=".$status);
	exit;
}


if($id=="deactivate") {
	updateTable($table_name," status='N', expiryDate='$wdt' where slno='$recordID' and status='Y' ");	
	$_SESSION[session_message]=$record_type.' has been deactivated successfully.';
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize."&status=".$status);
	exit;
}

if($id=="activate") {
	$res=getResult("banner_tbl"," where slno='$recordID'","duration");
	$dur=$res[duration];
	$exp_dt=date("Y-m-d", mktime(0,0,0,date("m"),date("d")+$dur,date("Y")));
	updateTable($table_name," status='Y', activationDate='$wdt', expiryDate='$exp_dt' where slno='$recordID' and status='N' ");
	$_SESSION[session_message]=$record_type.' has been activated successfully.';
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize."&status=".$status);
	exit;
}

if ($id=="delete") {
	updateTable($table_name," status='D' where slno='$recordID'");
	$_SESSION[session_message]=$record_type.' has been deleted from the admin panel.';
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize."&status=".$status);
	exit;
}

admin_header("Manage $record_type");
?>
<table width="99%" border="0" align="center" cellpadding="0" cellspacing="0" valign="top">
 <tr>
	<td align="left" width="50%"><strong><b class="myTitle">Manage <?=$record_type;?>s</b></strong></td>
	<td align="right">
	 <form name="pgcolor_form" action="<?=$PHP_SELF?>" method="post" style="margin:0px;padding:0px;">
	 <table width="98%" border="0" align="center" cellpadding="0" cellspacing="0" valign="top">
	  <tr>
	   <td align="right">Records Per Page:
	    <select name="pagesize" onchange="this.form.submit();">
	     <option value="0">Select</option>
	     <?php
	     foreach($_GLOBALS[pgsize_arr] as $val) {
		     if(trim($pagesize)==trim($val)) {
			     echo '<option value="'.$val.'" selected>'.$val.'</option>';
		     }
		     else{
			     echo '<option value="'.$val.'">'.$val.'</option>';
		     }
	     }
	     ?>
	    </select>	    
		 </td>
		</tr>
	 </table>
	 </form>
	</td>
 </tr>
</table>
<hr size="2" width="99%" color="#DA251C">
 
<?php
if ($status!="") {
	$cond="where status='$status'";
	$link="&status=$status";
}
else {
	$cond="where 1";
}
$qry="select * from $table_name $cond";
$keyword=trim($keyword);
if($keyword!=""){
  $qry.=" and (personName LIKE '%$keyword%' or companyName LIKE '%$keyword%' or industry LIKE '%$keyword%' )"; 
}

if($sortName!=''){
	$qry.= " order by $sortName $sortBy";
}
else {
	$qry.= " order by slno desc";
}

$qry1= db_query($qry);
$reccnt = NumRows($qry1);
$qry.= " limit $start, $pagesize";
$qry= db_query($qry);
echo pageNavigation($reccnt,$pagesize,$start,$link);
print_message();
?>

<table width="100%"  border="0" cellpadding="6" cellspacing="2" class="lightBorder">
 <tr>
  <td class="lightGrayBg" align="left"><b>Search Banners</b><br>
  <form name="search2" method="POST" action="">
   Keyword: <input name="keyword" type="text" class="input" size="25" value="<?=$keyword;?>">
   <input type="hidden"  class="button" name="id" value="Search2">
   <input type="submit"  class="button" name="search" value="Search">	
  </form>
  </td>
  <td class="lightGrayBg">
  <b>Sort Banners<b><br>
  <form name="search3" method="POST" action="">
   <select name="sortName" size=1>
    <option value=''>--------Select---------</option>
    <option value='personName' <?php if($sortName=="personName"){ echo "selected";}?>>Person Name</option>
    <option value='companyName' <?php if($sortName=="companyName"){ echo "selected";}?>>Company Name</option>
    <option value='recvDate' <?php if($sortName=="recvDate"){ echo "selected";}?>>Receive Date</option>
    <option value='activationDate' <?php if($sortName=="activationDate"){ echo "selected";}?>>Activation Date</option>
    <option value='expiryDate' <?php if($sortName=="expiryDate"){ echo "selected";}?>>Expiry Date</option>    
   </select>
   <select name="sortBy">
    <option value=''>-----Sort By-----</option>
    <option value="asc" <?php if($sortBy=="asc"){ echo "selected";}?>>Ascending</option>
    <option value="desc" <?php if($sortBy=="desc"){ echo "selected";}?>>Descending</option>
   </select>
   <input type="hidden"  class="button" name="id" value="Search3">
   <input type="submit"  class="button" name="search" value="Sort">
  </form>
  </td>
  <td class="lightGrayBg">
   <b>Select Status</b><br/>
   <form name="sts_form" action="<?=$PHP_SELF?>" method="post" style="margin:0px;padding:0px;">
	 <table width="100%" border="0" align="left" cellpadding="0" cellspacing="0" valign="top">
	  <tr>
	   <td align="left">
	    <select name="status" onchange="this.form.submit();">
	     <option value="">All Banners</option>
	     <option value="Y" <?php if($status=="Y"){ echo "selected";}?>>Active</option>
	     <option value="N" <?php if($status=="N"){ echo "selected";}?>>InActive</option>
	     <option value="D" <?php if($status=="D"){ echo "selected";}?>>Deleted</option>
	    </select>
	    <input type="hidden" name="keyword" value="<?=$keyword;?>" />
	    <input type="hidden" name="pagesize" value="<?=$pagesize;?>" />
		 </td>
		</tr>
	 </table>
	 </form>
	</td> 
 </tr>
</table><br />

<?php
if($reccnt>0){
	?>
	<form name="formn" method="post" action="" enctype="multipart/form-data" onsubmit="return validate(this);">
	<table width="99%" border="0" cellpadding="2" cellspacing="2" class="lightBorder">
	 <tr height="25" bgcolor="#EAEAEA">
	  <td width="5%"><input type="checkbox" name="chk" onclick="CheckAll()" class="NoStyle"></td>
	  <td width="25%"><b>Sender Details</b></td>
	  <td width="30%"><b>Banner Details</td>
	  <td width="11%"><b>Validity</td>
	  <td width="11%"><b>Receive Date</b></td>
	  <td width="8%" align="center"><b> Status</b></td>
	  <td width="10%"><b>Options </td>
	 </tr>
	 <?php
	 $sl=$start+1;
	 while ($res=mysql_fetch_array($qry)) {
		 $res=ms_htmlentities_decode($res);		 
		 if($sl%2==0){
			 $color="f0f0f0";
		 }
		 else{
			 $color="f9f9f9";
		 }
		 ?>
		 <tr bgcolor="<?=$color;?>">
		  <td valign="top"><input type=checkbox name="cid[]" value="<?=$res[slno]?>" class="NoStyle"></td>
		  <td valign="top">
		   Name: <b><?=ucwords($res[personName]);?></b>
		   <br/>Company Name: <b><?=ucwords($res[companyName]);?></b>
		   <br/>Industry: <b><?=ucwords($res[industry]);?></b>
		  </td>
		  <td valign='top'>
		   <b>Banner URL</b>: <?=$res[bannerUrl];?>
		   <br/><b>Page</b>: <?=$res[page];?>
		   <br/><b>Position</b>: <?=$res[position];?>
		   <br/><b>Duration</b>: <?=$res[duration];?> Days
		  </td>
		  <td valign="top">
		   <?php
		   if($res[activationDate]!='0000-00-00') {
			   echo show_date($res[activationDate]);
		   }
		   if($res[expiryDate]!='0000-00-00') {
			   ?>
			   <br/>To<br/><?=show_date($res[expiryDate])?>
			   <?php
		   }
		   ?>
		  </td>
		  <td valign='top'><?=show_date($res[recvDate])?></td>
		  <td valign='top' align="center">
		   <?php
		   if($res[status]=='Y') {
			   ?>
			   <strong>Active</strong>
			   <?php
		   }
		   else if($res[status]=='D') {
			   ?>
			   <strong>Deleted</strong>
			   <?php
		   }
		   else {
			   ?>
			   <strong>Inactive</strong>
			   <?php
		   }
		   ?>
		  </td>
		  <td valign='top'>
		   <?php
		   if($res[status]=='Y') {
			   ?>
			   &nbsp;<a href="<?=SITE_ADMIN_URL?>/<?=$script_name;?>?id=deactivate&recordID=<?=$res[slno];?>&start=<?=$start;?>&pagesize=<?=$pagesize?>"><img src="<?=SITE_ADMIN_URL?>/images/active.gif" alt="Deactivate This <?=$record_type;?>" title="Deactivate This <?=$record_type;?>" border="0"></a>
			   <?php
		   }
		   if($res[status]=='N') {
			   ?>
			   &nbsp;<a href="<?=SITE_ADMIN_URL?>/<?=$script_name;?>?id=activate&recordID=<?=$res[slno];?>&start=<?=$start;?>&pagesize=<?=$pagesize?>"><img src="<?=SITE_ADMIN_URL?>/images/deactive.gif" alt="Activate This <?=$record_type;?>" title="Activate This <?=$record_type;?>" border="0"></a>
			   <?php
		   }
		   $file1=SITE_FS_PATH."/uploaded_files/banners/".$res[banner];
		   if(file_exists($file1)) {
			   $dimens=GetImageSize($file1);
			   if($dimens[0]>130) {
				   $dimens[0]=$dimens[0]+50;
				   $dimens[1]=$dimens[1]+80;
			   }
			   else{
				   $dimens[0]=200;
				   $dimens[1]=200;
			   }
			   ?>
			   &nbsp;<a href="javascript:void(0);" onClick="openwin('<?=SITE_ADMIN_URL;?>/popup.php?img=banners/<?=$res[banner];?>', '<?=$dimens[0];?>', '<?=$dimens[1];?>')"><img src="<?=SITE_ADMIN_URL?>/images/preview.gif" alt="View This <?=$record_type;?> Details" title="View This <?=$record_type;?> Details" border="0"></a>
			   <?php
		   }
		   ?>		   
		   &nbsp;<a href="<?=$script_name;?>?id=delete&recordID=<?=$res[slno]?>&start=<?=$start?>&pagesize=<?=$pagesize?>" onClick="return confirm('Are you sure you want to delete this record')"><img src="<?=SITE_ADMIN_URL?>/images/cancel.gif" alt="Delete This <?=$record_type;?>" title="Delete This <?=$record_type;?>" border="0"></a>
		  </td>
		 </tr>
		 <?php
		 $sl++;
	 }
	 ?>
	 <tr>
	  <td colspan="7">
	   <?php
	   if($status=="Y"){
		   ?>
		   <input type="submit" name="action" value="Delete"  onclick="return chkdelete();" class="button">
		   <input type="submit" name="action" value="Deactivate"  onclick="return chkactivate();" class="button">
		   <?php
	   }
	   else if ($status=="N"){
		   ?>
		   <input type="submit" name="action" value="Delete"  onclick="return chkdelete();" class="button">
		   <input type="submit" name="action" value="Activate"  onclick="return chkactivate();" class="button">
		   <?php
	   }
	   else if ($status=="D"){		   
	   }
	   else {
		   ?>
		   <input type="submit" name="action" value="Delete"  onclick="return chkdelete();" class="button">
		   <!--
		   <input type="submit" name="action" value="Deactivate"  onclick="return chkactivate();" class="button">
		   <input type="submit" name="action" value="Activate"  onclick="return chkactivate();" class="button">
		   !-->
		   <?php
	   }
	   ?>
	   <input type="hidden" name="start" value="<?=$start;?>">
	   <input type="hidden" name="status" value="<?=$status;?>">
	   <input type="hidden" name="pagesize" value="<?=$pagesize;?>">
	  </td>
	 </tr>
	</table><br>
	<?php
	/*
	if ($add_id!="new") {
		?>
		<table width="100%"  border="0" cellpadding="6" cellspacing="2" class="lightBorder">
		 <tr>
		  <td width="10%" align="left"><b>Subject *</b></td>
		  <td align="left"><input type="text" name="subject" value="" size=100></td>
		 </tr>
		 <tr>
		  <td valign="top" align="left"><b>Message *</b></td>
		  <td align="left"><textarea name="message" cols=100 rows=10></textarea></td>
		 </tr>
		 <tr>
		  <td class="lightGrayBg">&nbsp;</td>
		  <td align="left" class="lightGrayBg"><input class="button" type="submit" name="id" value="Sendmail" onclick="return chksendmail(this);"></td>
		 </tr>
		</table>
		</form>
		<script type="text/javascript">
		var oFCKeditor=new FCKeditor('message');
		oFCKeditor.BasePath="fckeditor/";
		oFCKeditor.Height="350px";
		oFCKeditor.ReplaceTextarea();
		</script>
		<?php
	}
	*/
}
else {
	?>
	<table width="100%"  border="0" cellpadding="6" cellspacing="2" class="lightBorder">
	 <tr><td><b>No Record Found.............</b></td></tr>
	</table>
	<?php
}

admin_footer();
exit;
?>