]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/RecycleBinController.php
Added a "skip to content" link.
[bookstack] / app / Http / Controllers / RecycleBinController.php
1 <?php namespace BookStack\Http\Controllers;
2
3 use BookStack\Actions\ActivityType;
4 use BookStack\Entities\Models\Deletion;
5 use BookStack\Entities\Tools\TrashCan;
6
7 class RecycleBinController extends Controller
8 {
9
10     protected $recycleBinBaseUrl = '/settings/recycle-bin';
11
12     /**
13      * On each request to a method of this controller check permissions
14      * using a middleware closure.
15      */
16     public function __construct()
17     {
18         $this->middleware(function ($request, $next) {
19             $this->checkPermission('settings-manage');
20             $this->checkPermission('restrictions-manage-all');
21             return $next($request);
22         });
23     }
24
25
26     /**
27      * Show the top-level listing for the recycle bin.
28      */
29     public function index()
30     {
31         $deletions = Deletion::query()->with(['deletable', 'deleter'])->paginate(10);
32
33         $this->setPageTitle(trans('settings.recycle_bin'));
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         $this->logActivity(ActivityType::RECYCLE_BIN_RESTORE, $deletion);
61         $restoreCount = (new TrashCan())->restoreFromDeletion($deletion);
62
63         $this->showSuccessNotification(trans('settings.recycle_bin_restore_notification', ['count' => $restoreCount]));
64         return redirect($this->recycleBinBaseUrl);
65     }
66
67     /**
68      * Show the page to confirm a Permanent deletion of the element attached to the deletion of the given id.
69      */
70     public function showDestroy(string $id)
71     {
72         /** @var Deletion $deletion */
73         $deletion = Deletion::query()->findOrFail($id);
74
75         return view('settings.recycle-bin.destroy', [
76             'deletion' => $deletion,
77         ]);
78     }
79
80     /**
81      * Permanently delete the content associated with the given deletion.
82      * @throws \Exception
83      */
84     public function destroy(string $id)
85     {
86         /** @var Deletion $deletion */
87         $deletion = Deletion::query()->findOrFail($id);
88         $this->logActivity(ActivityType::RECYCLE_BIN_DESTROY, $deletion);
89         $deleteCount = (new TrashCan())->destroyFromDeletion($deletion);
90
91         $this->showSuccessNotification(trans('settings.recycle_bin_destroy_notification', ['count' => $deleteCount]));
92         return redirect($this->recycleBinBaseUrl);
93     }
94
95     /**
96      * Empty out the recycle bin.
97      * @throws \Exception
98      */
99     public function empty()
100     {
101         $deleteCount = (new TrashCan())->empty();
102
103         $this->logActivity(ActivityType::RECYCLE_BIN_EMPTY);
104         $this->showSuccessNotification(trans('settings.recycle_bin_destroy_notification', ['count' => $deleteCount]));
105         return redirect($this->recycleBinBaseUrl);
106     }
107 }