Merge pull request #956 from openclassify/vedat3.8

fixed delete categories
This commit is contained in:
Fatih Alp 2021-02-18 10:12:01 +03:00 committed by GitHub
commit 9048aeff46
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 122 additions and 70 deletions

View File

@ -114,4 +114,9 @@ class CategoryRepository extends EntryRepository implements CategoryRepositoryIn
return $cats; return $cats;
} }
public function getDeletedCategories()
{
return $this->newQuery()->whereNotNull('deleted_at')->get();
}
} }

View File

@ -15,4 +15,6 @@ interface CategoryRepositoryInterface extends EntryRepositoryInterface
public function getParentCategoryById($id); public function getParentCategoryById($id);
public function getLevelById($id); public function getLevelById($id);
public function getDeletedCategories();
} }

View File

@ -45,32 +45,6 @@ class CategoryTableBuilder extends TableBuilder
'name', '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. * The table actions.
* *

View File

@ -0,0 +1,34 @@
<?php namespace Visiosoft\CatsModule\Category\Table;
use Anomaly\Streams\Platform\Entry\Contract\EntryInterface;
class CategoryTableButtons
{
public function handle(CategoryTableBuilder $builder)
{
$builder->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;
}
]
]);
}
}

View File

@ -1,26 +1,14 @@
<?php namespace Visiosoft\CatsModule\Category\Table\Handler; <?php namespace Visiosoft\CatsModule\Category\Table\Handler;
use Anomaly\Streams\Platform\Ui\Table\Component\Action\ActionHandler; use Anomaly\Streams\Platform\Ui\Table\Component\Action\ActionHandler;
use Visiosoft\CatsModule\Category\Contract\CategoryRepositoryInterface; use Visiosoft\CatsModule\Category\Traits\DeleteCategory;
use Visiosoft\CatsModule\Category\Table\CategoryTableBuilder;
class Delete extends ActionHandler class Delete extends ActionHandler
{ {
public function handle( use DeleteCategory;
CategoryTableBuilder $builder, array $selected,
CategoryRepositoryInterface $categoryRepository
)
{
try {
foreach ($selected as $id) {
//Todo Delete category and Sub Categories
}
if ($selected) { public function handle(array $selected)
$this->messages->success(trans('visiosoft.module.cats::message.categories_mass_delete_success')); {
} $this->deleteCategories($selected);
} catch (\Exception $e) {
$this->messages->error($e->getMessage());
}
} }
} }

View File

@ -0,0 +1,42 @@
<?php namespace Visiosoft\CatsModule\Category\Traits;
use Anomaly\Streams\Platform\Message\MessageBag;
use Anomaly\Streams\Platform\Model\EloquentModel;
use Visiosoft\CatsModule\Category\CategoryModel;
trait DeleteCategory
{
public function deleteCategories(array $selected)
{
$messages = app(MessageBag::class);
$count = 0;
foreach ($selected as $id) {
if ($this->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;
}
}

View File

@ -55,14 +55,17 @@ class CatsModuleServiceProvider extends AddonServiceProvider
* @type array|null * @type array|null
*/ */
protected $routes = [ 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/adcountcalc' => 'Visiosoft\CatsModule\Http\Controller\Admin\CategoryController@adCountCalc',
'admin/cats/catlevelcalc' => 'Visiosoft\CatsModule\Http\Controller\Admin\CategoryController@catLevelCalc', 'admin/cats/catlevelcalc' => 'Visiosoft\CatsModule\Http\Controller\Admin\CategoryController@catLevelCalc',
'admin/cats' => 'Visiosoft\CatsModule\Http\Controller\Admin\CategoryController@index', 'admin/cats' => 'Visiosoft\CatsModule\Http\Controller\Admin\CategoryController@index',
'admin/cats/create' => 'Visiosoft\CatsModule\Http\Controller\Admin\CategoryController@create', 'admin/cats/create' => 'Visiosoft\CatsModule\Http\Controller\Admin\CategoryController@create',
'admin/cats/edit/{id}' => 'Visiosoft\CatsModule\Http\Controller\Admin\CategoryController@edit', '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
'sitemap.xml' => 'Visiosoft\CatsModule\Http\Controller\SitemapController@index', 'sitemap.xml' => 'Visiosoft\CatsModule\Http\Controller\SitemapController@index',

View File

@ -16,6 +16,7 @@ use Visiosoft\CatsModule\Category\Form\CategoryFormBuilder;
use Visiosoft\CatsModule\Category\Table\CategoryTableBuilder; use Visiosoft\CatsModule\Category\Table\CategoryTableBuilder;
use Anomaly\Streams\Platform\Http\Controller\AdminController; use Anomaly\Streams\Platform\Http\Controller\AdminController;
use Visiosoft\AdvsModule\Adv\Contract\AdvRepositoryInterface; use Visiosoft\AdvsModule\Adv\Contract\AdvRepositoryInterface;
use Visiosoft\CatsModule\Category\Traits\DeleteCategory;
class CategoryController extends AdminController class CategoryController extends AdminController
{ {
@ -23,6 +24,8 @@ class CategoryController extends AdminController
private $categoryEntryTranslationsModel; private $categoryEntryTranslationsModel;
private $str; private $str;
use DeleteCategory;
public function __construct( public function __construct(
CategoryRepositoryInterface $categoryRepository, CategoryRepositoryInterface $categoryRepository,
CatsCategoryEntryTranslationsModel $categoryEntryTranslationsModel, CatsCategoryEntryTranslationsModel $categoryEntryTranslationsModel,
@ -37,11 +40,6 @@ class CategoryController extends AdminController
public function index(CategoryTableBuilder $table, Request $request) 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 == "") { if (!isset($request->cat) || $request->cat == "") {
$categories = CategoryModel::query()->where('parent_category_id', '')->orWhereNull('parent_category_id')->get(); $categories = CategoryModel::query()->where('parent_category_id', '')->orWhereNull('parent_category_id')->get();
$categories = $categories->where('deleted_at', null); $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); 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 ($this->deleteCategory($id)) {
if ($request->parent != "") { $this->messages->success(trans('streams::message.delete_success', ['count' => 1]));
$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 (!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(); // $cats = $this->categoryRepository->getDeletedCategories();
$deletedCatsCount = 0; //
foreach ($cats as $cat) { // $delete_category_keys = $cats->pluck('id');
$parentCatId = $cat->parent_category_id; //
$parentCat = $this->categoryRepository->find($parentCatId); // dd($delete_category_keys);
if (is_null($parentCat) && !is_null($parentCatId)) { //
$this->categoryEntryTranslationsModel->where('entry_id', $cat->id)->delete(); // foreach ($cats as $cat) {
//Todo Delete Category and Sub Categories // $parentCatId = $cat->parent_category_id;
$deletedCatsCount++; // $parentCat = $this->categoryRepository->find($parentCatId);
} // if (is_null($parentCat) && !is_null($parentCatId)) {
} // $this->categoryEntryTranslationsModel->where('entry_id', $cat->id)->delete();
return redirect('admin/cats')->with('success', [$deletedCatsCount . ' categories has been deleted.']); // //Todo Sub Categories
// $deletedCatsCount++;
// }
// }
return redirect('admin/cats');
} }
public function adCountCalc() public function adCountCalc()