Esempio n. 1
0
 /**
  * Update item by his id
  * @return void
  */
 public function update()
 {
     $id = $this->uri->segment(4);
     $cat_id = $this->uri->segment(5);
     //if save button was clicked, get the data sent via post
     if ($this->input->server('REQUEST_METHOD') === 'POST') {
         $this->form_validation->set_rules('category_id', 'Category', 'required');
         $this->form_validation->set_rules('title', 'Title', 'required|edit_unique[products.title.' . $id . ']');
         $this->form_validation->set_rules('product_type', 'Product Type', 'required');
         $this->form_validation->set_rules('uom', 'Unit of Measurement', 'required');
         $this->form_validation->set_rules('qty', 'Quantity', 'required');
         $this->form_validation->set_rules('price', 'Price', 'required');
         $chckedImage = $_FILES['images']['name'];
         //            if (empty($chckedImage)) {
         //                $this->form_validation->set_rules('images', 'Product Image', 'required');
         //            }
         $this->form_validation->set_error_delimiters('<div class="alert alert-error"><a class="close" data-dismiss="alert">&#215;</a><strong>', '</strong></div>');
         if ($this->form_validation->run()) {
             $path = './uploads/product';
             $this->load->library('upload');
             $this->upload->initialize(array("upload_path" => $path, "allowed_types" => "*"));
             if (!empty($chckedImage)) {
                 $data = $this->functions->do_upload_one('images', './uploads/product');
                 $file_name = $data['upload_data']['file_name'];
                 $_POST['images'] = $file_name;
                 @unlink("./uploads/product/" . $this->input->post('old_image'));
             } else {
                 $file_name = $this->input->post('old_image');
                 $_POST['images'] = $file_name;
             }
             //if the insert has returned true then we show the flash message
             $redirect_url = $this->input->post('redirect_url');
             foreach ($_POST as $k => $v) {
                 if (in_array($k, array('redirect_url'))) {
                     unset($_POST[$k]);
                 }
             }
             $uid = Access_level::session_user_id();
             $products_id = $this->input->post('products_id');
             $category_id = $this->input->post('category_id');
             $qty = $this->input->post('qty');
             $title = $this->input->post('title');
             $price = $this->input->post('price');
             $images = $this->input->post('images');
             $description = $this->input->post('description');
             $status = $this->input->post('status');
             $uom = $this->input->post('uom');
             $product_type = $this->input->post('product_type');
             $where = " AND uom_id={$uom}";
             $uom_unit = $this->common_model->getFieldData('uom', 'uom', $where);
             if ($uom_unit == "KG" || $uom_unit == "LTR") {
                 $convertedQty = Access_level::convertToMlOrGm($qty);
             } else {
                 $convertedQty = $qty;
             }
             $is_group = $this->input->post('is_group');
             if (!empty($is_group)) {
                 $is_combo = $is_group;
             } else {
                 $is_combo = "NO";
             }
             $data_to_store = array("uid" => $uid, "category_id" => $category_id, "title" => $title, "images" => $images, "description" => $description, "price" => $price, "qty" => $convertedQty, "is_group" => $is_combo, "status" => $status, "product_type" => $product_type, "uom" => $uom);
             if ($this->products_model->update_products($data_to_store, $products_id) == TRUE) {
                 $this->session->set_flashdata('flash_message', 'updated');
             } else {
                 $this->session->set_flashdata('flash_message', 'not_updated');
             }
             $this->session->set_flashdata('flash_message', 'update');
             redirect($redirect_url);
         }
         //validation run
     }
     $data['product_type_opt'] = $this->config->item('product_type_flag');
     $data['uom_opt'] = $this->common_model->getDDArray('uom', 'uom_id', 'uom');
     //if we are updating, and the data did not pass trough the validation
     //the code below wel reload the current data
     //product data
     $where = " AND parent_id= '0' ";
     $data['main_category_opt'] = $this->common_model->getDDArray('category', 'category_id', 'category_name', $where);
     //$data['country_opt'] = $this->common_model->getDDArray('country', 'country_id', 'country_name');
     $data['products'] = $this->products_model->get_products_by_id($id);
     if ($data['products'][0]['category_id'] != "") {
         $where_subcategory = " AND parent_id!= '0' AND category_id='{$data['products'][0]['category_id']}'";
         $data['subcategory_opt'] = $this->common_model->getDDArray('category', 'category_id', 'category_name', $where_subcategory);
     }
     //echo $id; die;
     $data['category'] = $this->category_model->get_category_by_id($cat_id);
     //echo "<pre>"; print_r($data['category']); die;
     $data['par_cat_array'] = $this->category_model->getParentCategoryList(0, $old_cat = "", 0, 1, 5);
     $data['main_content'] = 'admin/products/edit';
     $this->load->view('admin/includes/template', $data);
 }