<?php 

class role_edit extends db_query
{
   
    function getrole_parent_data($id)
    {
        global $db;
        $secure_data=DB_fetch_assoc(DB_query("select * from securityroles where secroleid='".$id."'",$db));
        return $secure_data;
    }
     function getrole_child_data($id)
    {
        global $db;
        
        $secure_childsql=DB_query("select * from company_profile where roll_id='".$id."'",$db);
        while($secure_childdata1=DB_fetch_assoc($secure_childsql))
        {
            $secure_childdata[$secure_childdata1['frm_moduleid']]=$secure_childdata1;
        }
        return $secure_childdata;
    }
    function save_edit_data($data)
    {
        global $db;
        //echo "<pre>";
       // print_r($data['role_form']);print_r($data['form_pkid']); 
        
       $str=implode(',',$data['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(',',$data['company']);
         DB_query("update securityroles set secrolename='".$data['rolename']."', role_name='".$data['rolename']."',modules_allowed='".$mod_allowed."',companyid='".$company."',rolestatus='".$data['rolestatus']."', modified_by='".$_SESSION['UserID']."',modified_date='".date('Y-m-d')."' where secroleid='".$data['Id']."' ",$db);

         //DB_query(" delete from company_profile where roll_id='".$data['Id']."' ",$db);
    foreach($data['role_form'] as $key => $val)
    {
       if($data['add'][$val]==1)
           $add=1;
       else
           $add=0;
       
       if($data['edit'][$val]==1)
           $edit=1;
       else
           $edit=0;
       
       if($data['delete'][$val]==1)
           $delete=1;
       else
           $delete=0;
       
          if($data['view'][$val]==1)
           $view=1;
       else
           $view=0;
       
          if($data['approval'][$val]==1)
           $approval=1;
       else
           $approval=0;
       
         // if($data['cancel'][$val]==1)
         //  $cancel=1;
      // else
         //  $cancel=0;
       
  $permission=$add.','.$edit.','.$delete.','.$view.','.$approval;
 
  if($data['form_pkid'][$val]!=0)
  { 
 DB_query("update  company_profile set Permission='".$permission."', PerAdd='".$add."',PerEdit='".$edit."',PerDelete='".$delete."' ,PerView='".$view."' ,PerApprove='".$approval."', modified_by='".$_SESSION['UserID']."',modified_date='".date('Y-m-d')."' where id='".$data['form_pkid'][$val]."'  ",$db);
  }
  else if($data['form_pkid'][$val]==0)
  {
      DB_query("insert into company_profile (moduleid,frm_moduleid,roll_id,Permission,PerAdd,PerEdit,PerDelete,PerView,PerApprove,created_by,created_date) value('".$data['moduleid'][$val]."','".$val."','".$data['Id']."','".$permission."','".$add."','".$edit."','".$delete."','".$view."','".$approval."','".$_SESSION['UserID']."','".date('Y-m-d')."')",$db);
  }
      
    } 
     foreach($data['form_pkid'] as $k=>$v)
        { 
           if(!in_array($k,$data['role_form']) && $v!=0)
                 DB_query("delete from company_profile where id='".$v."' ",$db);
        } 

      parent::pageLocation("index.php?route=Setup/RoleMaster&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']);    
    }
}
?>