<?php

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

class Kunjungan extends CI_Controller
{
    
        
    function __construct()
    {
        parent::__construct();
        $this->load->model(array('Kunjungan_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() . 'kunjungan/?q=' . urlencode($q);
            $config['first_url'] = base_url() . 'kunjungan/?q=' . urlencode($q);
        } else {
            $config['base_url'] = base_url() . 'kunjungan/';
            $config['first_url'] = base_url() . 'kunjungan/';
        }

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

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

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

        $data = array(
            'kunjungan_data' => $kunjungan,
            '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','kunjungan/kunjungan_list', $data);
    }

    public function read($id) 
    {
        $id = decrypt_url($id);
        $row = $this->Kunjungan_model->get_by_id($id);
        $rows = $this->Kunjungan_model->get_style();
        if ($row) {
            $data = array(
				'id_kunjungan' => $row->id_kunjungan,
				'id_card' => $row->id_card,
				'tanggal' => $row->tanggal,
				'nik' => $row->nik,
				'nama' => $row->nama,
				'tempat_lahir' => $row->tempat_lahir,
				'tanggal_lahir' => $row->tanggal_lahir,
				'jenis_kelamin' => $row->jenis_kelamin,
				'alamat' => $row->alamat,
				'telepon' => $row->telepon,
				'dari' => $row->dari,
				'tujuan' => $row->tujuan,
				'sub_tujuan' => $row->sub_tujuan,
				'keperluan' => $row->keperluan,
				'foto' => $row->foto,		
                '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','kunjungan/kunjungan_read', $data);
        } else {
            $this->session->set_flashdata('message', '9797');
            redirect(site_url('kunjungan'));
        }
    }

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

        $data = array(
            'button' => 'Simpan',
            'action' => site_url('kunjungan/create_action'),
		    'id_kunjungan' => set_value('id_kunjungan'),
		    'id_card' => set_value('id_card'),
		    'tanggal' => set_value('tanggal'),
		    'nik' => set_value('nik'),
		    'nama' => set_value('nama'),
		    'tempat_lahir' => set_value('tempat_lahir'),
		    'tanggal_lahir' => set_value('tanggal_lahir'),
		    'jenis_kelamin' => set_value('jenis_kelamin'),
		    'alamat' => set_value('alamat'),
		    'telepon' => set_value('telepon'),
		    'dari' => set_value('dari'),
		    'tujuan' => set_value('tujuan'),
		    'sub_tujuan' => set_value('sub_tujuan'),
		    'keperluan' => set_value('keperluan'),
		    'foto' => set_value('foto'),	
            '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','kunjungan/kunjungan_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 = 'kunjungan-' . 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);
			if (!empty($_FILES['foto']['name'])) {
                if ($this->upload->do_upload('foto')) {
                    $file_upld = $this->upload->data();
                    $data = array(
						'id_card' => $this->input->post('id_card',TRUE),
						'tanggal' => $this->input->post('tanggal',TRUE),
						'nik' => $this->input->post('nik',TRUE),
						'nama' => $this->input->post('nama',TRUE),
						'tempat_lahir' => $this->input->post('tempat_lahir',TRUE),
						'tanggal_lahir' => $this->input->post('tanggal_lahir',TRUE),
						'jenis_kelamin' => $this->input->post('jenis_kelamin',TRUE),
						'alamat' => $this->input->post('alamat',TRUE),
						'telepon' => $this->input->post('telepon',TRUE),
						'dari' => $this->input->post('dari',TRUE),
						'tujuan' => $this->input->post('tujuan',TRUE),
						'sub_tujuan' => $this->input->post('sub_tujuan',TRUE),
						'keperluan' => $this->input->post('keperluan',TRUE),
						'foto' => $file_upld['file_name'],
					);
                    $this->Kunjungan_model->insert($data);
                    $this->session->set_flashdata('message', '1111');
                    redirect(site_url('kunjungan'));
                }else{
                    $this->session->set_flashdata('message', '7373');
                    redirect(site_url('kunjungan'));
                }
            }else{
                $data = array(
					'id_card' => $this->input->post('id_card',TRUE),
					'tanggal' => $this->input->post('tanggal',TRUE),
					'nik' => $this->input->post('nik',TRUE),
					'nama' => $this->input->post('nama',TRUE),
					'tempat_lahir' => $this->input->post('tempat_lahir',TRUE),
					'tanggal_lahir' => $this->input->post('tanggal_lahir',TRUE),
					'jenis_kelamin' => $this->input->post('jenis_kelamin',TRUE),
					'alamat' => $this->input->post('alamat',TRUE),
					'telepon' => $this->input->post('telepon',TRUE),
					'dari' => $this->input->post('dari',TRUE),
					'tujuan' => $this->input->post('tujuan',TRUE),
					'sub_tujuan' => $this->input->post('sub_tujuan',TRUE),
					'keperluan' => $this->input->post('keperluan',TRUE),
				);
                $this->Kunjungan_model->insert($data);
                $this->session->set_flashdata('message', '1111');
                redirect(site_url('kunjungan'));
            }
        }
    }

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

