<?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;

$heading="Manage Safe Business Images";
$record_type="Safe Business Image";
$script_name="manage_safe_business_images.php";
$table_name="safe_business_images_tbl";
#################### Add Record ##################
if($_REQUEST[action]=="add_record") {
	if($_FILES[image][name]!="") {
		$file_extension=strtolower(getExtension($_FILES[image][name]));
		if($file_extension!="jpg" && $file_extension!="gif" && $file_extension!="jpeg" && $file_extension!="png") {
			$_SESSION['session_message']='Please upload image (.jpg,.gif,.png Format Only)';
			header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&recordID=".$recordID."&pagesize=".$pagesize);
			exit;
		}
		if($_FILES[image][name]!="") {
			$img_name="SBI_".date("Y_m_d_H_i_s");
			$img=upload_file($img_name,"image","uploaded_files/safe_business_images");
		}
	}
	
	$sql=db_query("insert into $table_name set image='$img', caption='$caption'");
	$_SESSION['session_message']=$record_type.' has been added successfully';
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&recordID=".$recordID."&pagesize=".$pagesize);
	exit;
}

################ Update Record ##################

if($_REQUEST[action]=="update_record") {
	@extract($_REQUEST);
	
	if($_FILES[image][name]!="") {
		$file_extension=strtolower(getExtension($_FILES[image][name]));
		if($file_extension!="jpg" && $file_extension!="gif" && $file_extension!="jpeg" && $file_extension!="png") {
			$_SESSION['session_message']='Please upload image (.jpg,.gif,.png Format Only)';
			header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&recordID=".$recordID."&pagesize=".$pagesize);
			exit;
		}
		if($_FILES[image][name]!="") {
			$img_name="SBI_".date("Y_m_d_H_i_s");
			$img=upload_file($img_name,"image","uploaded_files/safe_business_images");
				updateTable($table_name," image='$img' where slno='$recordID'");
			}
		}
		
	updateTable($table_name," caption='$caption' where slno='$recordID'");
  $_SESSION['session_message']=$record_type.' has been updated successfully';
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&recordID=".$recordID."&pagesize=".$pagesize);
	exit;
}

####################################################

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

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

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

########################################################
admin_header();	
?>
<table width="99%" border="0" align="center" cellpadding="0" cellspacing="0" valign="top">
 <tr>
	<td align="left" width="50%" class="myTitle"><?=$heading;?></td>
	<td align="right">
	 <form name="pgflavor_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
##################Product Search Option###################
?>
<form name="srch" method="post" action="" style="display:inline" enctype="multipart/form-data">
<table width="100%"  border="0" cellpadding="0" cellspacing="2">
 <tr>
  <td align="right"><strong>Search Safe Business Image</strong> [by caption etc.] :-
   <input type="text" name="keyword" value="" size="40">
   <input type="submit" class="button" value="GO">   
  </td>
 </tr>   
</table>
</form>

<?php
$cond="";
if (strlen($keyword)) {
	?>
	<br/>
	<div style="width:99%;">
	 <div style="float:left;"><strong>Search For :</strong> <?=$keyword;?></div>
	 <div style="float:right;"><a href="<?=$script_name;?>"><strong>View All</strong></a></div>
	</div> 
	<?php
	$cond=" and caption like '%$keyword%'";
}
$qry1= db_query("select * from $table_name where status!='D' $cond ");
$reccnt = NumRows($qry1);
$qry= db_query("select * from $table_name where status!='D' $cond limit $start, $pagesize");
echo pageNavigation($reccnt,$pagesize,$start);
print_message();
?>

<form name="formn" method="post" action="" enctype="multipart/form-data">
<table width="100%"  border="0" cellpadding="2" cellspacing="2" class="lightBorder">
 <?php
 if($reccnt>0) {
	 ?>
	 <tr bgcolor="#EAEAEA">
	  <td width="5%" height="20" align="left"><strong>S.No.</strong></td>
	  <td width="35%" align="left"><strong>Safe Business Image</strong></td>
	  <td width="40%" align="center"><strong>Caption</strong></td>
	  <td width="10%" align="center"><strong>Status</strong></td>
	  <td width="10%" align="left"><strong>Options</strong></td>
	 </tr>
	 
	 <?php
	 $sl=$start+1;
	 while($arr=mysql_fetch_array($qry)) {
		 $arr=ms_htmlentities_decode($arr);
		 if($sl%2!=0) {
			 $color="#f9f9f9";
		 }
		 else {
			 $color="#f0f0f0";
		 }
		 ?>
		 
		 <tr  bgcolor="<?=$color;?>">
		  <td align="left"><?=$sl;?>.</td>
		  <td align="center"><img src="<?=SITE_URL_PATH;?>/uploaded_files/safe_business_images/<?=$arr['image'];?>" alt="" vspace="5" /></td>
		  <td align="left"><?=ucfirst($arr['caption']);?></td>
		  <td align="center">
		   <?php
		   if($arr['status']=="Y") {
			   ?>
			   <span class='icon'>Active</span>
			   <?php
		   }
		   else {
			   ?>
			   <span class='icon'>Inactive</span>
			   <?php
		   }
		   ?>
		  </td>
		  <td class="<?=$class?>" align="left">
		   &nbsp;<a href="<?=SITE_ADMIN_URL?>/<?=$script_name;?>?id=edit_record&start=<?=$start;?>&recordID=<?=$arr[slno];?>&pagesize=<?=$pagesize;?>"><img src="<?=SITE_ADMIN_URL?>/images/edit.gif" alt="Edit This <?=$record_type;?>" title="Edit This <?=$record_type;?>" border="0"></a>
		   <?php
		   if($arr['status']=="Y") {
			   ?>
			   &nbsp;<a href="<?=SITE_ADMIN_URL?>/<?=$script_name;?>?id=deactivate_record&start=<?=$start;?>&recordID=<?=$arr[slno];?>&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($arr['status']=="N") {
			   ?>
			   &nbsp;<a href="<?=SITE_ADMIN_URL?>/<?=$script_name;?>?id=activate_record&start=<?=$start;?>&recordID=<?=$arr[slno];?>&pagesize=<?=$pagesize;?>"><img src="<?=SITE_ADMIN_URL?>/images/inactive.gif" alt="Activate This <?=$record_type;?>" title="Activate This <?=$record_type;?>" border="0"></a>
			   <?php
		   }
		   ?>
		   
		   &nbsp;<a href="<?=SITE_ADMIN_URL?>/<?=$script_name;?>?id=delete_record&start=<?=$start;?>&recordID=<?=$arr[slno];?>&pagesize=<?=$pagesize;?>" onClick="return confirm('Are you sure to delete this <?=$record_type;?>...');"><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++;
	 }
 }
 else {
	 ?>
	 <tr><td><strong>No Record Found........</strong></td></tr>
	 <?php
 }
 ?>
