]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/RecycleBinController.php
Cleaned up api docs implementation, added missing titles
[bookstack] / app / Http / Controllers / RecycleBinController.php
1 <?php namespace BookStack\Http\Controllers;
2
3 use BookStack\Actions\ActivityType;
4 use BookStack\Entities\Deletion;
5 use BookStack\Entities\Managers\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         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         $this->setPageTitle(trans('settings.recycle_bin'));
35         return view('settings.recycle-bin.index', [
36             'deletions' => $deletions,
37         ]);
38     }
39
40     /**
41      * Show the page to confirm a restore of the deletion of the given id.
42      */
43     public function showRestore(string $id)
44     {
45         /** @var Deletion $deletion */
46         $deletion = Deletion::query()->findOrFail($id);
47
48         return view('settings.recycle-bin.restore', [
49             'deletion' => $deletion,
50         ]);
51     }
52
53     /**
54      * Restore the element attached to the given deletion.
55      * @throws \Exception
56      */
57     public function restore(string $id)
58     {
59         /** @var Deletion $deletion */
60         $deletion = Deletion::query()->findOrFail($id);
61         $this->logActivity(ActivityType::RECYCLE_BIN_RESTORE, $deletion);
62         $restoreCount = (new TrashCan())->restoreFromDeletion($deletion);
63
64         $this->showSuccessNotification(trans('settings.recycle_bin_restore_notification', ['count' => $restoreCount]));
65         return redirect($this->recycleBinBaseUrl);
66     }
67
68     /**
69      * Show the page to confirm a Permanent deletion of the element attached to the deletion of the given id.
70      */
71     public function showDestroy(string $id)
72     {
73         /** @var Deletion $deletion */
74         $deletion = Deletion::query()->findOrFail($id);
75
76         return view('settings.recycle-bin.destroy', [
77             'deletion' => $deletion,
78         ]);
79     }
80
81     /**
82      * Permanently delete the content associated with the given deletion.
83      * @throws \Exception
84      */
85     public function destroy(string $id)
86     {
87         /** @var Deletion $deletion */
88         $deletion = Deletion::query()->findOrFail($id);
89         $this->logActivity(ActivityType::RECYCLE_BIN_DESTROY, $deletion);
90         $deleteCount = (new TrashCan())->destroyFromDeletion($deletion);
91
92         $this->showSuccessNotification(trans('settings.recycle_bin_destroy_notification', ['count' => $deleteCount]));
93         return redirect($this->recycleBinBaseUrl);
94     }
95
96     /**
97      * Empty out the recycle bin.
98      * @throws \Exception
99      */
100     public function empty()
101     {
102         $deleteCount = (new TrashCan())->empty();
103
104         $this->logActivity(ActivityType::RECYCLE_BIN_EMPTY);
105         $this->showSuccessNotification(trans('settings.recycle_bin_destroy_notification', ['count' => $deleteCount]));
106         return redirect($this->recycleBinBaseUrl);
107     }
108 }