
<link rel="stylesheet" href="css/new_mxerp_role.css">
<style type="text/css">


/* Hide footnote and breadcrumb stuff for screen. See print.css for the code that displays these. */
#ku_breadcrumbs, sup.footnote_tag, #footnote_wrapper {
	display: none;
}



/***** Tabbox ****/

/**
 * Tabbed widget
 */
.tabbox > .nav {
    list-style-type: none;
    margin: 0 0 -1px 0;
    padding-left: 0;
}

.tabbox > .nav li {
    background-color: #ebebeb;
    border: 1px solid #ccc;
    display:inline-block;
    height: 30px;
    line-height: 30px;
    margin: 0;
    margin-left: -6px;
    padding-bottom: 0;
    padding-left: 0;
    padding-top: 0;
    text-indent: 0;
}

.tabbox.tabbox-rounded > .nav li {
    border-radius: 10px 10px 0 0;
}

.tabbox > .nav li.active {
    border-bottom: 1px solid #fff;
    background-color: #ffffff;
}

.tabbox > .nav li:first-child {
    margin-left: 0;
}

.tabbox > .nav li a {
    display: block;
    height: 100%;
    padding: 0 10px;
    text-decoration: none;
	font-size:13px;
	padding-top:5px;
}

.tabbox.tabbox-rounded > .nav li a {
    border-radius: 10px 10px 0 0;
}

.tabbox > .nav li.active a {
    background-color: #fff;
    color: #e8000d;
}

.tabbox > .content {
    background: #fff;
    border: 1px solid #ccc;
    padding: 10px;
}

.tabbox > .content > .tab {
    height: 0;
    visibility: hidden;
    width: 0;
    display: none;
}

.tabbox > .content > .active {
    display: block;
}

.tabbox > .content > .tab.active {
    height: auto;
    visibility: visible;
    width: auto;
}

#region-sidebar-first .tabbox > .nav li {
    background: #fff;
    border: 1px solid #fff;
    border-width: 1px 1px 0 1px;
    margin-left: -5px;
}

#region-sidebar-first .tabbox > .nav li:first-child {
    margin-left: 5px;
}

#region-sidebar-first .tabbox > .nav li.active {
    border: 1px solid #ccc;
    border-bottom: 1px solid #fff;
}

#region-sidebar-first .tabbox > .content {
    border: 0;
    border-top: 1px solid #ccc;
}

/***** Accordions ****/
.accordion {
    cursor: pointer;
    font-family: Georgia, Times, "Times New Roman", serif;
    margin: 10px 0;
    font-size: 14px;
    font-weight: bold;
    padding-left: 12px;
    background: transparent url(../images/accordion-closed.gif) no-repeat center left;
    color: #0022B4;
}

.active_accordion {
    background: transparent url(../images/accordion-open.gif) no-repeat center left;
}

.accordion_content {
    padding: 0 0 10px 32px;
}


/*--For Tree Structure--*/
.treeview, .treeview ul { 
	padding: 0;
	margin: 0;
	list-style: none;
}

.treeview ul {
	background-color: white;
	margin-top: 4px;
}

.treeview .hitarea {
	background: url(images/treeview-default.gif) -64px -25px no-repeat;
	height: 16px;
	width: 16px;
	margin-left: -16px;
	float: left;
	cursor: pointer;
}
/* fix for IE6 */
* html .hitarea {
	display: inline;
	float:none;
}

.treeview li { 
	margin: 0;
	padding: 3px 0pt 3px 16px;
}

.treeview a.selected {
	background-color: #eee;
}

.treecontrol { margin: 1em 0; display: none; }

.treeview .hover { color: red; cursor: pointer; }

.treeview li { background: url(images/treeview-default-line.gif) 0 0 no-repeat; }
.treeview li.collapsable, .treeview li.expandable { background-position: 0 -176px; }

.treeview .expandable-hitarea { background-position: -80px -3px; }

.treeview li.last { background-position: 0 -1766px }
.treeview li.lastCollapsable, .treeview li.lastExpandable { background-image: url(images/treeview-default.gif); }  
.treeview li.lastCollapsable { background-position: 0 -111px }
.treeview li.lastExpandable { background-position: -32px -67px }

.treeview div.lastCollapsable-hitarea, .treeview div.lastExpandable-hitarea { background-position: 0; }

.treeview-red li { background-image: url(images/treeview-red-line.gif); }
.treeview-red .hitarea, .treeview-red li.lastCollapsable, .treeview-red li.lastExpandable { background-image:url(images/login/treeview-red.gif); } 

