3 namespace BookStack\Http\Controllers;
5 use BookStack\Actions\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;
11 class RecycleBinController extends Controller
13 protected $recycleBinBaseUrl = '/settings/recycle-bin';
16 * On each request to a method of this controller check permissions
17 * using a middleware closure.
19 public function __construct()
21 $this->middleware(function ($request, $next) {
22 $this->checkPermission('settings-manage');
23 $this->checkPermission('restrictions-manage-all');
25 return $next($request);
30 * Show the top-level listing for the recycle bin.
32 public function index()
34 $deletions = Deletion::query()->with(['deletable', 'deleter'])->paginate(10);
36 $this->setPageTitle(trans('settings.recycle_bin'));
38 return view('settings.recycle-bin.index', [
39 'deletions' => $deletions,
44 * Show the page to confirm a restore of the deletion of the given id.
46 public function showRestore(string $id)
48 /** @var Deletion $deletion */
49 $deletion = Deletion::query()->findOrFail($id);
51 // Walk the parent chain to find any cascading parent deletions
52 $currentDeletable = $deletion->deletable;
54 while ($searching && $currentDeletable instanceof Entity) {
55 $parent = $currentDeletable->getParent();
56 if ($parent && $parent->trashed()) {
57 $currentDeletable = $parent;
63 /** @var ?Deletion $parentDeletion */
64 $parentDeletion = ($currentDeletable === $deletion->deletable) ? null : $currentDeletable->deletions()->first();
66 return view('settings.recycle-bin.restore', [
67 'deletion' => $deletion,
68 'parentDeletion' => $parentDeletion,
73 * Restore the element attached to the given deletion.
77 public function restore(DeletionRepo $deletionRepo, string $id)
79 $restoreCount = $deletionRepo->restore((int) $id);
81 $this->showSuccessNotification(trans('settings.recycle_bin_restore_notification', ['count' => $restoreCount]));
83 return redirect($this->recycleBinBaseUrl);
87 * Show the page to confirm a Permanent deletion of the element attached to the deletion of the given id.
89 public function showDestroy(string $id)
91 /** @var Deletion $deletion */
92 $deletion = Deletion::query()->findOrFail($id);
94 return view('settings.recycle-bin.destroy', [
95 'deletion' => $deletion,
100 * Permanently delete the content associated with the given deletion.
104 public function destroy(DeletionRepo $deletionRepo, string $id)
106 $deleteCount = $deletionRepo->destroy((int) $id);
108 $this->showSuccessNotification(trans('settings.recycle_bin_destroy_notification', ['count' => $deleteCount]));
110 return redirect($this->recycleBinBaseUrl);
114 * Empty out the recycle bin.
118 public function empty()
120 $deleteCount = (new TrashCan())->empty();
122 $this->logActivity(ActivityType::RECYCLE_BIN_EMPTY);
123 $this->showSuccessNotification(trans('settings.recycle_bin_destroy_notification', ['count' => $deleteCount]));
125 return redirect($this->recycleBinBaseUrl);