예제 #1
0
 public function deleteCategory($category_id)
 {
     // check category exist or not
     if (!$this->isCategoryExists($category_id)) {
         return json_encode(array('status' => 'error', 'error_msg' => 'Category not found!'));
     }
     // check products added for the selected category or its subcategories
     if ($this->isCategoryProductExists($category_id)) {
         return json_encode(array('status' => 'error', 'error_msg' => 'The category or sub categories are in use, so category cannot be deleted!'));
     }
     // delete category details in all assigned attributes & category image.
     $cat_details = ProductCategory::whereIn('id', $this->sub_category_ids)->get(array('id'));
     if (count($cat_details) > 0) {
         foreach ($cat_details as $cat) {
             // Delete all attributes assigned to the selected category & its subcategories
             ProductCategoryAttributes::whereRaw('category_id = ?', array($cat->id))->delete();
         }
     }
     //store the values of the left and right of the category to be deleted
     //delete all those cateogries b/w the above 2
     // update the cateogies to the right of the deleted category  - reduce left and right bu width of the deleted category
     $cat_info = Products::getCategoryDetails($category_id);
     if (count($cat_info) > 0) {
         $category_left = $cat_info['category_left'];
         $category_right = $cat_info['category_right'];
         $width = $category_right - $category_left + 1;
         ProductCategory::whereRaw(DB::raw('category_left  between  ' . $category_left . ' AND ' . $category_right))->delete();
         //To update category left
         ProductCategory::whereRaw(DB::raw('category_left >  ' . $category_right))->update(array("category_left" => DB::raw('category_left - ' . $width)));
         //To update category right
         ProductCategory::whereRaw(DB::raw('category_right >  ' . $category_right))->update(array("category_right" => DB::raw('category_right - ' . $width)));
     }
     return json_encode(array('status' => 'success', 'category_id' => $category_id));
 }
예제 #2
0
 public function save()
 {
     $cat_id = 0;
     $category_id = 0;
     if ($this->category_id != '') {
         $cat_id = $this->category_id;
     }
     $parent_category_id = Products::getRootCategoryId();
     if (isset($this->fields_arr['parent_category_id']) && $this->fields_arr['parent_category_id'] > 0) {
         $parent_category_id = $this->fields_arr['parent_category_id'];
     }
     $this->fields_arr['parent_category_id'] = $parent_category_id;
     $validator_arr = $this->validateCategoryDetails($this->fields_arr, $cat_id, $parent_category_id);
     $validator = \Validator::make($this->fields_arr, $validator_arr['rules'], $validator_arr['messages']);
     if ($validator->passes()) {
         if ($cat_id == 0) {
             if (count($this->fields_arr) > 0) {
                 if (list($left_id, $right_id, $level) = $this->getNodeInfo($parent_category_id)) {
                     ProductCategory::where('category_right', '>=', $right_id)->update(array("category_left" => DB::raw('IF(category_left > ' . $right_id . ',category_left + 2,category_left)'), "category_right" => DB::raw('IF(category_right >= ' . $right_id . ',category_right + 2, category_right)')));
                     $this->fields_arr['date_added'] = DB::raw('NOW()');
                     $this->fields_arr['category_level'] = $this->getCategoryLevel($parent_category_id);
                     $this->fields_arr['category_left'] = $right_id;
                     $this->fields_arr['category_right'] = $right_id + 1;
                     $this->fields_arr['available_sort_options'] = 'all';
                     $category_id = ProductCategory::insertGetId($this->fields_arr);
                 }
             }
         } else {
             if (count($this->fields_arr) > 0) {
                 $category_id = $cat_id;
                 $this->fields_arr['category_level'] = $this->getCategoryLevel($parent_category_id);
                 ProductCategory::whereRaw('id = ?', array($category_id))->update($this->fields_arr);
             }
         }
         return json_encode(array('status' => 'success', 'category_id' => $category_id));
     } else {
         $error_msg = $validator->errors()->all();
         return json_encode(array('status' => 'error', 'error_messages' => $error_msg));
     }
 }