.treeview-black li { background-image: url(images/treeview-black-line.gif); }
.treeview-black .hitarea, .treeview-black li.lastCollapsable, .treeview-black li.lastExpandable { background-image: url(images/treeview-black.gif); }  

.treeview-gray li { background-image: url(images/treeview-gray-line.gif); }
.treeview-gray .hitarea, .treeview-gray li.lastCollapsable, .treeview-gray li.lastExpandable { background-image: url(images/treeview-gray.gif); } 

.treeview-famfamfam li { background-image: url(images/treeview-famfamfam-line.gif); }
.treeview-famfamfam .hitarea, .treeview-famfamfam li.lastCollapsable, .treeview-famfamfam li.lastExpandable { background-image: url(images/treeview-famfamfam.gif); } 


.filetree li { padding: 3px 0 2px 16px; }
.filetree span.folder, .filetree span.file { padding: 1px 0 1px 16px; display: block; }
.filetree span.folder { background: url(images/folder.gif) 0 0 no-repeat; }
.filetree li.expandable span.folder { background: url(images/folder-closed.gif) 0 0 no-repeat; }
.filetree span.file { background: url(images/file.gif) 0 0 no-repeat; }

.f13{font-family:Arial, Helvetica, sans-serif; font-size:12px; font-weight:bold;}
.f12{font-family:Arial, Helvetica, sans-serif; font-size:12px;}
</style>
<script type="text/javascript" src="js/jquery_005.js"></script>
<script type="text/javascript" src="js/ku_common.js"></script>

<script type="text/javascript" src="js/tree/jquery_002.js"></script>
<script src="js/tree/jquery.js" type="text/javascript"></script>
<script src="js/tree/jquery_003.js" type="text/javascript"></script>
<script type="text/javascript" src="js/tree/demo.js"></script>

