Added recycle bin empty notification response with count

This commit is contained in:
Dan Brown
2020-10-03 18:53:09 +01:00
parent 04197e393a
commit ff7cbd14fc
3 changed files with 23 additions and 8 deletions

View File

@ -90,10 +90,11 @@ class TrashCan
* Remove a bookshelf from the system. * Remove a bookshelf from the system.
* @throws Exception * @throws Exception
*/ */
public function destroyShelf(Bookshelf $shelf) public function destroyShelf(Bookshelf $shelf): int
{ {
$this->destroyCommonRelations($shelf); $this->destroyCommonRelations($shelf);
$shelf->forceDelete(); $shelf->forceDelete();
return 1;
} }
/** /**
@ -101,20 +102,24 @@ class TrashCan
* Destroys any child chapters and pages. * Destroys any child chapters and pages.
* @throws Exception * @throws Exception
*/ */
public function destroyBook(Book $book) public function destroyBook(Book $book): int
{ {
$count = 0;
$pages = $book->pages()->withTrashed()->get(); $pages = $book->pages()->withTrashed()->get();
foreach ($pages as $page) { foreach ($pages as $page) {
$this->destroyPage($page); $this->destroyPage($page);
$count++;
} }
$chapters = $book->chapters()->withTrashed()->get(); $chapters = $book->chapters()->withTrashed()->get();
foreach ($chapters as $chapter) { foreach ($chapters as $chapter) {
$this->destroyChapter($chapter); $this->destroyChapter($chapter);
$count++;
} }
$this->destroyCommonRelations($book); $this->destroyCommonRelations($book);
$book->forceDelete(); $book->forceDelete();
return $count + 1;
} }
/** /**
@ -122,24 +127,27 @@ class TrashCan
* Destroys all pages within. * Destroys all pages within.
* @throws Exception * @throws Exception
*/ */
public function destroyChapter(Chapter $chapter) public function destroyChapter(Chapter $chapter): int
{ {
$count = 0;
$pages = $chapter->pages()->withTrashed()->get(); $pages = $chapter->pages()->withTrashed()->get();
if (count($pages)) { if (count($pages)) {
foreach ($pages as $page) { foreach ($pages as $page) {
$this->destroyPage($page); $this->destroyPage($page);
$count++;
} }
} }
$this->destroyCommonRelations($chapter); $this->destroyCommonRelations($chapter);
$chapter->forceDelete(); $chapter->forceDelete();
return $count + 1;
} }
/** /**
* Remove a page from the system. * Remove a page from the system.
* @throws Exception * @throws Exception
*/ */
public function destroyPage(Page $page) public function destroyPage(Page $page): int
{ {
$this->destroyCommonRelations($page); $this->destroyCommonRelations($page);
@ -150,6 +158,7 @@ class TrashCan
} }
$page->forceDelete(); $page->forceDelete();
return 1;
} }
/** /**
@ -172,24 +181,27 @@ class TrashCan
/** /**
* Destroy all items that have pending deletions. * Destroy all items that have pending deletions.
*/ */
public function destroyFromAllDeletions() public function destroyFromAllDeletions(): int
{ {
$deletions = Deletion::all(); $deletions = Deletion::all();
$deleteCount = 0;
foreach ($deletions as $deletion) { foreach ($deletions as $deletion) {
// For each one we load in the relation since it may have already // For each one we load in the relation since it may have already
// been deleted as part of another deletion in this loop. // been deleted as part of another deletion in this loop.
$entity = $deletion->deletable()->first(); $entity = $deletion->deletable()->first();
if ($entity) { if ($entity) {
$this->destroyEntity($deletion->deletable); $count = $this->destroyEntity($deletion->deletable);
$deleteCount += $count;
} }
$deletion->delete(); $deletion->delete();
} }
return $deleteCount;
} }
/** /**
* Destroy the given entity. * Destroy the given entity.
*/ */
protected function destroyEntity(Entity $entity) protected function destroyEntity(Entity $entity): int
{ {
if ($entity->isA('page')) { if ($entity->isA('page')) {
return $this->destroyPage($entity); return $this->destroyPage($entity);

View File

@ -29,7 +29,9 @@ class RecycleBinController extends Controller
$this->checkPermission('settings-manage'); $this->checkPermission('settings-manage');
$this->checkPermission('restrictions-manage-all'); $this->checkPermission('restrictions-manage-all');
(new TrashCan())->destroyFromAllDeletions(); $deleteCount = (new TrashCan())->destroyFromAllDeletions();
$this->showSuccessNotification(trans('settings.recycle_bin_empty_notification', ['count' => $deleteCount]));
return redirect('/settings/recycle-bin'); return redirect('/settings/recycle-bin');
} }
} }

View File

@ -92,6 +92,7 @@ return [
'recycle_bin_contents_empty' => 'The recycle bin is currently empty', 'recycle_bin_contents_empty' => 'The recycle bin is currently empty',
'recycle_bin_empty' => 'Empty Recycle Bin', 'recycle_bin_empty' => 'Empty Recycle Bin',
'recycle_bin_empty_confirm' => 'This will permanently destroy all items in the recycle bin including content contained within each item. Are you sure you want to empty the recycle bin?', 'recycle_bin_empty_confirm' => 'This will permanently destroy all items in the recycle bin including content contained within each item. Are you sure you want to empty the recycle bin?',
'recycle_bin_empty_notification' => 'Deleted :count total items from the recycle bin.',
// Audit Log // Audit Log
'audit' => 'Audit Log', 'audit' => 'Audit Log',