<?php

class PermissionLevel extends CI_Controller {
    
    var $tablename = PERMISSIONLEVEL;
    var $primarykey = "PLID";
    var $primarykey2 = "PermissionID";
    var $primarykey3 = "AMID";
    
        
    public function index(){               
        $data['current'] = "permissionlevel";                       
        $data['customers'] = $this->general->getAllRecordsWhere($this->tablename,array(),"$this->primarykey","asc");
        $this->load->view(ADMINFOLDER."/permissionlevel",$data);
    }
    
    
    public function AddItem(){
        
        
        $this->load->library('form_validation');
         
        $this->form_validation->set_rules('txt_pltitle', 'Permission Level Title', 'trim|required');
                        
        $data['message'] = "";
		
        if ($this->form_validation->run() == TRUE)
	{
            $plname = $this->input->post("txt_pltitle");     
            
            
                         $updateData = array(
                            "PLName"=>$plname,
                            
                            "is_active"          => $this->input->post("txt_active"),
                            "date_added"         => date("Y-m-d H:i:s"),
                            "added_by"           => $this->session->userdata("AdminID"),
                            "date_modified"      => date("Y-m-d H:i:s")
                            );

                        $this->general->insertRecord($this->tablename, $updateData, "PLID");
                        $data['message'] = "Permission Level Added Successfully";
                                                                                              
        }
            
            
        $this->load->view(ADMINFOLDER."/addpermissionlevel",$data);                
    }
    
    
    public function editItem($id){
        
        
        $this->load->library('form_validation');
         
        $this->form_validation->set_rules('txt_pltitle', 'Permission Level Title', 'trim|required');
                
        $data['message'] = "";
		
        if ($this->form_validation->run() == TRUE)
	{
            $plname = $this->input->post("txt_pltitle");     
            
            
            $updateData = array(
                "$this->primarykey" 	=> $id,                
                "PLName"=>$plname,
                                
                "is_active"             => $this->input->post("txt_active"),                
                "date_modified"   => date("Y-m-d H:i:s")
                );
            
                                                                                     
            $this->general->updateRecord($this->tablename, $updateData, "PLID");
                                       
            $data['message'] = "Permission Level Updated Successfully";
        }
            
        $data['record'] = $this->general->getRecordWhere($this->tablename,array("$this->primarykey"=>$id), "$this->primarykey","desc");    
        $this->load->view(ADMINFOLDER."/editpermissionlevel",$data);                
    }
    
                           
    public function delete($id){        
        $newsRecord = $this->general->getRecordWhere($this->tablename, array("$this->primarykey"=>$id));
			if(count($newsRecord)){				                                                                                                                                        
				$this->db->query("DELETE FROM ".$this->tablename." WHERE $this->primarykey = $id");
				$data['message'] = $this->general->generateMessage("Record Deleted Successfully !!!", "green");
			}else{
				$data['message'] = $this->general->generateMessage("No record found with the specified id.", "red");
			}                                            
            $this->index();              
    }
    
    public function editpermissions($plid){ 
        
        $this->load->library('form_validation');
        $this->form_validation->set_rules('submit', 'At Least One Checkbox to be Choosen', 'trim');
        
        
        $data['current'] = "permissionlevel";                       
        $data['permissions'] = $this->general->getAllRecordsWhere(PERMISSIONS,array("PLID"=>$plid),"$this->primarykey2","asc");
        $data['adminmenus'] = $this->general->getAllRecordsWhere(ADMINMNEUS,array("is_active"=>1),"$this->primarykey3","asc");
        
        
        if ($this->form_validation->run() == TRUE)
	{
            // update checkboxes in database            
            foreach($data['permissions'] as $row)
            {
                $viewresult = $this->input->post("view_".$row->PermissionID)?1:0;
                $editresult = $this->input->post("edit_".$row->PermissionID)?1:0;
                $deleteresult = $this->input->post("delete_".$row->PermissionID)?1:0;
                
                    $updateData = array(
                     "$this->primarykey2" 	=> $row->PermissionID,                
                     "View"=>$viewresult,
                     "Edit"=>$editresult,
                     "Delete"=>$deleteresult                                                                  
                     );   
                    
                    $this->general->updateRecord(PERMISSIONS, $updateData, "$this->primarykey2"); 
            }
        }
        
        if(count($data['permissions']) > 0)
        {
            // check if all permissions available in permissions database
            foreach ($data['adminmenus'] as $row)
            {
                $exist = 0;
                foreach ($data['permissions'] as $row2)
                {
                    if($row2->AdminMenuID == $row->AMID)
                    {  $exist = 1; }
                }
                
                if($exist == 0)
                {
                    // add admin menu to permissions
                    $updateData = array(
                            "AdminMenuID"    => $row->AMID,                            
                            "PLID"           => $plid,
                            "View"           => 0,
                            "Edit"           => 0,
                            "Delete"         => 0
                            );
                    $this->general->insertRecord(PERMISSIONS, $updateData, "$this->primarykey2");
                }
            }                        
        }
        else
        {
            // add all menus to permissions related to this level
            foreach ($data['adminmenus'] as $row)
            {
                $updateData = array(
                            "AdminMenuID"    => $row->AMID,                            
                            "PLID"           => $plid,
                            "View"           => 0,
                            "Edit"           => 0,
                            "Delete"         => 0
                            );
                $this->general->insertRecord(PERMISSIONS, $updateData, "$this->primarykey2");
            }            
        }
        
                                
        $query = "select * FROM ed_adminmenus AM LEFT JOIN ed_permissions P on AM.AMID = P.AdminMenuID Where P.PLID = " . $plid;
        $data["finallist"] = $this->general->get_query_result($query);
        $this->load->view(ADMINFOLDER."/editpermissions",$data);   
    }
    
}
