Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2.  
  3. class Service_model extends CI_Model
  4. {
  5.     private $_table = "services";
  6.  
  7.     public $service_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, ["service_id" => $id])->row();
  38.     }
  39.  
  40.     public function save()
  41.     {
  42.         $post = $this->input->post();
  43.         $this->service_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->service_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.         } else {
  61.             $this->image = $post["old_image"];
  62.         }
  63.  
  64.         $this->description = $post["description"];
  65.         return $this->db->update($this->_table, $this, array('service_id' => $post['id']));
  66.     }
  67.  
  68.     public function delete($id)
  69.     {
  70.         $this->_deleteImage($id);
  71.         return $this->db->delete($this->_table, array("service_id" => $id));
  72.     }
  73.  
  74.     private function _uploadImage()
  75.     {
  76.         $config['upload_path']          = './upload/service/';
  77.         $config['allowed_types']        = 'gif|jpg|png';
  78.         $config['file_name']            = $this->service_id;
  79.         $config['overwrite']            = true;
  80.         $config['max_size']             = 1024; // 1MB
  81.         // $config['max_width']            = 1024;
  82.         // $config['max_height']           = 768;
  83.  
  84.         $this->load->library('upload', $config);
  85.  
  86.         if ($this->upload->do_upload('image')) {
  87.             return $this->upload->data("file_name");
  88.         }
  89.        
  90.         return "default.jpg";
  91.     }
  92.  
  93.     private function _deleteImage($id)
  94.     {
  95.         $service = $this->getById($id);
  96.         if ($service->image != "default.jpg") {
  97.             $filename = explode(".", $service->image)[0];
  98.             return array_map('unlink', glob(FCPATH."upload/service/$filename.*"));
  99.         }
  100.     }
  101. }