<?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 Success Story";
$record_type="Manage Success Story";
$script_name="manage_success_stories.php";
$table_name="safe_business_tbl";

####################################################
if($id=='remove_image'){
	mysqli_query($dbconn,"UPDATE $table_name SET $field='' where slno = '$catID'");
	$_SESSION[session_message]=$record_type.' has been updated successfully';
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&catID=".$catID."&pagesize=".$pagesize);
	exit;
}
if($id=="add_record") {
	$description = str_ireplace("'", "&apos;", $description);
	if ($title!="" && trim(strip_tags($description))!="") {
		$numCnt=getCount($table_name," where title='$title'");
		if($numCnt>0) {	
			$_SESSION[session_message]=$record_type.' already exist';
		}
		else{
			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."&parentID=".$parentID."&catID=".$catID."&pagesize=".$pagesize);
				exit;
			}
			if($_FILES[image][name]!=""){
				$img_name="testimonial".date("s");
				$img=upload_file($img_name,"image","uploaded-files/testimonials");	
			}
		}		
			$maxRes=db_fetch_array(db_query("select max(sbtOrder) from $table_name"));
			$sbtOrder=$maxRes[0]+1;
			$sql=db_query("insert into $table_name set title='$title',person_city='$person_city', description='$description', image='$img', sbtOrder='$sbtOrder', recvDate=now()");
			$_SESSION[session_message]='New '.$record_type.' has been added successfully';
			header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
			exit;
		}
	}
	else {
		$_SESSION[session_message]='Record not added : Title and Description are mandatory';
		header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
		exit;
	}
}

if($id=="update_record") {
	$description = str_ireplace("'", "&apos;", $description);
	if ($title!="" && trim(strip_tags($description))!="") {
		$res=getResult($table_name," where slno='$recordID'");
		if($res[title]!=$title) {
			$numCnt=getCount($table_name," where title='$title'");
			if($numCnt > 0) {
				$_SESSION[session_message]=$record_type.' already exist';
				header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
				exit;
			}
		}
		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."&parentID=".$parentID."&catID=".$catID."&pagesize=".$pagesize);
			exit;
		}
		if($_FILES[image][name]!=""){
			$categoryRes=getResult($table_name," where slno='$recordID'","image");
			$filename=$categoryRes['image'];
			if($filename!="") {
				$file=SITE_DIR_PATH."/uploaded-files/testimonials/".$filename;
				if(file_exists($file)) {
					unlink($file);
				}
			}
			$img_name="testimonial".date("s");
			$img=upload_file($img_name,"image","uploaded-files/testimonials");
			updateTable($table_name," image='$img' where slno='$recordID'");
		}
	}
		updateTable($table_name," title='$title',person_city='$person_city', description='$description' where slno='$recordID'");
		$_SESSION[session_message]=$record_type.' has been updated successfully';
		header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
		exit;
	}
	else {
		$_SESSION[session_message]='Record not updated : Title and Description are mandatory';
		header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
		exit;
	}
}

if($action=="Update"){
	reset($_POST);
	while (list($key,$val)=each($_POST)) {
		if (substr($key,0,6)=="order_") {
			$key1 = substr($key,6,strlen($key)-6);
			updateTable($table_name," sbtOrder='$val' where slno='$key1'");
		}
	}
	$_SESSION[session_message]=$record_type.' order has been updated successfully';
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&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."&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."&pagesize=".$pagesize);
	exit;
}

if ($id=="delete_record") {
// 	mysqli_query($dbconn,"delete from $table_name where slno='$recordID'");
	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);
	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="pg_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
$qry1= db_query("select * from $table_name");
$reccnt = NumRows($qry1);
$qry=  mysqli_query($dbconn, "SELECT * FROM $table_name");
print_message();
?>

