<?php 
// Include main file
include_once('../libs/main.php');

// Check module Login
include_once('checkLogin.php');

// Logical section of page
$roleObj =  new  role();
$adminlogObj = new admin_log();
$modulepagesObj = new module_pages();

$filename=basename($_SERVER['SCRIPT_FILENAME']);
if($filename == 'roleadd.php')
{
    $module_id = $adminlogObj->getModuleId($filename);
    $page_id = $adminlogObj->getPageId($filename);
    $allpages = $modulepagesObj->fromId($page_id);
    $pagename = $allpages->page;
}

if(isset($_REQUEST['act']) && $_REQUEST['act']=='edit' && isset($_REQUEST['id'])){
	//$moduleDetail = $modulesObj->fromId($_REQUEST['id']);
    $where = "role_id=".$_REQUEST['id'];
    $data   = array('where' => $where);
    $roleDetails = $roleObj->fromFields($data);
}

if(isset($_REQUEST['act']) && $_REQUEST['act']=='validate_name' && isset($_REQUEST['role_name']) && $_REQUEST['role_name'] != "")
{
    
    if(isset($_REQUEST['role_id']) && $_REQUEST['role_id'] != '' && $_REQUEST['role_name'] != "")
    {

        $fields = array('role_id','role_name');
        $where = "role_name='".$_REQUEST['role_name']."'";
        $data =array('fields'=>$fields,'where'=>$where);        
        $roleDetails = $roleObj->fromFields($data);
        //print_r($roleDetails);exit;
        if($roleDetails)
        {
            if($_REQUEST['role_id'] != $roleDetails->role_id)
            {
                echo "Name already exists";
                exit;    
            }else{
                echo "success";
                exit;
            }
        }else
        {
            echo "success";
            exit;
        }
    }else
    {
        $fields = array('role_id','role_name');
        $where = "role_name='".$_REQUEST['role_name']."'";
        $data =array('fields'=>$fields,'where'=>$where);        
        $roleDetails = $roleObj->fromFields($data);
        //print_r($roleDetails);exit;
        if($roleDetails)
        {
            echo "Name already exists";
            exit;
        }else
        {
            echo "success";
            exit;
        }    
    }    
    
    return true;

}
//exit;
//if(isset($_POST['submit'])){
    if(isset($_REQUEST['act']) && $_REQUEST['act']=='load' && isset($_REQUEST['role_name']) && $_REQUEST['role_name'] != "")
    {
	   
    	$data=array(
		  'role_name'=>trim($_POST['role_name']),
          'role_status'=>'1'
	    );

        
        $data['role_access'] =  (isset($_POST['module_access']) && count($_POST['module_access']) > 0) ? implode(',',$_POST['module_access']) : ''; 
        
       $roleDts = $roleObj->fromId($_POST['role_id']);
        
       if($roleDts->role_name != $_POST['role_name'])
        {
            $role_detail .= " <b>Role Name</b>: "." ".$roleDts->role_name." <b>To</b> ".$_POST['role_name']." and ";
            
            $chages_data = true;
        }

        if(!empty($data['role_access']))
        {
            $new_all_role = explode(",",$data['role_access']);
            if(in_array("Full",$new_all_role))
            {
                $data['role_access'] = 'Full';
            }
        }
        if(isset($roleDts->role_access) && $roleDts->role_access!= '')
        {
            $old_all_role = explode(",",$roleDts->role_access);    
        }
        
        $result_role=array_diff($old_all_role,$new_all_role);

        if(!empty($result_role))
        {
            $role_detail .= " <b>Role access</b>: ".$roleDts->role_access." <b>To Role</b>: ".rtrim($data['role_access'],",");
            $chages_data = true;  
        }else{
            if(!empty($new_all_role) && !empty($old_all_role))
            {   
                //print_r($_POST['module_access']);
                if(count($new_all_role) > count($old_all_role))
                {
                    $role_detail .= " <b>Role access</b> : ".$roleDts->role_access." <b>To Role access</b>: ".rtrim($data['role_access'],",")." and ";
                    $chages_data = true; 
                }
            }
        }

	if (isset($_POST['role_id'])){
        $role_id =  $_POST['role_id'];
		$rs=$roleObj->updateRecords($data,$role_id);
		if ($rs)
        {
            if($chages_data == true)
            {
                $roffer_detail = substr($offer_detail, 0, -4);
                $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$role_detail);
            }
           //echo "Successfully update";
           header('Location:rolelist.php');
			exit;
		}

	}else{
        $rs = $roleObj->addRecords($data);

        $roledata = "<b>Add Record</b> ";
        $roledata.= " <b>Role Data</b>: ".json_encode($data);

        if($rs)
        {
            //echo "Successfully insert";
            $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$roledata);
    		header('Location:rolelist.php');
    		exit;
        }
	}
}


/*$modulesObj = new modules();
$tables = array('tbl_module_pages mp');
$jfields = array('mp.module_id = m.id');
$jtype   = array("LEFT");
$data   = array('joinTables' =>$tables, 'joinFields' =>$jfields, 'joinTypes' => $jtype);
$allModules = $modulesObj->getAllRecords($data);*/


$modulesObj = new modules();
$allModules = $modulesObj->getAllRecords();

// Include Template 
include_once('../libs/routs.php');
?>