<?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 Stories";
$record_type="Success Story";
$script_name="manage_success_stories.php";
$table_name="testimonial_tbl";
######################################################
if ($id=="Sendmail"){
	for ($x=0; $x < sizeof($_POST[cid]); $x++){
		$arr=$_POST[cid];
		$qur=getResult($table_name," where slno='$arr[$x]'");
		$toEmail=$qur[email]; 
		$fromEmail=ADMIN_EMAIL;
		$fromName=ADMIN_NAME;
		$mess=ms_htmlentities_decode($_POST[message]);
		$mess=stripslashes($mess);
		sent_mail($fromName,$fromEmail,$toEmail,$subject,$mess);
		updateTable($table_name," replyStatus='Y' where slno='$arr[$x]'");
	}
	$_SESSION[session_message]='Mail has been sent to selected member(s)';
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
	exit;
}

if($id=="View"){
	adminheader_popup("View $record_type");
	echo view_testimonial($slno);
	adminfooter_popup();
	exit;
}

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];
		
		$ssRes=getResult($table_name," where slno='".$arr[$x]."'","image");
		$filename=$ssRes['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);
			}
		}
	
		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);
	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");
		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($_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");
		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") {
	
	$ssRes=getResult($table_name," where slno='$recordID'","image");
	$filename=$ssRes['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);
		}
	}
		
	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("Manage $record_type");
?>
<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
$qry1= db_query("select * from $table_name where status!='D'");
$reccnt = NumRows($qry1);
$qry= db_query("select * from $table_name where status!='D' 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
		   }
		   ?>
		   <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><br>
	<?php
	if ($add_id!="new") {
		?>
		<table width="100%"  border="0" cellpadding="6" cellspacing="2" class="lightBorder">
		 <tr>
		  <td width="10%" align="left"><strong>Subject *</strong></td>
		  <td align="left"><input type="text" name="subject" value="" style="width:594px;"></td>
		 </tr>
		 <tr>
		  <td valign="top" align="left"><strong>Message *</strong></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.Width="600px";
		oFCKeditor.Height="400px";
		oFCKeditor.ReplaceTextarea();
		</script>
		<?php
	}
}
else {
	?>
	<table width="100%"  border="0" cellpadding="6" cellspacing="2" class="lightBorder">
	 <tr><td><strong>No Record Found.............</strong></td></tr>
	</table>
	<?php
}

admin_footer();
exit;
?>