<?php
//-------------ID user = 4

class Ps_user 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', '4');
        $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']          = 'User';
        $data['link']           = 'ps_user';
        $data['main_content']   = 'setting/user/v_user';
        $data['error']          = $error;
        
        $this->load->model('m_user');
        $datanya = $this->m_user->index();
        $this->load->vars('us', $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_user');}
        //----end validate page tambah---------//
        $data['titlebefore']    = 'User';
        $data['title']          = 'Tambah User';
        $data['link']           = 'ps_user';
        $data['actionform']     = 'prosesadd';
        $data['main_content']   = 'setting/user/form_user';
        
        $this->load->model('m_hakakses');
        $datatu = $this->m_hakakses->tipeuseraktif();
        $this->load->vars('tu', $datatu);
        
        $this->load->model('m_loket_rajal');
        $datalo = $this->m_loket_rajal->loketst();
        $this->load->vars('lo', $datalo);
        
        $this->load->model('m_klinik');
        $datakl = $this->m_klinik->klinikst();
        $this->load->vars('kl', $datakl);
        
        $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_user');}
        //----end validate page tambah---------//
        $this->load->helper('form');
        $this->load->helper('url');
        $this->load->library('form_validation');
        $this->form_validation->set_rules('nama','Nama Lengkap','required');
        $this->form_validation->set_rules('user','Nama User','required');
        $this->form_validation->set_rules('passworduser','Password User','required');
        $this->form_validation->set_rules('repassworduser', 'Retype Password', 'required|matches[passworduser]');
        
        if($this->form_validation->run()==FALSE){
			$data['titlebefore']    = 'User';
            $data['title']          = 'Tambah User';
            $data['link']           = 'ps_user';
            $data['actionform']     = 'prosesadd';
            $data['main_content']   = 'setting/user/form_user';
            
            $this->load->model('m_hakakses');
            $datatu = $this->m_hakakses->tipeuseraktif();
            $this->load->vars('tu', $datatu);
            
            $this->load->view('includes/template', $data);
		}else{
            $this->load->model('m_user');
            $this->m_user->insert();
            $this->session->set_flashdata('message', 'Anda berhasil Menambahkan User baru');
			redirect('index.php/ps_user');
		}
    }

//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_user');}
        //----end validate page edit---------//
        $data['titlebefore']    = 'User';
        $data['title']          = 'Edit User';
        $data['link']           = 'ps_user';
        $data['actionform']     = 'prosesedit';
        $data['main_content']   = 'setting/user/form_user';
        
        $this->load->model('m_user');
        $datanya = $this->m_user->edit($id);
        $this->load->vars('us', $datanya);
        
        $this->load->model('m_hakakses');
        $datatu = $this->m_hakakses->tipeuseraktif();
        $this->load->vars('tu', $datatu);

        $this->load->model('m_loket_rajal');
        $datalo = $this->m_loket_rajal->loketst();
        $this->load->vars('lo', $datalo);
        
        $this->load->model('m_klinik');
        $datakl = $this->m_klinik->klinikst();
        $this->load->vars('kl', $datakl);
        
        $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_user');}
        //----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','Nama Lengkap','required');
        $this->form_validation->set_rules('user','Nama User','required');
        if(!empty($setpass)){
            $this->form_validation->set_rules('passworduser','Password User','required');
            $this->form_validation->set_rules('repassworduser', 'Retype Password', 'required|matches[passworduser]');
        }
        
        if($this->form_validation->run()==FALSE){
            $data['titlebefore']    = 'User';
            $data['title']          = 'Edit User';
            $data['link']           = 'ps_user';
            $data['actionform']     = 'prosesedit';
            $data['main_content']   = 'setting/user/form_user';
            
            $this->load->model('m_user');
            $datanya = $this->m_user->edit($id);
            $this->load->vars('us', $datanya);
            
            $this->load->model('m_hakakses');
            $datatu = $this->m_hakakses->tipeuseraktif();
            $this->load->vars('tu', $datatu);

            $this->load->model('m_loket_rajal');
            $datalo = $this->m_loket_rajal->loketst();
            $this->load->vars('lo', $datalo);
            
            $this->load->model('m_klinik');
            $datakl = $this->m_klinik->klinikst();
            $this->load->vars('kl', $datakl);
            
            $this->load->view('includes/template', $data);
        }
        else{
            $this->session->set_flashdata('message', 'Anda berhasil Mengedit User');
            $this->load->model('m_user');
            $this->m_user->prosesedit();
            redirect('index.php/ps_user');
        }
    }

//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_user');}
        //----end validate page view---------//
        $data['titlebefore']    = 'User';
        $data['title']          = 'View User';
        $data['actionform']     = 'prosesview';
        $data['link']           = 'ps_user';
        $data['main_content'] = 'setting/user/form_user';
        
        $this->load->model('m_user');
        $datanya = $this->m_user->edit($id);
        $this->load->vars('us', $datanya);

        $this->load->model('m_loket_rajal');
        $datalo = $this->m_loket_rajal->loketst();
        $this->load->vars('lo', $datalo);
        
        $this->load->model('m_klinik');
        $datakl = $this->m_klinik->klinikst();
        $this->load->vars('kl', $datakl);
        
        $this->load->view('includes/template', $data);
    }

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

?>