<?php

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

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

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

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

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

        $data = array(
            'sampel_data' => $sampel,
            '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','sampel/sampel_list', $data);
    }

    public function read($id) 
    {
        $id = decrypt_url($id);
        $row = $this->Sampel_model->get_by_id($id);
        $rows = $this->Sampel_model->get_style();
        if ($row) {
            $data = array(
				'id_sampel' => $row->id_sampel,
				'sampel_text' => $row->sampel_text,
				'sampel_tanggal' => $row->sampel_tanggal,
				'sampel_time' => $row->sampel_time,
				'sampel_enum' => $row->sampel_enum,
				'sampel_file' => $row->sampel_file,
				'sampel_number' => $row->sampel_number,		
                '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','sampel/sampel_read', $data);
        } else {
            $this->session->set_flashdata('message', '9797');
            redirect(site_url('sampel'));
        }
    }

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

        $data = array(
            'button' => 'Simpan',
            'action' => site_url('sampel/create_action'),
		    'id_sampel' => set_value('id_sampel'),
		    'sampel_text' => set_value('sampel_text'),
		    'sampel_tanggal' => set_value('sampel_tanggal'),
		    'sampel_time' => set_value('sampel_time'),
		    'sampel_enum' => set_value('sampel_enum'),
		    'sampel_file' => set_value('sampel_file'),
		    'sampel_number' => set_value('sampel_number'),	
            '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','sampel/sampel_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 = 'sampel-' . 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['sampel_file']['name'])) {
                if ($this->upload->do_upload('sampel_file')) {
                    $file_upld = $this->upload->data();
                    $data = array(
						'sampel_text' => $this->input->post('sampel_text',TRUE),
						'sampel_tanggal' => $this->input->post('sampel_tanggal',TRUE),
						'sampel_time' => $this->input->post('sampel_time',TRUE),
						'sampel_enum' => $this->input->post('sampel_enum',TRUE),
						'sampel_file' => $file_upld['file_name'],
						'sampel_number' => $this->input->post('sampel_number',TRUE),
					);
                    $this->Sampel_model->insert($data);
                    $this->session->set_flashdata('message', '1111');
                    redirect(site_url('sampel'));
                }else{
                    $this->session->set_flashdata('message', '7373');
                    redirect(site_url('sampel'));
                }
            }else{
                $data = array(
					'sampel_text' => $this->input->post('sampel_text',TRUE),
					'sampel_tanggal' => $this->input->post('sampel_tanggal',TRUE),
					'sampel_time' => $this->input->post('sampel_time',TRUE),
					'sampel_enum' => $this->input->post('sampel_enum',TRUE),
					'sampel_number' => $this->input->post('sampel_number',TRUE),
				);
                $this->Sampel_model->insert($data);
                $this->session->set_flashdata('message', '1111');
                redirect(site_url('sampel'));
            }
        }
    }

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

        if ($row) {
            $data = array(
                'button' => 'Update',
                'action' => site_url('sampel/update_action'),
				'id_sampel' => set_value('id_sampel', $row->id_sampel),
				'sampel_text' => set_value('sampel_text', $row->sampel_text),
				'sampel_tanggal' => set_value('sampel_tanggal', $row->sampel_tanggal),
				'sampel_time' => set_value('sampel_time', $row->sampel_time),
				'sampel_enum' => set_value('sampel_enum', $row->sampel_enum),
				'sampel_file' => set_value('sampel_file', $row->sampel_file),
				'sampel_number' => set_value('sampel_number', $row->sampel_number),	
                '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','sampel/sampel_form', $data);
        } else {
            $this->session->set_flashdata('message', '9797');
            redirect(site_url('sampel'));
        }
    }
    
    public function update_action2() 
    {
        $this->_rules();

        if ($this->form_validation->run() == FALSE) {
            $this->update(encrypt_url($this->input->post('id_sampel', TRUE)));
        } else {
            $data = array(
				'sampel_text' => $this->input->post('sampel_text',TRUE),
				'sampel_tanggal' => $this->input->post('sampel_tanggal',TRUE),
				'sampel_time' => $this->input->post('sampel_time',TRUE),
				'sampel_enum' => $this->input->post('sampel_enum',TRUE),
				'sampel_file' => $this->input->post('sampel_file',TRUE),
				'sampel_number' => $this->input->post('sampel_number',TRUE),
			);

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

        if ($this->form_validation->run() == FALSE) {
            $this->update(encrypt_url($this->input->post('id_sampel', TRUE)));
        } else {
            $user = $this->ion_auth->user()->row();
            $id_user = $user->id;
            $nama_file = 'sampel-' . 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['sampel_file']['name'])) {
                if ($this->upload->do_upload('sampel_file')) {
                    $file_upld = $this->upload->data();
                    $data = array(
						'sampel_text' => $this->input->post('sampel_text',TRUE),
						'sampel_tanggal' => $this->input->post('sampel_tanggal',TRUE),
						'sampel_time' => $this->input->post('sampel_time',TRUE),
						'sampel_enum' => $this->input->post('sampel_enum',TRUE),
						'sampel_file' => $file_upld['file_name'],
						'sampel_number' => $this->input->post('sampel_number',TRUE),
					);
                    @unlink($path . $this->input->post('filelama'));
                    $this->Sampel_model->update($this->input->post('id_sampel', TRUE), $data);
                    $this->session->set_flashdata('message', '2222');
                    redirect(site_url('sampel'));
                }else{
                    $this->session->set_flashdata('message', '7373');
                    redirect(site_url('sampel'));
                }
            }else{
                $data = array(
					'sampel_text' => $this->input->post('sampel_text',TRUE),
					'sampel_tanggal' => $this->input->post('sampel_tanggal',TRUE),
					'sampel_time' => $this->input->post('sampel_time',TRUE),
					'sampel_enum' => $this->input->post('sampel_enum',TRUE),
					'sampel_number' => $this->input->post('sampel_number',TRUE),
				);
                $this->Sampel_model->update($this->input->post('id_sampel', TRUE), $data);
                $this->session->set_flashdata('message', '2222');
                redirect(site_url('sampel'));
            }
        }
    }

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

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

    public function import_data()
    {
        $user = $this->ion_auth->user()->row();
        $id_user = $user->id;
        $kode = "sampel-" . 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(
						'sampel_text' => $row['A'],
						'sampel_tanggal' => $row['B'],
						'sampel_time' => $row['C'],
						'sampel_enum' => $row['D'],
						'sampel_file' => $row['E'],
						'sampel_number' => $row['F'],));
                }
                $numrow++;
            }

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

    public function _rules() 
    {
	$this->form_validation->set_rules('sampel_text', 'sampel text', 'trim|required');
	$this->form_validation->set_rules('sampel_tanggal', 'sampel tanggal', 'trim|required');
	$this->form_validation->set_rules('sampel_time', 'sampel time', 'trim|required');
	$this->form_validation->set_rules('sampel_enum', 'sampel enum', 'trim|required');
	$this->form_validation->set_rules('sampel_file', 'sampel file', 'trim|');
	$this->form_validation->set_rules('sampel_number', 'sampel number', 'trim|required');

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

    public function excel()
    {
        $this->load->helper('exportexcel');
        $namaFile = "sampel.xls";
        $judul = "sampel";
        $tablehead = 0;
        $tablebody = 1;
        $nourut = 1;
        //penulisan header
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
        header("Content-Type: application/force-download");
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download");
        header("Content-Disposition: attachment;filename=" . $namaFile . "");
        header("Content-Transfer-Encoding: binary ");

        xlsBOF();

        $kolomhead = 0;
        xlsWriteLabel($tablehead, $kolomhead++, "No");
		xlsWriteLabel($tablehead, $kolomhead++, "Sampel Text");
		xlsWriteLabel($tablehead, $kolomhead++, "Sampel Tanggal");
		xlsWriteLabel($tablehead, $kolomhead++, "Sampel Time");
		xlsWriteLabel($tablehead, $kolomhead++, "Sampel Enum");
		xlsWriteLabel($tablehead, $kolomhead++, "Sampel File");
		xlsWriteLabel($tablehead, $kolomhead++, "Sampel Number");

		foreach ($this->Sampel_model->get_all() as $data) {
            $kolombody = 0;

            //ubah xlsWriteLabel menjadi xlsWriteNumber untuk kolom numeric
            xlsWriteNumber($tablebody, $kolombody++, $nourut);
		    xlsWriteLabel($tablebody, $kolombody++, $data->sampel_text);
		    xlsWriteLabel($tablebody, $kolombody++, $data->sampel_tanggal);
		    xlsWriteLabel($tablebody, $kolombody++, $data->sampel_time);
		    xlsWriteLabel($tablebody, $kolombody++, $data->sampel_enum);
		    xlsWriteLabel($tablebody, $kolombody++, $data->sampel_file);
		    xlsWriteNumber($tablebody, $kolombody++, $data->sampel_number);

		    $tablebody++;
            $nourut++;
        }

        xlsEOF();
        exit();
    }

    public function word()
    {
        header("Content-type: application/vnd.ms-word");
        header("Content-Disposition: attachment;Filename=sampel.doc");

        $data = array(
            'sampel_data' => $this->Sampel_model->get_all(),
            'start' => 0
        );
        
        $this->load->view('sampel/sampel_doc',$data);
    }

    function pdf()
    {
        $data = array(
            'sampel_data' => $this->Sampel_model->get_all(),
            'start' => 0
        );

        $this->load->library('pdf');

        $this->pdf->setPaper('A4', 'portrait');
        $this->pdf->filename = 'sampel.pdf';
        $this->pdf->load_view('sampel/sampel_pdf', $data, true);
    }

}

/* End of file Sampel.php */
/* Location: ./application/controllers/Sampel.php */
/* Please DO NOT modify this information : */
/* Generated by IDETech Codeigniter CRUD Generator 2021-06-09 17:05:27 */
/* http://idetech.id */