Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- defined('BASEPATH') OR exit('No direct script access allowed');
- class Resi extends CI_Controller {
- function __construct()
- {
- parent::__construct();
- $this->load->library(array('template', 'form_validation'));
- $this->load->model('resis');
- }
- public function index()
- {
- $this->cek_login();
- $this->template->admin('admin/menage_resi');
- }
- public function ajax_list()
- {
- $list = $this->resis->get_datatables();
- $data = array();
- $no = $_POST['start'];
- foreach ($list as $i) {
- $status = ($i->aktif == 1) ? "Dikirim" : 'Selesai';
- $no++;
- $row = array();
- $row[] = $no;
- $row[] = $i->nama;
- $row[] = $i->no_resi;
- $row[] = $status;
- $row[] = $i->pengiriman;
- $row[] = '<a href="'.site_url('resi/detail/'.$i->id_resi).'" class="btn btn-success btn-xs"><i class="fa fa-search-plus"></i></a>
- <a href="'.site_url('resi/update_resi/'.$i->id_resi).'" class="btn btn-warning btn-xs"><i class="fa fa-edit"></i></a>';
- $data[] = $row;
- }
- $output = array(
- "draw" => $_POST['draw'],
- "recordsTotal" => $this->resis->count_all(),
- "recordsFiltered" => $this->resis->count_filtered(),
- "data" => $data
- );
- //output to json format
- echo json_encode($output);
- }
- public function add_resi()
- {
- $this->cek_login();
- if ($this->input->post('submit', TRUE) == 'Submit') {
- //validasi
- $this->form_validation->set_rules('nama', 'Nama', 'required|min_length[4]');
- $this->form_validation->set_rules('no_resi', 'No Resi', 'required|numeric');
- $this->form_validation->set_rules('status', 'Status', 'required|numeric');
- $this->form_validation->set_rules('pengiriman', 'Pengiriman', 'required|min_length[4]');
- if ($this->form_validation->run() == TRUE)
- {
- $config['upload_path'] = './assets/upload/';
- $config['allowed_types'] = 'jpg|png|jpeg';
- $config['max_size'] = '2048';
- $config['file_name'] = 'gambar'.time();
- $this->load->library('upload', $config);
- if ($this->upload->do_upload('foto'))
- {
- $gbr = $this->upload->data();
- //proses insert data item
- $resis = array (
- 'link' => time(),
- 'nama' => $this->input->post('nama', TRUE),
- 'no_resi' => $this->input->post('no_resi', TRUE),
- 'aktif' => $this->input->post('status', TRUE),
- 'gambar' => $gbr['file_name'],
- 'pengiriman' => $this->input->post('pengiriman', TRUE)
- );
- $id_resi = $this->resis->insert_last('t_resis', $resis);
- //akses function kategori
- //upload Foto Lainnya
- $len = count($_FILES['gb']['name']); //hitung jumlah form
- for ($i=0; $i < $len; $i++) {
- $foto = '';
- //masukkan data file ke variabel foto sesuai index array
- $_FILES[$foto]['name'] = $_FILES['gb']['name'][$i];
- $_FILES[$foto]['type'] = $_FILES['gb']['type'][$i];
- $_FILES[$foto]['tmp_name'] = $_FILES['gb']['tmp_name'][$i];
- $_FILES[$foto]['size'] = $_FILES['gb']['size'][$i];
- $_FILES[$foto]['error'] = $_FILES['gb']['error'][$i];
- $config['file_name'] = 'img'.time().$i; //rename foto yang diupload
- $this->upload->initialize($config);
- if ($this->upload->do_upload($foto))
- {
- //fetch data file yang diupload
- $gb = $this->upload->data();
- $data = [
- 'id_resi' => $id_resi,
- 'img' => $gb['file_name']
- ];
- //insert data img
- $this->resis->insert('t_img', $data);
- }
- }
- redirect('resi');
- } else {
- $this->session->set_flashdata('alert', 'anda belum memilih foto');
- }
- }
- }
- $data['kategori'] = $this->input->post('kategori', TRUE);
- $data['kat'] = $this->resis->get_all('t_kategori');
- $data['nama'] = $this->input->post('nama', TRUE);
- $data['no_resi'] = $this->input->post('no_resi', TRUE);
- $data['status'] = $this->input->post('status', TRUE);
- $data['pengiriman'] = $this->input->post('pengiriman', TRUE);
- $data['stok'] = $this->input->post('stok', TRUE);
- $data['rk'] = '';
- $data['header'] = "Add New Item";
- $this->template->admin('admin/resi_form', $data);
- }
- public function detail()
- {
- $this->cek_login();
- $id_resi = $this->uri->segment(3);
- $item = $this->resis->get_where('t_resis', array('id_resi' => $id_resi));
- foreach ($resi->result() as $key) {
- $data['id_resi'] = $key->id_resi;
- $data['nama'] = $key->nama;
- $data['no_resi'] = $key->no_resi;
- $data['status'] = $key->aktif;
- $data['gambar'] = $key->gambar;
- $data['pengiriman'] = $key->pengiriman;
- }
- $table = "t_rkategori rk
- JOIN t_kategori k ON (rk.id_kategori = k.id_kategori)";
- $data['kategori'] = $this->resis->get_where($table, ['rk.id_resi' => $id_resi]);
- //ambil data img berdasarkan id_resi
- $data['img'] = $this->resis->get_where('t_img', ['id_resi' => $id_resi]);
- $this->template->admin('admin/detail_resi', $data);
- }
- public function update_resi()
- {
- $this->cek_login();
- $id_resi = $this->uri->segment(3);
- if ($this->input->post('submit', TRUE) == 'Submit') {
- //validasi
- $this->form_validation->set_rules('nama', 'Nama', 'required|min_length[4]');
- $this->form_validation->set_rules('no_resi', 'No Resi', 'required|numeric');
- $this->form_validation->set_rules('status', 'Status', 'required|numeric');
- $this->form_validation->set_rules('pengiriman', 'Pengiriman', 'required|min_length[4]');
- if ($this->form_validation->run() == TRUE)
- {
- $config['upload_path'] = './assets/upload/';
- $config['allowed_types'] = 'jpg|png|jpeg';
- $config['max_size'] = '2048';
- $config['file_name'] = 'gambar'.time();
- $this->load->library('upload', $config);
- $resis = array (
- 'nama_item' => $this->input->post('nama', TRUE),
- 'no_resi' => $this->input->post('no_resi', TRUE),
- 'aktif' => $this->input->post('status', TRUE),
- 'pengiriman' => $this->input->post('pengiriman', TRUE)
- );
- if ($this->upload->do_upload('foto'))
- {
- //fetch data file yang diupload
- $gbr = $this->upload->data();
- //hapus file img dari folder
- unlink('assets/upload/'.$this->input->post('old_pict', TRUE));
- $resis['gambar'] = $gbr['file_name'];
- $this->resis->update('t_resis', $resis, array('id_resi' => $id_resi));
- } else {
- $this->resis->update('t_resis', $resis, array('id_resi' => $id_resi));
- }
- $this->resis->delete('t_rkategori', ['id_resi' => $id_resi]);
- $this->kategori($id_resi, $this->input->post('kategori', TRUE));
- $len = count($_FILES['gb']['name']); //hitung jumlah form
- for ($i=0; $i < $len; $i++) {
- $foto = '';
- //masukkan data file ke variabel foto sesuai index array
- $_FILES[$foto]['name'] = $_FILES['gb']['name'][$i];
- $_FILES[$foto]['type'] = $_FILES['gb']['type'][$i];
- $_FILES[$foto]['tmp_name'] = $_FILES['gb']['tmp_name'][$i];
- $_FILES[$foto]['size'] = $_FILES['gb']['size'][$i];
- $_FILES[$foto]['error'] = $_FILES['gb']['error'][$i];
- $config['file_name'] = 'img'.time().$i; //rename foto yang diupload
- $this->upload->initialize($config);
- if ($this->upload->do_upload($foto))
- {
- $gb = $this->upload->data();
- $data = [
- 'id_resi' => $id_resi,
- 'img' => $gb['file_name']
- ];
- $this->resis->insert('t_img', $data);
- }
- }
- redirect('resi');
- }
- }
- $item = $this->resis->get_where('t_resis', array('id_resi' => $id_resi));
- $table = "t_rkategori rk
- JOIN t_kategori k ON (rk.id_kategori = k.id_kategori)";
- $rk = $this->resis->get_where($table, ['rk.id_resi' => $id_resi]);
- $value = '';
- foreach ($rk->result() as $k) {
- $value .= ', '.$k->kategori;
- }
- foreach($item->result() as $key) {
- $data['nama'] = $key->nama;
- $data['no_resi'] = $key->no_resi;
- $data['status'] = $key->aktif;
- $data['pengiriman'] = $key->pengiriman;
- $data['gambar'] = $key->gambar;
- }
- $data['kat'] = $this->resis->get_all('t_kategori');
- $data['kategori'] = trim($value, ', ');
- $data['rk'] = $rk;
- //ambil data file img berdasarkan id_resi
- $gb = $this->resis->get_where('t_img', ['id_resi' => $id_resi]);
- //cek data img
- if ($gb->num_rows() != 0)
- {
- $data['gb'] = $gb;
- } else {
- $data['gb'] = null;
- }
- $data['header'] = "Update Data Resi";
- $this->template->admin('admin/resi_form', $data);
- }
- private function kategori($id_resi, $kategori)
- {
- $kat = explode(", ", $kategori);
- $len = count($kat);
- $a = array(' ');
- $b = array ('`','~','!','@','#','$','%','^','&','*','(',')','_','+','=','[',']','{','}','\'','"',':',';','/','\\','?','/','<','>');
- for ($i = 0; $i < $len; $i ++) {
- $url = str_replace($b, '', $kat[$i]);
- $url = str_replace($a, '-', strtolower($url));
- $cek = $this->resis->get_where('t_kategori', ['url' => $url]);
- if ($cek->num_rows() > 0) {
- $get = $cek->row();
- $id = $get->id_kategori;
- } else {
- $data = array(
- 'kategori' => ucwords(trim($kat[$i])),
- 'url' => $url
- );
- $id = $this->resis->insert_last('t_kategori', $data);
- }
- $cek2 = $this->resis->get_where('t_rkategori', ['id_resi' => $id_resi, 'id_kategori' => $id]);
- if ($cek2->num_rows() < 1) {
- $this->resis->insert('t_rkategori', ['id_resi' => $id_resi, 'id_kategori' => $id]);
- }
- }
- }
- public function del_img()
- {
- $this->cek_login();
- if (!$this->uri->segment(3))
- {
- redirect('resi');
- }
- //hapus file image dari folder
- unlink('assets/upload/'.$this->uri->segment(3));
- //hapus data yang ada pada database
- $this->resis->delete('t_img', ['img' => $this->uri->segment(3)]);
- echo '<script type="text/javascript">window.history.go(-1)</script>';
- }
- public function update_img()
- {
- $this->cek_login();
- if (!$this->uri->segment(3))
- {
- redirect('item');
- }
- if ($this->input->post('submit', TRUE) == 'Submit')
- {
- $config['upload_path'] = './assets/upload/';
- $config['allowed_types'] = 'jpg|png|jpeg';
- $config['max_size'] = '2048';
- $config['file_name'] = 'img'.time();
- $this->load->library('upload', $config);
- if ($this->upload->do_upload('img'))
- {
- //hapus file image
- unlink('assets/upload/'.$this->uri->segment(3));
- $gbr = $this->upload->data();
- //proses update Database
- $this->resis->update('t_img', ['img' => $gbr['file_name']], ['img' => $this->uri->segment(3)]);
- echo '<script type="text/javascript">window.history.go(-2)</script>';
- } else {
- $this->session->set_flashdata('alert', 'anda belum memilih foto');
- }
- }
- $this->template->admin('admin/up_img');
- }
- function cek_login()
- {
- if (!$this->session->userdata('admin'))
- {
- redirect('login');
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement