<?php $table='modules';
$daction=$_REQUEST['daction'];
$query = "SELECT COUNT(*) as num FROM modules";
$total_pages =DB_fetch_array(DB_query($query,$db));
$total_pages = $total_pages['num'];
 	/* Setup vars for query. */
	$targetpage="index.php?route=Setup/SecurityModule&Application".$_REQUEST['Application']."&modid=".$_REQUEST['modid'].""; 	//your file name  (the name of this file)
	$limit = 50; 								//how many items to show per page
	$page = $_GET['page'];
	if($page) 
		$start = ($page - 1) * $limit; 			//first item to display on this page
	else
		$start = 0;								//if no page var is given, set start to 0



 if($_REQUEST['Search'])
   {
	  $filter=array();
      if($_REQUEST['module_code']!='')
	  $filter[]="module_code='".$_REQUEST['module_code']."'";
	  if($_REQUEST['modulename']!='')
	  $filter[]="modulename='".$_REQUEST['modulename']."'";
	  echo count($filter);
		if(count($filter)>0)
		{
		   $filterval=implode(' and ',$filter );
		    $GetRecord = "Select * from $table where  $filterval LIMIT $start, $limit";
		}else{
		  $GetRecord = "Select * from $table where 1=1 LIMIT $start, $limit";
		}
    }
   else{
   $GetRecord = "Select * from $table  LIMIT $start, $limit ";
   }
   
//**********************************************   
$msgErr=array();
$fstatus=true;
if($_REQUEST['modulename']='')
{
   $msgErr[]='Please Enter Module Name';
   $fstatus=false;
}

if($_REQUEST['description']='')
{
   $msgErr[]='Please Enter Description of Module';
   $fstatus=false;
}

if($_REQUEST['status']='')
{
   $msgErr[]='Please Select Status';
   $fstatus=false;
}
//**********************DELETE****************************************************
if($_REQUEST['action']=='del')
{
			$condition="id='".$_REQUEST['Id']."'";
			$s->DeleteRecord($table,$condition);
			$s->pageLocation('index.php?route=Setup/SecurityModule&Application='.$_REQUEST['system'].'&modid='.$_REQUEST['modid'].'&daction=Deleted');
}
//*********************************GET DATA FROM DATA BASE*****************************
if($_REQUEST['action']=='Edit'  && $_REQUEST['Id']!='')
{
  	$condition="id='".$_REQUEST['Id']."'";
  	$query=DB_query("select * from $table where $condition ",$db);
	$RData=DB_fetch_array($query);	
	
 	$modulename=$RData['modulename'];
	$modname=$RData['modulename'];;
	$module_code=$RData['module_code'];
	$description=$RData['description'];
 	$sequence=$RData['sequence'];
	$status=$RData['status'];
 }
 
//*************************Update DATA FROM DATA BASE***************************************
if($_REQUEST['AddSecurity'] && $_REQUEST['action']=='Edit'  )
{
   	$data['modulename']=trim($_REQUEST['modname']); 
//	$data['module_code']='MODU'.$lastid;
 	$data['description']=trim($_REQUEST['moddescription']);
	$data['sequence']=trim($_REQUEST['sequence']);
	$data['status']=trim($_REQUEST['fstatus']);
  	$data['modified_date']=date('Y-m-d');
	$cond="id='".$_REQUEST['Id']."'";
	$update= $s->EditRecord($table, $data, $cond);
	$row=DB_query($update,$db);
	if(!$row)
	{
	   die('Error'.DB_error());
	}
$s->pageLocation('index.php?route=Setup/SecurityModule&Application='.$_REQUEST['system'].'&modid='.$_REQUEST['modid'].'&daction=Updated');
}


//*************************INSERT************************************
 if($_REQUEST['AddSecurity'] && $_REQUEST['action']=='add'&& $fstatus)
  {
 	$getlastid=DB_query("select * from $table order by id desc limit 1",$db) ;
	$rid=DB_fetch_array($getlastid);
    $lastid= $rid['id']+1;
	
  	$data['modulename']=trim($_REQUEST['modname']); 
 	$data['module_code']='MODU'.$lastid;
	$data['description']=trim($_REQUEST['moddescription']);
	$data['sequence']=trim($_REQUEST['sequence']);
 	$data['status']=trim($_REQUEST['fstatus']);
 	$data['modified_date']=date('Y-m-d');
    $insert= $s->insertRecord($table, $data);
	$row=DB_query($insert,$db);
	if(!$row)
	{
	   die('Error'.DB_error());
	}
	 $s->pageLocation('index.php?route=Setup/SecurityModule&Application='.$_REQUEST['Application'].'&modid='.$_REQUEST['modid'].'&daction=Inserted');
  }
?>



<?php 

	/* Setup page vars for display. */
	if ($page == 0) $page = 1;					//if no page var is given, default to 1.
	$prev = $page - 1;							//previous page is page - 1
	$next = $page + 1;							//next page is page + 1
	$lastpage = ceil($total_pages/$limit);		//lastpage is = total pages / items per page, rounded up.
	$lpm1 = $lastpage - 1;						//last page minus 1
	
	include('paging.php');
?>
<link href="css/pagnation.css" rel="stylesheet" type="text/css">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>

