]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/RecycleBinController.php
Split out settings view and made functional
[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
62         /** @var ?Deletion $parentDeletion */
63         $parentDeletion = ($currentDeletable === $deletion->deletable) ? null : $currentDeletable->deletions()->first();
64
65         return view('settings.recycle-bin.restore', [
66             'deletion'       => $deletion,
67             'parentDeletion' => $parentDeletion,
68         ]);
69     }
70
71     /**
72      * Restore the element attached to the given deletion.
73      *
74      * @throws \Exception
75      */
76     public function restore(string $id)
77     {
78         /** @var Deletion $deletion */
79         $deletion = Deletion::query()->findOrFail($id);
80         $this->logActivity(ActivityType::RECYCLE_BIN_RESTORE, $deletion);
81         $restoreCount = (new TrashCan())->restoreFromDeletion($deletion);
82
83         $this->showSuccessNotification(trans('settings.recycle_bin_restore_notification', ['count' => $restoreCount]));
84
85         return redirect($this->recycleBinBaseUrl);
86     }
87
88     /**
89      * Show the page to confirm a Permanent deletion of the element attached to the deletion of the given id.
90      */
91     public function showDestroy(string $id)
92     {
93         /** @var Deletion $deletion */
94         $deletion = Deletion::query()->findOrFail($id);
95
96         return view('settings.recycle-bin.destroy', [
97             'deletion' => $deletion,
98         ]);
99     }
100
101     /**
102      * Permanently delete the content associated with the given deletion.
103      *
104      * @throws \Exception
105      */
106     public function destroy(string $id)
107     {
108         /** @var Deletion $deletion */
109         $deletion = Deletion::query()->findOrFail($id);
110         $this->logActivity(ActivityType::RECYCLE_BIN_DESTROY, $deletion);
111         $deleteCount = (new TrashCan())->destroyFromDeletion($deletion);
112
113         $this->showSuccessNotification(trans('settings.recycle_bin_destroy_notification', ['count' => $deleteCount]));
114
115         return redirect($this->recycleBinBaseUrl);
116     }
117
118     /**
119      * Empty out the recycle bin.
120      *
121      * @throws \Exception
122      */
123     public function empty()
124     {
125         $deleteCount = (new TrashCan())->empty();
126
127         $this->logActivity(ActivityType::RECYCLE_BIN_EMPTY);
128         $this->showSuccessNotification(trans('settings.recycle_bin_destroy_notification', ['count' => $deleteCount]));
129
130         return redirect($this->recycleBinBaseUrl);
131     }
132 }