diff --git a/addons/default/visiosoft/cats-module/src/Category/CategoryRepository.php b/addons/default/visiosoft/cats-module/src/Category/CategoryRepository.php index c3b01bcb5..5f33fe2b6 100644 --- a/addons/default/visiosoft/cats-module/src/Category/CategoryRepository.php +++ b/addons/default/visiosoft/cats-module/src/Category/CategoryRepository.php @@ -114,4 +114,9 @@ class CategoryRepository extends EntryRepository implements CategoryRepositoryIn return $cats; } + + public function getDeletedCategories() + { + return $this->newQuery()->whereNotNull('deleted_at')->get(); + } } \ No newline at end of file diff --git a/addons/default/visiosoft/cats-module/src/Category/Contract/CategoryRepositoryInterface.php b/addons/default/visiosoft/cats-module/src/Category/Contract/CategoryRepositoryInterface.php index edc7d2303..c0ae36c8d 100644 --- a/addons/default/visiosoft/cats-module/src/Category/Contract/CategoryRepositoryInterface.php +++ b/addons/default/visiosoft/cats-module/src/Category/Contract/CategoryRepositoryInterface.php @@ -15,4 +15,6 @@ interface CategoryRepositoryInterface extends EntryRepositoryInterface public function getParentCategoryById($id); public function getLevelById($id); + + public function getDeletedCategories(); } diff --git a/addons/default/visiosoft/cats-module/src/Category/Table/CategoryTableBuilder.php b/addons/default/visiosoft/cats-module/src/Category/Table/CategoryTableBuilder.php index 7bdc602c0..b1ffb62dd 100644 --- a/addons/default/visiosoft/cats-module/src/Category/Table/CategoryTableBuilder.php +++ b/addons/default/visiosoft/cats-module/src/Category/Table/CategoryTableBuilder.php @@ -45,32 +45,6 @@ class CategoryTableBuilder extends TableBuilder 'name', ]; - /** - * The table buttons. - * - * @var array|string - */ - protected $buttons = [ - 'edit' => [ - 'href' => '/admin/cats/edit/{entry.id}?parent={entry.parent_category_id}' - ], - 'add_sub_category' => [ - 'icon' => 'fa fa-caret-square-o-down', - 'type' => 'success', - 'href' => '/admin/cats/create?parent={entry.id}' - ], - 'sub_category' => [ - 'icon' => 'fa fa-caret-square-o-down', - 'type' => 'success', - 'href' => '/admin/cats?cat={entry.id}' - ], - 'delete' => [ - 'icon' => 'fa fa-trash', - 'type' => 'danger', - 'href' => '/admin/cats/category/delete/{entry.id}?parent={entry.parent_category_id}' - ] - ]; - /** * The table actions. * diff --git a/addons/default/visiosoft/cats-module/src/Category/Table/CategoryTableButtons.php b/addons/default/visiosoft/cats-module/src/Category/Table/CategoryTableButtons.php new file mode 100644 index 000000000..4bdc6f1a5 --- /dev/null +++ b/addons/default/visiosoft/cats-module/src/Category/Table/CategoryTableButtons.php @@ -0,0 +1,34 @@ +setButtons([ + 'edit' => [ + 'href' => '/admin/cats/edit/{entry.id}?parent={entry.parent_category_id}' + ], + 'add_sub_category' => [ + 'icon' => 'fa fa-caret-square-o-down', + 'type' => 'success', + 'href' => '/admin/cats/create?parent={entry.id}' + ], + 'sub_category' => [ + 'icon' => 'fa fa-caret-square-o-down', + 'type' => 'success', + 'href' => '/admin/cats?cat={entry.id}' + ], + 'delete' => [ + 'icon' => 'fa fa-trash', + 'type' => 'danger', + 'href' => function(EntryInterface $entry) + { + return route('visiosoft.module.cats::admin.delete_category', ['id' => $entry->getId()])."?parent=".$entry->parent_category_id; + } + ] + ]); + } +} diff --git a/addons/default/visiosoft/cats-module/src/Category/Table/Handler/Delete.php b/addons/default/visiosoft/cats-module/src/Category/Table/Handler/Delete.php index dc9e2da8a..6fa1e7343 100644 --- a/addons/default/visiosoft/cats-module/src/Category/Table/Handler/Delete.php +++ b/addons/default/visiosoft/cats-module/src/Category/Table/Handler/Delete.php @@ -1,26 +1,14 @@ messages->success(trans('visiosoft.module.cats::message.categories_mass_delete_success')); - } - } catch (\Exception $e) { - $this->messages->error($e->getMessage()); - } + public function handle(array $selected) + { + $this->deleteCategories($selected); } } \ No newline at end of file diff --git a/addons/default/visiosoft/cats-module/src/Category/Traits/DeleteCategory.php b/addons/default/visiosoft/cats-module/src/Category/Traits/DeleteCategory.php new file mode 100644 index 000000000..d4b2fd424 --- /dev/null +++ b/addons/default/visiosoft/cats-module/src/Category/Traits/DeleteCategory.php @@ -0,0 +1,42 @@ +deleteCategory($id)) { + $count++; + } + } + + if ($selected && $count > 0) { + $messages->success(trans('streams::message.delete_success', compact('count'))); + } + } + + public function deleteCategory($id) + { + $model = new CategoryModel(); + + $entry = $model->find($id); + + $deletable = true; + + if ($entry instanceof EloquentModel) { + $deletable = $entry->isDeletable(); + } + + if ($entry && $deletable && $entry->delete()) { + return true; + } + return false; + } +} \ No newline at end of file diff --git a/addons/default/visiosoft/cats-module/src/CatsModuleServiceProvider.php b/addons/default/visiosoft/cats-module/src/CatsModuleServiceProvider.php index c6c6278d8..60915487a 100644 --- a/addons/default/visiosoft/cats-module/src/CatsModuleServiceProvider.php +++ b/addons/default/visiosoft/cats-module/src/CatsModuleServiceProvider.php @@ -55,14 +55,17 @@ class CatsModuleServiceProvider extends AddonServiceProvider * @type array|null */ protected $routes = [ - 'admin/cats/clean_subcats' => 'Visiosoft\CatsModule\Http\Controller\Admin\CategoryController@cleanSubcats', + 'admin/cats/clean_subcats' => 'Visiosoft\CatsModule\Http\Controller\Admin\CategoryController@cleanSubCategories', 'admin/cats/adcountcalc' => 'Visiosoft\CatsModule\Http\Controller\Admin\CategoryController@adCountCalc', 'admin/cats/catlevelcalc' => 'Visiosoft\CatsModule\Http\Controller\Admin\CategoryController@catLevelCalc', 'admin/cats' => 'Visiosoft\CatsModule\Http\Controller\Admin\CategoryController@index', 'admin/cats/create' => 'Visiosoft\CatsModule\Http\Controller\Admin\CategoryController@create', 'admin/cats/edit/{id}' => 'Visiosoft\CatsModule\Http\Controller\Admin\CategoryController@edit', - 'admin/cats/category/delete/{id}' => 'Visiosoft\CatsModule\Http\Controller\Admin\CategoryController@delete', + 'admin/cats/category/delete/{id}' => [ + 'as' => 'visiosoft.module.cats::admin.delete_category', + 'uses' => 'Visiosoft\CatsModule\Http\Controller\Admin\CategoryController@delete', + ], // Sitemap 'sitemap.xml' => 'Visiosoft\CatsModule\Http\Controller\SitemapController@index', 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 cb44a9244..955fb0d4b 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 @@ -16,6 +16,7 @@ use Visiosoft\CatsModule\Category\Form\CategoryFormBuilder; use Visiosoft\CatsModule\Category\Table\CategoryTableBuilder; use Anomaly\Streams\Platform\Http\Controller\AdminController; use Visiosoft\AdvsModule\Adv\Contract\AdvRepositoryInterface; +use Visiosoft\CatsModule\Category\Traits\DeleteCategory; class CategoryController extends AdminController { @@ -23,6 +24,8 @@ class CategoryController extends AdminController private $categoryEntryTranslationsModel; private $str; + use DeleteCategory; + public function __construct( CategoryRepositoryInterface $categoryRepository, CatsCategoryEntryTranslationsModel $categoryEntryTranslationsModel, @@ -37,11 +40,6 @@ class CategoryController extends AdminController public function index(CategoryTableBuilder $table, Request $request) { - if ($this->request->action == "delete") { - foreach ($this->request->id as $item) { - //Todo Delete sub Categories - } - } if (!isset($request->cat) || $request->cat == "") { $categories = CategoryModel::query()->where('parent_category_id', '')->orWhereNull('parent_category_id')->get(); $categories = $categories->where('deleted_at', null); @@ -199,32 +197,38 @@ class CategoryController extends AdminController return $this->view->make('visiosoft.module.cats::cats/admin-cat')->with('id', $id); } - public function delete(CategoryRepositoryInterface $categoryRepository, Request $request, CategoryModel $categoryModel, $id) + public function delete(CategoryRepositoryInterface $categoryRepository, $id) { - //Todo Delete Category and Sub Categories - if ($request->parent != "") { - $subCats = $categoryRepository->getCategoryById($request->parent); - if (count($subCats)) { - return redirect('admin/cats?cat=' . $request->parent)->with('success', ['Category and related sub-categories deleted successfully.']); + if ($this->deleteCategory($id)) { + $this->messages->success(trans('streams::message.delete_success', ['count' => 1])); + } + + if (!empty($parent = $this->request->parent)) { + if (count($categoryRepository->getCategoryById($parent))) { + return redirect('admin/cats?cat=' . $parent); } } - return redirect('admin/cats')->with('success', ['Category and related sub-categories deleted successfully.']); + return redirect('admin/cats'); } - public function cleanSubcats() + public function cleanSubCategories() { - $cats = $this->categoryRepository->all(); - $deletedCatsCount = 0; - foreach ($cats as $cat) { - $parentCatId = $cat->parent_category_id; - $parentCat = $this->categoryRepository->find($parentCatId); - if (is_null($parentCat) && !is_null($parentCatId)) { - $this->categoryEntryTranslationsModel->where('entry_id', $cat->id)->delete(); - //Todo Delete Category and Sub Categories - $deletedCatsCount++; - } - } - return redirect('admin/cats')->with('success', [$deletedCatsCount . ' categories has been deleted.']); +// $cats = $this->categoryRepository->getDeletedCategories(); +// +// $delete_category_keys = $cats->pluck('id'); +// +// dd($delete_category_keys); +// +// foreach ($cats as $cat) { +// $parentCatId = $cat->parent_category_id; +// $parentCat = $this->categoryRepository->find($parentCatId); +// if (is_null($parentCat) && !is_null($parentCatId)) { +// $this->categoryEntryTranslationsModel->where('entry_id', $cat->id)->delete(); +// //Todo Sub Categories +// $deletedCatsCount++; +// } +// } + return redirect('admin/cats'); } public function adCountCalc()