From 04c693fc4f02f76d7e51f01087807fdb5e27a27b Mon Sep 17 00:00:00 2001 From: devansh bawari Date: Tue, 19 Jan 2021 14:38:37 +0530 Subject: [PATCH 1/5] Category Checked By ID --- .../Category/src/Http/Controllers/CategoryController.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/Webkul/Category/src/Http/Controllers/CategoryController.php b/packages/Webkul/Category/src/Http/Controllers/CategoryController.php index 7c10d524a..c8f9e7c78 100755 --- a/packages/Webkul/Category/src/Http/Controllers/CategoryController.php +++ b/packages/Webkul/Category/src/Http/Controllers/CategoryController.php @@ -147,7 +147,8 @@ class CategoryController extends Controller { $category = $this->categoryRepository->findOrFail($id); - if(strtolower($category->name) == "root") { + /* the very first category which comes with db seeder can't be deleted */ + if ($category->id === 1) { session()->flash('warning', trans('admin::app.response.delete-category-root', ['name' => 'Category'])); } else { try { @@ -199,7 +200,7 @@ class CategoryController extends Controller $category->delete(); Event::dispatch('catalog.category.delete.after', $categoryId); - + } catch(\Exception $e) { session()->flash('error', trans('admin::app.response.delete-failed', ['name' => 'Category'])); } From 6879605b52c5649ddab9a7ab23391ff3c9eb5611 Mon Sep 17 00:00:00 2001 From: devansh bawari Date: Fri, 22 Jan 2021 11:15:53 +0530 Subject: [PATCH 2/5] Channel Root Category IDs Check Added --- .../Category/src/Http/Controllers/CategoryController.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/Webkul/Category/src/Http/Controllers/CategoryController.php b/packages/Webkul/Category/src/Http/Controllers/CategoryController.php index c8f9e7c78..424dbdbd9 100755 --- a/packages/Webkul/Category/src/Http/Controllers/CategoryController.php +++ b/packages/Webkul/Category/src/Http/Controllers/CategoryController.php @@ -2,6 +2,7 @@ namespace Webkul\Category\Http\Controllers; +use Webkul\Core\Models\Channel; use Illuminate\Support\Facades\Event; use Webkul\Category\Repositories\CategoryRepository; use Webkul\Attribute\Repositories\AttributeRepository; @@ -145,10 +146,14 @@ class CategoryController extends Controller */ public function destroy($id) { + /* grabbing root category ids for all associated channles */ + $rootIdInChannels = Channel::pluck('root_category_id'); + + /* fetching category by id */ $category = $this->categoryRepository->findOrFail($id); - /* the very first category which comes with db seeder can't be deleted */ - if ($category->id === 1) { + /* root category and first category which comes with db seeder can't be deleted */ + if ($category->id === 1 || $rootIdInChannels->contains($category->id)) { session()->flash('warning', trans('admin::app.response.delete-category-root', ['name' => 'Category'])); } else { try { From b81c91e3b2277ff8a37427fddbf7353ed8a41416 Mon Sep 17 00:00:00 2001 From: devansh bawari Date: Fri, 22 Jan 2021 11:17:25 +0530 Subject: [PATCH 3/5] Typo Fixed --- .../Webkul/Category/src/Http/Controllers/CategoryController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/Webkul/Category/src/Http/Controllers/CategoryController.php b/packages/Webkul/Category/src/Http/Controllers/CategoryController.php index 424dbdbd9..b7832d4f9 100755 --- a/packages/Webkul/Category/src/Http/Controllers/CategoryController.php +++ b/packages/Webkul/Category/src/Http/Controllers/CategoryController.php @@ -146,7 +146,7 @@ class CategoryController extends Controller */ public function destroy($id) { - /* grabbing root category ids for all associated channles */ + /* grabbing root category ids for all associated channels */ $rootIdInChannels = Channel::pluck('root_category_id'); /* fetching category by id */ From 44374fd3497dd76acffccf8d8e4fa6fd8116b542 Mon Sep 17 00:00:00 2001 From: devansh bawari Date: Fri, 22 Jan 2021 13:08:12 +0530 Subject: [PATCH 4/5] Mass Delete Check Added --- .../Http/Controllers/CategoryController.php | 51 +++++++++++++------ 1 file changed, 36 insertions(+), 15 deletions(-) diff --git a/packages/Webkul/Category/src/Http/Controllers/CategoryController.php b/packages/Webkul/Category/src/Http/Controllers/CategoryController.php index b7832d4f9..1b1887c34 100755 --- a/packages/Webkul/Category/src/Http/Controllers/CategoryController.php +++ b/packages/Webkul/Category/src/Http/Controllers/CategoryController.php @@ -10,7 +10,7 @@ use Webkul\Attribute\Repositories\AttributeRepository; class CategoryController extends Controller { /** - * Contains route related configuration + * Contains route related configuration. * * @var array */ @@ -146,22 +146,18 @@ class CategoryController extends Controller */ public function destroy($id) { - /* grabbing root category ids for all associated channels */ - $rootIdInChannels = Channel::pluck('root_category_id'); - - /* fetching category by id */ $category = $this->categoryRepository->findOrFail($id); - /* root category and first category which comes with db seeder can't be deleted */ - if ($category->id === 1 || $rootIdInChannels->contains($category->id)) { + if ($this->isCategoryDeletable($category)) { session()->flash('warning', trans('admin::app.response.delete-category-root', ['name' => 'Category'])); } else { try { Event::dispatch('catalog.category.delete.before', $id); - if($category->products->count() > 0) { + if ($category->products->count() > 0) { $category->products()->delete(); } + $category->delete(); Event::dispatch('catalog.category.delete.after', $id); @@ -169,7 +165,7 @@ class CategoryController extends Controller session()->flash('success', trans('admin::app.response.delete-success', ['name' => 'Category'])); return response()->json(['message' => true], 200); - } catch(\Exception $e) { + } catch (\Exception $e) { session()->flash('error', trans('admin::app.response.delete-failed', ['name' => 'Category'])); } } @@ -178,7 +174,7 @@ class CategoryController extends Controller } /** - * Remove the specified resources from database + * Remove the specified resources from database. * * @return \Illuminate\Http\Response */ @@ -191,7 +187,7 @@ class CategoryController extends Controller $category = $this->categoryRepository->find($categoryId); if (isset($category)) { - if(strtolower($category->name) == "root") { + if ($this->isCategoryDeletable($category)) { $suppressFlash = false; session()->flash('warning', trans('admin::app.response.delete-category-root', ['name' => 'Category'])); } else { @@ -199,14 +195,14 @@ class CategoryController extends Controller $suppressFlash = true; Event::dispatch('catalog.category.delete.before', $categoryId); - if($category->products->count() > 0) { + if ($category->products->count() > 0) { $category->products()->delete(); } + $category->delete(); Event::dispatch('catalog.category.delete.after', $categoryId); - - } catch(\Exception $e) { + } catch (\Exception $e) { session()->flash('error', trans('admin::app.response.delete-failed', ['name' => 'Category'])); } } @@ -220,7 +216,11 @@ class CategoryController extends Controller return redirect()->route($this->_config['redirect']); } - + /** + * Get category product count. + * + * @return json + */ public function categoryProductCount() { $indexes = explode(",", request()->input('indexes')); $product_count = 0; @@ -232,4 +232,25 @@ class CategoryController extends Controller return response()->json(['product_count' => $product_count], 200); } + + /** + * Check whether the current category is deletable or not. + * + * This method will fetch all root category ids from the channel. If `id` is present, + * then it is not deletable. + * + * @param $category + * + * @return bool + */ + private function isCategoryDeletable($category) + { + static $rootIdInChannels; + + if (! $rootIdInChannels) { + $rootIdInChannels = Channel::pluck('root_category_id'); + } + + return $category->id === 1 || $rootIdInChannels->contains($category->id); + } } \ No newline at end of file From 0bd2762da9efa58b6042882e454754563389e7e3 Mon Sep 17 00:00:00 2001 From: devansh bawari Date: Fri, 22 Jan 2021 13:25:49 +0530 Subject: [PATCH 5/5] Comments Updated --- .../Category/src/Http/Controllers/CategoryController.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/Webkul/Category/src/Http/Controllers/CategoryController.php b/packages/Webkul/Category/src/Http/Controllers/CategoryController.php index 1b1887c34..6014f076c 100755 --- a/packages/Webkul/Category/src/Http/Controllers/CategoryController.php +++ b/packages/Webkul/Category/src/Http/Controllers/CategoryController.php @@ -219,7 +219,7 @@ class CategoryController extends Controller /** * Get category product count. * - * @return json + * @return \Illuminate\Http\Response */ public function categoryProductCount() { $indexes = explode(",", request()->input('indexes')); @@ -239,8 +239,7 @@ class CategoryController extends Controller * This method will fetch all root category ids from the channel. If `id` is present, * then it is not deletable. * - * @param $category - * + * @param \Webkul\Category\Models\Category $category * @return bool */ private function isCategoryDeletable($category)