<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1>Security Form</h1> 
<?php 
//unset($_SESSION['SecModule']);
$table='modules_form';
include("includes/fun-images.php");
$daction=$_REQUEST['daction'];
$query = "SELECT COUNT(*) as num FROM modules_form";
$total_pages =DB_fetch_array(DB_query($query,$db));
$total_pages = $total_pages['num'];
/* Setup vars for query. */
$targetpage = "index.php?route=Setup/SecurityForm&Application=".$_REQUEST['Application']."&modid=".$_REQUEST['modid'].""; 	//your file name  (the name of this file)
$limit = 100; 								//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
	/* 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');
//*************************Search********************************************************************


if(isset($_GET['mode']))
{
	switch ($_GET['mode'])
	{
		case 'swapsorting':
			$s->swap_sortid_category( $_GET['currid'], $_GET['newid']);
			$s->pageLocation("index.php?route=".$_REQUEST['route'].$rurl);
			 break;		
	}
}


if(!isset($_REQUEST['Search']))
{
	$rscat=DB_fetch_assoc(DB_query("SELECT id FROM modules order by RAND() limit 0,1",$db));
	$cId=$rscat['id'];
}
else
{
	$cId=$_REQUEST['moduleid'];
}
if(isset($_REQUEST['MODULEID']))
{
	$_SESSION['modulid']=$_REQUEST['MODULEID'];
	$cId=$_SESSION['modulid'];
}
//echo $cId;


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

if($_REQUEST['frmdescription']='')
{
   $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($targetpage.'&daction=Deleted');
}
if($_REQUEST['action']=='Status')
{
	if(trim($_REQUEST['SecStatus'])=='Y')
 	$data['status']='N';
	else
 	$data['status']='Y';
   	$data['modified_date']=date('Y-m-d');
	$cond="id='".$_REQUEST['Id']."'";
	$rowupdate= $s->RecordEdit($table, $data, $cond);
	$s->pageLocation($targetpage);

}
//*********************************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);	
 	$moduleid=$RData['moduleid'];
	$categoryid=$RData['module_form_category_id'];
	$frm_name=$RData['frm_name'];
	$frm_url=$RData['frm_url'];	 
	$description=$RData['description'];
	$PerAdd=$RData['PerAdd'];
	$PerEdit=$RData['PerEdit'];
	$PerDelete=$RData['PerDelete'];
	 $frmstatus=$RData['status'];
	 $setuplogo=$RData['setuplogo'];
//	 $frmtype=$RData['frmtype'];
 }
//*************************Update DATA FROM DATA BASE***************************************
if($_REQUEST['AddSecurity'] && $_REQUEST['action']=='Edit' && $fstatus )
{
 
    	$data['frm_name']=trim($_REQUEST['form_name']); 
 	$data['moduleid']=trim($_REQUEST['moduleid']); 
	$data['module_form_category_id']=trim($_REQUEST['subcat']);
  	$data['frm_url']=trim($_REQUEST['frm_url']); 
 	$data['description']=trim($_REQUEST['description']);
 	$data['status']=trim($_REQUEST['frmstatus']);
	$data['PerAdd']=trim($_REQUEST['PerAdd']);
	$data['PerEdit']=trim($_REQUEST['PerEdit']);
	$data['PerDelete']=trim($_REQUEST['PerDelete']);
	
 		if($_FILES['setuplogo']['size'][0]>0)
		{
			$upload_path = 'companies/db_mxerp/security-logo/';
		    $img= $s->GetFieldValue('modules_form',"id='".$_REQUEST['Id']."'",'setuplogo');
			if($img!=''){
 			unlink($img);}
			$r= ImageUpload($upload_path,'setuplogo',"",50, 50);
			$data['setuplogo']=$r;
		} 
 	
//	$data['frmtype']=trim($_REQUEST['frmtype']);
 	$data['modified_date']=date('Y-m-d');
	$cond="id='".$_REQUEST['Id']."'";
	$rowupdate= $s->RecordEdit($table, $data, $cond);
 	if(!$rowupdate)
	{
	   die('Error'.DB_error());
	}
	    $s->pageLocation($targetpage.'&daction=Updated');
}


//*************************INSERT************************************
 if($_REQUEST['AddSecurity'] && $_REQUEST['action']=='add'&& $fstatus)
  {
 	$getlastid=DB_query("select MAX(sequence) as FinSequence from $table where moduleid='".$_REQUEST['moduleid']."'",$db) ;
	$rid=DB_fetch_array($getlastid);
    $lastid= $rid['FinSequence']+1;
	
	//echo "select * from $table where module_form_category_id='".$_REQUEST['subcat']."' and form_ord='".."'";
//	$getordSQL=DB_query("select * from $table where module_form_category_id='".$_REQUEST['subcat']."'",$db) ;
	
 	$data['moduleid']=trim($_REQUEST['moduleid']);
	$data['module_form_category_id']=trim($_REQUEST['subcat']); 
 	$data['frm_name']=trim($_REQUEST['form_name']); 
 	$data['frm_url']=trim($_REQUEST['frm_url']); 
//	$data['frmtype']=trim($_REQUEST['frmtype']);
	$data['description']=trim($_REQUEST['description']);
	$data['sequence']=$lastid;
	//$data['form_ord']=$_REQUEST['form_after'];
	$data['PerAdd']=trim($_REQUEST['PerAdd']);
	$data['PerEdit']=trim($_REQUEST['PerEdit']);
	$data['PerDelete']=trim($_REQUEST['PerDelete']);
	$data['status']=trim($_REQUEST['frmstatus']);
 	$data['modified_date']=date('Y-m-d');
//	echo "<pre>";
//	print_r($data);
//	exit;
    $rowinsert= $s->RecordInsert($table, $data);
	 
	if(!$rowinsert)
	{
	   die('Error'.DB_error());
	}
	 $s->pageLocation($targetpage.'&daction=Inserted');
  }
?>

<script type="text/javascript" language="javascript">
function change_sorting(newval, curval) {
	var myForm=document.category_manage_frm;
	document.category_manage_frm.action="index.php?route=Setup/SecurityForm&modid=<?php echo $_REQUEST['modid'] ?>&frmid=<?php echo $_REQUEST['frmid']; ?>&MODULEID=<?php echo $cId; ?>&mode=swapsorting&currid=" + curval + "&newid=" + newval
	
	document.category_manage_frm.submit();
}
</script>

<script src="js/function-ajax.js" language="javascript"></script>
<table width="100%" border="0" cellspacing="0" cellpadding="0" >
<tr class="row1">
	<td style="text-align:left;" class="tabledata1">
	<?php if($_REQUEST['action']==''){	?>
	<a href="<?php echo $targetpage;?>&action=add"><input type="button" value="AddNew" class="search" /></a>
	<?php }?>
	</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*********************************************     
?>
</table>
<?php  if($_REQUEST['action']=='' || $_SESSION['SecModule1']!=''){?>	
<form action="" method="post">
<table width="100%" border="0" cellspacing="0" cellpadding="3">
<tr class="row1">
    <td> Form Name :<input name="frm_name" type="text" value="<?php echo $_REQUEST['frm_name'] ?>"></td>
    <td> Module  :
	<select name="moduleid">
		<?php
		  $query=DB_query("select * from modules",$db);
		  while($row=DB_fetch_array($query))
		  {
		  
		    $p='';
			if($row['id']==$cId)
			$p='selected';
		 ?>
		<option value="<?php echo $row['id'] ?>" <?php  echo $p;?>><?php echo $row['modulename'] ?></option>
		<?php } ?>
		</select>
	 </td>
	<td><input name="Search" class="search"  value="Search" type="submit"></td>
  </tr>
</table>
</form>
<form name="category_manage_frm" method="post">

<table width="100%" border="1" cellspacing="0" cellpadding="3">
<tr class="row1"> 
	<td class="tabledata"><strong>Module</strong></td>
	<td class="tabledata"><strong>Form Name</strong></td>
	<td class="tabledata"><strong>Form url</strong></td>
	<td class="tabledata"><strong>Sub Category</strong></td>
	<td class="tabledata"><strong>Status</strong></td>
	<td class="tabledata"><strong>Sequence</strong></td>
	<td class="tabledata">
	<table width="100%" border="0" cellspacing="0" cellpadding="0">
	<tr>
	<td class="tabledata" style="text-align:center;"  colspan="3">Permission</td>
	</tr>
	<tr>
	<td class="tabledata">Add</td>
	<td class="tabledata">Edit</td>
	<td class="tabledata">Delete</td>
	</tr>
	</table>
	</td>
	<td class="tabledata">Edit</td>
	<td class="tabledata">Delete</td>
</tr>
<tr class="row1"> 
   <?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><?php  echo  $s->getField('modules','id',$RowData['moduleid'],'modulename');  ?>	  </td>
   	<td><?php echo $RowData['frm_name']; ?>	  </td>
   	<td><?php echo $RowData['frm_url']; ?>	  </td>
	<td><?php echo  $s->getField('module_form_categories','id',$RowData['module_form_category_id'],'cat_desc');  ?>	  </td>
	<td><?php   $SecStatus= ($RowData['status']=='Y'?'Active':'Inactive');?> 
		  <a href="<?php echo $targetpage;?>&action=Status&Id=<?php echo $RowData['id']; ?>&SecStatus=<?php echo $RowData['status'];?>"> <?php echo  $SecStatus; ?></a></td>
	<td><select style="width:40px; height:25px; border:1px solid #000000;"  name="sorted_list" onChange="change_sorting(this.value, <?php echo $RowData['id'];?>);" id="" value="GO">
	   	<?php echo $s->get_category_sorted_list_selectbox($RowData['sequence'],$RowData['moduleid']); ?>
		</select></td>
	  <td>
	   <table width="100%" border="0" cellspacing="0" cellpadding="0">
	  <tr class="row1"> 
		<td><input name="PerAdd" type="checkbox" value="<?php echo $RowData['PerAdd']; ?>" <?php echo ($RowData['PerAdd']==1?"checked":"")?>/></td>
		<td><input name="PerEdit" type="checkbox" value="<?php echo $RowData['PerEdit']; ?>" <?php echo ($RowData['PerEdit']==1?"checked":"")?> /></td>
		<td><input name="PerDelete" type="checkbox" value="<?php echo $RowData['PerDelete']; ?>"  <?php echo ($RowData['PerDelete']==1?"checked":"")?>/></td>
	  </tr>
	</table>
	</td>
   <td><div align="center"><a href="<?php echo $targetpage;?>&action=Edit&Id=<?php echo $RowData['id']; ?>"><img src="images/icon-edit.png"  width="10" height="10"/></a></div></td>
	<td><div align="center"><a href="<?php echo $targetpage;?>&action=del&Id=<?php echo $RowData['id']; ?>" onClick="return confirm('Do You Want To Delete This Record')"><img src="images/Icon_Delete3.gif" width="10" height="10" /></a></div></td>	
  </tr>

	<?php $i++;}   
   ?> 
<tr class="row1"> 
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td><?php //echo $pagination;?>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
	<td>&nbsp;</td>
    <td>&nbsp;</td>
	<td>&nbsp;</td>
</tr>
</table>
</form>

<?php }else{ ?>
<p><a href="javascript:history.go(-1)"><input type="button" value="[Go Back]" class="search" /></a> </p>
<form action="" method="post" enctype="multipart/form-data" >
<table width="50%" align="center" border="0" cellspacing="0" cellpadding="3">
<tr class="row1"> 
	<td class="tabledata1"><strong>Module Category</strong></td>
	<td><select name="moduleid" id="moduleid" style="width:250px;"  onchange="ShowDiv(this.value,'hiddiv','disp_module_category.php')">>
	<?php $query=DB_query("select * from modules",$db);
	while($row=DB_fetch_array($query))
	{
	$p='';
	if($moduleid==$row['id'])
	$p='selected';
	?>
	<option value="<?php echo $row['id'] ?>" <?php  echo $p;?>><?php echo $row['modulename'] ?></option>
	<?php } ?>
	</select> 
	</td>
</tr>
<tr class="row1">
	<td class="tabledata1"><strong>Sub Category</strong></td>
	<td>
	<div id='hiddiv' style="width:200px;">
	<select name="subcat" id="subcat" style="width:250px;">
	<option value="">Select</option>
	<?php
	if(empty($moduleid))
	{
	$query=DB_query("select * from module_form_categories ",$db);
	}
	else {
	$query=DB_query("select * from module_form_categories where module_id='".$moduleid."'",$db);
	}
	while($row=DB_fetch_array($query))
	{
	$k='';
	if($categoryid==$row['id'])
	{
	$k="selected";	
	}
	$query2=DB_query("select * from modules where id='".$row['module_id']."'",$db); 
	$row2=DB_fetch_array($query2);
	
	?>
	<option value="<?php echo $row['id'] ?>"<?php echo $k; ?>><?php echo "[".$row2['modulename']."]"."------".$row['cat_desc'] ?></option>
	<?php } ?>
	</select> </div>
	
</tr>
<tr class="row1">
	<td class="tabledata1"><strong>Form Name</strong></td>
	<td><input name="form_name" id="form_name" type="text"  value="<?php echo $frm_name; ?>" style="width:250px;"/></td>
</tr>
<tr class="row1">
	<td class="tabledata1"><strong>Form Url</strong></td>
	<td><input name="frm_url" id="frm_url" type="text"  value="<?php echo $frm_url; ?>" style="width:250px;"/></td>
</tr>
<tr class="row1">
	<td class="tabledata1"><strong>Description</strong></td>
	<td><input name="description" id="description" type="text"  value="<?php echo $description; ?>" style="width:250px;"/></td>
</tr>
<tr class="row1">
	<td class="tabledata1"><strong>Form Image </strong></td>
	<td><input type="file" name="setuplogo[]"></td>
</tr>
<tr class="row1">
	<td class="tabledata1"><strong>Permissions</strong></td> 
	<td><input name="PerAdd" type="checkbox"  checked="checked"value="1" <?php echo ($PerAdd==1?"checked":"") ?>  />ADD
		<input name="PerEdit" type="checkbox"checked="checked" value="1" <?php echo ($PerEdit==1?"checked":"") ?>  />Edit
		<input name="PerDelete" type="checkbox" checked="checked" value="1"  <?php echo ($PerDelete==1?"checked":"") ?> />Delete
	  </td>
</tr>
<tr class="row1">
	<td class="tabledata1"><strong>Status</strong></td> 
	<td><input name="frmstatus" type="radio"  value="Y"   <?php  echo ($frmstatus=='Y'?'checked':'')?>/>Active
		<input name="frmstatus" type="radio" value="N" <?php  echo ($frmstatus=='N'?'checked':'')?>/>Inactive</td>
</tr>
<tr class="row1">
	<td class="tabledata1" style="text-align:center;" colspan="2">
	<input type="submit" name="AddSecurity" class="search" value="Security" onclick="return validate()"></td>
</tr>
</table>
</form>

<?php } ?>
<script type="text/javascript" language="javascript">
function getformnameandord(str)
{
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
	xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
	{
	if (xmlhttp.readyState==4 && xmlhttp.status==200)
	{
	document.getElementById("FrmOrd").innerHTML=xmlhttp.responseText;
	}
	}
	xmlhttp.open("GET","Get_Formname_Ord.php?id="+str,true);
	xmlhttp.send();	
}

function validate()
{
	if(document.getElementById("moduleid").value=='')
	{
		alert("Please Select The Module First");
		document.getElementById("moduleid").focus();
		return false;
	}
	if(document.getElementById("subcat").value=='')
	{
		alert("Please Select The Sub Category First");
		document.getElementById("subcat").focus();
		return false;
	}
	if(document.getElementById("form_name").value=='')
	{
		alert("Please Enter The Form Name First");
		document.getElementById("form_name").focus();
		return false;
	}
	
	if(document.getElementById("frm_url").value=='')
	{
		alert("Please Enter The Form URL First");
		document.getElementById("frm_url").focus();
		return false;
	}
	
	if(document.getElementById("description").value=='')
	{
		alert("Please Enter The Form Description First");
		document.getElementById("description").focus();
		return false;
	}
	
}

</script>
