SHARE
TWEET

controller_products

salim4rt May 21st, 2019 84 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. defined('BASEPATH') OR exit('No direct script access allowed');
  4.  
  5. class Controller_Products extends Admin_Controller
  6. {
  7.     public function __construct()
  8.     {
  9.         parent::__construct();
  10.  
  11.         $this->not_logged_in();
  12.  
  13.         $this->data['page_title'] = 'Products';
  14.  
  15.         $this->load->model('model_products');
  16.         $this->load->model('model_brands');
  17.         $this->load->model('model_category');
  18.         $this->load->model('model_stores');
  19.         $this->load->model('model_attributes');
  20.     }
  21.  
  22.     /*
  23.     * It only redirects to the manage product page
  24.     */
  25.     public function index()
  26.     {
  27.         if(!in_array('viewProduct', $this->permission)) {
  28.             redirect('dashboard', 'refresh');
  29.         }
  30.  
  31.         $this->render_template('products/index', $this->data); 
  32.     }
  33.  
  34.     /*
  35.     * It Fetches the products data from the product table
  36.     * this function is called from the datatable ajax function
  37.     */
  38.     public function fetchProductData()
  39.     {
  40.         $result = array('data' => array());
  41.  
  42.         $data = $this->model_products->getProductData();
  43.  
  44.         foreach ($data as $key => $value) {
  45.  
  46.             $store_data = $this->model_stores->getStoresData($value['store_id']);
  47.             // button
  48.             $buttons = '';
  49.             if(in_array('updateProduct', $this->permission)) {
  50.                 $buttons .= '<a href="'.base_url('Controller_Products/update/'.$value['id']).'" class="btn btn-warning btn-sm"><i class="fa fa-pencil"></i></a>';
  51.             }
  52.  
  53.             if(in_array('deleteProduct', $this->permission)) {
  54.                 $buttons .= ' <button type="button" class="btn btn-danger btn-sm" onclick="removeFunc('.$value['id'].')" data-toggle="modal" data-target="#removeModal"><i class="fa fa-trash"></i></button>';
  55.             }
  56.            
  57.  
  58.             $img = '<img src="'.base_url($value['image']).'" alt="'.$value['name'].'" class="img-circle" width="50" height="50" />';
  59.  
  60.             $availability = ($value['availability'] == 1) ? '<span class="label label-success">Active</span>' : '<span class="label label-warning">Inactive</span>';
  61.  
  62.             $qty_status = '';
  63.             if($value['qty'] <= 10) {
  64.                 $qty_status = '<span class="label label-warning">Low !</span>';
  65.             } else if($value['qty'] <= 0) {
  66.                 $qty_status = '<span class="label label-danger">Out of stock !</span>';
  67.             }
  68.  
  69.  
  70.             $result['data'][$key] = array(
  71.                 $img,
  72.                 // $value['sku'],
  73.                 $value['name'],
  74.                 $value['price'],
  75.                 $value['qty'] . ' ' . $qty_status,
  76.                 $store_data['name'],
  77.                 $availability,
  78.                 $buttons
  79.             );
  80.         } // /foreach
  81.  
  82.         echo json_encode($result);
  83.     }  
  84.  
  85.     /*
  86.     * If the validation is not valid, then it redirects to the create page.
  87.     * If the validation for each input field is valid then it inserts the data into the database
  88.     * and it stores the operation message into the session flashdata and display on the manage product page
  89.     */
  90.     public function create()
  91.     {
  92.         // echo 'came';
  93.         // exit();
  94.         if(!in_array('createProduct', $this->permission)) {
  95.             redirect('dashboard', 'refresh');
  96.         }
  97.  
  98.         $this->form_validation->set_rules('product_name', 'Product name', 'trim');
  99.         // $this->form_validation->set_rules('sku', 'SKU', 'trim|required');
  100.         $this->form_validation->set_rules('price', 'Price', 'trim|required');
  101.         $this->form_validation->set_rules('qty', 'Qty', 'trim|required');
  102.         $this->form_validation->set_rules('store', 'Store', 'trim|required');
  103.         $this->form_validation->set_rules('availability', 'Availability', 'trim|required');
  104.        
  105.    
  106.         if ($this->form_validation->run() == TRUE) {
  107.             // true case
  108.             $upload_image = $this->upload_image();
  109.  
  110.             $data = array(
  111.                
  112.                 'name' => $this->input->post('product_name'),
  113.                 // 'sku' => $this->input->post('sku'),
  114.                 'price' => $this->input->post('price'),
  115.                 'qty' => $this->input->post('qty'),
  116.                 'image' => $upload_image,
  117.                 'description' => $this->input->post('description'),
  118.                 'attribute_value_id' => json_encode($this->input->post('attributes_value_id')),
  119.                 'brand_id' => json_encode($this->input->post('brands')),
  120.                 'category_id' => json_encode($this->input->post('category')),
  121.                 'store_id' => $this->input->post('store'),
  122.                 'availability' => $this->input->post('availability'),
  123.             );
  124.  
  125.             $create = $this->model_products->create($data);
  126.             if($create == true) {
  127.                 $this->session->set_flashdata('success', 'Successfully created');
  128.                 redirect('Controller_Products/', 'refresh');
  129.             }
  130.             else {
  131.                 $this->session->set_flashdata('errors', 'Error occurred!!');
  132.                 redirect('Controller_Products/create', 'refresh');
  133.             }
  134.         }
  135.         else {
  136.             // false case
  137.  
  138.             // attributes
  139.             $attribute_data = $this->model_attributes->getActiveAttributeData();
  140.  
  141.             $attributes_final_data = array();
  142.             foreach ($attribute_data as $k => $v) {
  143.                 $attributes_final_data[$k]['attribute_data'] = $v;
  144.  
  145.                 $value = $this->model_attributes->getAttributeValueData($v['id']);
  146.  
  147.                 $attributes_final_data[$k]['attribute_value'] = $value;
  148.             }
  149.  
  150.             $this->data['attributes'] = $attributes_final_data;
  151.             $this->data['brands'] = $this->model_brands->getActiveBrands();        
  152.             $this->data['category'] = $this->model_category->getActiveCategroy();          
  153.             $this->data['stores'] = $this->model_stores->getActiveStore();         
  154.  
  155.             $this->render_template('products/create', $this->data);
  156.         }  
  157.     }
  158.  
  159.     /*
  160.     * This function is invoked from another function to upload the image into the assets folder
  161.     * and returns the image path
  162.     */
  163.     public function upload_image()
  164.     {
  165.         // assets/images/product_image
  166.         $config['upload_path'] = 'assets/images/product_image';
  167.         $config['file_name'] =  uniqid();
  168.         $config['allowed_types'] = 'gif|jpg|png';
  169.         $config['max_size'] = '1000';
  170.  
  171.         // $config['max_width']  = '1024';s
  172.         // $config['max_height']  = '768';
  173.  
  174.         $this->load->library('upload', $config);
  175.         if ( ! $this->upload->do_upload('product_image'))
  176.         {
  177.             $error = $this->upload->display_errors();
  178.             return $error;
  179.         }
  180.         else
  181.         {
  182.             $data = array('upload_data' => $this->upload->data());
  183.             $type = explode('.', $_FILES['product_image']['name']);
  184.             $type = $type[count($type) - 1];
  185.            
  186.             $path = $config['upload_path'].'/'.$config['file_name'].'.'.$type;
  187.             return ($data == true) ? $path : false;            
  188.         }
  189.     }
  190.  
  191.     /*
  192.     * If the validation is not valid, then it redirects to the edit product page
  193.     * If the validation is successfully then it updates the data into the database
  194.     * and it stores the operation message into the session flashdata and display on the manage product page
  195.     */
  196.     public function update($product_id)
  197.     {      
  198.         if(!in_array('updateProduct', $this->permission)) {
  199.             redirect('dashboard', 'refresh');
  200.         }
  201.  
  202.         if(!$product_id) {
  203.             redirect('dashboard', 'refresh');
  204.         }
  205.  
  206.         $this->form_validation->set_rules('product_name', 'Product name', 'trim|required');
  207.         // $this->form_validation->set_rules('sku', 'SKU', 'trim|required');
  208.         $this->form_validation->set_rules('price', 'Price', 'trim|required');
  209.         $this->form_validation->set_rules('qty', 'Qty', 'trim|required');
  210.         $this->form_validation->set_rules('store', 'Store', 'trim|required');
  211.         $this->form_validation->set_rules('availability', 'Availability', 'trim|required');
  212.  
  213.         if ($this->form_validation->run() == TRUE) {
  214.             // true case
  215.            
  216.             $data = array(
  217.                 'name' => $this->input->post('product_name'),
  218.                 // 'sku' => $this->input->post('sku'),
  219.                 'price' => $this->input->post('price'),
  220.                 'qty' => $this->input->post('qty'),
  221.                 'description' => $this->input->post('description'),
  222.                 'attribute_value_id' => json_encode($this->input->post('attributes_value_id')),
  223.                 'brand_id' => json_encode($this->input->post('brands')),
  224.                 'category_id' => json_encode($this->input->post('category')),
  225.                 'store_id' => $this->input->post('store'),
  226.                 'availability' => $this->input->post('availability'),
  227.             );
  228.  
  229.            
  230.             if($_FILES['product_image']['size'] > 0) {
  231.                 $upload_image = $this->upload_image();
  232.                 $upload_image = array('image' => $upload_image);
  233.                
  234.                 $this->model_products->update($upload_image, $product_id);
  235.             }
  236.  
  237.             $update = $this->model_products->update($data, $product_id);
  238.             if($update == true) {
  239.                 $this->session->set_flashdata('success', 'Successfully updated');
  240.                 redirect('Controller_Products/', 'refresh');
  241.             }
  242.             else {
  243.                 $this->session->set_flashdata('errors', 'Error occurred!!');
  244.                 redirect('Controller_Products/update/'.$product_id, 'refresh');
  245.             }
  246.         }
  247.         else {
  248.             // attributes
  249.             $attribute_data = $this->model_attributes->getActiveAttributeData();
  250.  
  251.             $attributes_final_data = array();
  252.             foreach ($attribute_data as $k => $v) {
  253.                 $attributes_final_data[$k]['attribute_data'] = $v;
  254.  
  255.                 $value = $this->model_attributes->getAttributeValueData($v['id']);
  256.  
  257.                 $attributes_final_data[$k]['attribute_value'] = $value;
  258.             }
  259.            
  260.             // false case
  261.             $this->data['attributes'] = $attributes_final_data;
  262.             $this->data['brands'] = $this->model_brands->getActiveBrands();        
  263.             $this->data['category'] = $this->model_category->getActiveCategroy();          
  264.             $this->data['stores'] = $this->model_stores->getActiveStore();          
  265.  
  266.             $product_data = $this->model_products->getProductData($product_id);
  267.             $this->data['product_data'] = $product_data;
  268.             $this->render_template('products/edit', $this->data);
  269.         }  
  270.     }
  271.  
  272.     /*
  273.     * It removes the data from the database
  274.     * and it returns the response into the json format
  275.     */
  276.     public function remove()
  277.     {
  278.         if(!in_array('deleteProduct', $this->permission)) {
  279.             redirect('dashboard', 'refresh');
  280.         }
  281.        
  282.         $product_id = $this->input->post('product_id');
  283.  
  284.         $response = array();
  285.         if($product_id) {
  286.             $delete = $this->model_products->remove($product_id);
  287.             if($delete == true) {
  288.                 $response['success'] = true;
  289.                 $response['messages'] = "Successfully removed";
  290.             }
  291.             else {
  292.                 $response['success'] = false;
  293.                 $response['messages'] = "Error in the database while removing the product information";
  294.             }
  295.         }
  296.         else {
  297.             $response['success'] = false;
  298.             $response['messages'] = "Refersh the page again!!";
  299.         }
  300.  
  301.         echo json_encode($response);
  302.     }
  303.  
  304. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top