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 BarangCRUD_model extends CI_Model
- {
- private $_table = "barang";
- public $id;
- public $nama;
- public $foto;
- public $deskripsi;
- public $kategori;
- public $harga_beli;
- public $harga_jual;
- public $stok;
- public function getAll()
- {
- return $this->db->get($this->_table)->result();
- }
- public function getById($id)
- {
- return $this->db->get_where($this->_table, ["id" => $id])->row();
- }
- public function save()
- {
- $post = $this->input->post();
- $this->nama = $post["nama"];
- $this->foto = $this->_uploadImage();
- $this->deskripsi = $post["deskripsi"];
- $this->kategori = $post["kategori"];
- $this->harga_beli = $post["harga_beli"];
- $this->harga_jual = $post["harga_jual"];
- $this->stok = $post["stok"];
- $this->db->insert($this->_table, $this);
- }
- public function update()
- {
- $post = $this->input->post();
- $this->id = $post["id"];
- $this->nama = $post["nama"];
- // $this->foto = $this->_uploadImage();
- $this->deskripsi = $post["deskripsi"];
- $this->kategori = $post["kategori"];
- $this->harga_beli = $post["harga_beli"];
- $this->harga_jual = $post["harga_jual"];
- $this->stok = $post["stok"];
- if (!empty($_FILES["foto"]["name"])) {
- $this->foto = $this->_uploadImage();
- } else {
- $this->foto = $post["foto"];
- }
- $this->db->update($this->_table, $this, array('id' => $post['id']));
- }
- public function delete($id)
- {
- $this->_deleteImage($id);
- return $this->db->delete($this->_table, array("id" => $id));
- }
- private function _uploadImage()
- {
- $config['upload_path'] = './upload/barang/';
- $config['allowed_types'] = 'gif|jpg|png';
- $config['file_name'] = $this->nama;
- $config['overwrite'] = true;
- $config['max_size'] = 1024; // 1MB
- // $config['max_width'] = 1024;
- // $config['max_height'] = 768;
- $this->load->library('upload', $config);
- if ($this->upload->do_upload('foto')) {
- return $this->upload->data("file_name");
- }
- return "default.jpg";
- }
- private function _deleteImage($id)
- {
- $barang = $this->getById($id);
- if ($barang->foto != "deffault.jpg") {
- $filename = explode(".", $barang->foto)[0];
- return array_map('unlink', glob(FCPATH."upload/barang/$filename.*"));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement