3 namespace BookStack\Entities\Controllers;
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;
12 use Illuminate\Database\Eloquent\Builder;
14 class RecycleBinApiController extends ApiController
16 public function __construct()
18 $this->middleware(function ($request, $next) {
19 $this->checkPermission('settings-manage');
20 $this->checkPermission('restrictions-manage-all');
22 return $next($request);
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.
34 public function list()
36 return $this->apiListingResponse(Deletion::query()->with('deletable'), [
43 ], [Closure::fromCallable([$this, 'listFormatter'])]);
47 * Restore a single deletion from the recycle bin.
48 * Requires permission to manage both system settings and permissions.
50 public function restore(DeletionRepo $deletionRepo, string $deletionId)
52 $restoreCount = $deletionRepo->restore(intval($deletionId));
54 return response()->json(['restore_count' => $restoreCount]);
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.
62 public function destroy(DeletionRepo $deletionRepo, string $deletionId)
64 $deleteCount = $deletionRepo->destroy(intval($deletionId));
66 return response()->json(['delete_count' => $deleteCount]);
70 * Load some related details for the deletion listing.
72 protected function listFormatter(Deletion $deletion)
74 $deletable = $deletion->deletable;
75 $withTrashedQuery = fn (Builder $query) => $query->withTrashed();
77 if ($deletable instanceof BookChild) {
78 $parent = $deletable->getParent();
79 $parent->setAttribute('type', $parent->getType());
80 $deletable->setRelation('parent', $parent);
83 if ($deletable instanceof Book || $deletable instanceof Chapter) {
84 $countsToLoad = ['pages' => $withTrashedQuery];
85 if ($deletable instanceof Book) {
86 $countsToLoad['chapters'] = $withTrashedQuery;
88 $deletable->loadCount($countsToLoad);