]> BookStack Code Mirror - bookstack/blob - app/Entities/Controllers/RecycleBinApiController.php
Played around with a new app structure
[bookstack] / app / Entities / Controllers / RecycleBinApiController.php
1 <?php
2
3 namespace BookStack\Entities\Controllers;
4
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\BookChild;
7 use BookStack\Entities\Models\Chapter;
8 use BookStack\Entities\Models\Deletion;
9 use BookStack\Entities\Repos\DeletionRepo;
10 use BookStack\Http\Controllers\ApiController;
11 use Closure;
12 use Illuminate\Database\Eloquent\Builder;
13
14 class RecycleBinApiController extends ApiController
15 {
16     public function __construct()
17     {
18         $this->middleware(function ($request, $next) {
19             $this->checkPermission('settings-manage');
20             $this->checkPermission('restrictions-manage-all');
21
22             return $next($request);
23         });
24     }
25
26     /**
27      * Get a top-level listing of the items in the recycle bin.
28      * The "deletable" property will reflect the main item deleted.
29      * For books and chapters, counts of child pages/chapters will
30      * be loaded within this "deletable" data.
31      * For chapters & pages, the parent item will be loaded within this "deletable" data.
32      * Requires permission to manage both system settings and permissions.
33      */
34     public function list()
35     {
36         return $this->apiListingResponse(Deletion::query()->with('deletable'), [
37             'id',
38             'deleted_by',
39             'created_at',
40             'updated_at',
41             'deletable_type',
42             'deletable_id',
43         ], [Closure::fromCallable([$this, 'listFormatter'])]);
44     }
45
46     /**
47      * Restore a single deletion from the recycle bin.
48      * Requires permission to manage both system settings and permissions.
49      */
50     public function restore(DeletionRepo $deletionRepo, string $deletionId)
51     {
52         $restoreCount = $deletionRepo->restore(intval($deletionId));
53
54         return response()->json(['restore_count' => $restoreCount]);
55     }
56
57     /**
58      * Remove a single deletion from the recycle bin.
59      * Use this endpoint carefully as it will entirely remove the underlying deleted items from the system.
60      * Requires permission to manage both system settings and permissions.
61      */
62     public function destroy(DeletionRepo $deletionRepo, string $deletionId)
63     {
64         $deleteCount = $deletionRepo->destroy(intval($deletionId));
65
66         return response()->json(['delete_count' => $deleteCount]);
67     }
68
69     /**
70      * Load some related details for the deletion listing.
71      */
72     protected function listFormatter(Deletion $deletion)
73     {
74         $deletable = $deletion->deletable;
75         $withTrashedQuery = fn (Builder $query) => $query->withTrashed();
76
77         if ($deletable instanceof BookChild) {
78             $parent = $deletable->getParent();
79             $parent->setAttribute('type', $parent->getType());
80             $deletable->setRelation('parent', $parent);
81         }
82
83         if ($deletable instanceof Book || $deletable instanceof Chapter) {
84             $countsToLoad = ['pages' => $withTrashedQuery];
85             if ($deletable instanceof Book) {
86                 $countsToLoad['chapters'] = $withTrashedQuery;
87             }
88             $deletable->loadCount($countsToLoad);
89         }
90     }
91 }