<?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 News";
$record_type="News";
$script_name="manage_news.php";
$table_name="news_tbl";
######################################################
if($id=="add_record") {
	if ($newsHeading!="" && $newsDescription!="") {
		$sql=db_query("insert into $table_name set newsHeading=='$newsHeading', newsDescription='$newsDescription', featured='$featured', recvDate=now()");
		$_SESSION[session_message]='New '.$record_type.' has been added successfully';
	}
	else {
		$_SESSION[session_message]='News Heading and News Description are required.';
	}
	rss_write();
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize."&add_id=".$add_id);
	exit;	
}

if($id=="update_record") {
	if ($newsHeading!="" && $newsDescription!="") {
		updateTable($table_name," newsHeading=='$newsHeading', newsDescription='$newsDescription', featured='$featured' where slno='$recordID'");
		$_SESSION[session_message]=$record_type.' has been updated successfully';
	}
	else {
		$_SESSION[session_message]='News Heading and News Description are required.';
	}
	rss_write();
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
	exit;
}

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'];
		updateTable($table_name," status='D' where slno='$arr[$x]'");		
	}
	rss_write();
	$_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]'");
	}
	rss_write();
	$_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]'");
	}
	rss_write();
	$_SESSION[session_message]='Selected record(s) deactivated 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.';
	rss_write();
	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' ");
	rss_write();
	$_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") {
	updateTable($table_name," status='D' where slno='$recordID'");
	rss_write();
	$_SESSION[session_message]=$record_type.' has been deleted from the admin panel.';
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
	exit;
}

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

if($id=="unset_featured") {
	updateTable($table_name," featured='N' where slno='$recordID'");
	$_SESSION[session_message]=$record_type.' has been unset as featured successfully.';
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize."&region=".$region);
	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%"><input type="checkbox" name="chk" onclick="CheckAll()" class="NoStyle"></td>
	  <td width="25%"><strong>News Heading</strong></td>
	  <td width="50%"><strong>News Description</strong></td>
	  <!-- <td width="20%"><strong>News Link</strong></td> !-->
	  <!-- <td width="10%"><strong>Date</strong></td> !-->
	  <td width="10%"><strong>Featured</strong></td>
	  <td width="10%"><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"><input type=checkbox name="cid[]" value="<?=$arr[slno]?>" class="NoStyle"></td>
		  <td valign="top"><?=ucwords($arr[newsHeading]);?></td>
		  <td valign="top"><?=ucwords(substr($arr[newsDescription], 0, 200));?> ...</strong></td>
		  <!-- <td valign='top'><?=$arr[newsLink];?></td> !-->
		  <!-- <td valign='top'><?=show_date($arr[recvDate])?></td> !-->
		  <td valign='top'>
		   <?php
		   if ($arr[featured]=="N") {
			   ?>
			   <strong>No</strong>&nbsp;&nbsp;<a href="<?=SITE_ADMIN_URL?>/<?=$script_name;?>?id=set_featured&start=<?=$start;?>&pagesize=<?=$pagesize;?>&recordID=<?=$arr[slno];?>"><strong>Set</strong></a>
			   <?php
		   }
		   else {
			   ?>
			   <strong>Yes</strong>&nbsp;&nbsp;<a href="<?=SITE_ADMIN_URL?>/<?=$script_name;?>?id=unset_featured&start=<?=$start;?>&pagesize=<?=$pagesize;?>&recordID=<?=$arr[slno];?>"><strong>Unset</strong></a>
			   <?php
		   }
		   ?>
		  </td>
		  <td 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];?>#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&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>
	</form>
	
	<?php	
}
else {
	?>
	<table width="100%"  border="0" cellpadding="6" cellspacing="2" class="lightBorder">
	 <tr><td><strong>No Record Found.............</strong></td></tr>
	</table>
	<?php
}
?>

<br/>
<form name="formn1" method="post" action="" enctype="multipart/form-data" onsubmit="return validate(this);">
<table width="99%"  border="0" cellpadding="0" cellspacing="2" class="lightBorder">
 <?php
 if($recordID=="" || $id!="edit_record") {
	 ?>
	 <tr><td colspan="3" align="left" class="lightBlueBg PadFive" height="20"><strong><a name="bot">Add New <?=$record_type;?></a></strong></td></tr>
	 <?php
 }
 else {
	 ?>
	 <tr>
	  <td align="left" class="lightBlueBg PadFive" height="20"><a name="bot"></a><strong>Edit <?=$record_type;?></strong></td>
	  <td align="right" class="lightBlueBg  PadFive" height="20"><a href="<?=$script_name;?>"><strong>Add New <?=$record_type;?></strong></a></td>
	 </tr>
	 <?php
 }
 $result=getResult($table_name," where slno='$recordID'");
 ?>
 <tr>
  <td class="brdrbtm" width="15%"><strong>News Heading <font color="red">*</font></strong></td>
  <td class="brdrbtm"><input size="120" type="text" name="newsHeading" value="<?=$result[newsHeading]; ?>" class="txtfieldadmin" id="NOBLANK~Please enter news heading~DM~" /></td>
 </tr>
 <!--
 <tr>
  <td class="brdrbtm"><strong>News Link</strong></td>
  <td class="brdrbtm"><input size="120" type="text" name="newsLink" value="<?=$result[newsLink]; ?>" class="txtfieldadmin" /></td>
 </tr>
 !-->
 <tr>
  <td class="brdrbtm"><strong>Featured News</strong></td>
  <td class="brdrbtm">
   <input name="featured" type="radio" value="N" class="NoStyle" <? if($result[featured]=='N' or $result[featured]!='N'){ echo "checked";}?>>No
   <input name="featured" class="NoStyle" type="radio" value="Y" <? if($result[featured]=='Y'){ echo "checked";}?>>Yes
   </td>
 </tr>
 <tr>
  <td class="brdrbtm"><strong>News Description <font color="red">*</font></strong></td>
  <td class="brdrbtm"><textarea name="newsDescription" class="txtfieldadmin" cols="120" rows="15" id="NOBLANK~Please enter news description~DM~"><?=$result[newsDescription];?></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="<? echo $result[slno]; ?>">
	   <input type="hidden" name="id" value="update_record">
	   <?php
   }
   else{
	   ?>
	   <input type="submit" name="submit" value="Add New <?=$record_type;?>" class="button">
	   <input type="hidden" name="id" value="add_record">
	   <?php
   }
   ?>
   <input type="hidden" name="add_id" value="new">	   
   <input type="hidden" name="start" value="<? echo $start; ?>">
   <input type="hidden" name="pagesize" value="<? echo $pagesize; ?>">
  </td>
 </tr>
</table>
</form>

<script type="text/javascript">
var oFCKeditor = new FCKeditor("newsDescription");
oFCKeditor.BasePath = "fckeditor/";
//oFCKeditor.ToolbarSet="Basic";
oFCKeditor.Height="500";
oFCKeditor.Width="625";
oFCKeditor.ReplaceTextarea();
</script>

<?php
admin_footer();
exit;
?>