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\Tools\TrashCan;
10 class RecycleBinController extends Controller
12 protected $recycleBinBaseUrl = '/settings/recycle-bin';
15 * On each request to a method of this controller check permissions
16 * using a middleware closure.
18 public function __construct()
20 $this->middleware(function ($request, $next) {
21 $this->checkPermission('settings-manage');
22 $this->checkPermission('restrictions-manage-all');
24 return $next($request);
29 * Show the top-level listing for the recycle bin.
31 public function index()
33 $deletions = Deletion::query()->with(['deletable', 'deleter'])->paginate(10);
35 $this->setPageTitle(trans('settings.recycle_bin'));
37 return view('settings.recycle-bin.index', [
38 'deletions' => $deletions,
43 * Show the page to confirm a restore of the deletion of the given id.
45 public function showRestore(string $id)
47 /** @var Deletion $deletion */
48 $deletion = Deletion::query()->findOrFail($id);
50 // Walk the parent chain to find any cascading parent deletions
51 $currentDeletable = $deletion->deletable;
53 while ($searching && $currentDeletable instanceof Entity) {
54 $parent = $currentDeletable->getParent();
55 if ($parent && $parent->trashed()) {
56 $currentDeletable = $parent;
62 /** @var ?Deletion $parentDeletion */
63 $parentDeletion = ($currentDeletable === $deletion->deletable) ? null : $currentDeletable->deletions()->first();
65 return view('settings.recycle-bin.restore', [
66 'deletion' => $deletion,
67 'parentDeletion' => $parentDeletion,
72 * Restore the element attached to the given deletion.
76 public function restore(string $id)
78 /** @var Deletion $deletion */
79 $deletion = Deletion::query()->findOrFail($id);
80 $this->logActivity(ActivityType::RECYCLE_BIN_RESTORE, $deletion);
81 $restoreCount = (new TrashCan())->restoreFromDeletion($deletion);
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(string $id)
108 /** @var Deletion $deletion */
109 $deletion = Deletion::query()->findOrFail($id);
110 $this->logActivity(ActivityType::RECYCLE_BIN_DESTROY, $deletion);
111 $deleteCount = (new TrashCan())->destroyFromDeletion($deletion);
113 $this->showSuccessNotification(trans('settings.recycle_bin_destroy_notification', ['count' => $deleteCount]));
115 return redirect($this->recycleBinBaseUrl);
119 * Empty out the recycle bin.
123 public function empty()
125 $deleteCount = (new TrashCan())->empty();
127 $this->logActivity(ActivityType::RECYCLE_BIN_EMPTY);
128 $this->showSuccessNotification(trans('settings.recycle_bin_destroy_notification', ['count' => $deleteCount]));
130 return redirect($this->recycleBinBaseUrl);