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;
12 class RecycleBinController extends Controller
14 protected string $recycleBinBaseUrl = '/settings/recycle-bin';
17 * On each request to a method of this controller check permissions
18 * using a middleware closure.
20 public function __construct()
22 $this->middleware(function ($request, $next) {
23 $this->checkPermission('settings-manage');
24 $this->checkPermission('restrictions-manage-all');
26 return $next($request);
31 * Show the top-level listing for the recycle bin.
33 public function index()
35 $deletions = Deletion::query()->with(['deletable', 'deleter'])->paginate(10);
37 $this->setPageTitle(trans('settings.recycle_bin'));
39 return view('settings.recycle-bin.index', [
40 'deletions' => $deletions,
45 * Show the page to confirm a restore of the deletion of the given id.
47 public function showRestore(string $id)
49 /** @var Deletion $deletion */
50 $deletion = Deletion::query()->findOrFail($id);
52 // Walk the parent chain to find any cascading parent deletions
53 $currentDeletable = $deletion->deletable;
55 while ($searching && $currentDeletable instanceof Entity) {
56 $parent = $currentDeletable->getParent();
57 if ($parent && $parent->trashed()) {
58 $currentDeletable = $parent;
64 /** @var ?Deletion $parentDeletion */
65 $parentDeletion = ($currentDeletable === $deletion->deletable) ? null : $currentDeletable->deletions()->first();
67 return view('settings.recycle-bin.restore', [
68 'deletion' => $deletion,
69 'parentDeletion' => $parentDeletion,
74 * Restore the element attached to the given deletion.
78 public function restore(DeletionRepo $deletionRepo, string $id)
80 $restoreCount = $deletionRepo->restore((int) $id);
82 $this->showSuccessNotification(trans('settings.recycle_bin_restore_notification', ['count' => $restoreCount]));
84 return redirect($this->recycleBinBaseUrl);
88 * Show the page to confirm a Permanent deletion of the element attached to the deletion of the given id.
90 public function showDestroy(string $id)
92 /** @var Deletion $deletion */
93 $deletion = Deletion::query()->findOrFail($id);
95 return view('settings.recycle-bin.destroy', [
96 'deletion' => $deletion,
101 * Permanently delete the content associated with the given deletion.
105 public function destroy(DeletionRepo $deletionRepo, string $id)
107 $deleteCount = $deletionRepo->destroy((int) $id);
109 $this->showSuccessNotification(trans('settings.recycle_bin_destroy_notification', ['count' => $deleteCount]));
111 return redirect($this->recycleBinBaseUrl);
115 * Empty out the recycle bin.
119 public function empty(TrashCan $trash)
121 $deleteCount = $trash->empty();
123 $this->logActivity(ActivityType::RECYCLE_BIN_EMPTY);
124 $this->showSuccessNotification(trans('settings.recycle_bin_destroy_notification', ['count' => $deleteCount]));
126 return redirect($this->recycleBinBaseUrl);