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;
61 /** @var ?Deletion $parentDeletion */
62 $parentDeletion = ($currentDeletable === $deletion->deletable) ? null : $currentDeletable->deletions()->first();
64 return view('settings.recycle-bin.restore', [
65 'deletion' => $deletion,
66 'parentDeletion' => $parentDeletion,
71 * Restore the element attached to the given deletion.
75 public function restore(string $id)
77 /** @var Deletion $deletion */
78 $deletion = Deletion::query()->findOrFail($id);
79 $this->logActivity(ActivityType::RECYCLE_BIN_RESTORE, $deletion);
80 $restoreCount = (new TrashCan())->restoreFromDeletion($deletion);
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(string $id)
107 /** @var Deletion $deletion */
108 $deletion = Deletion::query()->findOrFail($id);
109 $this->logActivity(ActivityType::RECYCLE_BIN_DESTROY, $deletion);
110 $deleteCount = (new TrashCan())->destroyFromDeletion($deletion);
112 $this->showSuccessNotification(trans('settings.recycle_bin_destroy_notification', ['count' => $deleteCount]));
114 return redirect($this->recycleBinBaseUrl);
118 * Empty out the recycle bin.
122 public function empty()
124 $deleteCount = (new TrashCan())->empty();
126 $this->logActivity(ActivityType::RECYCLE_BIN_EMPTY);
127 $this->showSuccessNotification(trans('settings.recycle_bin_destroy_notification', ['count' => $deleteCount]));
129 return redirect($this->recycleBinBaseUrl);