<?php ob_start(); error_reporting(0);
$url_index= "index.php?route=Setup/SecurityCategoryForm&Application".$_REQUEST['Application']."&modid=".$_REQUEST['modid']."";
if(isset($_REQUEST['save']))
{		
	$path = "images/SecurityFromImages/";	
	for($i=0;$i<count($_REQUEST['module_form_categories']['cat_desc']);$i++)
	{
		if(isset($_REQUEST['module_form_categories']['cat_desc'][$i]) && !empty($_REQUEST['module_form_categories']['cat_desc'][$i]) && !empty($_REQUEST['module_form_categories']['module_id'][$i]))
		{			
			//echo "i= ".$i.$_REQUEST['module_form_categories']['cat_desc'][$i]; 			
			
			if(!empty($_FILES['image']['name'][$i]))
		    {
			    $name = $_FILES['image']['name'][$i]; 
				$tmp = $_FILES['image']['tmp_name'][$i];
				move_uploaded_file($tmp, $path.$name);				
		    }
			$insert="insert into module_form_categories set cat_desc='".$_REQUEST['module_form_categories']['cat_desc'][$i]."',
			module_id='".$_REQUEST['module_form_categories']['module_id'][$i]."',
			sequence='".$_REQUEST['module_form_categories']['sequence'][$i]."',
			status='".$_REQUEST['module_form_categories']['status'][$i]."',image='".$name."'";
			$query=DB_query($insert,$db);
		}
	}
	
		echo "<script>location.href='$url_index'</script>";
}

	if($_REQUEST['action']=='delete' && !empty($_REQUEST['cid']))
	{
		 
		$delete="delete from module_form_categories where id='".$_REQUEST['cid']."'";
		
		$query=DB_query($delete,$db);
			echo "<script>location.href='$url_index'</script>";
		
	}

if(isset($_REQUEST['action']) && isset($_REQUEST['cid']))
{
	if($_REQUEST['action']=='edit' && !empty($_REQUEST['cid']))
	{	$fnlib=DB_query("select * from module_form_categories where id='".$_REQUEST['cid']."'",$db);
		$recEdit=DB_fetch_array($fnlib);
		extract($recEdit);
	}
}

if(isset($_REQUEST['update']))
{		
	 
	if(!empty($_REQUEST['module_form_categories']['cat_desc']) && !empty($_REQUEST['module_form_categories']['module_id']))
	{			
		if(!empty($_FILES['image']['name']))
		    {
			    $name = $_FILES['image']['name']; 
				$tmp = $_FILES['image']['tmp_name'];
				move_uploaded_file($tmp, $path.$name);	
				$_REQUEST['module_form_categories']['image']=$name;			
		    }
		
		extract($_REQUEST['module_form_categories']);
		$update="update module_form_categories set cat_desc='".$_REQUEST['module_form_categories']['cat_desc']."',module_id='".$_REQUEST['module_form_categories']['module_id']."'
		,sequence='".$_REQUEST['module_form_categories']['sequence']."',status='".$_REQUEST['module_form_categories']['status']."' where id='".$_REQUEST['cid']."'";
	    $query=DB_query($update,$db);   
	}
	$s->pageLocation("index.php?route=Setup/SecurityCategoryForm&Application=".$_REQUEST['modid']."&modid=".$_REQUEST['modid']);
	
}
if(isset($_REQUEST['search']))
	{
		if(!empty($_REQUEST['module_form_categories']['cat_desc']))
		{
		$cat_desc=$_REQUEST['module_form_categories']['cat_desc'];
		$category_query=DB_query("select * from module_form_categories where cat_desc='".$cat_desc."'",$db);
		}
		else
		{
			//print_r($_REQUEST); exit;
			if(!empty($_REQUEST['module_form_categories']['module_id']))
			{
			$category_query=DB_query("select * from module_form_categories where module_id='".$_REQUEST['module_form_categories']['module_id']."'",$db);
			
			
			}
		}
}				


