Merge pull request #465 from openclassify/dia

#1151 Delete sub categories when deleting a parent category [delete a…
This commit is contained in:
Fatih Alp 2020-03-30 17:10:30 +03:00 committed by GitHub
commit 02ac2ff573
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 8 deletions

View File

@ -587,9 +587,13 @@ class AdvsController extends PublicController
// Auto approve // Auto approve
if (setting_value('visiosoft.module.advs::auto_approve')) { if (setting_value('visiosoft.module.advs::auto_approve')) {
if ($adv->status == 'pending_admin' || $adv->status == 'pending_user') { $defaultAdPublishTime = setting_value('visiosoft.module.advs::default_published_time');
$adv->status = 'approved';
} $adv->update([
'status' => 'approved',
'finish_at' => date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s') . ' + ' . $defaultAdPublishTime . ' day')),
'publish_at' => date('Y-m-d H:i:s')
]);
} }
$form->render($request->update_id); $form->render($request->update_id);

View File

@ -24,7 +24,7 @@ class CategoryModel extends CatsCategoryEntryModel implements CategoryInterface
$cat_ids[] = $cat->id; $cat_ids[] = $cat->id;
$subCat = $cat->parent_category_id; $subCat = $cat->parent_category_id;
if ($subCat != null) { if ($subCat != null) {
for ($i = 0; $i < 7; $i++) { for ($i = 0; $i < 10; $i++) {
$parCat = $this->getCat($subCat); $parCat = $this->getCat($subCat);
if (isset($parCat)) { if (isset($parCat)) {
if ($parCat->parent_category_id == "") { if ($parCat->parent_category_id == "") {
@ -83,9 +83,10 @@ class CategoryModel extends CatsCategoryEntryModel implements CategoryInterface
public function deleteSubCategories($id) public function deleteSubCategories($id)
{ {
$subCategories = $this->getAllSubCategories($id); $subCategories = $this->getAllSubCategories($id);
foreach ($subCategories as $subCategory) { if (count($subCategories)) {
$this->find($subCategory)->delete(); $this->newQuery()->whereIn('id', $subCategories)->delete();
} }
return true; return true;
} }

View File

@ -1,6 +1,7 @@
<?php namespace Visiosoft\CatsModule\Category; <?php namespace Visiosoft\CatsModule\Category;
use Anomaly\Streams\Platform\Model\Cats\CatsCategoryEntryModel; use Anomaly\Streams\Platform\Model\Cats\CatsCategoryEntryModel;
use Visiosoft\AdvsModule\Adv\Contract\AdvRepositoryInterface;
use Visiosoft\CatsModule\Category\Contract\CategoryRepositoryInterface; use Visiosoft\CatsModule\Category\Contract\CategoryRepositoryInterface;
use Anomaly\Streams\Platform\Entry\EntryRepository; use Anomaly\Streams\Platform\Entry\EntryRepository;
@ -13,15 +14,18 @@ class CategoryRepository extends EntryRepository implements CategoryRepositoryIn
* @var CategoryModel * @var CategoryModel
*/ */
protected $model; protected $model;
protected $advRepository;
/** /**
* Create a new CategoryRepository instance. * Create a new CategoryRepository instance.
* *
* @param CategoryModel $model * @param CategoryModel $model
* @param AdvRepositoryInterface $advRepository
*/ */
public function __construct(CategoryModel $model) public function __construct(CategoryModel $model, AdvRepositoryInterface $advRepository)
{ {
$this->model = $model; $this->model = $model;
$this->advRepository = $advRepository;
} }
public function findById($id) public function findById($id)
@ -64,8 +68,31 @@ class CategoryRepository extends EntryRepository implements CategoryRepositoryIn
return $this->model->orderBy('sort_order')->get(); return $this->model->orderBy('sort_order')->get();
} }
public function removeCatFromAds($id)
{
$category = $this->find($id);
$catLevelNum = is_null($category->parent_category_id) ? 1 : $this->model->getCatLevel($category->id);
$catLevelText = "cat" . $catLevelNum;
$advs = $this->advRepository->newQuery()->where($catLevelText, $category->id)->get();
foreach ($advs as $adv) {
$nullableCats = array();
for ($i = $catLevelNum; $i <= 10; $i++) {
$nullableCats['cat' . $i] = null;
}
$adv->update($nullableCats);
}
}
public function DeleteCategories($id) public function DeleteCategories($id)
{ {
// Remove deleted category from ads
$this->removeCatFromAds($id);
// Delete the category
$this->model->find($id)->delete(); $this->model->find($id)->delete();
// Delete the subcategories
$this->model->deleteSubCategories($id);
} }
} }

View File

@ -209,5 +209,4 @@ class CategoryController extends AdminController
return redirect('admin/cats')->with('success', [$deletedCatsCount . ' categories has been deleted.']); return redirect('admin/cats')->with('success', [$deletedCatsCount . ' categories has been deleted.']);
} }
} }