]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/RecycleBinController.php
Added per-item recycle-bin delete and restore
[bookstack] / app / Http / Controllers / RecycleBinController.php
1 <?php namespace BookStack\Http\Controllers;
2
3 use BookStack\Entities\Deletion;
4 use BookStack\Entities\Managers\TrashCan;
5
6 class RecycleBinController extends Controller
7 {
8
9     protected $recycleBinBaseUrl = '/settings/recycle-bin';
10
11     /**
12      * On each request to a method of this controller check permissions
13      * using a middleware closure.
14      */
15     public function __construct()
16     {
17         // TODO - Check this is enforced.
18         $this->middleware(function ($request, $next) {
19             $this->checkPermission('settings-manage');
20             $this->checkPermission('restrictions-manage-all');
21             return $next($request);
22         });
23         parent::__construct();
24     }
25
26
27     /**
28      * Show the top-level listing for the recycle bin.
29      */
30     public function index()
31     {
32         $deletions = Deletion::query()->with(['deletable', 'deleter'])->paginate(10);
33
34         return view('settings.recycle-bin.index', [
35             'deletions' => $deletions,
36         ]);
37     }
38
39     /**
40      * Show the page to confirm a restore of the deletion of the given id.
41      */
42     public function showRestore(string $id)
43     {
44         /** @var Deletion $deletion */
45         $deletion = Deletion::query()->findOrFail($id);
46
47         return view('settings.recycle-bin.restore', [
48             'deletion' => $deletion,
49         ]);
50     }
51
52     /**
53      * Restore the element attached to the given deletion.
54      * @throws \Exception
55      */
56     public function restore(string $id)
57     {
58         /** @var Deletion $deletion */
59         $deletion = Deletion::query()->findOrFail($id);
60         $restoreCount = (new TrashCan())->restoreFromDeletion($deletion);
61
62         $this->showSuccessNotification(trans('settings.recycle_bin_restore_notification', ['count' => $restoreCount]));
63         return redirect($this->recycleBinBaseUrl);
64     }
65
66     /**
67      * Show the page to confirm a Permanent deletion of the element attached to the deletion of the given id.
68      */
69     public function showDestroy(string $id)
70     {
71         /** @var Deletion $deletion */
72         $deletion = Deletion::query()->findOrFail($id);
73
74         return view('settings.recycle-bin.destroy', [
75             'deletion' => $deletion,
76         ]);
77     }
78
79     /**
80      * Permanently delete the content associated with the given deletion.
81      * @throws \Exception
82      */
83     public function destroy(string $id)
84     {
85         /** @var Deletion $deletion */
86         $deletion = Deletion::query()->findOrFail($id);
87         $deleteCount = (new TrashCan())->destroyFromDeletion($deletion);
88
89         $this->showSuccessNotification(trans('settings.recycle_bin_destroy_notification', ['count' => $deleteCount]));
90         return redirect($this->recycleBinBaseUrl);
91     }
92
93     /**
94      * Empty out the recycle bin.
95      * @throws \Exception
96      */
97     public function empty()
98     {
99         $deleteCount = (new TrashCan())->destroyFromAllDeletions();
100
101         $this->showSuccessNotification(trans('settings.recycle_bin_destroy_notification', ['count' => $deleteCount]));
102         return redirect($this->recycleBinBaseUrl);
103     }
104 }