 <?php $table='company_Profile';

 $daction=$_REQUEST['daction'];
  $query = "SELECT COUNT(*) as num FROM $table";
$total_pages =DB_fetch_array(DB_query($query,$db));
  $total_pages = $total_pages['num'];
 	/* Setup vars for query. */
	$targetpage = "ControlPanel.php?route=ProfileMaster"; 	//your file name  (the name of this file)
	$limit = 20; 								//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($_REQUEST['Search'])
{
	  $filter=array();
      if($_REQUEST['moduleid']!='')
	  $filter[]="moduleid='".$_REQUEST['moduleid']."'";
	  if($_REQUEST['profile_name']!='')
	  $filter[]="profile_name='".$_REQUEST['profile_name']."'";
 		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";
		}
		echo $GetRecord;
}
else
{
	   $GetRecord = "Select * from $table group by profile_name  LIMIT $start, $limit ";
}  

//**********************DELETE****************************************************
if($_REQUEST['action']=='del')
{
	$condition="id='".$_REQUEST['Id']."'";
	$s->DeleteRecord($table,$condition);
	$s->pageLocation('ControlPanel.php?route=ProfileMaster&daction=Deleted');
}


 //*********************************GET DATA FROM DATA BASE*****************************
 
if($_REQUEST['action']=='Edit'  && $_REQUEST['Id']!='' && !$_REQUEST['Save'])
{
  	$condition="id='".$_REQUEST['Id']."'";
  	$query=DB_query("select * from $table where $condition ",$db);
	$RData=DB_fetch_array($query);	
   	$moduleid=$RData['moduleid'];
 	$profile_name=$RData['profile_name'];
  	$frm_moduleid=$RData['frm_moduleid'];
 	$Permission=$RData['Permission']; 
	$PerAdd=$RData['PerAdd']; 
	$PerEdit=$RData['PerEdit']; 
	$PerDelete=$RData['PerDelete']; 
 	$profilestatus=$RData['profilestatus'];	 
  }
  else if($_REQUEST['Id']=='')
  {
	$profilestatus=trim($_REQUEST['profilestatus']); 
	$profile_name=$_REQUEST['profile_name'];
	$moduleid=trim($_REQUEST['moduleid']); 
	$Permission=trim($_REQUEST['Permission']); 
    $PerAdd=$_REQUEST['PerAdd']; 
	$PerEdit=$_REQUEST['PerEdit']; 
	$PerDelete=$_REQUEST['PerDelete']; 
  }  
//*************************Update DATA FROM DATA BASE***************************************
if($_REQUEST['Save'] && $_REQUEST['action']=='Edit')
{
 	$i=0;
   	foreach( $_REQUEST['frmid'] as $key => $value){
 		$data['moduleid']=$_REQUEST['frmmoduleid'][$key];		
		$data['frm_moduleid']=$key;
		$data['profile_name']=$_REQUEST['profile_name'];
		// for($p=0;$p<count())
		if($_REQUEST[$key][0]!='')
		$data['PerAdd'] =$_REQUEST[$key][0]; 
		else
		$data['PerAdd'] =0; 
		
		if($_REQUEST[$key][1]!='')
		$data['PerEdit'] =$_REQUEST[$key][1]; 
		else
		$data['PerEdit'] =0; 
		
		if($_REQUEST[$key][2]!='')
		$data['PerDelete'] =$_REQUEST[$key][2]; 
		else
		$data['PerDelete'] =0; 
		
		$data['Permission'] = $data['PerAdd'].','.$data['PerEdit'].','.$data['PerDelete']; 
		$data['profilestatus'] =$_REQUEST['profilestatus'];
		
//  Check Value Exist
$chkstatus=DB_query("select * from company_Profile where  moduleid='".$_REQUEST['frmmoduleid'][$key]."' and frm_moduleid='".$key."' and profile_name='".$_REQUEST['profile_name']."' ",$db);
	if(DB_num_rows($chkstatus)==0)
	{		
 			$insert= $s->insertRecord($table, $data);
 			  $row=DB_query($insert,$db);
			if(!$row)
			{
			   die('Error'.DB_error());
			} 
	}
	else
	{
			$cond="id='".$_REQUEST['Id']."'";
			$update= $s->EditRecord($table, $data, $cond);
			 $row=DB_query($update,$db);
			if(!$row)
			{
			   die('Error'.DB_error());
			}
	}		
	
}	
    $s->pageLocation('ControlPanel.php?route=ProfileMaster&daction=Updated');
}

