<?php
//-------------ID hak akses = 3

class Ps_hakakses extends CI_Controller {
    
//-----------start variables for check action access----------//
    public $vartambah   = null;
    public $varedit     = null;
    public $vardelete   = null;
    public $varview     = null;
//-----------start variables for check action access----------//
    
    function __construct(){
		parent::__construct();
		$this->check_isvalidated();
        $this->load->helper(array('form', 'date'));        
	}
    private function check_isvalidated(){
	   if(! $this->session->userdata('validated')){
	       redirect('index.php/ps_login');
        }
//--------------start check access this form page-------------//
        $this->db->where('mh_tipeuser_id', $this->session->userdata('s_FK_TipeUser'));
        $this->db->where('mh_menu_id', '3');
        $this->db->where('mh_access', 1);
        $cekMenu = $this->db->get('m_hakakses');
        $rowcount = $cekMenu->num_rows();
        if($rowcount==0){
            redirect('index.php');
        }else{
            foreach ($cekMenu->result() as $cm)
                {
                    $this->vartambah= $cm->mh_add;
                    $this->varedit  = $cm->mh_edit;
                    $this->vardelete= $cm->mh_delete;
                    $this->varview  = $cm->mh_view;
                }
        }
//--------------end check access this form page-------------//
    }

    public function index($error = NULL) {
        $data['title']          = 'Hak Akses';
        $data['link']           = 'ps_hakakses';
        $data['main_content']   = 'setting/hakakses/v_hakakses';
        $data['error']          = $error;
        
        $this->load->model('m_hakakses');
        $datanya = $this->m_hakakses->index();
        $this->load->vars('ha', $datanya);
        
        $this->load->view('includes/template', $data);
    }

// fungsi untuk menampilkan form tambah data
    public function add() {
        //----start validate page tambah-------//
        if($this->vartambah==null){redirect('index.php/ps_hakakses');}
        //----end validate page tambah---------//
        $data['titlebefore']    = 'Hak Akses';
        $data['title']          = 'Tambah Tipe User';
        $data['link']           = 'ps_hakakses';
        $data['actionform']     = 'prosesadd';
        $data['main_content']   = 'setting/hakakses/form_tipeuser';
        
        $this->load->view('includes/template', $data);
    }

//fungsi untuk memproses penambahan dengan memanggil model 
    public function prosesadd() {
        //----start validate page tambah-------//
        if($this->vartambah==null){redirect('index.php/ps_hakakses');}
        //----end validate page tambah---------//
        $this->load->helper('form');
        $this->load->helper('url');
        $this->load->library('form_validation');
        $this->form_validation->set_rules('nama','Tipe User','required');
        
        if($this->form_validation->run()==FALSE){
			$data['titlebefore']    = 'Hak Akses';
            $data['title']          = 'Tambah Tipe User';
            $data['link']           = 'ps_hakakses';
            $data['actionform']     = 'prosesadd';
            $data['main_content']   = 'setting/hakakses/form_tipeuser';
            $this->load->view('includes/template', $data);
		}else{
            $this->load->model('m_hakakses');
            $this->m_hakakses->insert_tipeuser();
            $this->session->set_flashdata('message', 'Anda berhasil Menambahkan Tipe User baru');
			redirect('index.php/ps_hakakses');
		}
    }

//fungsi untuk menampilkan form edit data dengan data terpilih
    public function edit($id) {
        //----start validate page edit-------//
        if($this->varedit==null){redirect('index.php/ps_hakakses');}
        //----end validate page edit---------//
        $data['titlebefore']    = 'Hak Akses';
        $data['title']          = 'Edit Tipe User';
        $data['link']           = 'ps_hakakses';
        $data['actionform']     = 'prosesedit';
        $data['main_content']   = 'setting/hakakses/form_tipeuser';
        
        $this->load->model('m_hakakses');
        $datanya = $this->m_hakakses->edit_tipeuser($id);
        $this->load->vars('ha', $datanya);
        
        $this->load->view('includes/template', $data);
    }

//funsi untuk memproses update data 
    public function prosesedit() {
        //----start validate page edit-------//
        if($this->varedit==null){redirect('index.php/ps_hakakses');}
        //----end validate page edit---------//
        $id = $this->input->post('id');
        $this->load->helper('form');
        $this->load->helper('url');
        $this->load->library('form_validation');
        $this->form_validation->set_rules('nama','Tipe User','required');
        
        if($this->form_validation->run()==FALSE){
            $data['titlebefore']    = 'Hak Akses';
            $data['title']          = 'Edit Tipe User';
            $data['link']           = 'ps_hakakses';
            $data['actionform']     = 'prosesedit';
            $data['main_content']   = 'setting/hakakses/form_tipeuser';
            
            $this->load->model('m_hakakses');
            $datanya = $this->m_hakakses->edit_tipeuser($id);
            $this->load->vars('ha', $datanya);
            
            $this->load->view('includes/template', $data);
        }
        else{
            $this->session->set_flashdata('message', 'Anda berhasil Mengedit Tipe User');
            $this->load->model('m_hakakses');
            $this->m_hakakses->prosesedit_tipeuser();
            redirect('index.php/ps_hakakses');
        }
    }

//fungsi untuk menampilkan form view data dengan data terpilih
    public function view($id) {
        //----start validate page view-------//
        if($this->varview==null){redirect('index.php/ps_hakakses');}
        //----end validate page view---------//
        $data['titlebefore']    = 'Hak Akses';
        $data['title']          = 'View Tipe User';
        $data['actionform']     = 'prosesview';
        $data['link']           = 'ps_hakakses';
        $data['main_content'] = 'setting/hakakses/form_tipeuser';
        
        $this->load->model('m_hakakses');
        $datanya = $this->m_hakakses->edit_tipeuser($id);
        $this->load->vars('ha', $datanya);
        
        $this->load->view('includes/template', $data);
    }

//fungsi untuk delete 
    function delete($id) {
        //----start validate page delete-------//
        if($this->vardelete==null){redirect('index.php/ps_hakakses');}
        //----end validate page delete---------//
        $this->session->set_flashdata('message', 'Anda berhasil Menghapus Tipe User');
        $this->load->model('m_hakakses');
        $this->m_hakakses->delete_tipeuser($id);
        redirect('index.php/ps_hakakses');
    }
    