</table>
</form><br />
<table width="96%" align="center" cellpadding="0" cellspacing="0" border="0">
 <tr><td align="right"><input type="button" onclick="window.location.href='<?=SITE_ADMIN_URL;?>/<?=$script_name;?>?id=add_new#bot'" value="Add New <?=$record_type;?>" class="button"></td></tr>
</table>

<?php
if ($id == "add_new" || $id == "edit_record") {
	?>
	<a name="bot"></a>
	<br/><br/>
	<form name="cpFrm" method="post" action="" enctype="multipart/form-data" onsubmit="return validate(this);">
	<table width="99%"  border="1" cellpadding="5" cellspacing="2" class="lightBorder">
	 <?php
	 if($recordID=="" || $id!="edit_record") {
		 ?>
		 <tr bgcolor="#EAEAEA"><td colspan="2" align="left" height="20"><strong><a name="bot">Add New <?=$record_type;?></a></strong></td></tr>
		 <?php
	 }
	 else {
		 ?>
		 <tr bgcolor="#EAEAEA">
		  <td align="left" height="20"><strong>Edit <?=$record_type;?></strong></td>
		  <td align="right" height="20"><a href="<?=$script_name;?>"><strong>Add New <?=$record_type;?></strong></a></td>
		 </tr>
		 <?php
	 }
	 $res=getResult($table_name," where slno='$recordID'");
	 if($res['slno']!="") {
		 @extract($res);	 
	 }
	 else {
		 @extract($_POST);
	 }
	 ?>
	 <tr><td colspan="2" align="right"><font color="red">*</font> fields are mandatory</td></tr>
	 <tr>
	  <td class="brdrbtm" width="20%"><strong>Safe Business Image*</strong></td>
	  <td class="brdrbtm">
	   <?php
	   if ($id!="edit_record") {
		   ?>
		   <input name="image" type="file" class="button" style="width:300px;" id="NOBLANK~Please upload safe business image~DM~">
		   <?php
	   }
	   else {
		   ?>
		   <input name="image" type="file" class="button" style="width:300px;">
		   <?php
	   }
	   
	   if($res['image']!="") {
		   $file1=SITE_DIR_PATH."/uploaded_files/safe_business_images/".$res['image'];
		   if(file_exists($file1)) {
			   $dimens=GetImageSize($file1);
			   if($dimens[0]>130) {
				   $dimens[0]=$dimens[0]+30;
				   $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=safe_business_images/<?=$res['image'];?>', '<?=$dimens[0];?>', '<?=$dimens[1];?>')" class="linkgray fontbold"><strong>View Image</strong></a>
			   <?php
		   }
	   }
	   ?>
	   <br/>Image Size: Width=200px fixed &amp; Height=60px adjustable for better display
	  </td>
	 </tr>
	 <tr>
	  <td class="brdrbtm"><strong>Caption</strong></td>
	  <td class="brdrbtm"><textarea name="caption" class="txtfieldadmin" rows="2" style="width:295px;"><?=$caption?></textarea></td>
	 </tr>
	 <tr>
	  <td></td>
	  <td class="brdrbtm">
	   <?php
	   if($res['slno']=="") {
		   ?>
		   <input type="submit" name="add_record" class="button" value="Add <?=$record_type;?>"><input type="hidden" name="action" value="add_record">
		   <?php
	   }
	   else {
		   ?>
		   <input type="submit" name="update_record" class="button" value="Update <?=$record_type;?>"><input type="hidden" name="action" value="update_record">
		   <?php
	   }
	   ?>
	   &nbsp;&nbsp;<input type="button" name="cancel" class="button" value="Cancel" onclick="javascript: window.location.href='<?=SITE_ADMIN_URL;?>/<?=$script_name;?>?recordID=<?=$slno;?>&start=<?=$start;?>&pagesize=<?=$pagesize;?>'">
	  </td>
	 </tr>
	</table>
	</form>	
	<?php
}

admin_footer();
exit;
?>