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\Models\Page;
10 use BookStack\Entities\Repos\DeletionRepo;
11 use BookStack\Http\ApiController;
12 use Illuminate\Database\Eloquent\Builder;
13 use Illuminate\Database\Eloquent\Relations\HasMany;
15 class RecycleBinApiController extends ApiController
17 public function __construct()
19 $this->middleware(function ($request, $next) {
20 $this->checkPermission('settings-manage');
21 $this->checkPermission('restrictions-manage-all');
23 return $next($request);
28 * Get a top-level listing of the items in the recycle bin.
29 * The "deletable" property will reflect the main item deleted.
30 * For books and chapters, counts of child pages/chapters will
31 * be loaded within this "deletable" data.
32 * For chapters & pages, the parent item will be loaded within this "deletable" data.
33 * Requires permission to manage both system settings and permissions.
35 public function list()
37 return $this->apiListingResponse(Deletion::query()->with('deletable'), [
44 ], [$this->listFormatter(...)]);
48 * Restore a single deletion from the recycle bin.
49 * Requires permission to manage both system settings and permissions.
51 public function restore(DeletionRepo $deletionRepo, string $deletionId)
53 $restoreCount = $deletionRepo->restore(intval($deletionId));
55 return response()->json(['restore_count' => $restoreCount]);
59 * Remove a single deletion from the recycle bin.
60 * Use this endpoint carefully as it will entirely remove the underlying deleted items from the system.
61 * Requires permission to manage both system settings and permissions.
63 public function destroy(DeletionRepo $deletionRepo, string $deletionId)
65 $deleteCount = $deletionRepo->destroy(intval($deletionId));
67 return response()->json(['delete_count' => $deleteCount]);
71 * Load some related details for the deletion listing.
73 protected function listFormatter(Deletion $deletion): void
75 $deletable = $deletion->deletable;
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' => static::withTrashedQuery(...)];
85 if ($deletable instanceof Book) {
86 $countsToLoad['chapters'] = static::withTrashedQuery(...);
88 $deletable->loadCount($countsToLoad);
93 * @param Builder<Chapter|Page> $query
95 protected static function withTrashedQuery(Builder $query): void
97 $query->withTrashed();