if($_REQUEST['Save'] && $_REQUEST['action']!='Edit' )
{
$data_master['profile_name']=$_REQUEST['profile_name'];
$data_master['modified_date']=date('Y-m-d');
 $insert_master= $s->insertRecord('company_profile_master', $data_master);
 $row_master=DB_query($insert_master,$db);
if(!$row_master)
{
	   die('Error'.DB_error());
}  
echo $lastid=$s->GetLastRecord('company_profile_master', '1=1','id');



  foreach( $_REQUEST['frmid'] as $key => $value)
	{
	    $data['moduleid']=$_REQUEST['frmmoduleid'][$key];
 		$data['profile_id']=$lastid;
		$data['profile_name']=$_REQUEST['profile_name'];
		$data['frm_moduleid']=$key;
		 
 		 if($_REQUEST[$key][0]!='')
		$data['PerAdd'] =$_REQUEST[$key][0]; 
		else
		$data['PerAdd'] =0; 
		
		if($_REQUEST[$key][1]!='')
		$data['PerEdit'] =$_REQUEST[$key][1]; 
		else
		$data['PerEdit'] =0; 
		
		if($_REQUEST[$key][2]!='')
		$data['PerDelete'] =$_REQUEST[$key][2]; 
		else
		$data['PerDelete'] =0; 
		
		$data['Permission'] = $data['PerAdd'].','.$data['PerEdit'].','.$data['PerDelete']; 
		$data['profilestatus'] =$_REQUEST['profilestatus'];
		$insert= $s->insertRecord($table, $data);
		 $row=DB_query($insert,$db);
		if(!$row)
		{
		   die('Error'.DB_error());
		}  
   	}	
   $s->pageLocation('ControlPanel.php?route=ProfileMaster&daction=Inserted');
 
}
?>
<script language="javascript" >
 function FProfileMaster(str,hiddiv, url)
{

if(url=='ModuleFile.php'){
 var ele = document.getElementById(hiddiv);
 	if(ele.style.display == "block") {
    		ele.style.display = "none";
   	}
	else {
		ele.style.display = "block";
 	}
}

  
   if (str=="")
  {
  document.getElementById(hiddiv).innerHTML="";
  return;
  }
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(hiddiv).innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET",url+"?q="+str,true);
xmlhttp.send();
}



var formblock;
var forminputs;
 
function prepare() {
  formblock= document.getElementById('form_id');
  forminputs = formblock.getElementsByTagName('input');
}
 
function select_all(name, value,id ,arr) {

str_array = arr.split(',');
if(value==1)
document.getElementById('POPID_'+id ).value=0;
else
document.getElementById('POPID_'+id ).value=1;

  for (i = 0; i < forminputs.length; i++) {
    // regex here to check name attribute
    var regex = new RegExp(name, "i");
    if (regex.test(forminputs[i].getAttribute('name'))) {
  
      if (value == '1') {
        forminputs[i].checked = true;
      } else {
        forminputs[i].checked = false;
  }
    }
  }
}
 
if (window.addEventListener) {
  window.addEventListener("load", prepare, false);
} else if (window.attachEvent) {
  window.attachEvent("onload", prepare)
} else if (document.getElementById) {
  window.onload = prepare;
}
 
 


</script>
<link href="css/pagnation.css" rel="stylesheet" type="text/css">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>

