<?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=10:$pagesize;

$heading="Manage Success Stories";
$record_type="Success Story";
$script_name="admin_staff_manage_success_stories.php";
$table_name="testimonial_tbl";
################## Add Record #########################
if($action=="add_record") {
	$numCnt=getCount($table_name," where name='$name' and email='$email' and status!='D'");
	if($numCnt>0) {	
		$_SESSION['session_message']=$record_type.' already exist';
	}
	else{
		$form_check_alert=dynamic_check_form_values(array("rp-2-Please enter person name" => "".$name."", "rp-4-Please enter valid email-ID" => "".$email_id."", "rp-11-Please enter success story title" => "".$title."", "rp-11-Please enter success story content" => "".$content.""), $validation_ans_arr);
		
		if($_FILES[image][name]!="") {
			$file_extension=strtolower(getExtension($_FILES[image][name]));
			if($file_extension!="jpg" && $file_extension!="gif" && $file_extension!="jpeg" && $file_extension!="png") {
				$form_check_alert .="<li>Please upload small image(.jpg,.gif,.png Format Only)</li>";
			}
		}
		if(!strlen($form_check_alert)>0) {
			if($_FILES[image][name]!="") {
				$img_name="success_story_".date("Y_m_d_H_i_s");
				$ss_image=upload_file($img_name,"image","uploaded_files/success_story_images");
				
				$width="100";
				$height="100";
				$source_folder=SITE_DIR_PATH."/uploaded_files/success_story_images/";
				$destination_folder=SITE_DIR_PATH."/uploaded_files/success_story_images/thumbs/";
				saveResizedImage($ss_image, $width, $height, $source_folder, $destination_folder);
			}
			db_query("insert into $table_name set name='$name', email='$email_id', title='$title', content='$content', image='$ss_image', addedBy='".$_SESSION['MyAdminUserSLNO']."', recvDate=now()");
			$_SESSION['session_message']=$record_type." has been added sucessfully.";
			header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
			exit;
		}
		else{
			$msg=html_head();
			$msg.=$form_check_alert;
			$msg.=html_foot();
		}
	}
}


################### Update Record ######################
if($action=="update_record" && $tsHeading!="") {
	$numCnt=getCount($table_name," where name='$name' and email='$email_id' and status!='D' and slno!='$recordID'");
	if($numCnt>0) {	
		$_SESSION['session_message']=$record_type.' already exist';
	}
	else{
		$form_check_alert=dynamic_check_form_values(array("rp-2-Please enter person name" => "".$name."", "rp-4-Please enter valid email-ID" => "".$email_id."", "rp-11-Please enter success story title" => "".$title."", "rp-11-Please enter success story content" => "".$content.""), $validation_ans_arr);
		
		if($_FILES[image][name]!="") {
			$file_extension=strtolower(getExtension($_FILES[image][name]));
			if($file_extension!="jpg" && $file_extension!="gif" && $file_extension!="jpeg" && $file_extension!="png") {
				$form_check_alert .="<li>Please upload small image(.jpg,.gif,.png Format Only)</li>";
			}
		}
		if(!strlen($form_check_alert)>0) {
			if($_FILES[image][name]!="") {
				$tsRes=getResult($table_name," where slno='$recordID'","image");
				$filename=$tsRes['image'];
				if($filename!="") {
					$file=SITE_DIR_PATH."/uploaded_files/success_story_images/".$filename;
					if(file_exists($file)) {
						unlink($file);
					}
					
					$file_thumb=SITE_DIR_PATH."/uploaded_files/success_story_images/thumbs/".$filename;
					if(file_exists($file_thumb)) {
						unlink($file_thumb);
					}
				}
				
				$img_name="success_story_".date("Y_m_d_H_i_s");
				$ss_image=upload_file($img_name,"image","uploaded_files/success_story_images");
				
				$width="100";
				$height="100";
				$source_folder=SITE_DIR_PATH."/uploaded_files/success_story_images/";
				$destination_folder=SITE_DIR_PATH."/uploaded_files/success_story_images/thumbs/";
				saveResizedImage($ss_image, $width, $height, $source_folder, $destination_folder);
				updateTable($table_name, "image='$ss_image' where slno='$recordID'");
			}
			
			updateTable($table_name, "name='$name', email='$email_id', title='$title', content='$content', recvDate=now() 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{
			$msg=html_head();
			$msg.=$form_check_alert;
			$msg.=html_foot();
		}
	}
}

if($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];
		
		$tsRes=getResult($table_name," where slno='$arr[$x]'","image");
		$filename=$tsRes['image'];
		if($filename!="") {
			$file=SITE_DIR_PATH."/uploaded_files/success_story_images/".$filename;
			if(file_exists($file)) {
				unlink($file);
			}
			
			$file_thumb=SITE_DIR_PATH."/uploaded_files/success_story_images/thumbs/".$filename;
			if(file_exists($file_thumb)) {
				unlink($file_thumb);
			}
		}
		
		db_query("delete from $table_name 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);
	exit;
}

if($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");
		exit;
	}
	for ($x=0; $x < sizeof($_POST[cid]); $x++){
		$arr=$_POST[cid];
		updateTable($table_name," status='Y' where slno='$arr[$x]'");
	}
	$_SESSION['session_message']='Selected record(s) activated successfully.';
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
	exit;
}

