<?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") {
	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=str_ireplace(' ','-',$title.date("his"));
				$img=upload_file($img_name,"image","uploaded-files/testimonials");	
			}
		}		

		$title = str_ireplace("'", "&apos;", $title);
	$description = str_ireplace("'", "&apos;", $description);
			$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") {
	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=str_ireplace(' ','-',$title.date("his"));
			$img=upload_file($img_name,"image","uploaded-files/testimonials");
			updateTable($table_name," image='$img' where slno='$recordID'");
		}
	}

	$title = str_ireplace("'", "&apos;", $title);
	$description = str_ireplace("'", "&apos;", $description);
		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();
?>
<div class="content-wrapper">

<div class="page-header">
        <h3 class="page-title">
            <?=$heading;?>
        </h3>
        <nav aria-label="breadcrumb">
		<ol class="breadcrumb">
                <li class="breadcrumb-item"><a href="welcome.php">Admin</a></li>
                <li class="breadcrumb-item active" aria-current="page">Manage Testimonials</li>
                </ol>
        </nav>
    </div>
<div class="row">
            <div class="col-12 grid-margin stretch-card">
              <div class="card">
                <div class="card-body">    


<?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 id="order-listing" class="table table-bordered table-hover">
 <?php
 if($reccnt>0) {
	 ?>
<thead>
	 <tr>

	  <th><strong>S.No.</strong></th>
	  <th><strong>Success Story</strong></th>
	  <th><strong> Display Order</strong></th>
	  <th><strong>Options</strong></th>
	 </tr>
</thead>
	 <?php
	 $sl=$start+1;
	 while($arr=mysqli_fetch_array($qry)) {
		 if($sl%2==0){
			 $color="f0f0f0";
		 }
		 else{
			 $color="f9f9f9";
		 }
		 ?>
	<tbody>
		 <tr>
		  <td ><strong><?=$sl;?></strong></td>
		  <td>
		   <strong>Title : </strong><?=$arr['title'];?><br/>
		   <strong>Description : </strong><?=$arr['description'];?>
		  </td>
		  <td><input name="order_<?=$arr[slno];?>" class="txtfieldadmin" type="text" size="5" value="<?=$arr[sbtOrder];?>"></td>
		  <td>
		   <!-- <?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"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-edit-3"><path d="M12 20h9"></path><path d="M16.5 3.5a2.121 2.121 0 0 1 3 3L7 19l-4 1 1-4L16.5 3.5z"></path></svg></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...');"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-trash-2"><polyline points="3 6 5 6 21 6"></polyline><path d="M19 6l-2 14a2 2 0 0 1-2 2H9a2 2 0 0 1-2-2L5 6m5 0V4a2 2 0 0 1 2-2h0a2 2 0 0 1 2 2v2"></path><line x1="10" y1="11" x2="10" y2="17"></line><line x1="14" y1="11" x2="14" y2="17"></line></svg></a>
		  </td>
		 </tr>
		 <?php
		 $sl++;
	 }
	 ?>
	 <tr>
	  <td colspan="2"></td>
	  <td><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>

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

<?php
if ($id == "add_new" || $id == "edit_record") {
	?>
	<div class="row">
            <div class="col-12 grid-margin stretch-card">
              <div class="card">
                <div class="card-body">
	<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>
	</div>
	</div>
	</div>
	</div>
	<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;
?>