Advertisement
darmariduan

controllers/Resi.php

Jul 31st, 2019
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.06 KB | None | 0 0
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3.  
  4. class Resi extends CI_Controller {
  5.  
  6.     function __construct()
  7.     {
  8.         parent::__construct();
  9.         $this->load->library(array('template', 'form_validation'));
  10.       $this->load->model('resis');
  11.     }
  12.  
  13.    public function index()
  14.    {
  15.         $this->cek_login();
  16.  
  17.         $this->template->admin('admin/menage_resi');
  18.    }
  19.  
  20.     public function ajax_list()
  21.    {
  22.       $list = $this->resis->get_datatables();
  23.       $data = array();
  24.       $no = $_POST['start'];
  25.       foreach ($list as $i) {
  26.             $status = ($i->aktif == 1) ? "Dikirim" : 'Selesai';
  27.          $no++;
  28.          $row = array();
  29.          $row[] = $no;
  30.          $row[] = $i->nama;
  31.          $row[] = $i->no_resi;
  32.          $row[] = $status;
  33.          $row[] = $i->pengiriman;
  34.          $row[] = '<a href="'.site_url('resi/detail/'.$i->id_resi).'" class="btn btn-success btn-xs"><i class="fa fa-search-plus"></i></a>
  35.                 <a href="'.site_url('resi/update_resi/'.$i->id_resi).'" class="btn btn-warning btn-xs"><i class="fa fa-edit"></i></a>';
  36.  
  37.          $data[] = $row;
  38.       }
  39.  
  40.       $output = array(
  41.                 "draw" => $_POST['draw'],
  42.                 "recordsTotal" => $this->resis->count_all(),
  43.                 "recordsFiltered" => $this->resis->count_filtered(),
  44.                 "data" => $data
  45.                 );
  46.       //output to json format
  47.     echo json_encode($output);
  48.    }
  49.  
  50.  
  51.    public function add_resi()
  52.    {
  53.         $this->cek_login();
  54.  
  55.       if ($this->input->post('submit', TRUE) == 'Submit') {
  56.          //validasi
  57.          $this->form_validation->set_rules('nama', 'Nama', 'required|min_length[4]');
  58.          $this->form_validation->set_rules('no_resi', 'No Resi', 'required|numeric');
  59.          $this->form_validation->set_rules('status', 'Status', 'required|numeric');
  60.          $this->form_validation->set_rules('pengiriman', 'Pengiriman', 'required|min_length[4]');
  61.  
  62.          if ($this->form_validation->run() == TRUE)
  63.          {
  64.                 $config['upload_path'] = './assets/upload/';
  65.                 $config['allowed_types'] = 'jpg|png|jpeg';
  66.                 $config['max_size'] = '2048';
  67.                 $config['file_name'] = 'gambar'.time();
  68.  
  69.                 $this->load->library('upload', $config);
  70.  
  71.                 if ($this->upload->do_upload('foto'))
  72.                 {
  73.                     $gbr = $this->upload->data();
  74.                     //proses insert data item
  75.                  $resis = array (
  76.                         'link' => time(),
  77.                     'nama' => $this->input->post('nama', TRUE),
  78.                     'no_resi' => $this->input->post('no_resi', TRUE),
  79.                     'aktif' => $this->input->post('status', TRUE),
  80.                         'gambar' => $gbr['file_name'],
  81.                     'pengiriman' => $this->input->post('pengiriman', TRUE)
  82.                  );
  83.  
  84.                     $id_resi = $this->resis->insert_last('t_resis', $resis);
  85.                     //akses function kategori
  86.  
  87.                     //upload Foto Lainnya
  88.                     $len = count($_FILES['gb']['name']); //hitung jumlah form
  89.  
  90.                     for ($i=0; $i < $len; $i++) {
  91.                         $foto = '';
  92.                         //masukkan data file ke variabel foto sesuai index array
  93.                         $_FILES[$foto]['name'] = $_FILES['gb']['name'][$i];
  94.                       $_FILES[$foto]['type'] = $_FILES['gb']['type'][$i];
  95.                       $_FILES[$foto]['tmp_name'] = $_FILES['gb']['tmp_name'][$i];
  96.                         $_FILES[$foto]['size'] = $_FILES['gb']['size'][$i];
  97.                         $_FILES[$foto]['error'] = $_FILES['gb']['error'][$i];
  98.  
  99.                         $config['file_name'] = 'img'.time().$i; //rename foto yang diupload
  100.  
  101.                         $this->upload->initialize($config);
  102.  
  103.                         if ($this->upload->do_upload($foto))
  104.                         {
  105.                             //fetch data file yang diupload
  106.                             $gb = $this->upload->data();
  107.  
  108.                             $data = [
  109.                                 'id_resi' => $id_resi,
  110.                                 'img' => $gb['file_name']
  111.                             ];
  112.                             //insert data img
  113.                             $this->resis->insert('t_img', $data);
  114.                         }
  115.                   }
  116.  
  117.                     redirect('resi');
  118.  
  119.                 } else {
  120.                     $this->session->set_flashdata('alert', 'anda belum memilih foto');
  121.                 }
  122.          }
  123.       }
  124.  
  125.         $data['kategori'] = $this->input->post('kategori', TRUE);
  126.         $data['kat']        = $this->resis->get_all('t_kategori');
  127.       $data['nama']         = $this->input->post('nama', TRUE);
  128.       $data['no_resi']  = $this->input->post('no_resi', TRUE);
  129.  
  130.       $data['status']   = $this->input->post('status', TRUE);
  131.       $data['pengiriman']       = $this->input->post('pengiriman', TRUE);
  132.         $data['stok']       = $this->input->post('stok', TRUE);
  133.         $data['rk']         = '';
  134.  
  135.       $data['header'] = "Add New Item";
  136.  
  137.       $this->template->admin('admin/resi_form', $data);
  138.    }
  139.  
  140.     public function detail()
  141.     {
  142.         $this->cek_login();
  143.         $id_resi = $this->uri->segment(3);
  144.         $item = $this->resis->get_where('t_resis', array('id_resi' => $id_resi));
  145.  
  146.         foreach ($resi->result() as $key) {
  147.             $data['id_resi'] = $key->id_resi;
  148.             $data['nama'] = $key->nama;
  149.             $data['no_resi'] = $key->no_resi;
  150.  
  151.             $data['status'] = $key->aktif;
  152.  
  153.             $data['gambar'] = $key->gambar;
  154.             $data['pengiriman'] = $key->pengiriman;
  155.         }
  156.  
  157.         $table = "t_rkategori rk
  158.                         JOIN t_kategori k ON (rk.id_kategori = k.id_kategori)";
  159.         $data['kategori'] = $this->resis->get_where($table, ['rk.id_resi' => $id_resi]);
  160.         //ambil data img berdasarkan id_resi
  161.         $data['img'] = $this->resis->get_where('t_img', ['id_resi' => $id_resi]);
  162.  
  163.         $this->template->admin('admin/detail_resi', $data);
  164.     }
  165.  
  166.     public function update_resi()
  167.    {
  168.         $this->cek_login();
  169.         $id_resi = $this->uri->segment(3);
  170.  
  171.       if ($this->input->post('submit', TRUE) == 'Submit') {
  172.          //validasi
  173.          $this->form_validation->set_rules('nama', 'Nama', 'required|min_length[4]');
  174.  
  175.          $this->form_validation->set_rules('no_resi', 'No Resi', 'required|numeric');
  176.  
  177.          $this->form_validation->set_rules('status', 'Status', 'required|numeric');
  178.          $this->form_validation->set_rules('pengiriman', 'Pengiriman', 'required|min_length[4]');
  179.  
  180.          if ($this->form_validation->run() == TRUE)
  181.          {
  182.                 $config['upload_path'] = './assets/upload/';
  183.                 $config['allowed_types'] = 'jpg|png|jpeg';
  184.                 $config['max_size'] = '2048';
  185.                 $config['file_name'] = 'gambar'.time();
  186.  
  187.                 $this->load->library('upload', $config);
  188.  
  189.                 $resis = array (
  190.                     'nama_item' => $this->input->post('nama', TRUE),
  191.                     'no_resi' => $this->input->post('no_resi', TRUE),
  192.  
  193.                     'aktif' => $this->input->post('status', TRUE),
  194.                     'pengiriman' => $this->input->post('pengiriman', TRUE)
  195.                 );
  196.  
  197.                 if ($this->upload->do_upload('foto'))
  198.                 {
  199.                     //fetch data file yang diupload
  200.                     $gbr = $this->upload->data();
  201.                     //hapus file img dari folder
  202.                     unlink('assets/upload/'.$this->input->post('old_pict', TRUE));
  203.                     $resis['gambar'] = $gbr['file_name'];
  204.  
  205.                  $this->resis->update('t_resis', $resis, array('id_resi' => $id_resi));
  206.                 } else {
  207.                     $this->resis->update('t_resis', $resis, array('id_resi' => $id_resi));
  208.                 }
  209.  
  210.                 $this->resis->delete('t_rkategori', ['id_resi' => $id_resi]);
  211.                 $this->kategori($id_resi, $this->input->post('kategori', TRUE));
  212.  
  213.                 $len = count($_FILES['gb']['name']); //hitung jumlah form
  214.  
  215.                 for ($i=0; $i < $len; $i++) {
  216.                     $foto = '';
  217.                     //masukkan data file ke variabel foto sesuai index array
  218.                     $_FILES[$foto]['name'] = $_FILES['gb']['name'][$i];
  219.                     $_FILES[$foto]['type'] = $_FILES['gb']['type'][$i];
  220.                     $_FILES[$foto]['tmp_name'] = $_FILES['gb']['tmp_name'][$i];
  221.                     $_FILES[$foto]['size'] = $_FILES['gb']['size'][$i];
  222.                     $_FILES[$foto]['error'] = $_FILES['gb']['error'][$i];
  223.  
  224.                     $config['file_name'] = 'img'.time().$i; //rename foto yang diupload
  225.  
  226.                     $this->upload->initialize($config);
  227.  
  228.                     if ($this->upload->do_upload($foto))
  229.                     {
  230.                         $gb = $this->upload->data();
  231.  
  232.                         $data = [
  233.                             'id_resi' => $id_resi,
  234.                             'img' => $gb['file_name']
  235.                         ];
  236.  
  237.                         $this->resis->insert('t_img', $data);
  238.                     }
  239.                 }
  240.  
  241.                 redirect('resi');
  242.          }
  243.       }
  244.  
  245.         $item = $this->resis->get_where('t_resis', array('id_resi' => $id_resi));
  246.  
  247.         $table = "t_rkategori rk
  248.                         JOIN t_kategori k ON (rk.id_kategori = k.id_kategori)";
  249.         $rk = $this->resis->get_where($table, ['rk.id_resi' => $id_resi]);
  250.  
  251.         $value = '';
  252.         foreach ($rk->result() as $k) {
  253.             $value .= ', '.$k->kategori;
  254.         }
  255.  
  256.         foreach($item->result() as $key) {
  257.           $data['nama'] = $key->nama;
  258.  
  259.           $data['no_resi'] = $key->no_resi;
  260.           $data['status'] = $key->aktif;
  261.           $data['pengiriman'] = $key->pengiriman;
  262.             $data['gambar'] = $key->gambar;
  263.  
  264.         }
  265.  
  266.         $data['kat'] = $this->resis->get_all('t_kategori');
  267.         $data['kategori'] = trim($value, ', ');
  268.         $data['rk'] = $rk;
  269.         //ambil data file img berdasarkan id_resi
  270.         $gb = $this->resis->get_where('t_img', ['id_resi' => $id_resi]);
  271.         //cek data img
  272.         if ($gb->num_rows() != 0)
  273.         {
  274.             $data['gb'] = $gb;
  275.         } else {
  276.             $data['gb'] = null;
  277.         }
  278.  
  279.       $data['header'] = "Update Data Resi";
  280.  
  281.       $this->template->admin('admin/resi_form', $data);
  282.    }
  283.  
  284.     private function kategori($id_resi, $kategori)
  285.     {
  286.         $kat = explode(", ", $kategori);
  287.         $len = count($kat);
  288.         $a = array(' ');
  289.         $b = array ('`','~','!','@','#','$','%','^','&','*','(',')','_','+','=','[',']','{','}','\'','"',':',';','/','\\','?','/','<','>');
  290.  
  291.         for ($i = 0; $i  < $len; $i ++) {
  292.             $url = str_replace($b, '', $kat[$i]);
  293.             $url = str_replace($a, '-', strtolower($url));
  294.  
  295.             $cek = $this->resis->get_where('t_kategori', ['url' => $url]);
  296.  
  297.             if ($cek->num_rows() > 0) {
  298.  
  299.                 $get = $cek->row();
  300.                 $id = $get->id_kategori;
  301.  
  302.             } else {
  303.  
  304.                 $data = array(
  305.                     'kategori' => ucwords(trim($kat[$i])),
  306.                     'url' => $url
  307.                 );
  308.  
  309.                 $id = $this->resis->insert_last('t_kategori', $data);
  310.             }
  311.  
  312.             $cek2 = $this->resis->get_where('t_rkategori', ['id_resi' => $id_resi, 'id_kategori' => $id]);
  313.  
  314.             if ($cek2->num_rows() < 1) {
  315.                 $this->resis->insert('t_rkategori', ['id_resi' => $id_resi, 'id_kategori' => $id]);
  316.             }
  317.         }
  318.     }
  319.  
  320.     public function del_img()
  321.     {
  322.         $this->cek_login();
  323.         if (!$this->uri->segment(3))
  324.         {
  325.             redirect('resi');
  326.         }
  327.         //hapus file image dari folder
  328.         unlink('assets/upload/'.$this->uri->segment(3));
  329.         //hapus data yang ada pada database
  330.         $this->resis->delete('t_img', ['img' => $this->uri->segment(3)]);
  331.  
  332.         echo '<script type="text/javascript">window.history.go(-1)</script>';
  333.     }
  334.  
  335.     public function update_img()
  336.     {
  337.         $this->cek_login();
  338.         if (!$this->uri->segment(3))
  339.         {
  340.             redirect('item');
  341.         }
  342.  
  343.         if ($this->input->post('submit', TRUE) == 'Submit')
  344.         {
  345.             $config['upload_path'] = './assets/upload/';
  346.             $config['allowed_types'] = 'jpg|png|jpeg';
  347.             $config['max_size'] = '2048';
  348.             $config['file_name'] = 'img'.time();
  349.  
  350.             $this->load->library('upload', $config);
  351.  
  352.             if ($this->upload->do_upload('img'))
  353.             {
  354.                 //hapus file image
  355.                 unlink('assets/upload/'.$this->uri->segment(3));
  356.  
  357.                 $gbr = $this->upload->data();
  358.                 //proses update Database
  359.                 $this->resis->update('t_img', ['img' => $gbr['file_name']], ['img' => $this->uri->segment(3)]);
  360.  
  361.                 echo '<script type="text/javascript">window.history.go(-2)</script>';
  362.             } else {
  363.                 $this->session->set_flashdata('alert', 'anda belum memilih foto');
  364.             }
  365.         }
  366.  
  367.         $this->template->admin('admin/up_img');
  368.     }
  369.  
  370.     function cek_login()
  371.     {
  372.         if (!$this->session->userdata('admin'))
  373.         {
  374.             redirect('login');
  375.         }
  376.     }
  377. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement