<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Jabatan extends CI_Controller
{
    
        
    function __construct()
    {
        parent::__construct();
        $this->load->model(array('Jabatan_model', 'Ion_auth_model'));
        $this->load->database();
        $this->load->library(array('ion_auth', 'form_validation', 'upload'));
        $this->load->helper(array('url', 'language'));

        $this->form_validation->set_error_delimiters($this->config->item('error_start_delimiter', 'ion_auth'), $this->config->item('error_end_delimiter', 'ion_auth'));

        $this->lang->load('auth');
    }

    public function index()
    {
        $q = urldecode($this->input->get('q', TRUE));
        $start = intval($this->input->get('start'));
        
        if ($q <> '') {
            $config['base_url'] = base_url() . 'jabatan/?q=' . urlencode($q);
            $config['first_url'] = base_url() . 'jabatan/?q=' . urlencode($q);
        } else {
            $config['base_url'] = base_url() . 'jabatan/';
            $config['first_url'] = base_url() . 'jabatan/';
        }

        $config['per_page'] = 5;
        $config['page_query_string'] = TRUE;
        $config['total_rows'] = $this->Jabatan_model->total_rows($q);
        $jabatan = $this->Jabatan_model->get_limit_data($config['per_page'], $start, $q);

        $this->load->library('pagination');
        $this->pagination->initialize($config);

        $row = $this->Jabatan_model->get_style();

        $data = array(
            'jabatan_data' => $jabatan,
            'q' => $q,
            'pagination' => $this->pagination->create_links(),
            'total_rows' => $config['total_rows'],
            'start' => $start,
            'web_meta_color' => $row->meta_color,
            'web_navbar_color' => $row->navbar_color,
            'web_box_color' => $row->box_color,
            'web_box_bottom' => $row->box_bottom,
        );
        $this->template->load('template','jabatan/jabatan_list', $data);
    }

    public function read($id) 
    {
        $id = decrypt_url($id);
        $row = $this->Jabatan_model->get_by_id($id);
        $rows = $this->Jabatan_model->get_style();
        if ($row) {
            $data = array(
				'id_jabatan' => $row->id_jabatan,
				'nama_jabatan' => $row->nama_jabatan,
				'atasan_jabatan' => $row->atasan_jabatan,		
                'web_meta_color' => $rows->meta_color,
                'web_navbar_color' => $rows->navbar_color,
                'web_box_color' => $rows->box_color,
                'web_box_bottom' => $rows->box_bottom,
            );

            $this->template->load('template','jabatan/jabatan_read', $data);
        } else {
            $this->session->set_flashdata('message', '9797');
            redirect(site_url('jabatan'));
        }
    }

    public function create() 
    {
        $row = $this->Jabatan_model->get_style();

        $data = array(
            'button' => 'Simpan',
            'action' => site_url('jabatan/create_action'),
		    'id_jabatan' => set_value('id_jabatan'),
		    'nama_jabatan' => set_value('nama_jabatan'),
		    'atasan_jabatan' => set_value('atasan_jabatan'),	
            'web_meta_color' => $row->meta_color,
            'web_navbar_color' => $row->navbar_color,
            'web_box_color' => $row->box_color,
            'web_box_bottom' => $row->box_bottom,
        );

        $this->template->load('template','jabatan/jabatan_form', $data);
    }
    
    public function create_action() 
    {
        $this->_rules();

        if ($this->form_validation->run() == FALSE) {
            $this->create();
        } else {
            $user = $this->ion_auth->user()->row();
            $id_user = $user->id;
            $nama_file = 'jabatan-' . date("YmdHis") . '-' . $id_user;

            $config['upload_path'] = './public/data/upload/';
            $config['allowed_types'] = 'pdf|doc|docx|xls|xlsx|cdr|psd|png|bmp|jpg|jpeg';
            $config['file_name'] = $nama_file;

            $this->upload->initialize($config);
			$data = array(
				'nama_jabatan' => $this->input->post('nama_jabatan',TRUE),
				'atasan_jabatan' => $this->input->post('atasan_jabatan',TRUE),
			);
            $this->Jabatan_model->insert($data);
            $this->session->set_flashdata('message', '1111');
            redirect(site_url('jabatan'));
        }
    }

    public function update($id) 
    {
        $id = decrypt_url($id);
        $row = $this->Jabatan_model->get_by_id($id);
        $rows = $this->Jabatan_model->get_style();

        if ($row) {
            $data = array(
                'button' => 'Update',
                'action' => site_url('jabatan/update_action'),
				'id_jabatan' => set_value('id_jabatan', $row->id_jabatan),
				'nama_jabatan' => set_value('nama_jabatan', $row->nama_jabatan),
				'atasan_jabatan' => set_value('atasan_jabatan', $row->atasan_jabatan),	
                'web_meta_color' => $rows->meta_color,
                'web_navbar_color' => $rows->navbar_color,
                'web_box_color' => $rows->box_color,
                'web_box_bottom' => $rows->box_bottom,
            );

            $this->template->load('template','jabatan/jabatan_form', $data);
        } else {
            $this->session->set_flashdata('message', '9797');
            redirect(site_url('jabatan'));
        }
    }
    
    public function update_action2() 
    {
        $this->_rules();

        if ($this->form_validation->run() == FALSE) {
            $this->update(encrypt_url($this->input->post('id_jabatan', TRUE)));
        } else {
            $data = array(
				'nama_jabatan' => $this->input->post('nama_jabatan',TRUE),
				'atasan_jabatan' => $this->input->post('atasan_jabatan',TRUE),
			);

            $this->Jabatan_model->update($this->input->post('id_jabatan', TRUE), $data);
            $this->session->set_flashdata('message', '2222');
            redirect(site_url('jabatan'));
        }
    }
    
    public function update_action() 
    {
        $this->_rules();

        if ($this->form_validation->run() == FALSE) {
            $this->update(encrypt_url($this->input->post('id_jabatan', TRUE)));
        } else {
            $user = $this->ion_auth->user()->row();
            $id_user = $user->id;
            $nama_file = 'jabatan-' . date("YmdHis") . '-' . $id_user;

            $path = './public/data/upload/';

            $config['upload_path'] = './public/data/upload/';
            $config['allowed_types'] = 'pdf|doc|docx|xls|xlsx|cdr|psd|png|bmp|jpg|jpeg';
            $config['file_name'] = $nama_file;

            $this->upload->initialize($config);

            $data = array(
				'nama_jabatan' => $this->input->post('nama_jabatan',TRUE),
				'atasan_jabatan' => $this->input->post('atasan_jabatan',TRUE),
			);
            $this->Jabatan_model->update($this->input->post('id_jabatan', TRUE), $data);
            $this->session->set_flashdata('message', '2222');
            redirect(site_url('jabatan'));
        }
    }

    public function delete($id) 
    {
        $id = decrypt_url($id);
        $row = $this->Jabatan_model->get_by_id($id);

        if ($row) {
            $path = './public/data/upload/';
            $this->Jabatan_model->delete($id);
            $this->session->set_flashdata('message', '3333');
            redirect(site_url('jabatan'));
        } else {
            $this->session->set_flashdata('message', '9797');
            redirect(site_url('jabatan'));
        }
    }
    
    public function delete_list() 
    {
        $path = './public/data/upload/';
        $id = $_POST['pilih'];
        $jml_dipilih = count($id);
        for ($x = 0; $x < $jml_dipilih; $x++) {
            $row[$x] = $this->Jabatan_model->get_by_id($id[$x]);
            $this->Jabatan_model->delete($id[$x]);
        }
        $this->session->set_flashdata('message', '3333');
        redirect(site_url('jabatan'));
    }

    public function import_data()
    {
        $user = $this->ion_auth->user()->row();
        $id_user = $user->id;
        $kode = "jabatan-" . date("Ymd") . "-" . $id_user;
        $this->load->library('upload');

        $config['upload_path'] = './public/data/import/';
        $config['allowed_types'] = 'xlsx';
        $config['max_size']    = '2048';
        $config['overwrite'] = true;
        $config['file_name'] = $kode;

        $this->upload->initialize($config);
        if ($this->upload->do_upload('file')) {
            include APPPATH . 'third_party/PHPExcel/PHPExcel.php';

            $excelreader = new PHPExcel_Reader_Excel2007();
            $loadexcel = $excelreader->load('public/data/import/' . $kode . '.xlsx');
            $sheet = $loadexcel->getActiveSheet()->toArray(null, true, true, true);

            $data = array();

            $numrow = 1;
            foreach ($sheet as $row) {
                if ($numrow > 1) {
                    array_push($data, array(
						'nama_jabatan' => $row['A'],
						'atasan_jabatan' => $row['B'],));
                }
                $numrow++;
            }

            $this->Jabatan_model->insert_multiple($data);
            $this->session->set_flashdata('message', '7575');
            redirect(site_url('jabatan'));
        } else {
            $this->session->set_flashdata('message', '7373');
            redirect(site_url('jabatan'));
        }
    }

    public function _rules() 
    {
	$this->form_validation->set_rules('nama_jabatan', 'nama jabatan', 'trim|required');
	$this->form_validation->set_rules('atasan_jabatan', 'atasan jabatan', 'trim|required');

	$this->form_validation->set_rules('id_jabatan', 'id_jabatan', 'trim');
	$this->form_validation->set_error_delimiters('<span class="text-danger">', '</span>');
    }

}

/* End of file Jabatan.php */
/* Location: ./application/controllers/Jabatan.php */
/* Please DO NOT modify this information : */
/* Generated by IDETech Codeigniter CRUD Generator 2021-12-21 04:34:04 */
/* http://idetech.id */