    function disabled($id) {
        //----start validate page delete-------//
        if($this->varedit==null){redirect('index.php/ps_hakakses');}
        //----end validate page delete---------//
        $this->session->set_flashdata('message', 'Anda berhasil Disabled Tipe User');
        $this->load->model('m_hakakses');
        $this->m_hakakses->disabled_tipeuser($id);
        redirect('index.php/ps_hakakses');
    }
    
    function active($id) {
        //----start validate page delete-------//
        if($this->varedit==null){redirect('index.php/ps_hakakses');}
        //----end validate page delete---------//
        $this->session->set_flashdata('message', 'Anda berhasil Aktif Tipe User');
        $this->load->model('m_hakakses');
        $this->m_hakakses->active_tipeuser($id);
        redirect('index.php/ps_hakakses');
    }

//--------------------------------------------------HAK AKSES---------------------------------------------------------//
    public function edithakakses($id) {
        //----start validate page edit-------//
        if($this->varedit==null){redirect('index.php/ps_hakakses');}
        //----end validate page edit---------//
        $data['titlebefore']    = 'Hak Akses';
        $data['title']          = 'Edit Hak Akses Menu';
        $data['actionform']     = 'prosesedithakakses';
        $data['link']           = 'ps_hakakses';
        $data['main_content']   = 'setting/hakakses/form_hakakses';
        $data['id']             = $id;
        
        $this->load->model('m_hakakses');
        $tipeuser = $this->m_hakakses->edit_tipeuser($id);
        $this->load->vars('tu', $tipeuser);
        
        $this->load->model('m_hakakses');
        $menu = $this->m_hakakses->menuall();
        $this->load->vars('mn', $menu);
        
        $this->load->model('m_hakakses');
        $hakakses = $this->m_hakakses->edit_hakakses($id);
        $this->load->vars('um', $hakakses);
        
        $this->load->view('includes/template', $data);
    }
    
    //funsi untuk memproses update data karyawan
    function prosesedithakakses() {
        $this->db->where('mh_tipeuser_id', $this->input->post('id'));
        $this->db->delete('m_hakakses');
        
        if(isset($_POST['akses'])){
            $postakses= $_POST['akses'];
            foreach($postakses as $key1 =>$valueakses)
            {
                $accessaction   = null;
                $viewaction     = null;
                $tambahaction   = null;
                $editaction     = null;
                $deleteaction   = null;
                $dataakses      = explode ("-",$valueakses);
            
                if(isset($_POST['tambah'])){
                    $posttambah= $_POST['tambah'];
                    foreach($posttambah as $key2 =>$valuetambah)
                    {
                        if($dataakses[1]==$valuetambah){
                            $tambahaction="1";
                        }
                    }
                }
                if(isset($_POST['edit'])){
                    $postedit= $_POST['edit'];
                    foreach($postedit as $key3 =>$valueedit)
                    {
                        if($dataakses[1]==$valueedit){
                            $editaction="1";
                        }
                    }
                }
                if(isset($_POST['delete'])){
                    $postdelete= $_POST['delete'];
                    foreach($postdelete as $key4 =>$valuedelete)
                    {
                        if($dataakses[1]==$valuedelete){
                            $deleteaction="1";
                        }
                    }
                }
                if(isset($_POST['view'])){
                    $postview= $_POST['view'];
                    foreach($postview as $key5 =>$valueview)
                    {
                        if($dataakses[1]==$valueview){
                            $viewaction="1";
                        }
                    }
                }
                if(isset($_POST['akses'])){
                    $postakses= $_POST['akses'];
                    foreach($postakses as $key6 =>$valueaksesnya)
                    { 
                        $valueaksessplit = explode("-",$valueaksesnya);
                        if($dataakses[1]==$valueaksessplit[1]){
                            $accessaction="1";
                        }
                    }
                }
                
            $insert_hakakses = array(
                    'mh_tipeuser_id'=> $this->input->post('id'),
                    'mh_menu_id'    => $dataakses[1],
                    'mh_access'     => $accessaction,
                    'mh_view'       => $viewaction,
                    'mh_add'        => $tambahaction,
                    'mh_edit'       => $editaction,
                    'mh_delete'     => $deleteaction,
                );
        
            $insert = $this->db->insert('m_hakakses', $insert_hakakses);
            }
        }
        $this->session->set_flashdata('message', 'Anda berhasil Mengedit Hak Akses');
        redirect('index.php/ps_hakakses/edithakakses/'.$this->input->post('id'));
    }
}

?>