<h3>Security Module</h3>
<table width="70%" border="0" cellspacing="0" cellpadding="0" align="left">
   <tr>
    <td align="left"><div align="left">
	    <?php if($_REQUEST['action']==''){	?>
	    <a href="index.php?route=Setup/SecurityModule&Application=<?php echo $_REQUEST['Application']?>&modid=<?php echo $_REQUEST['modid'];?>&action=add">
	      AddNew</a>
	    <?php } else{?>
	    <A HREF="javascript:history.go(-1)"> [Go Back]</A> 
	    <?php } ?>
	    </div></td>
	</tr>
   
     
   
   <?php
//**************************Display Mess********************************************     
   
     if(isset($daction))
	 { ?>
	  <tr>
    <td ><div align="left" class="success" >Record <?php echo $_REQUEST['daction']; ?> Successfully </div>    </td>
	   </tr>
	 <?php 
	 }
	 
	 
//**************************Error Section*********************************************     
	
	 
if(isset($msgErr)&&$msgErr!='' && count($msgErr)>0 )
{?>
    <tr>
     <td ><div class="error">
  <?php 
	for($kp=0;$kp<count($msgErr);$kp++)
	 {
		echo  $msgErr[$kp].'<br>';
	 }
  ?></div></td>
  </tr>
  
   <?php } 
 //**************************Error Section*********************************************     
 ?>
 
 
  <?php  if($_REQUEST['action']==''){?>	

 <tr><td >
 <form action="" method="post">
 	<table width="63%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td> Module Code :<input name="module_code" type="text" value="<?php echo $_REQUEST['module_code'] ?>"></td>
    <td> Module Name :<input name="modulename" type="text" value="<?php echo $_REQUEST['modulename'] ?>"></td>
	<td><input name="Search"  value="Search" type="submit"></td>
  </tr>
</table>
</form>
 </td>
</tr>
<?php } else{ ?>





<tr><td >
<form action="" method="post" >
		<table width="55%" border="0" cellspacing="0" cellpadding="0">
		
		
			<tr>
			<td>Module Name</td>
			<td colspan="2"><input name="modname" type="text"  value="<?php echo $modulename; ?>" style="width:100%"/></td>
		  </tr>
			<tr>
			<td>Description</td>
			 <td colspan="2"><input name="moddescription" type="text"  value="<?php echo $description; ?>" style="width:100%"/></td>
			</tr>
			<tr>
			<td>Sequence</td>
			 <td colspan="2"><input name="sequence" type="text"  value="<?php echo $sequence; ?>" style="width:100%"/></td>
			</tr>
			  
		  
		  <tr>
			<td>Status</td> 
			 <td colspan="2"><input name="fstatus" type="radio"  value="Y"   <?php  echo ($status=='Y'?'checked':'')?>/>Active
			<input name="fstatus" type="radio" value="N" <?php  echo ($status=='N'?'checked':'')?>/>
			Inactive	</td>
		  </tr>
		  <tr>
			<td>&nbsp;</td>
			 <td colspan="2"><input type="submit" name="AddSecurity" value="Security"></td>
		  </tr>
		</table>
</form>


</td>
</tr>
<?php } ?>
</table>
 </td>
   
  </tr>
  <tr>
    <td><table width="100%" border="0" cellpadding="2" cellspacing="1" style="border:#999999 solid 1px;">
  <tr> <th scope="col">Module Code </th>
	<th scope="col">Module Name </th>
	  <th scope="col">Description</th>
	  
	    <th scope="col">Status</th>
		   <th scope="col">Edit</th>
    <th scope="col">Delete</th>
  </tr>
 
  <tr bgcolor="#FFFFFF">
   <?php 
  
   $ResultData = DB_query($GetRecord,$db);
   $i=1;
   if(DB_num_rows($ResultData)==0)
   echo "<td colspan='3'>--No Record--</td>";
   
   while($RowData = DB_fetch_array($ResultData))
   {?> <td bgcolor="#FFFFFF"><?php echo $RowData['module_code']; ?>	  </td>
    <td bgcolor="#FFFFFF"><?php echo $RowData['modulename']; ?>	  </td>
	    <td bgcolor="#FFFFFF"><?php echo $RowData['description']; ?>	  </td>
	
	       <td bgcolor="#FFFFFF"><?php echo ($RowData['status']=='Y'?'Active':'Inactive'); ?>	  </td>
      <td bgcolor="#FFFFFF"><div align="center"><a href="index.php?route=Setup/SecurityModule&Application=<?php echo $_REQUEST['Application']?>&modid=<?php echo $_REQUEST['modid']?>&action=Edit&Id=<?php echo $RowData['id']; ?>"><img src="images/icon-edit.png"  width="10" height="10"/></a></div></td>
	<td bgcolor="#FFFFFF"><div align="center"><a href="index.php?route=Setup/SecurityModule&Application=<?php echo $_REQUEST['Application']?>&modid=<?php echo $_REQUEST['modid']?>&action=del&Id=<?php echo $RowData['id']; ?>" onclick="return confirm('Do You Want To Delete This Record')"><img src="images/Icon_Delete.png" width="10" height="10" /></a></div></td>	
  </tr>
  <tr bgcolor="#FFFFFF">
    <td bgcolor="#FFFFFF">&nbsp;</td>
    <td bgcolor="#FFFFFF"><?=$pagination?>&nbsp;</td>
    <td bgcolor="#FFFFFF">&nbsp;</td>
    <td bgcolor="#FFFFFF">&nbsp;</td>
    </tr>
	<?php $i++;}   
   ?> 
</table>
    </td>
   </tr>
</table>
