Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. <?php
  2. defined('BASEPATH') or exit('No direct script access allowed');
  3. class Product extends MY_Controller {
  4.     public function __construct() {
  5.         parent::__construct();
  6.  
  7.         $role = $this->session->userdata('role');
  8.  
  9.         if ($role != 'admin') {
  10.             redirect(base_url('/'));
  11.             return;
  12.         }
  13.     }
  14.  
  15.     public function index($page = null) {
  16.         $data['title']      = 'Admin: Produk';
  17.         $data['content']    = $this->product->select(
  18.             [
  19.                 'product.id', 'product.title AS product_title', 'product.image', 'product.price', 'product.is_available',
  20.                 'category.title AS category_title'
  21.             ]
  22.         )
  23.             ->join('category')     // Query untuk mencari suatu data produk beserta kategorinya
  24.             ->paginate($page)
  25.             ->get();
  26.         $data['total_rows'] = $this->product->count();
  27.         $data['pagination'] = $this->product->makePagination(base_url('product'), 2, $data['total_rows']);
  28.         $data['page']       = 'pages/product/index';
  29.  
  30.         $this->view($data);
  31.     }
  32.  
  33.     public function search($page = null) {
  34.         if (isset($_POST['keyword'])) {
  35.             $this->session->set_userdata('keyword', $this->input->post('keyword'));
  36.         } else {
  37.             redirect(base_url('product'));
  38.         }
  39.  
  40.         $keyword = $this->session->userdata('keyword');
  41.  
  42.         $data['title']      = 'Admin: Produk';
  43.         $data['content']    = $this->product->select(
  44.             [
  45.                 'product.id', 'product.title AS product_title', 'product.image', 'product.price', 'product.is_available',
  46.                 'category.title AS category_title'
  47.             ]
  48.         )
  49.             ->join('category')
  50.             ->like('product.title', $keyword)
  51.             ->orLike('description', $keyword)   // Tidak hanya mencari di title melainkan di desc juga
  52.             ->paginate($page)
  53.             ->get();
  54.         $data['total_rows'] = $this->product->like('product.title', $keyword)->orLike('description', $keyword)->count();
  55.         $data['pagination'] = $this->product->makePagination(base_url('product/search'), 3, $data['total_rows']);
  56.         $data['page']       = 'pages/product/index';
  57.  
  58.         $this->view($data);
  59.     }
  60.  
  61.     public function reset() {
  62.         $this->session->unset_userdata('keyword');
  63.         redirect(base_url('product'));
  64.     }
  65.  
  66.     public function create() {
  67.         if (!$_POST) {
  68.             $input = (object) $this->product->getDefaultValues();
  69.         } else {
  70.             $input = (object) $this->input->post(null, true);
  71.         }
  72.  
  73.         if (!empty($_FILES) && $_FILES['image']['name'] !== '') {
  74.             $imageName  = url_title($input->title, '-', true) . '-' . date('YmdHis');  // Membuat slug
  75.             $upload     = $this->product->uploadImage('image', $imageName);    // Mulai upload
  76.             if ($upload) {
  77.                 $input->image   = $upload['file_name'];
  78.             } else {
  79.                 redirect(base_url('product/create'));
  80.             }
  81.         }
  82.  
  83.         if (!$this->product->validate()) {
  84.             $data['title']          = 'Tambah Produk';
  85.             $data['input']          = $input;
  86.             $data['form_action']    = base_url('product/create');
  87.             $data['page']           = 'pages/product/form';
  88.  
  89.             $this->view($data);
  90.             return;
  91.         }
  92.  
  93.         if ($this->product->create($input)) {   // Jika insert berhasil
  94.             $this->session->set_flashdata('success', 'Data berhasil disimpan');
  95.         } else {
  96.             $this->session->set_flashdata('error', 'Maaf, Terjadi suatu kesalahan');
  97.         }
  98.  
  99.         redirect(base_url('product'));
  100.     }
  101.  
  102.     public function edit($id) {
  103.         $data['content'] = $this->product->where('id', $id)->first();
  104.  
  105.         if (!$data['content']) {
  106.             $this->session->set_flashdata('warning', 'Maaf data tidak ditemukan');
  107.             redirect(base_url('product'));
  108.         }
  109.  
  110.         if (!$_POST) {
  111.             $data['input'] = $data['content'];
  112.         } else {
  113.             $data['input'] = (object) $this->input->post(null, true);
  114.         }
  115.  
  116.         if (!empty($_FILES) && $_FILES['image']['name'] !== '') {
  117.             $imageName  = url_title($data['input']->title, '-', true) . '-' . date('YmdHis');  // Membuat slug
  118.             $upload     = $this->product->uploadImage('image', $imageName);    // Mulai upload
  119.             if ($upload) {
  120.                 if ($data['content']->image !== '') {
  121.                     // Jika data di database ini memiliki gambar, maka hapus dulu file gambarnya
  122.                     $this->product->deleteImage($data['content']->image);
  123.                 }
  124.                 // Jika upload berhasil, pasang nama file yang diupload ke dalam database
  125.                 $data['input']->image   = $upload['file_name'];
  126.             } else {
  127.                 redirect(base_url("product/edit/$id"));
  128.             }
  129.         }
  130.  
  131.         if (!$this->product->validate()) {
  132.             $data['title']          = 'Ubah Produk';
  133.             $data['form_action']    = base_url("product/edit/$id");
  134.             $data['page']           = 'pages/product/form';
  135.  
  136.             $this->view($data);
  137.             return;
  138.         }
  139.  
  140.         if ($this->product->where('id', $id)->update($data['input'])) {   // Update data
  141.             $this->session->set_flashdata('success', 'Data berhasil diubah');
  142.         } else {
  143.             $this->session->set_flashdata('error', 'Maaf, Terjadi suatu kesalahan');
  144.         }
  145.  
  146.         redirect(base_url('product'));
  147.     }
  148.  
  149.     public function delete($id) {
  150.         if (!$_POST) {
  151.             redirect(base_url('product'));
  152.         }
  153.  
  154.         $product = $this->product->where('id', $id)->first();
  155.  
  156.         if (!$product) {
  157.             $this->session->set_flashdata('warning', 'Maaf data tidak ditemukan');
  158.             redirect(base_url('product'));
  159.         }
  160.  
  161.         if ($this->product->where('id', $id)->delete()) {   // Lakukan penghapusan di db
  162.             $this->product->deleteImage($product->image);   // Lakukan penghapusan gambar
  163.             $this->session->set_flashdata('success', 'Data berhasil dihapus');
  164.         } else {
  165.             $this->session->set_flashdata('error', 'Maaf, Terjadi kesalahan');
  166.         }
  167.  
  168.         redirect(base_url('product'));
  169.     }
  170.  
  171.     public function unique_slug() {
  172.         $slug       = $this->input->post('slug');
  173.         $id         = $this->input->post('id');
  174.         $product    = $this->product->where('slug', $slug)->first(); // Akan terisi jika terdapat slug yang sama
  175.  
  176.         if ($product) {
  177.             if ($id == $product->id) {  // Keperluan edit tidak perlu ganti slug, jadi tidak masalah
  178.                 return true;
  179.             }
  180.  
  181.             // Jika terdapat suatu nilai pada $product, berikan pesan error pertanda slug sudah ada di db
  182.             $this->load->library('form_validation');
  183.             $this->form_validation->set_message('unique_slug', '%s sudah digunakan');
  184.             return false;
  185.         }
  186.  
  187.         return true;
  188.     }
  189. }
  190.