<table width="70%" border="0" cellspacing="0" cellpadding="0" align="left">
   <tr>
    <td colspan="2"><h2>Profile Master </h2></td>
	<td>
	<div align="right">
	 <?php if($_REQUEST['action']==''){	?>
	<a href="ControlPanel.php?route=ProfileMaster&amp;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 colspan="3" ><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 colspan="3" ><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 colspan="3" >
 <form action="" method="post">
 	<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>   Profile Name   :
      <input name="profile_name" type="text" value="<?php echo $_REQUEST['profile_name'] ?>"></td>
    <td> Module  :
	<select name="moduleid">
	<option value="0">Select Module</option>
		<?php
		  $query=DB_query("select * from modules",$db);
		  while($row=DB_fetch_array($query))
		  {
		  
		    $p='';
			if($row['id']==$_REQUEST['moduleid'])
			$p='selected';
		 ?>
		<option value="<?php echo $row['id'] ?>" <?php  echo $p;?>><?php echo $row['modulename'] ?></option>
		<?php } ?>
		</select>
       </td>
	<td><input name="Search"  value="Search" type="submit"></td>
  </tr>
</table>
</form>
 </td>
</tr>
<?php } else{ ?>
 
<tr><td colspan="3" >
<form action="" method="post"  id="form_id" name="myform">
		<table width="100%" border="0" cellspacing="0" cellpadding="0">
		
		
			<tr>
			<td>Profile Name </td>
			<td colspan="2"><input name="profile_name" type="text"  value="<?php echo $profile_name; ?>" size="60"/></td>
		  </tr>
		  
		  
			
			 <tr>
			<td>Permissions</td> 
			 <td colspan="2">  
			 
		<?php
		  $query=DB_query("select * from modules ",$db);
		  while($row=DB_fetch_array($query))
		  {
			  $p='';
		  	if($row['id']==$moduleid)
			$p='checked';
		  
		    $divid='txtfiles_'.$row['id'];
		 ?><input name="moduleid" type="checkbox" value="<?php echo $row['id'] ?>" onclick="FProfileMaster(this.value,'txtfiles_'+<?=$row['id']?>,'ModuleFile.php')" ;  <?php echo $p?>  />
		 <b><?php echo $row['modulename']  ?></b><br />
		
<?php
 if($_REQUEST['action']!='Edit'){
 ?>
 <div id="<?php echo $divid; ?>" >
  <?php }
 else{ 
 ?>		 
<div id="<?php echo $divid; ?>" >
<?php }

//*******************************************************************************************************************
		$query_modfrm=DB_query("select * from modules_form where moduleid='".$row['id']."' and id='".$frm_moduleid."' ",$db);
 	// echo 	"select * from modules_form where moduleid='".$row['id']."' ";
//	echo DB_num_rows($query_modfrm);
  ?>
  <?php $i=0;
   while($row_frmmod=DB_fetch_array($query_modfrm))
   {
   		$p1='';
	  	if($row_frmmod['id']==$frm_moduleid)
		$p1='checked';
		$arr=$row_frmmod['PerAdd'].','.$row_frmmod['PerEdit'].','.$row_frmmod['PerDelete'] ;
	//	print_r($arr);
  ?>
 	<table width="100%" border="0" cellspacing="0" cellpadding="0" style="background:#D7E8FD">
 	  <tr>
	  <td  width="40%">
 <input name="frmmoduleid[<?php echo $row_frmmod['id'];?>]" type="hidden" value="<?php echo $row['id']; ?>" />
<input name="POPID" type="hidden" id= "POPID_<?php  echo $row_frmmod['id'];?>" value="0">
<input name="ARRID" type="hidden" id= "ARRID_<?php  echo $row_frmmod['id'];?>" value="<?php echo $arr;?>">
<?php  //echo $row_frmmod['id'];?>
<input name="frmid[<?php echo $row_frmmod['id'];?>]" type="checkbox" value="<?php  echo $row_frmmod['id'];?>" 
onClick="select_all(<?php  echo $row_frmmod['id'];?>,document.getElementById('POPID_'+<?php  echo $row_frmmod['id'];?>).value,<?php echo $row_frmmod['id'];?>,document.getElementById('ARRID_'+<?php  echo $row_frmmod['id'];?>).value );" <?php echo $p1?> >
<?php  echo ucwords(strtolower($row_frmmod['frm_name']));?>
 	  </td>
     	<td width="20%">
		
		<?php 
//echo $Permission;
//echo $row_frmmod['PerAdd'];
 
 
 if($PerAdd!=0){
?>
<input name="<?php  echo $row_frmmod['id'];?>[0]" type="checkbox" value="<?php  echo $row_frmmod['PerAdd']?>" checked="checked" >Add
<?php } else{?>
<input name="<?php  echo $row_frmmod['id'];?>[0]" type="checkbox" value="<?php  echo $row_frmmod['PerAdd']?>">Add

<?php }
 
 ?>
</td>
<td  width="20%"><?php 
 
 //echo $row_frmmod['PerEdit']; 
if($PerEdit!=0){	
?>
<input name="<?php  echo $row_frmmod['id'];?>[1]" type="checkbox" value="<?php  echo $row_frmmod['PerEdit']?>" checked="checked" >Edit
<?php } else{?>
<input name="<?php  echo $row_frmmod['id'];?>[1]" type="checkbox" value="<?php  echo $row_frmmod['PerEdit']?>">Edit

<?php } 
 ?>
		 
		</td>
 <td  width="20%">
<?php  
 

//echo $row_frmmod['PerDelete'];
if($PerDelete!=0){	
?> 
<input name="<?php  echo $row_frmmod['id'];?>[2]" type="checkbox" value="<?php  echo $row_frmmod['PerDelete']?>" checked="checked" >Delete
<?php }else{?>
<input name="<?php  echo $row_frmmod['id'];?>[2]" type="checkbox" value="<?php  echo $row_frmmod['PerDelete']?>">Delete

<?php }
  ?>
 		</td>
 	  </tr>
	</table>
<?php $i++; } 
//**********************************************************************************************************************		 
?>
		  </div>
		 
 		<?php } ?>
		 
			 
			 
			<!-- <input name="PerAdd" type="checkbox" value="1" <?php echo ($PerAdd==1?"checked":"") ?>  />ADD
			 <input name="PerEdit" type="checkbox" value="1" <?php echo ($PerEdit==1?"checked":"") ?>  />Edit
			 <input name="PerDelete" type="checkbox" value="1"  <?php echo ($PerDelete==1?"checked":"") ?> />Delete		-->			 </td>
		  </tr>
		  
		  
		  <tr>
			<td>Status</td> 
			 <td colspan="2"><input name="profilestatus" type="radio"  value="Y"   <?php  echo ($profilestatus =='Y'?'checked':'')?>/>Active
			<input name="profilestatus" type="radio" value="N" <?php  echo ($profilestatus =='N'?'checked':'')?>/>
			Inactive	</td>
		  </tr>
		  <tr>
			<td>&nbsp;</td>
			 <td colspan="2"><input type="submit" name="Save" value="Save"></td>
		  </tr>
		</table>
</form>


</td>
</tr>
<?php } ?>

</table>
 </td>
   
  </tr>
  <tr>
    <td>
	 <?php if($_REQUEST['action']==''){	?>
	<table width="100%" border="0" cellspacing="1" cellpadding="2">
  <tr>  <th scope="col" width="10%">Profile Name</th>
  <th scope="col" width="10%">Module </th>
   
	<th scope="col" width="10%">Form Modules  </th>
	<th scope="col" width="10%">Status</th>
	      <th scope="col" width="30%">
		   <table width="100%" border="0" cellspacing="0" cellpadding="0">
		  <tr>

			<th scope="col"  colspan="3">Permission</th>
		  </tr>
		  <tr>
			<th scope="col">Add</th>
			<th scope="col">Edit</th>
			<th scope="col">Delete</th>
		  </tr>
		</table>		   </th>
				 
 	<th scope="col" width="10%">Edit</th>
    <th scope="col"width="10%">Delete</th>
  </tr>
 
  <tr>
  <td colspan="7">
  
  

  
   <?php 
  $GetRecord="select * from company_profile_master ";
   $ResultData = DB_query($GetRecord,$db);
   $i=1;
   if(DB_num_rows($ResultData)==0)
   echo "--No Record--";
   
   while($RowData = DB_fetch_array($ResultData))
   {?><hr />
    <table bgcolor='#CCCCCC' border='0' width='100%'>
	<?php 
	  echo '<b>'.$RowData['profile_name'].'</b>';  
  	$grecord=DB_query("Select * from company_Profile where profile_id='".$RowData['id']."'  ",$db);
 	  while($RowData1 = DB_fetch_array($grecord))
	  {
   ?> 
      <tr> <td width="10%"><?php echo $RowData['profile_name']; ?>&nbsp;	  </td>
   
   <td width="10%"><?php echo  $s->getField('modules','id',$RowData1['moduleid'],'modulename');   ?>	  </td>
 
   <td width="10%"><?php echo  $s->getField('modules_form','id',$RowData1['frm_moduleid'],'frm_name'); ?>	  </td>
    <td width="10%"><?php echo ($RowData1['profilestatus']=='Y'?'Active':'Inactive'); ?>	  </td>
      <td width="30%">
	   <table width="100%" border="0" cellspacing="0" cellpadding="0">
	  <tr>
		<td>
		<?php   $split=explode(',',$RowData1['Permission'])?>
		<input name="PerAdd" type="checkbox" value="<?php echo $RowData1['PerAdd']; ?>" <?php echo ($RowData1['PerAdd']==1?"checked":"")?>/></td>
		<td><input name="PerEdit" type="checkbox" value="<?php echo $RowData1['PerEdit']; ?>" <?php echo ($RowData1['PerEdit']==1?"checked":"")?> /></td>
		<td><input name="PerDelete" type="checkbox" value="<?php echo $RowData1['PerDelete']; ?>"  <?php echo ($RowData1['PerDelete']==1?"checked":"")?>/></td>
	  </tr>
	</table></td>
   <td width="10%"><div align="center"><a href="ControlPanel.php?route=ProfileMaster&amp;action=Edit&amp;Id=<?php echo $RowData1['id']; ?>"><img src="images/icon-edit.png"  width="10" height="10"/></a></div></td>
	<td width="10%"><div align="center"><a href="ControlPanel.php?route=ProfileMaster&amp;action=del&amp;Id=<?php echo $RowData1['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>
<?php $i++;
		} 
	?> </table> 
<?php 
 }   
   ?>  
  
  
  
  </td>
  </tr>
   <?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))
   {
   
  	$grecord=DB_query("Select * from company_Profile where profile_name='".$RowData['profile_name']."' LIMIT 0, 10",$db);
	echo DB_num_rows($grecord);
	 
   ?> 
      <tr> <td><?php echo $RowData['profile_name']; ?>	  </td>
   
   <td><?php 
    echo  $s->getField('modules','id',$RowData['moduleid'],'modulename');
    
    ?>	  </td>
 
   <td><?php      echo  $s->getField('modules_form','id',$RowData['frm_moduleid'],'frm_name')
    
   
    ?>	  </td>
    <td><?php echo ($RowData['profilestatus']=='Y'?'Active':'Inactive'); ?>	  </td>
      <td>
	   <table width="100%" border="0" cellspacing="0" cellpadding="0">
	  <tr>
		<td>
		<?php   $split=explode(',',$RowData['Permission'])?>
		<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="ControlPanel.php?route=ProfileMaster&amp;action=Edit&amp;Id=<?php echo $RowData['id']; ?>"><img src="images/icon-edit.png"  width="10" height="10"/></a></div></td>
	<td><div align="center"><a href="ControlPanel.php?route=ProfileMaster&amp;action=del&amp;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>
	<?php $i++;}   
	*/
   ?> <tr>
    <td colspan="7"><div align="center"> <?=$pagination?></div></td></tr>

</table>
	<?php } ?>
     </td>
   </tr>
</table>