<?php 
if($_REQUEST['action']=="add")
{
if(isset($_REQUEST['save']))
{
   $str=implode(',',$_REQUEST['role_form']);
   $modsql=DB_query("SELECT DISTINCT(a.id) as mid,a.modulename
FROM modules a, modules_form  b
WHERE a.id=b.moduleid AND b.id IN(".$str.")",$db);
   while($mod_res1=DB_fetch_assoc($modsql)){
       $mod_res[]=$mod_res1['mid'];
   }
   
   $mod_allowed=implode(',',$mod_res);
   $company=implode(',',$_REQUEST['company']);
    Db_query("insert into securityroles(secrolename,role_name,modules_allowed,rolepid,companyid,rolestatus,created_by,created_date)value('".$_REQUEST['rolename']."','".$_REQUEST['rolename']."','".$mod_allowed."','0','".$company."','".$_REQUEST['rolestatus']."','".$_SESSION['UserID']."','".date('Y-m-d')."')",$db);
    $lastid=DB_Last_Insert_ID($db,'securityroles','secroleid');
    foreach($_REQUEST['role_form'] as $key => $val)
    {
       if($_REQUEST['add'][$val]==1)
           $add=1;
       else
           $add=0;
       
       if($_REQUEST['edit'][$val]==1)
           $edit=1;
       else
           $edit=0;
       
       if($_REQUEST['delete'][$val]==1)
           $delete=1;
       else
           $delete=0;
       
          if($_REQUEST['view'][$val]==1)
           $view=1;
       else
           $view=0;
       
          if($_REQUEST['approval'][$val]==1)
           $approval=1;
       else
           $approval=0;
       
         
       
  $permission=$add.','.$edit.','.$delete.','.$view.','.$approval;
       DB_query("insert into company_profile (moduleid,frm_moduleid,roll_id,Permission,PerAdd,PerEdit,PerDelete,PerView,PerApprove
       	,created_by,created_date) value('".$_REQUEST['moduleid'][$val]."','".$val."','".$lastid."','".$permission."','".$add."','".$edit."','".$delete."','".$view."','".$approval."','".$_SESSION['UserID']."','".date('Y-m-d')."')",$db);
    }
     $s->pageLocation("index.php?route=Setup/RoleMaster&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']);
}
?>

<form method="post" action="" onsubmit="javascript:return validate()">

<div class="tabbox">

    <table align="center" border="0" cellpadding="0" cellspacing="0" width="100%">
    		<tbody><tr>
    			<td>
    				<fieldset>
	<table align="center" border="0" cellpadding="5" cellspacing="2" width="100%">
	<tbody><tr>
		<td class="paddfeild"><font color="red">Role</font></td>
		<td>
		<input name="rolename" id="rolename" style="width:200px;background-color:#FFFFFF;" value="" type="text">				</td>				
		<td class="paddfeild"><font color="red">Status</font></td>
                <td><input name="rolestatus" type="radio" checked="true"  value="Y"  id="rolestatus1"/>
		  Active
            <input name="rolestatus" type="radio" value="N"  id="rolestatus"/>
Inactive</td>
                <td class="paddfeild"><font color="red">Company</font></td>
                <td><select name="company[]" multiple="multiple" id="mul_company"  >
            <?php $com_sql=DB_query("select * from companies",$db); while($com_res=DB_fetch_assoc($com_sql)){               echo  '<option value="'.$com_res['CompanyId'].'">'.$com_res['CompanyName'].'</option>';                
            }?>
           
            
          </select></td>
	</tr>
	</tbody></table>
    				</fieldset>
                          
                                
                            </fieldset>
    			</td>
    		</tr>
    	</tbody></table>
<ul class="nav" style="text-align:left;">
    <?php  
    $nn=0;$query=DB_query("select * from modules where status='Y'",$db);
                     while($row=DB_fetch_array($query))
                     { ?>
    <li class="<?php if($nn==0){?>active<?php } ?>"><a href="#"><?php echo $row['modulename'] ?></a></li>

                     <?php $nn++;} ?>
</ul>
  
<div class="content">   
    

 <?php  
 $mm=0;
 $m=0;
 
 $query=DB_query("select * from modules Where status='Y'",$db);
                     while($row=DB_fetch_array($query))
                     {   ?>
                   <div style="display: block; text-align:left;" class="tab <?php if($mm==0){?>active<?php } ?>">
<div align="center" style="margin-bottom:20px; width:500px; margin-left:330px;"><table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="50" align="left" valign="bottom" class="f12">
      <input type="checkbox" id="alladd_<?php echo $row['id'] ?>"  class="all_add" />Add</td>
    <td width="50" align="left" valign="bottom"> 
      <input type="checkbox" id="alledit_<?php echo $row['id'] ?>" class="all_edit" />Edit</td>
    <td width="55" align="left" valign="bottom"> 
      <input type="checkbox" id="alldelete_<?php echo $row['id'] ?>"  class="all_delete" />Delete</td>
    <td width="50" align="left" valign="bottom"> 
      <input type="checkbox" id="allview_<?php echo $row['id'] ?>"  class="all_view" />View</td>
    <td width="70" align="left" valign="bottom"> 
      <input type="checkbox" id="allapproval_<?php echo $row['id'] ?>"  class="all_approve" />Approval</td>
    <td align="left" valign="bottom"> 
      <input type="checkbox" id="allcancel_<?php echo $row['id'] ?>"  class="all_cancel" />Cancel</td>
  </tr>
</table>
</div>	
                       <div><input type="checkbox" class="full_module">
                       	<span style="font-weight: bold;font-style: italic;color: red">Full Module</span></div>
<ul class="red treeview-red treeview">
<?php 
$mod_cat=DB_query("select * from module_form_categories where module_id=".$row['id']." and status='Y'",$db); 
while($mod_cat_res=DB_fetch_assoc($mod_cat)){ 
?>

	<li class="expandable"><div class="hitarea expandable-hitarea"></div><span class="f13"><?php echo $mod_cat_res['cat_desc'] ?></span>
            
           <ul style="display: block;">
                <li class="last"><span class="f12" style="font-style: italic;color: #0a0;font-weight: bold"><input type="checkbox"  class="module_cat_checked" >All</span></li>
               <?php 
$form_sql=DB_query("select * from modules_form where module_form_category_id=".$mod_cat_res['id']." and status='Y' ",$db); 
while($form_res=DB_fetch_assoc($form_sql))
{ 
?>
            <li class="last">
                <table width="100%" border="0" cellspacing="0" cellpadding="0">
                    <tr>
                        <td width="300"><span class="f12"><input type="checkbox" id="role_form_<?php echo $form_res['id'] ?>"   value="<?php echo $form_res['id'] ?>"   name="role_form[]" ><?php echo $form_res['frm_name'] ?><input type="hidden" name="moduleid[<?php echo $form_res['id'] ?>]" value="<?php echo $row['id']; ?>"></span></td>
                        <td width="50" align="left"><input type="checkbox"  class="add_permission_<?php echo $row['id'] ?>" id="add_permission_<?php echo $form_res['id'] ?>" name="add[<?php echo $form_res['id'] ?>]" value="1" /></td>
                        <td width="50" align="left"><input type="checkbox"   class="edit_permission_<?php echo $row['id'] ?>" id="edit_permission_<?php echo $form_res['id'] ?>" name="edit[<?php echo $form_res['id'] ?>]" value="1" /></td>
                        <td width="55" align="left"><input type="checkbox"  class="delete_permission_<?php echo $row['id'] ?>" id="delete_permission_<?php echo $form_res['id'] ?>" name="delete[<?php echo $form_res['id'] ?>]" value="1" /></td>
                        <td width="50" align="left"><input type="checkbox"  class="view_permission_<?php echo $row['id'] ?>" id="view_permission_<?php echo $form_res['id'] ?>" name="view[<?php echo $form_res['id'] ?>]" value="1" /></td>
                        <td width="70"  align="left"><input type="checkbox" class="approval_permission_<?php echo $row['id'] ?>" id="approval_permission_<?php echo $form_res['id'] ?>"name="approval[<?php echo $form_res['id'] ?>]" value="1" /></td>
                        <td align="left"><input type="checkbox"  class="cancel_permission_<?php echo $row['id'] ?>" id="cancel_permission_<?php echo $form_res['id'] ?>" name="cancel[<?php echo $form_res['id'] ?>]" value="1" /></td>
                    </tr>
                </table>
            </li>
<?php $m++;} ?>
	</ul>
	</li>
	
	<?php } ?>
        </ul>
</div>
<?php $mm++;} ?>
</div>
</div>
    <input type="submit" value="Save" name="save">&nbsp; <input type="button" value="Cancel" name="">
</form>
<?php } else { ?>

<div class="panel-heading">
              <div class="panel-title"> <span class="glyphicon glyphicon-list-alt"></span>Roles</div>
              <div class="panel-btns pull-right margin-left">

<?php if($_REQUEST['action']==''){
    $targetpage = "index.php?route=Setup/RoleMaster&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."";
    ?>
<a href="<?php echo $targetpage;?>&action=add" class="btn btn-primary">AddNew</a> <?php } else{?>
<a href="javascript:history.go(-1)" class="btn btn-primary"> Go Back</a> 
<?php } ?>         
                
              </div>
            </div>
<div class="panel-body">
<div >
<table width="100%" border="0" cellspacing="1" cellpadding="2" style="border:#999999 solid 1px;">
  <tr> <th scope="col"><strong>Company </strong></th>
    <th scope="col"><strong>Role Name</strong></th> 
    <th scope="col"><strong>Under</strong></th>
	 
	<th scope="col"><strong>Status</strong></th>
	      <th scope="col"><strong>Edit</strong></th>
    <th scope="col"><strong>Delete</strong></th>
  </tr>
 
 
 
   <?php 
    $GetRecord = "Select * from securityroles  order by companyid ASC";
   $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))
   { $del_check_status='';
     $check=DB_fetch_array(DB_query("select count(*) as count from www_users where rolepid='".$RowData['secroleid']."'",$db));
     if($check['count']>0)
     $del_check_status='1';
   

   	?> 
    <tr bgcolor="#FFFFFF">
   <td><?php  echo  $s->getField('companies','CompanyId',$RowData['companyid'],'CompanyName');
   
    ?>	  </td>
	
   <td><?php echo $RowData['role_name']; ?>	  </td>
    <td><?php //echo $RowData['rolepid'];	 
	   if($RowData['rolepid']!=0){echo  $s->getField('securityroles','secroleid',$RowData['rolepid'],'role_name');}else{echo "Primary";}
	  ?>	  </td>
  
    <td><?php echo ($RowData['rolestatus']=='Y'?'Active':'Inactive'); ?>	  </td>
      <td><div align="center"><a href="index.php?route=Setup/RoleMaster_edit&modid=<?php echo $_REQUEST['modid']?>&frmid=<?php echo $_REQUEST['frmid'] ?>&action=Edit&Id=<?php echo $RowData['secroleid']; ?>"><img src="images/icon-edit.png"  width="10" height="10"/></a></div></td>
	<td><div align="center"><?php if($del_check_status=='') { ?>
	<a href="<?php echo $targetpage;?>&action=del&Id=<?php echo $RowData['secroleid']; ?>" onClick="return confirm('Do You Want To Delete This Record')"><img src="images/Icon_Delete.png" width="10" height="10" /></a>
    <?php } else { echo "No Permission"; } ?></div></td>	
  </tr>
  
	<?php $i++;}    
        
   ?> 
<?php } ?>
</table>
</div>
</div>
<script>
    $(document).ready(function() {
        $('.all_add').click(function(){
            var id=this.id.split("_");
         if($("#alladd_"+id[1]).attr('checked')==true)
             { 
              $('.add_permission_'+id[1]).attr('checked',true);
             }
            else if($("#alladd_"+id[1]).attr('checked')==false)
             {
                $('.add_permission_'+id[1]).removeAttr('checked');
             }
        });
        
        
        $('.all_edit').click(function(){
            var id1=this.id.split("_");
         if($("#alledit_"+id1[1]).attr('checked')==true)
             { 
              $('.edit_permission_'+id1[1]).attr('checked',true);
             }
            else if($("#alledit_"+id1[1]).attr('checked')==false)
             {
                $('.edit_permission_'+id1[1]).removeAttr('checked');
             }
        });
        
         $('.all_delete').click(function(){
            var id2=this.id.split("_");
         if($("#alldelete_"+id2[1]).attr('checked')==true)
             { 
              $('.delete_permission_'+id2[1]).attr('checked',true);
             }
            else if($("#alldelete_"+id2[1]).attr('checked')==false)
             {
                $('.delete_permission_'+id2[1]).removeAttr('checked');
             }
        });
      
      
       $('.all_view').click(function(){
            var id3=this.id.split("_");
         if($("#allview_"+id3[1]).attr('checked')==true)
             { 
              $('.view_permission_'+id3[1]).attr('checked',true);
             }
            else if($("#allview_"+id3[1]).attr('checked')==false)
             {
                $('.view_permission_'+id3[1]).removeAttr('checked');
             }
        });
        
         $('.all_approve').click(function(){
            var id4=this.id.split("_");
            
         if($("#allapproval_"+id4[1]).attr('checked')==true)
             { 
              $('.approval_permission_'+id4[1]).attr('checked',true);
             }
            else if($("#allapproval_"+id4[1]).attr('checked')==false)
             {
                $('.approval_permission_'+id4[1]).removeAttr('checked');
             }
        });
        
        
        $('.all_cancel').click(function(){
            var id4=this.id.split("_");
         if($("#allcancel_"+id4[1]).attr('checked')==true)
             { 
              $('.cancel_permission_'+id4[1]).attr('checked',true);
             }
            else if($("#allcancel_"+id4[1]).attr('checked')==false)
             {
                $('.cancel_permission_'+id4[1]).removeAttr('checked');
             }
        });
        
        
      $('input[name="role_form[]"]').click(function(){
          var frm_id=this.id.split("_");
          if($('#'+this.id).attr('checked')==true)
              {
                 
                   $('#add_permission_'+frm_id[2]).attr('checked','true');
                   $('#edit_permission_'+frm_id[2]).attr('checked','true');
                   $('#delete_permission_'+frm_id[2]).attr('checked','true');
                   $('#view_permission_'+frm_id[2]).attr('checked','true');
                   $('#approval_permission_'+frm_id[2]).attr('checked','true');
                   $('#cancel_permission_'+frm_id[2]).attr('checked','true');
              }
              else
                  {
                   $('#add_permission_'+frm_id[2]).removeAttr('checked');
                   $('#edit_permission_'+frm_id[2]).removeAttr('checked');
                   $('#delete_permission_'+frm_id[2]).removeAttr('checked');
                   $('#view_permission_'+frm_id[2]).removeAttr('checked');
                   $('#approval_permission_'+frm_id[2]).removeAttr('checked');
                   $('#cancel_permission_'+frm_id[2]).removeAttr('checked');
                  }
      });
      
      $('.module_cat_checked').click(function(){
        $(this).parent().parent().siblings('li').find("input[type='checkbox']").attr('checked', this.checked);
        });
      $('.full_module').click(function(){
         $(this).parent().siblings('ul').find("input[type='checkbox']").attr('checked', this.checked);
      });
   });
  
function validate()
{
    
  
   if($('input[name="role_form[]"]').filter(':checked').length==0)
       {
           alert("Please Check atleast one form")
           return false;
       }
    if(document.getElementById("rolename").value=='')
    {
            alert('Please enter name of the role');
            document.getElementById("rolename").focus();
            return false;
    }
    if(document.getElementById("rolestatus1").checked==false && document.getElementById("rolestatus").checked==false)
    {
            alert('Please choose status of the role');
            //document.getElementById("rolestatus1").focus();
            return false;
    }
   
 if($('#mul_company').val()==null)
        {
             alert('Please select Company');
               // $('#mul_company').focus();
            return false;
        }
}

</script>    
