Merge pull request #602 from openclassify/vedatakd

fix category delete
This commit is contained in:
Ozcan Durak 2020-06-25 12:48:15 +03:00 committed by GitHub
commit d9c3d34118
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 17 deletions

View File

@ -76,10 +76,12 @@ class CategoryRepository extends EntryRepository implements CategoryRepositoryIn
return $this->model->orderBy('sort_order')->get(); return $this->model->orderBy('sort_order')->get();
} }
public function removeCatFromAds($id) public function removeCatFromAds($category)
{ {
$category = $this->find($id); $catLevelNum = 1;
$catLevelNum = is_null($category->parent_category_id) ? 1 : $this->model->getCatLevel($category->id); if (!is_null($category->parent_category_id)) {
$catLevelNum = $this->model->getCatLevel($category->id);
}
$catLevelText = "cat" . $catLevelNum; $catLevelText = "cat" . $catLevelNum;
$advs = $this->advRepository->newQuery()->where($catLevelText, $category->id)->get(); $advs = $this->advRepository->newQuery()->where($catLevelText, $category->id)->get();
@ -94,13 +96,15 @@ class CategoryRepository extends EntryRepository implements CategoryRepositoryIn
public function DeleteCategories($id) public function DeleteCategories($id)
{ {
// Remove deleted category from ads if (!is_null($category = $this->find($id))) {
$this->removeCatFromAds($id); // Remove deleted category from ads
$this->removeCatFromAds($category);
// Delete the category // Delete the category
$this->model->find($id)->delete(); $this->model->find($id)->delete();
// Delete the subcategories // Delete the subcategories
$this->model->deleteSubCategories($id); $this->model->deleteSubCategories($id);
}
} }
} }

View File

@ -47,8 +47,7 @@ class CategoryController extends AdminController
$this->messages->error('Selected category has no sub-categories.'); $this->messages->error('Selected category has no sub-categories.');
return back(); return back();
} }
if($request->view != "trash") if ($request->view != "trash") {
{
$table->setTableEntries($categories); $table->setTableEntries($categories);
} }
@ -157,7 +156,8 @@ class CategoryController extends AdminController
return $this->view->make('visiosoft.module.cats::cats/admin-cat'); return $this->view->make('visiosoft.module.cats::cats/admin-cat');
} }
public function endsWith($string, $test) { public function endsWith($string, $test)
{
$strlen = strlen($string); $strlen = strlen($string);
$testlen = strlen($test); $testlen = strlen($test);
if ($testlen > $strlen) return false; if ($testlen > $strlen) return false;
@ -185,12 +185,13 @@ class CategoryController extends AdminController
public function delete(CategoryRepositoryInterface $categoryRepository, Request $request, CategoryModel $categoryModel, $id) public function delete(CategoryRepositoryInterface $categoryRepository, Request $request, CategoryModel $categoryModel, $id)
{ {
$categoryRepository->DeleteCategories($id); $categoryRepository->DeleteCategories($id);
$subCats = $categoryRepository->getSubCatById($request->parent); if ($request->parent != "") {
if (count($subCats)) { $subCats = $categoryRepository->getSubCatById($request->parent);
return redirect('admin/cats?cat=' . $request->parent)->with('success', ['Category and related sub-categories deleted successfully.']); if (count($subCats)) {
} else { return redirect('admin/cats?cat=' . $request->parent)->with('success', ['Category and related sub-categories deleted successfully.']);
return redirect('admin/cats')->with('success', ['Category and related sub-categories deleted successfully.']); }
} }
return redirect('admin/cats')->with('success', ['Category and related sub-categories deleted successfully.']);
} }
public function cleanSubcats() public function cleanSubcats()