]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/RecycleBinController.php
Apply fixes from StyleCI
[bookstack] / app / Http / Controllers / RecycleBinController.php
1 <?php
2
3 namespace BookStack\Http\Controllers;
4
5 use BookStack\Actions\ActivityType;
6 use BookStack\Entities\Models\Deletion;
7 use BookStack\Entities\Models\Entity;
8 use BookStack\Entities\Tools\TrashCan;
9
10 class RecycleBinController extends Controller
11 {
12     protected $recycleBinBaseUrl = '/settings/recycle-bin';
13
14     /**
15      * On each request to a method of this controller check permissions
16      * using a middleware closure.
17      */
18     public function __construct()
19     {
20         $this->middleware(function ($request, $next) {
21             $this->checkPermission('settings-manage');
22             $this->checkPermission('restrictions-manage-all');
23
24             return $next($request);
25         });
26     }
27
28     /**
29      * Show the top-level listing for the recycle bin.
30      */
31     public function index()
32     {
33         $deletions = Deletion::query()->with(['deletable', 'deleter'])->paginate(10);
34
35         $this->setPageTitle(trans('settings.recycle_bin'));
36
37         return view('settings.recycle-bin.index', [
38             'deletions' => $deletions,
39         ]);
40     }
41
42     /**
43      * Show the page to confirm a restore of the deletion of the given id.
44      */
45     public function showRestore(string $id)
46     {
47         /** @var Deletion $deletion */
48         $deletion = Deletion::query()->findOrFail($id);
49
50         // Walk the parent chain to find any cascading parent deletions
51         $currentDeletable = $deletion->deletable;
52         $searching = true;
53         while ($searching && $currentDeletable instanceof Entity) {
54             $parent = $currentDeletable->getParent();
55             if ($parent && $parent->trashed()) {
56                 $currentDeletable = $parent;
57             } else {
58                 $searching = false;
59             }
60         }
61         /** @var ?Deletion $parentDeletion */
62         $parentDeletion = ($currentDeletable === $deletion->deletable) ? null : $currentDeletable->deletions()->first();
63
64         return view('settings.recycle-bin.restore', [
65             'deletion'       => $deletion,
66             'parentDeletion' => $parentDeletion,
67         ]);
68     }
69
70     /**
71      * Restore the element attached to the given deletion.
72      *
73      * @throws \Exception
74      */
75     public function restore(string $id)
76     {
77         /** @var Deletion $deletion */
78         $deletion = Deletion::query()->findOrFail($id);
79         $this->logActivity(ActivityType::RECYCLE_BIN_RESTORE, $deletion);
80         $restoreCount = (new TrashCan())->restoreFromDeletion($deletion);
81
82         $this->showSuccessNotification(trans('settings.recycle_bin_restore_notification', ['count' => $restoreCount]));
83
84         return redirect($this->recycleBinBaseUrl);
85     }
86
87     /**
88      * Show the page to confirm a Permanent deletion of the element attached to the deletion of the given id.
89      */
90     public function showDestroy(string $id)
91     {
92         /** @var Deletion $deletion */
93         $deletion = Deletion::query()->findOrFail($id);
94
95         return view('settings.recycle-bin.destroy', [
96             'deletion' => $deletion,
97         ]);
98     }
99
100     /**
101      * Permanently delete the content associated with the given deletion.
102      *
103      * @throws \Exception
104      */
105     public function destroy(string $id)
106     {
107         /** @var Deletion $deletion */
108         $deletion = Deletion::query()->findOrFail($id);
109         $this->logActivity(ActivityType::RECYCLE_BIN_DESTROY, $deletion);
110         $deleteCount = (new TrashCan())->destroyFromDeletion($deletion);
111
112         $this->showSuccessNotification(trans('settings.recycle_bin_destroy_notification', ['count' => $deleteCount]));
113
114         return redirect($this->recycleBinBaseUrl);
115     }
116
117     /**
118      * Empty out the recycle bin.
119      *
120      * @throws \Exception
121      */
122     public function empty()
123     {
124         $deleteCount = (new TrashCan())->empty();
125
126         $this->logActivity(ActivityType::RECYCLE_BIN_EMPTY);
127         $this->showSuccessNotification(trans('settings.recycle_bin_destroy_notification', ['count' => $deleteCount]));
128
129         return redirect($this->recycleBinBaseUrl);
130     }
131 }