        if ($row) {
            $data = array(
                'button' => 'Update',
                'action' => site_url('kunjungan/update_action'),
				'id_kunjungan' => set_value('id_kunjungan', $row->id_kunjungan),
				'id_card' => set_value('id_card', $row->id_card),
				'tanggal' => set_value('tanggal', $row->tanggal),
				'nik' => set_value('nik', $row->nik),
				'nama' => set_value('nama', $row->nama),
				'tempat_lahir' => set_value('tempat_lahir', $row->tempat_lahir),
				'tanggal_lahir' => set_value('tanggal_lahir', $row->tanggal_lahir),
				'jenis_kelamin' => set_value('jenis_kelamin', $row->jenis_kelamin),
				'alamat' => set_value('alamat', $row->alamat),
				'telepon' => set_value('telepon', $row->telepon),
				'dari' => set_value('dari', $row->dari),
				'tujuan' => set_value('tujuan', $row->tujuan),
				'sub_tujuan' => set_value('sub_tujuan', $row->sub_tujuan),
				'keperluan' => set_value('keperluan', $row->keperluan),
				'foto' => set_value('foto', $row->foto),	
                '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','kunjungan/kunjungan_form', $data);
        } else {
            $this->session->set_flashdata('message', '9797');
            redirect(site_url('kunjungan'));
        }
    }
    
    public function update_action2() 
    {
        $this->_rules();

        if ($this->form_validation->run() == FALSE) {
            $this->update(encrypt_url($this->input->post('id_kunjungan', TRUE)));
        } else {
            $data = array(
				'id_card' => $this->input->post('id_card',TRUE),
				'tanggal' => $this->input->post('tanggal',TRUE),
				'nik' => $this->input->post('nik',TRUE),
				'nama' => $this->input->post('nama',TRUE),
				'tempat_lahir' => $this->input->post('tempat_lahir',TRUE),
				'tanggal_lahir' => $this->input->post('tanggal_lahir',TRUE),
				'jenis_kelamin' => $this->input->post('jenis_kelamin',TRUE),
				'alamat' => $this->input->post('alamat',TRUE),
				'telepon' => $this->input->post('telepon',TRUE),
				'dari' => $this->input->post('dari',TRUE),
				'tujuan' => $this->input->post('tujuan',TRUE),
				'sub_tujuan' => $this->input->post('sub_tujuan',TRUE),
				'keperluan' => $this->input->post('keperluan',TRUE),
				'foto' => $this->input->post('foto',TRUE),
			);

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

        if ($this->form_validation->run() == FALSE) {
            $this->update(encrypt_url($this->input->post('id_kunjungan', TRUE)));
        } else {
            $user = $this->ion_auth->user()->row();
            $id_user = $user->id;
            $nama_file = 'kunjungan-' . 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);

            if (!empty($_FILES['foto']['name'])) {
                if ($this->upload->do_upload('foto')) {
                    $file_upld = $this->upload->data();
                    $data = array(
						'id_card' => $this->input->post('id_card',TRUE),
						'tanggal' => $this->input->post('tanggal',TRUE),
						'nik' => $this->input->post('nik',TRUE),
						'nama' => $this->input->post('nama',TRUE),
						'tempat_lahir' => $this->input->post('tempat_lahir',TRUE),
						'tanggal_lahir' => $this->input->post('tanggal_lahir',TRUE),
						'jenis_kelamin' => $this->input->post('jenis_kelamin',TRUE),
						'alamat' => $this->input->post('alamat',TRUE),
						'telepon' => $this->input->post('telepon',TRUE),
						'dari' => $this->input->post('dari',TRUE),
						'tujuan' => $this->input->post('tujuan',TRUE),
						'sub_tujuan' => $this->input->post('sub_tujuan',TRUE),
						'keperluan' => $this->input->post('keperluan',TRUE),
						'foto' => $file_upld['file_name'],
					);
                    @unlink($path . $this->input->post('filelama'));
                    $this->Kunjungan_model->update($this->input->post('id_kunjungan', TRUE), $data);
                    $this->session->set_flashdata('message', '2222');
                    redirect(site_url('kunjungan'));
                }else{
                    $this->session->set_flashdata('message', '7373');
                    redirect(site_url('kunjungan'));
                }
            }else{
                $data = array(
					'id_card' => $this->input->post('id_card',TRUE),
					'tanggal' => $this->input->post('tanggal',TRUE),
					'nik' => $this->input->post('nik',TRUE),
					'nama' => $this->input->post('nama',TRUE),
					'tempat_lahir' => $this->input->post('tempat_lahir',TRUE),
					'tanggal_lahir' => $this->input->post('tanggal_lahir',TRUE),
					'jenis_kelamin' => $this->input->post('jenis_kelamin',TRUE),
					'alamat' => $this->input->post('alamat',TRUE),
					'telepon' => $this->input->post('telepon',TRUE),
					'dari' => $this->input->post('dari',TRUE),
					'tujuan' => $this->input->post('tujuan',TRUE),
					'sub_tujuan' => $this->input->post('sub_tujuan',TRUE),
					'keperluan' => $this->input->post('keperluan',TRUE),
				);
                $this->Kunjungan_model->update($this->input->post('id_kunjungan', TRUE), $data);
                $this->session->set_flashdata('message', '2222');
                redirect(site_url('kunjungan'));
            }
        }
    }

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

        if ($row) {
            $path = './public/data/upload/';
            @unlink($path . $row->foto);
            $this->Kunjungan_model->delete($id);
            $this->session->set_flashdata('message', '3333');
            redirect(site_url('kunjungan'));
        } else {
            $this->session->set_flashdata('message', '9797');
            redirect(site_url('kunjungan'));
        }
    }
    
    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->Kunjungan_model->get_by_id($id[$x]);
            @unlink($path . $row[$x]->foto);
            $this->Kunjungan_model->delete($id[$x]);
        }
        $this->session->set_flashdata('message', '3333');
        redirect(site_url('kunjungan'));
    }

    public function import_data()
    {
        $user = $this->ion_auth->user()->row();
        $id_user = $user->id;
        $kode = "kunjungan-" . 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(
						'id_card' => $row['A'],
						'tanggal' => $row['B'],
						'nik' => $row['C'],
						'nama' => $row['D'],
						'tempat_lahir' => $row['E'],
						'tanggal_lahir' => $row['F'],
						'jenis_kelamin' => $row['G'],
						'alamat' => $row['H'],
						'telepon' => $row['I'],
						'dari' => $row['J'],
						'tujuan' => $row['K'],
						'sub_tujuan' => $row['L'],
						'keperluan' => $row['M'],
						'foto' => $row['N'],));
                }
                $numrow++;
            }

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

    public function _rules() 
    {
	$this->form_validation->set_rules('id_card', 'id card', 'trim|required');
	$this->form_validation->set_rules('tanggal', 'tanggal', 'trim|required');
	$this->form_validation->set_rules('nik', 'nik', 'trim|required');
	$this->form_validation->set_rules('nama', 'nama', 'trim|required');
	$this->form_validation->set_rules('tempat_lahir', 'tempat lahir', 'trim|required');
	$this->form_validation->set_rules('tanggal_lahir', 'tanggal lahir', 'trim|required');
	$this->form_validation->set_rules('jenis_kelamin', 'jenis kelamin', 'trim|required');
	$this->form_validation->set_rules('alamat', 'alamat', 'trim|required');
	$this->form_validation->set_rules('telepon', 'telepon', 'trim|required');
	$this->form_validation->set_rules('dari', 'dari', 'trim|required');
	$this->form_validation->set_rules('tujuan', 'tujuan', 'trim|required');
	$this->form_validation->set_rules('sub_tujuan', 'sub tujuan', 'trim|required');
	$this->form_validation->set_rules('keperluan', 'keperluan', 'trim|required');
	$this->form_validation->set_rules('foto', 'foto', 'trim|');

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

}

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