?><head>
<script type="text/javascript">
function showCategory(str)
{
if (str=="")
  {
  document.getElementById("txtHint").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("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","ajax_modulecategory.php?module="+str,true);
xmlhttp.send();
}
</script>
</head>

<?php 
if(isset($_REQUEST['action'])=='' || isset($_REQUEST['search']))
{
?>

<table width="100%" cellpadding="0" cellspacing="0" border="0">
  <tr>
    <td style="padding:5px"><!-- General details -->
      <table width="100%" cellspacing="0" cellpadding="0" border="0" class="small">
        <tr>
          <td style="padding:5px; text-align:right;" colspan="4" align="center">
		
		  <a href="<?php echo $url_index;?>&action=addnew"><input type="submit" value="Add New" name="button" class="add"></a></td>
        </tr><form name="module_form_categories" id="module_form_categories" method="POST">
		<table align="center" width="65%">
		<tr style="height:25px">
		<td width="8%" align="right" class="dvtCellLabel">Modules</td>
          <td width="34%" align="left" class="dvtCellInfo">
		  <?php $refer_query=DB_query("select * from modules",$db); ?>
		  <select name="module_form_categories[module_id]"  style="width:100%;"class="detailedViewTextBox" onchange="showCategory(this.value)" id="moduleid">
		  <option value="">--select module--</option>
		  <?php while($refer_row=DB_fetch_array($refer_query))
		  {
		  ?>
           <option value="<?php echo $refer_row['id']; ?>">
		   <?php echo $refer_row['modulename']; ?></option>
          <?php } ?> </select>
		  </td>
		  <td width="20%" align="right" class="dvtCellLabel">Module Category </td>
          <td width="38%" align="left" class="dvtCellInfo"><div id="txtHint">
		  <?php $refer_query=DB_query("select * from module_form_categories",$db);		  
		   ?>
		  <select name="module_form_categories[cat_desc]" style="width:100%;" class="detailedViewTextBox" >
  		  <option value="">--select module category--</option>

		  <?php while($refer_row=DB_fetch_array($refer_query))
		  {
		  ?>
           <option value="<?php echo $refer_row['cat_desc']; ?>"><?php echo $refer_row['cat_desc']; ?></option>
          <?php } ?> </select></div>
		  </td>
		<tr>
          <td style="padding:5px; text-align:center;" colspan="4" align="right">
		  <input type="submit" value="search" name="search" class="add"> 
		  <a href="<?php echo $url_cancel;?>"><input type="button" value="Cancel" name="button" class="add"></a>
		</td></tr>
		</table></form> 
        <tr>
          <td colspan="4" align="center" class="detailedViewHeader" style="text-align:center;"><b>Module Category</b></td>
        </tr>
        <tr>
          <td colspan="4"><table width="100%" cellspacing="1" cellpadding="3" border="0" class="lvt lists" bgcolor="#CCCCCC">
              <!-- Table Headers -->
              <tr>
                <td width="11%" class="lvtCol">Sl. No</td>
                <td width="26%" class="lvtCol">Module Name </td>
                <td width="31%" class="lvtCol">Category Name </td>
                <td width="12%" class="lvtCol">Squence</td>
                 <td width="5%" class="lvtCol">Image</td>
                <td width="10%" class="lvtCol">Status</td>
                
                <td class="lvtCol" colspan="2" style="padding:5px; text-align:center;">Action</td>
                
              </tr>
              <!-- Table Contents -->
			 	<?php
				if(isset($_REQUEST['search']))
				{			  	
				$i=0;
				while($category_row=DB_fetch_array($category_query))
				{
				$i++;
				extract($category_row);
				$db1=DB_query("select * from modules where id='".$module_id."'",$db);
				$module_name=DB_fetch_array($db1);
				
				?>
             	<tr bgcolor="white" id="row_4436" class="lvtColData">
	           	<td><?php echo "<b>". $i." : </b>";?></td>
                <td><?php echo $module_name['modulename'];?> </td>
	            <td><?php echo $cat_desc;?> </td>
                <td><?php echo $sequence;?></td>
                 <td><img src="<?php echo "category_images/+".$image; ?>" border="0" width="30" height="30"></td>
                <td><?php echo ($status=='Y'?'Active':'Inactive');;?></td>
                <td width="5%">
				<a href="<?php echo $url_index.'&action=edit&cid='.$id;?>"><img src="images/icon-edit.png"  width="10" height="10"/></a></td>
				<td width="5%"><a href="<?php echo $url_index.'&action=delete&cid='.$id;?>" onClick="return confirm('Are you sure you wish to delete this Record details ?')"><img src="images/Icon_Delete.png" width="10" height="10"/></a></td>
              </tr><?php }} 
			  	else {
			   	$category_query=DB_query("select * from module_form_categories ",$db);
				$i=0;
				while($category_row=DB_fetch_array($category_query))
				{
				$i++;
				extract($category_row);
				$fnlib=DB_query("select * from modules where id='".$category_row['module_id']."'",$db);
				$module_name=DB_fetch_array($fnlib);
				?>
             	<tr bgcolor="white" id="row_4436" class="lvtColData">
	           	<td><?php echo "<b>". $i." : </b>";?></td>
                <td><?php echo $module_name['modulename'];?> </td>
	            <td><?php echo $cat_desc;?> </td>
                <td><?php echo $sequence;?></td>
                 <td><img src="<?php echo "category_images/".$image; ?>" border="0" width="30" height="30"></td>
                <td><?php echo ($status=='Y'?'Active':'Inactive'); ?></td>
                 <td>
				<a href="<?php echo $url_index.'&action=edit&cid='.$id;?>"><img src="images/icon-edit.png"  width="10" height="10"/></a> </td>
				<td><a href="<?php echo $url_index.'&action=delete&cid='.$id;?>" onClick="return confirm('Are you sure you wish to delete this Record details ?')"><img src="images/Icon_Delete.png" width="10" height="10" /></a></td>
              </tr><?php }}?>
            </table></td>
        	<tr>
            </table></td>
        	<tr>
          <td style="padding:5px; text-align:right;" colspan="4" align="right"></td>
        </tr>
      </table></td>
  </tr>
</table>
<?php
}
 if($_REQUEST['action']=='addnew') {
 ?>
<form name="module_form_categories" id="module_form_categories" method="POST" enctype="multipart/form-data">
<table width="100%" cellpadding="0" cellspacing="0" border="0">
  <tr>
    <td style="padding:5px"><!-- General details -->
      <table width="100%" cellspacing="0" cellpadding="0" border="0" class="small">
        <tr>
          <td style="padding:5px; text-align:center;" colspan="4" align="right"></td>
        </tr>
        <tr>
          <td class="detailedViewHeader"  style="text-align:center;" colspan="8"><b>Add Category </b></td>
        </tr>
		<tr><td width="5%" align="center" class="dvtCellLabel"><b>Category Name</b> </td>
			<td width="5%" align="center" class="dvtCellLabel"><b>Module</b></td>
			<td width="5%" align="center" class="dvtCellLabel"><b>Sequence</b></td>
			<td width="5%" align="center" class="dvtCellLabel"><b>Status</b></td>
			<td width="5%" align="center" class="dvtCellLabel"><b>Image</b></td>
		</tr>        
		<?php for($i=0;$i<10;$i++) { ?>
        <tr style="height:25px">
		  
          <td width="23%" align="left" class="dvtCellInfo" >
		  <input type="text" value="<?php echo $cat_desc; ?>"  class="detailedViewTextBox" name="module_form_categories[cat_desc][]" ></td>
         
          <td width="25%" align="left" class="dvtCellInfo">
		  <?php $modules_query=DB_query("select * from modules",$db); ?>
		  <select name="module_form_categories[module_id][]"  class="detailedViewTextBox">
		  <option value="">---select module---</option>

		  <?php while($modules_row=DB_fetch_array($modules_query))
		  {
		  ?>
           <option value="<?php echo $modules_row['id']; ?>" <?php if($module_id==$modules_row['id']) echo "selected";?> >
		   <?php echo $modules_row['modulename']; ?></option>
          <?php } ?> </select></td>
          
          <td width="8%" align="left" class="dvtCellInfo" >
		  <input type="text" value="<?php echo $sequence;?>"   class="detailedViewTextBox" name="module_form_categories[sequence][]"></td>
		   
          <td width="23%" align="left" class="dvtCellInfo">
		  <select name="module_form_categories[status][]"  class="detailedViewTextBox"><option value="">---select status---</option>
           <option value="Y" <?php if($status=="Y") echo "selected";?> >Active</option>
           <option value="N" <?php if($status=="N") echo "selected";?> >Inactive</option>
          </select></td>
          <td><input type="file" name="image[]"></td>
		  <input type="hidden" name="module_form_categories[created][]" value="<?php echo date('Y-m-d')?>">
		  <input type="hidden" name="module_form_categories[created_by][]" value="<?php echo $_SESSION['UserID']?>">
        </tr> <?php }?>
		<tr><td>&nbsp; </td></tr>
        <tr>
          <td style="padding:5px; text-align:center;" colspan="8" align="right">
		   <input type="submit" value="Save" name="save" class="add">     <a href="<?php echo $url_index;?>"><input type="button" value="Cancel" name="button" class="add"></a>
</td>
        </tr>
      </table></td>
  </tr>
</table></form><?php } ?>
<?php
if($_REQUEST['action']=='edit' && !empty($_REQUEST['cid']))
	{ ?>
<form name="module_form_categories" id="module_form_categories" method="POST" enctype="multipart/form-data">
<table width="100%" cellpadding="0" cellspacing="0" border="0">
  <tr>
    <td style="padding:5px"><!-- General details -->
      <table width="100%" cellspacing="0" cellpadding="0" border="0" class="small">
        <tr>
          <td style="padding:5px; text-align:center;" colspan="4" align="right"></td>
        </tr>
        <tr>
          <td class="detailedViewHeader"  style="text-align:center;" colspan="8"><b>Edit Category </b></td>
        </tr>
		<tr><td width="5%" align="center" class="dvtCellLabel"><b>Category Name</b> </td>
			<td width="5%" align="center" class="dvtCellLabel"><b>Module</b></td>
			<td width="5%" align="center" class="dvtCellLabel"><b>Sequence</b></td>
			<td width="5%" align="center" class="dvtCellLabel"><b>Status</b></td>
			<td width="5%" align="center" class="dvtCellLabel"><b>Image</b></td>
		</tr>        
        <tr style="height:25px"><input type="hidden" name="module_form_categories[id]" value="<?php echo $id;?>">
		  
          <td width="23%" align="left" class="dvtCellInfo" >
		  <input type="text" value="<?php echo $cat_desc; ?>"  class="detailedViewTextBox" name="module_form_categories[cat_desc]" ></td>
         
          <td width="25%" align="left" class="dvtCellInfo">
		  <?php $modules_query=DB_query("select * from modules",$db); ?>
		  <select name="module_form_categories[module_id]"  class="detailedViewTextBox">
		  <option value="">---select module---</option>

		  <?php while($modules_row=DB_fetch_array($modules_query))
		  {
		  ?>
           <option value="<?php echo $modules_row['id']; ?>" <?php if($module_id==$modules_row['id']) echo "selected";?> >
		   <?php echo $modules_row['modulename']; ?></option>
          <?php } ?> </select></td>
          
          <td width="8%" align="left" class="dvtCellInfo" >
		  <input type="text" value="<?php echo $sequence;?>"   class="detailedViewTextBox" name="module_form_categories[sequence]"></td>
		   
          <td width="23%" align="left" class="dvtCellInfo">
		  <select name="module_form_categories[status]"  class="detailedViewTextBox"><option value="">---select status---</option>
           <option value="Y" <?php if($status=="Y") echo "selected";?> >Active</option>
           <option value="N" <?php if($status=="N") echo "selected";?> >Inactive</option>
          </select></td>
          <td><input type="file" name="image"></td>
		  <input type="hidden" name="module_form_categories[created]" value="<?php echo date('Y-m-d')?>">
        </tr>
		<tr><td>&nbsp; </td></tr>
        <tr>
          <td style="padding:5px; text-align:center;" colspan="8" align="right">
		   <input type="submit" value="Save" name="update" class="add">     <a href="<?php echo $url_index;?>"><input type="button" value="Cancel" name="button" class="add"></a>
</td>
        </tr>
      </table></td>
  </tr>
</table></form><?php } ?>

