]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Api/RecycleBinApiController.php
do some cleanup and add doc
[bookstack] / app / Http / Controllers / Api / RecycleBinApiController.php
1 <?php
2
3 namespace BookStack\Http\Controllers\Api;
4
5 use BookStack\Entities\Models\Deletion;
6 use BookStack\Entities\Repos\DeletionRepo;
7 use Closure;
8
9 class RecycleBinApiController extends ApiController
10 {
11     protected $fieldsToExpose = [
12         'id', 'deleted_by', 'created_at', 'updated_at', 'deletable_type', 'deletable_id',
13     ];
14
15     public function __construct()
16     {
17         $this->middleware(function ($request, $next) {
18             $this->checkPermission('settings-manage');
19             $this->checkPermission('restrictions-manage-all');
20
21             return $next($request);
22         });
23     }
24
25     /**
26      * Get a top-level listing of the items in the recycle bin.
27      * Requires the permission to manage settings and restrictions.
28      */
29     public function list()
30     {
31         return $this->apiListingResponse(Deletion::query()->with('deletable'), [
32             'id',
33             'deleted_by',
34             'created_at',
35             'updated_at',
36             'deletable_type',
37             'deletable_id',
38         ], [Closure::fromCallable([$this, 'listFormatter'])]);
39     }
40
41     /**
42      * Restore a single deletion from the recycle bin.
43      * You must provide the deletion id, not the id of the corresponding deleted item.
44      */
45     public function restore(DeletionRepo $deletionRepo, string $id)
46     {
47         $restoreCount = $deletionRepo->restore((int) $id);
48
49         return response()->json(['restore_count' => $restoreCount]);
50     }
51
52     /**
53      * Remove a single deletion from the recycle bin.
54      * Use this endpoint carefully as it will entirely remove the underlying deleted items from the system.
55      * You must provide the deletion id, not the id of the corresponding deleted item.
56      */
57     public function destroy(DeletionRepo $deletionRepo, string $id)
58     {
59         $deleteCount = $deletionRepo->destroy((int) $id);
60
61         return response()->json(['delete_count' => $deleteCount]);
62     }
63
64     protected function listFormatter(Deletion $deletion)
65     {
66         $deletion->makeVisible($this->fieldsToExpose);
67         $deletion->makeHidden('deletable');
68
69         $deletable = $deletion->deletable;
70         $isBook = $deletion->deletable_type === "BookStack\Book";
71         $parent = null;
72         $children = null;
73
74         if ($isBook) {
75             $chapterCount = $deletable->chapters()->withTrashed()->count();
76             $children['BookStack\Chapter'] = $chapterCount;
77         }
78
79         if ($isBook || $deletion->deletable_type === "BookStack\Chapter") {
80             $pageCount = $deletable->pages()->withTrashed()->count();
81             $children['BookStack\Page'] = $pageCount;
82         }
83
84         $parentEntity = $deletable->getParent();
85         $parent = null;
86
87         if ($parentEntity) {
88             $parent['type'] = $parentEntity->getMorphClass();
89             $parent['id'] = $parentEntity->getKey();
90         }
91
92         $deletion->setAttribute('parent', $parent);
93         $deletion->setAttribute('children', $children);
94     }
95 }