From 7dba9dd035cd59dad6a4ad347d93711b4e73ce1a Mon Sep 17 00:00:00 2001 From: Dia Date: Thu, 6 Jan 2022 17:48:33 +0300 Subject: [PATCH] #5090 [cat-module] Able to add same slug --- .../cats-module/resources/lang/en/message.php | 1 + .../Controller/Admin/CategoryController.php | 52 ++++++++++--------- 2 files changed, 28 insertions(+), 25 deletions(-) diff --git a/addons/default/visiosoft/cats-module/resources/lang/en/message.php b/addons/default/visiosoft/cats-module/resources/lang/en/message.php index 6c099c030..51acf5051 100644 --- a/addons/default/visiosoft/cats-module/resources/lang/en/message.php +++ b/addons/default/visiosoft/cats-module/resources/lang/en/message.php @@ -2,4 +2,5 @@ return [ 'categories_mass_delete_success' => 'Categories and related sub-categories has been deleted successfully!', + 'cat_slug_exists' => 'A category with a slug of :slug already exists!', ]; diff --git a/addons/default/visiosoft/cats-module/src/Http/Controller/Admin/CategoryController.php b/addons/default/visiosoft/cats-module/src/Http/Controller/Admin/CategoryController.php index 5d75a7cb5..76d329fe3 100644 --- a/addons/default/visiosoft/cats-module/src/Http/Controller/Admin/CategoryController.php +++ b/addons/default/visiosoft/cats-module/src/Http/Controller/Admin/CategoryController.php @@ -63,8 +63,6 @@ class CategoryController extends AdminController public function create(FileUploader $uploader, FolderRepositoryInterface $folderRepository, MountManager $manager) { if ($this->request->action == "save") { - - $all = $this->request->all(); $id = $all['parent_category']; $parent_id = $all['parent_category']; @@ -119,24 +117,40 @@ class CategoryController extends AdminController } } if (empty($isMultiCat)) { - $category = $this->categoryRepository->create(array_merge($translatableEntries, [ - 'slug' => $all['slug'], - 'parent_category' => $all['parent_category'] === "" ? null : $all['parent_category'], - 'seo_keyword' => $all['seo_keyword'], - 'seo_description' => $all['seo_description'], + $slug = $all['slug']; + if (!$this->categoryRepository->findBySlug($slug)) { + $category = $this->categoryRepository->create(array_merge($translatableEntries, [ + 'slug' => $all['slug'], + 'parent_category' => $all['parent_category'] === "" ? null : $all['parent_category'], + 'seo_keyword' => $all['seo_keyword'], + 'seo_description' => $all['seo_description'], + ])); + + $this->createIconFile($category->getId()); + + $this->dispatch(new CalculateCategoryLevel($category->getId())); + } + + $this->messages->error(trans('visiosoft.module.cats::message.cat_slug_exists', [ + 'slug' => $slug ])); - - $this->createIconFile($category->getId()); - - $this->dispatch(new CalculateCategoryLevel($category->getId())); - } else { for ($i = 0; $i < count($isMultiCat[0]); $i++) { foreach ($isMultiCat as $cat) { $translatableEntries = array_merge($translatableEntries, $cat[$i]); } + + $slug = $this->str->slug(reset($translatableEntries)['name'], '_'); + if ($this->categoryRepository->findBySlug($slug)) { + $this->messages->error(trans('visiosoft.module.cats::message.cat_slug_exists', [ + 'slug' => $slug + ])); + + continue; + } + $category = $this->categoryRepository->create(array_merge($translatableEntries, [ - 'slug' => $this->str->slug(reset($translatableEntries)['name'], '_'), + 'slug' => $slug, 'parent_category' => $all['parent_category'] === "" ? null : $all['parent_category'], 'seo_keyword' => $all['seo_keyword'], 'seo_description' => $all['seo_description'], @@ -148,18 +162,6 @@ class CategoryController extends AdminController } }; -// $this->categoryRepository->create(array_merge($translatableEntries, [ -// 'slug' => $all['slug'], -// 'parent_category' => $all['parent_category'], -// 'icon' => $all['icon'], -// 'seo_keyword' => $all['seo_keyword'], -// 'seo_description' => $all['seo_description'], -// ])); - -// $form->make(); -// if ($form->hasFormErrors()) { -// return $this->redirect->to('/admin/cats/create'); -// } if ($parent_id != "") { return $this->redirect->to('/admin/cats?cat=' . $parent_id); }