3 namespace BookStack\Entities\Controllers;
5 use BookStack\Activity\ActivityType;
6 use BookStack\Entities\Models\Deletion;
7 use BookStack\Entities\Models\Entity;
8 use BookStack\Entities\Repos\DeletionRepo;
9 use BookStack\Entities\Tools\TrashCan;
10 use BookStack\Http\Controller;
11 use BookStack\Permissions\Permission;
13 class RecycleBinController extends Controller
15 protected string $recycleBinBaseUrl = '/settings/recycle-bin';
18 * On each request to a method of this controller check permissions
19 * using a middleware closure.
21 public function __construct()
23 $this->middleware(function ($request, $next) {
24 $this->checkPermission(Permission::SettingsManage);
25 $this->checkPermission(Permission::RestrictionsManageAll);
27 return $next($request);
32 * Show the top-level listing for the recycle bin.
34 public function index()
36 $deletions = Deletion::query()->with(['deletable', 'deleter'])->paginate(10);
38 $this->setPageTitle(trans('settings.recycle_bin'));
40 return view('settings.recycle-bin.index', [
41 'deletions' => $deletions,
46 * Show the page to confirm a restore of the deletion of the given id.
48 public function showRestore(string $id)
50 /** @var Deletion $deletion */
51 $deletion = Deletion::query()->findOrFail($id);
53 // Walk the parent chain to find any cascading parent deletions
54 $currentDeletable = $deletion->deletable;
56 while ($searching && $currentDeletable instanceof Entity) {
57 $parent = $currentDeletable->getParent();
58 if ($parent && $parent->trashed()) {
59 $currentDeletable = $parent;
65 /** @var ?Deletion $parentDeletion */
66 $parentDeletion = ($currentDeletable === $deletion->deletable) ? null : $currentDeletable->deletions()->first();
68 return view('settings.recycle-bin.restore', [
69 'deletion' => $deletion,
70 'parentDeletion' => $parentDeletion,
75 * Restore the element attached to the given deletion.
79 public function restore(DeletionRepo $deletionRepo, string $id)
81 $restoreCount = $deletionRepo->restore((int) $id);
83 $this->showSuccessNotification(trans('settings.recycle_bin_restore_notification', ['count' => $restoreCount]));
85 return redirect($this->recycleBinBaseUrl);
89 * Show the page to confirm a Permanent deletion of the element attached to the deletion of the given id.
91 public function showDestroy(string $id)
93 /** @var Deletion $deletion */
94 $deletion = Deletion::query()->findOrFail($id);
96 return view('settings.recycle-bin.destroy', [
97 'deletion' => $deletion,
102 * Permanently delete the content associated with the given deletion.
106 public function destroy(DeletionRepo $deletionRepo, string $id)
108 $deleteCount = $deletionRepo->destroy((int) $id);
110 $this->showSuccessNotification(trans('settings.recycle_bin_destroy_notification', ['count' => $deleteCount]));
112 return redirect($this->recycleBinBaseUrl);
116 * Empty out the recycle bin.
120 public function empty(TrashCan $trash)
122 $deleteCount = $trash->empty();
124 $this->logActivity(ActivityType::RECYCLE_BIN_EMPTY);
125 $this->showSuccessNotification(trans('settings.recycle_bin_destroy_notification', ['count' => $deleteCount]));
127 return redirect($this->recycleBinBaseUrl);