ion_auth->logged_in()) { redirect('auth'); } else if (!$this->ion_auth->is_admin()) { show_error('Hanya Administrator yang diberi hak untuk mengakses halaman ini, Kembali ke menu awal', 403, 'Akses Terlarang'); } $this->load->library(['datatables', 'form_validation']); // Load Library Ignited-Datatables $this->load->model('Master_model', 'master'); $this->form_validation->set_error_delimiters('', ''); } public function output_json($data, $encode = true) { if ($encode) $data = json_encode($data); $this->output->set_content_type('application/json')->set_output($data); } public function index() { $data = [ 'user' => $this->ion_auth->user()->row(), 'judul' => 'Mata Kuliah', 'subjudul' => 'Data Mata Kuliah' ]; $this->load->view('_templates/dashboard/_header', $data); $this->load->view('master/matkul/data'); $this->load->view('_templates/dashboard/_footer'); } public function add() { $data = [ 'user' => $this->ion_auth->user()->row(), 'judul' => 'Tambah Mata Kuliah', 'subjudul' => 'Tambah Data Mata Kuliah', 'banyak' => $this->input->post('banyak', true) ]; $this->load->view('_templates/dashboard/_header', $data); $this->load->view('master/matkul/add'); $this->load->view('_templates/dashboard/_footer'); } public function data() { $this->output_json($this->master->getDataMatkulMKWU(), false); } public function edit() { $chk = $this->input->post('checked', true); if (!$chk) { redirect('matkul'); } else { $matkul = $this->master->getMatkulMKWUById($chk); $data = [ 'user' => $this->ion_auth->user()->row(), 'judul' => 'Edit Mata Kuliah', 'subjudul' => 'Edit Data Mata Kuliah', 'matkul' => $matkul ]; $this->load->view('_templates/dashboard/_header', $data); $this->load->view('master/matkul/edit'); $this->load->view('_templates/dashboard/_footer'); } } public function save() { $rows = count($this->input->post('nama_matkul', true)); $mode = $this->input->post('mode', true); for ($i = 1; $i <= $rows; $i++) { $nama_matkul = 'nama_matkul[' . $i . ']'; $this->form_validation->set_rules($nama_matkul, 'Matkul', 'required'); $this->form_validation->set_message('required', '{field} Wajib diisi'); if ($this->form_validation->run() === FALSE) { $error[] = [ $nama_matkul => form_error($nama_matkul) ]; $status = FALSE; } else { if ($mode == 'add') { $insert[] = [ 'nama_matkul' => $this->input->post($nama_matkul, true) ]; } else if ($mode == 'edit') { $update[] = array( 'id_matkul' => $this->input->post('id_matkul[' . $i . ']', true), 'nama_matkul' => $this->input->post($nama_matkul, true) ); } $status = TRUE; } } if ($status) { if ($mode == 'add') { $this->master->create('mkwu_matkul', $insert, true); $data['insert'] = $insert; } else if ($mode == 'edit') { $this->master->update('mkwu_matkul', $update, 'id_matkul', null, true); $data['update'] = $update; } } else { if (isset($error)) { $data['errors'] = $error; } } $data['status'] = $status; $this->output_json($data); } public function delete() { $chk = $this->input->post('checked', true); if (!$chk) { $this->output_json(['status' => false]); } else { if ($this->master->delete('mkwu_matkul', $chk, 'id_matkul')) { $this->output_json(['status' => true, 'total' => count($chk)]); } } } public function load_matkul() { $data = $this->master->getMatkulMKWU(); $this->output_json($data); } public function import($import_data = null) { $data = [ 'user' => $this->ion_auth->user()->row(), 'judul' => 'Mata Kuliah', 'subjudul' => 'Import Mata Kuliah' ]; if ($import_data != null) $data['import'] = $import_data; $this->load->view('_templates/dashboard/_header', $data); $this->load->view('master/matkul/import'); $this->load->view('_templates/dashboard/_footer'); } public function preview() { $config['upload_path'] = './uploads/import/'; $config['allowed_types'] = 'xls|xlsx|csv'; $config['max_size'] = 2048; $config['encrypt_name'] = true; $this->load->library('upload', $config); if (!$this->upload->do_upload('upload_file')) { $error = $this->upload->display_errors(); echo $error; die; } else { $file = $this->upload->data('full_path'); $ext = $this->upload->data('file_ext'); switch ($ext) { case '.xlsx': $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx(); break; case '.xls': $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xls(); break; case '.csv': $reader = new \PhpOffice\PhpSpreadsheet\Reader\Csv(); break; default: echo "unknown file ext"; die; } $spreadsheet = $reader->load($file); $sheetData = $spreadsheet->getActiveSheet()->toArray(); $matkul = []; for ($i = 1; $i < count($sheetData); $i++) { if ($sheetData[$i][0] != null) { $matkul[] = $sheetData[$i][0]; } } unlink($file); $this->import($matkul); } } public function do_import() { $data = json_decode($this->input->post('matkul', true)); $matkul = []; foreach ($data as $j) { $matkul[] = ['nama_matkul' => $j]; } $save = $this->master->create('mkwu_matkul', $matkul, true); if ($save) { redirect('matkul'); } else { redirect('matkul/import'); } } }