document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. <?php defined(\'BASEPATH\') OR exit(\'No direct script access allowed\');
  2.  
  3. class Product_model extends CI_Model
  4. {
  5.     private $_table = "products";
  6.  
  7.     public $product_id;
  8.     public $name;
  9.     public $price;
  10.     public $image = "default.jpg";
  11.     public $description;
  12.  
  13.     public function rules()
  14.     {
  15.         return [
  16.             [\'field\' => \'name\',
  17.             \'label\' => \'Name\',
  18.             \'rules\' => \'required\'],
  19.  
  20.             [\'field\' => \'price\',
  21.             \'label\' => \'Price\',
  22.             \'rules\' => \'numeric\'],
  23.  
  24.             [\'field\' => \'description\',
  25.             \'label\' => \'Description\',
  26.             \'rules\' => \'required\']
  27.         ];
  28.     }
  29.  
  30.     public function getAll()
  31.     {
  32.         return $this->db->get($this->_table)->result();
  33.     }
  34.  
  35.     public function getById($id)
  36.     {
  37.         return $this->db->get_where($this->_table, ["product_id" => $id])->row();
  38.     }
  39.  
  40.     public function save()
  41.     {
  42.         $post = $this->input->post();
  43.         $this->product_id = uniqid();
  44.         $this->name = $post["name"];
  45.         $this->price = $post["price"];
  46.         $this->image = $this->_uploadImage();
  47.         $this->description = $post["description"];
  48.         return $this->db->insert($this->_table, $this);
  49.     }
  50.  
  51.     public function update()
  52.     {
  53.         $post = $this->input->post();
  54.         $this->product_id = $post["id"];
  55.         $this->name = $post["name"];
  56.         $this->price = $post["price"];
  57.  
  58.         if (!empty($_FILES["image"]["name"])) {
  59.             $this->image = $this->_uploadImage();
  60.         }
  61.         else {
  62.             $this->image = $post["old_image"];
  63.         }
  64.  
  65.         $this->description = $post["description"];
  66.         return $this->db->update($this->_table, $this, array(\'product_id\' => $post[\'id\']));
  67.     }
  68.  
  69.     public function delete($id)
  70.     {
  71.         $this->_deleteImage($id);
  72.         return $this->db->delete($this->_table, array("product_id" => $id));
  73.     }
  74.  
  75.     private function _uploadImage()
  76.     {
  77.         $config[\'upload_path\']      = \'./upload/product/\';
  78.         $config[\'allowed_types\']    = \'gif|jpg|png\';
  79.         $config[\'file_name\']        = $this->product_id;
  80.         $config[\'overwrite\']        = true;
  81.         $config[\'max_size\']         = 1024; // 1MB
  82.         // $config[\'max_width\']     = 1024;
  83.         // $config[\'max_height\']    = 768;
  84.  
  85.         $this->load->library(\'upload\', $config);
  86.  
  87.         if ($this->upload->do_upload(\'image\')) {
  88.             return $this->upload->data("file_name");
  89.         }
  90.  
  91.         return "default.jpg";
  92.     }
  93.  
  94.     private function _deleteImage($id)
  95.     {
  96.         $product = $this->getById($id);
  97.         if ($product->image != "default.jpg") {
  98.             $filename = explode(".", $product->image)[0];
  99.             return array_map(\'unlink\', glob(FCPATH."upload/product/$filename.*"));
  100.         }
  101.     }
  102. }
');