Advertisement
Guest User

Barang_model

a guest
Mar 29th, 2020
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 2.59 KB | None | 0 0
  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2.  
  3. class BarangCRUD_model extends CI_Model
  4. {
  5.    private $_table = "barang";
  6.    public $id;
  7.    public $nama;
  8.    public $foto;
  9.    public $deskripsi;
  10.    public $kategori;
  11.    public $harga_beli;
  12.    public $harga_jual;
  13.    public $stok;
  14.  
  15.    public function getAll()
  16.    {
  17.        return $this->db->get($this->_table)->result();
  18.     }
  19.    
  20.     public function getById($id)
  21.     {
  22.         return $this->db->get_where($this->_table, ["id" => $id])->row();
  23.     }
  24.  
  25.     public function save()
  26.     {
  27.         $post = $this->input->post();
  28.         $this->nama = $post["nama"];
  29.         $this->foto = $this->_uploadImage();
  30.         $this->deskripsi = $post["deskripsi"];
  31.         $this->kategori = $post["kategori"];
  32.         $this->harga_beli = $post["harga_beli"];
  33.         $this->harga_jual = $post["harga_jual"];
  34.         $this->stok = $post["stok"];
  35.         $this->db->insert($this->_table, $this);
  36.     }
  37.  
  38.     public function update()
  39.     {
  40.         $post = $this->input->post();
  41.         $this->id = $post["id"];
  42.         $this->nama = $post["nama"];
  43.         // $this->foto = $this->_uploadImage();
  44.         $this->deskripsi = $post["deskripsi"];
  45.         $this->kategori = $post["kategori"];
  46.         $this->harga_beli = $post["harga_beli"];
  47.         $this->harga_jual = $post["harga_jual"];
  48.         $this->stok = $post["stok"];
  49.  
  50.         if (!empty($_FILES["foto"]["name"])) {
  51.             $this->foto = $this->_uploadImage();
  52.         } else {
  53.             $this->foto = $post["foto"];
  54.         }
  55.  
  56.         $this->db->update($this->_table, $this, array('id' => $post['id']));
  57.     }
  58.  
  59.     public function delete($id)
  60.     {
  61.         $this->_deleteImage($id);
  62.         return $this->db->delete($this->_table, array("id" => $id));
  63.     }
  64.    
  65.     private function _uploadImage()
  66.     {
  67.         $config['upload_path']          = './upload/barang/';
  68.         $config['allowed_types']        = 'gif|jpg|png';
  69.         $config['file_name']            = $this->nama;
  70.         $config['overwrite']            = true;
  71.         $config['max_size']             = 1024; // 1MB
  72.         // $config['max_width']            = 1024;
  73.         // $config['max_height']           = 768;
  74.  
  75.         $this->load->library('upload', $config);
  76.  
  77.         if ($this->upload->do_upload('foto')) {
  78.             return $this->upload->data("file_name");
  79.         }
  80.        
  81.         return "default.jpg";
  82.     }
  83.  
  84.     private function _deleteImage($id)
  85.     {
  86.         $barang = $this->getById($id);
  87.         if ($barang->foto != "deffault.jpg") {
  88.             $filename = explode(".", $barang->foto)[0];
  89.             return array_map('unlink', glob(FCPATH."upload/barang/$filename.*"));
  90.         }
  91.     }
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement