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