if($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");
		exit;
	}
	for ($x=0; $x < sizeof($_POST[cid]); $x++){
		$arr=$_POST[cid];
		updateTable($table_name," status='N' where slno='$arr[$x]'");
	}
	$_SESSION['session_message']='Selected record(s) deactivated successfully.';
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
	exit;
}


if($id=="deactivate") {
	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") {
	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") {
	$tsRes=getResult($table_name," where slno='$recordID'","image");
	$filename=$tsRes['image'];
	if($filename!="") {
		$file=SITE_DIR_PATH."/uploaded_files/success_story_images/".$filename;
		if(file_exists($file)) {
			unlink($file);
		}
		
		$file_thumb=SITE_DIR_PATH."/uploaded_files/success_story_images/thumbs/".$filename;
		if(file_exists($file_thumb)) {
			unlink($file_thumb);
		}
	}
				
	db_query("delete from $table_name where slno='$recordID'");
	$_SESSION['session_message']=$record_type.' is deleted successfully';
	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="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
$cond.=" and addedBy='".$_SESSION['MyAdminUserSLNO']."'";
$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 order by slno desc limit $start, $pagesize");
echo pageNavigation($reccnt,$pagesize,$start);
print_message();

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%" align="left"><input type="checkbox" name="chk" onclick="CheckAll()" class="NoStyle"></td>
	  <td width="30%" align="left"><strong>Sender Details</strong></td>
	  <td width="38%" align="left"><strong>Comments / Remarks</strong></td>
	  <td width="10%" align="left"><strong>Date</strong></td>
	  <td width="7%" align="center"><strong>Image</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="f0f0f0";
		 }
		 else{
			 $color="f9f9f9";
		 }
		 ?>
		 <tr bgcolor="<?=$color;?>">
		  <td valign="top" align="left"><input type=checkbox name="cid[]" value="<?=$arr[slno]?>" class="NoStyle"></td>
		  <td valign="top" align="left">
		   Name: <strong><?=$arr[name];?></strong>
		   <?php
		   if($arr[replyStatus]=="Y"){
			   ?>
			   <span class='icon'>Replied</span>
			   <?php
		   }
		   ?>
		   <br>Email: <?php echo $arr[email];
		   if($arr[title]!=""){
			   ?>
			   <br>Title: <?php echo ucwords($arr[title]);
		   }
		   ?>
		  </td>
		  <td valign="top" align="left"><?=nl2br($arr[content]);?></td>
		  <td valign="top" align="left"><?=show_date($arr[recvDate])?></td>
		  <td valign="top" align="center">
		   <?php
		   if($arr['image']!="") {
			   $file1=SITE_DIR_PATH."/uploaded_files/success_story_images/".$arr['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.php?img=success_story_images/<?=$arr['image'];?>', '<?=$dimens[0];?>', '<?=$dimens[1];?>')" class="linkgray fontbold"><strong>View</strong>
				   <?php
			   }
		   }
		   ?>
		  </td>
		  <td valign="top" align="left">
		   <?php
		   if($arr[status]=='Y') {
			   ?>
			   &nbsp;<a href="<?=SITE_ADMIN_URL?>/<?=$script_name;?>?id=deactivate&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&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];?>#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&recordID=<?=$arr[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="5">
	   <input type="submit"  class="button" name="action" value="Delete"  onclick="return chkdelete();">
	   <input type="submit"  class="button" name="action" value="Activate"  onclick="return chkactivate();">
	   <input type="submit"  class="button" name="action" value="Deactivate"  onclick="return chkactivate();">
	   <input type="hidden" name="start" value="<?=$start;?>">
	   <input type="hidden" name="pagesize" value="<?=$pagesize;?>">
	  </td>
	 </tr>
	</table>
	<?php
}
else {
	?>
	<table width="100%"  border="0" cellpadding="6" cellspacing="2" class="lightBorder">
	 <tr><td><strong>No Record Found.............</strong></td></tr>
	</table>
	<?php
}