<form name="cpFrm" method="post" action="" enctype="multipart/form-data">
<table width="99%" border="0" cellpadding="3" cellspacing="2" class="lightBorder">
 <?php
 if($reccnt>0) {
	 ?>
<thead>
	 <tr height="25" bgcolor="#EAEAEA">
	  <td width="5%" align="center"><strong>S.No.</strong></td>
	  <td width="73%" align="left"><strong>Success Story</strong></td>
	  <td width="12%" align="center"><strong> Display Order</strong></td>
	  <td width="10%" align="left"><strong>Options</strong></td>
	 </tr>
</thead>
	 <?php
	 $sl=$start+1;
	 while($arr=mysqli_fetch_array($qry)) {
		 if($sl%2==0){
			 $color="f0f0f0";
		 }
		 else{
			 $color="f9f9f9";
		 }
		 ?>
	<tbody>
		 <tr bgcolor="<?=$color;?>">
		  <td align="center" valign="top"><strong><?=$sl;?></strong></td>
		  <td align="left" valign="top">
		   <strong>Title : </strong><?=$arr['title'];?><br/>
		   <strong>Description : </strong><?=$arr['description'];?>
		  </td>
		  <td align="center" valign="top"><input name="order_<?=$arr[slno];?>" class="txtfieldadmin" type="text" size="5" value="<?=$arr[sbtOrder];?>"></td>
		  <td align="left" valign="top">
		   <?php
		   if($arr[status]=='Y') {
			   ?>
			   &nbsp;<a href="<?=SITE_ADMIN_URL?>/<?=$script_name;?>?id=deactivate_record&recordID=<?=$arr[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($arr[status]=='N') {
			   ?>
			   &nbsp;<a href="<?=SITE_ADMIN_URL?>/<?=$script_name;?>?id=activate_record&recordID=<?=$arr[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
		   }
		   ?>
		   &nbsp;<a href="<?=$script_name;?>?id=edit_record&start=<?=$start;?>&recordID=<?=$arr[slno];?>&pagesize=<?=$pagesize;?>#bot"><img src="<?=SITE_ADMIN_URL?>/images/edit.gif" alt="Edit This <?=$record_type;?>" title="Edit This <?=$record_type;?>" border="0"></a>
		   &nbsp;<a href="<?=$script_name;?>?id=delete_record&start=<?=$start;?>&recordID=<?=$arr[slno];?>&pagesize=<?=$pagesize;?>" onClick="return confirm('Are you sure to delete this record...');"><img src="<?=SITE_ADMIN_URL?>/images/cancel.gif" alt="Delete This <?=$record_type;?>" border="0">
		  </td>
		 </tr>
		 <?php
		 $sl++;
	 }
	 ?>
	 <tr bgcolor="<?=$color;?>">
	  <td colspan="2"></td>
	  <td align="center"><input type="submit" name="Submit" value="Update" class="button"><input type="hidden" name="action" value="Update" class="button"></td>
	  <td></td>
	 </tr>
	 <?php
 }
 else {
	 ?>
	 <tr><td><b>No Record Found........</b></td></tr>
	 <?php
 }
 ?>
        </tbody>	 
</table><br />
</form>
<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="faqFrm" method=post action="<?=$script_name;?>" enctype="multipart/form-data" onsubmit="return validate(this);">
		 <?php
	 if($recordID=="" || $id!="edit_record") {
		 ?>
        <div class="row mb-3 text-center">
          <h4 class="myTitle">Add New <?=$record_type;?></h4>
         </div>
    <?php
	 }
	 else {
		 ?>
        <div class="text-left">
            <b>Edit <?=$record_type;?></b>
        </div>
           <div class="text-right">
               <a href="<?=$script_name;?>"><b>Add New <?=$record_type;?></b></a>
        </div>
        		 <?php
	 }
	 $result=getResult($table_name," where slno='$recordID'");
	 ?>
       <div class="form-group">
       <label>Person Name <span>*</span></label>
           <input name="title" class="form-control" value="<?=$result['title'];?>"/>
        </div> 
        <div class="form-group">
       <label>Person Belong From <span>*</span></label>
           <input name="person_city" class="form-control" value="<?=$result['person_city'];?>" />
        </div>
        <div class="form-group">
       <label>Sucess Story<span>*</span></label>
           <textarea name="description" class="form-control" rows="7" ><?=$result['description'];?></textarea>
        </div>
        <div class="form-group">
            <label>Photo</label>
            <input name="image" type="file" class="form-control">
               <?php
   if($result[image]!=""){
	   $file1=SITE_DIR_PATH."/uploaded-files/testimonials/".$result[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;
		   }
		   ?>
		   <a href="javascript:void(0);" onClick="openwin('<?=SITE_ADMIN_URL;?>/popup_new.php?img=testimonials/<?=$result[image];?>', '<?=$dimens[0];?>', '<?=$dimens[1];?>')" class="linkgray fontbold"><strong>&nbsp;View Image</strong></a>&nbsp; <strong><a href="<?= SITE_ADMIN_URL ?>/<?= $script_name?>?catID=<?= $recordID?>&id=remove_image&field=image">Remove</a></strong>
		   <?php
	   }
   }
   ?>
        </div>
        <div class="form-group">
             <?php
	   if($recordID!="") {
		   ?>
		   <input type="submit" name="submit" value="Update <?=$record_type;?>" class="btn btn-success">
		   <input type="hidden" name="recordID" value="<?=$result[slno];?>">
		   <input type="hidden" name="id" value="update_record">
		   <?php
	   }
	   else{
		   ?>
		   <input type="submit" name="submit" value="Add New <?=$record_type;?>" class="btn btn-success">
		   <input type="hidden" name="id" value="add_record">
		   <?php
	   }
	   ?>
	   <input type="hidden" name="start" value="<?=$start;?>">
	   <input type="hidden" name="pagesize" value="<?=$pagesize;?>">
        </div>
	</form>
	<script type="text/javascript">
	var oFCKeditor = new FCKeditor("description");
	oFCKeditor.BasePath = "fckeditor/";
	oFCKeditor.Width="650px";
	oFCKeditor.Height="400px";
	oFCKeditor.ReplaceTextarea();
	</script>	
	<?php
}

admin_footer();
exit;
?>