echo $msg;
?>
<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="tsFrm" method="post" action="" enctype="multipart/form-data" onsubmit="return validate(this) && validate_date();">
	<table width="99%"  border="1" cellpadding="2" cellspacing="2" class="lightBorder">
	 <?php
	 if($recordID=="" || $id!="edit_record") {
		 ?>
		 <tr bgcolor="#EAEAEA"><td colspan="2" align="left" class="PadFive" height="20"><b><a name="bot">Add New <?=$record_type;?></a></b></td></tr>
		 <?php
	 }
	 else {
		 ?>
		 <tr bgcolor="#EAEAEA">
		  <td align="left" class="PadFive" height="20"><b>Edit <?=$record_type;?></b></td>
		  <td align="right" class="PadFive" height="20"><a href="<?=$script_name;?>"><b>Add New <?=$record_type;?></b></a></td>
		 </tr>
		 <?php
	 }
	 $res=getResult($table_name," where slno='$recordID'");
	 @extract($res);
	 ?>
	 <tr><td colspan="2" align="right"><font color="red">*</font> fields are mandatory</td></tr>
	 <tr>
	  <td class="brdrbtm" width="20%"><b>Person Name <font color="red">*</font></b></td>
	  <td class="brdrbtm"><input type="text" name="name" style="width:500px;" value="<?=$name;?>" id="NOBLANK~Please enter your name~DM~" /></td>
	 </tr>
	 <tr>
	  <td class="brdrbtm" valign="top"><b>Image</b></td>
	  <td class="brdrbtm" valign="top">
	   <?php
	   if($image!="") {
		   ?>
		   <input name="image" type="file" style="width:505px;" class="button" />
		   <?php
		   $file1=SITE_DIR_PATH."/uploaded_files/success_story_images/".$image;
		   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;
			   }
			   echo "&nbsp;<a href=\"javascript:void(0);\" onClick=\"openwin('".SITE_ADMIN_URL."/popup.php?img=success_story_images/".$image."', '$dimens[0]', '$dimens[1]')\" class=\"linkgray fontbold\"><b>View Image</b></a>"; 
		   }
	   }
	   else {
		   ?>
		   <input name="image" type="file" style="width:505px;" class="button" id="NOBLANK~Please upload your image~DM~IMAGE~Please upload only .jpg or .gif or .png file~DM~" />
		   <?php
	   }
	   ?>
	   <br/><strong>Image Size: Please upload square image for better view.</strong>
	  </td>
	 </tr> 
	 <tr>
	  <td class="brdrbtm"><b>Email ID <font color="red">*</font></b></td>
	  <td class="brdrbtm"><input type="text" name="email_id" style="width:500px;" value="<?=$email_id;?>" id="NOBLANK~Please enter your email~DM~EMAIL~Please enter a valid email~DM~" /></td>
	 </tr>
	 <tr>
	  <td class="brdrbtm"><b>Title <font color="red">*</font></b></td>
	  <td class="brdrbtm"><input type="text" name="title" style="width:500px;" value="<?=$title;?>" id="NOBLANK~Please enter your title~DM~" /></td>
	 </tr> 
	 <tr>
	  <td class="brdrbtm"><b>Comments <font color="red">*</font></b></td>
	  <td class="brdrbtm"><textarea name="content" rows="7" style="width:500px;" id="NOBLANK~Please enter your comments~DM~"><?=$content;?></textarea></td>
	 </tr> 
	 <tr>
	  <td class="brdrbtm"></td>
	  <td class="brdrbtm">
	   <?php
	   if($recordID!="") {
		   ?>
		   <input type="submit" name="submit" value="Update <?=$record_type;?>" class="button">
		   <input type="hidden" name="recordID" value="<?=$res[slno];?>">
		   <input type="hidden" name="action" value="update_record">
		   <?php	   
	   }
	   else{
		   ?>
		   <input type="submit" name="submit" value="Add New <?=$record_type;?>" class="button">
		   <input type="hidden" name="action" value="add_record">
		   <?php
	   }
	   ?>   
	  </td>
	 </tr>
	</table>
	</form>	
	<?php
}

admin_footer();
exit;
?>