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 BookStack\Permissions\Permission;
13 use Illuminate\Database\Eloquent\Builder;
14 use Illuminate\Database\Eloquent\Relations\HasMany;
16 class RecycleBinApiController extends ApiController
18 public function __construct()
20 $this->middleware(function ($request, $next) {
21 $this->checkPermission(Permission::SettingsManage);
22 $this->checkPermission(Permission::RestrictionsManageAll);
24 return $next($request);
29 * Get a top-level listing of the items in the recycle bin.
30 * The "deletable" property will reflect the main item deleted.
31 * For books and chapters, counts of child pages/chapters will
32 * be loaded within this "deletable" data.
33 * For chapters & pages, the parent item will be loaded within this "deletable" data.
34 * Requires permission to manage both system settings and permissions.
36 public function list()
38 return $this->apiListingResponse(Deletion::query()->with('deletable'), [
45 ], [$this->listFormatter(...)]);
49 * Restore a single deletion from the recycle bin.
50 * Requires permission to manage both system settings and permissions.
52 public function restore(DeletionRepo $deletionRepo, string $deletionId)
54 $restoreCount = $deletionRepo->restore(intval($deletionId));
56 return response()->json(['restore_count' => $restoreCount]);
60 * Remove a single deletion from the recycle bin.
61 * Use this endpoint carefully as it will entirely remove the underlying deleted items from the system.
62 * Requires permission to manage both system settings and permissions.
64 public function destroy(DeletionRepo $deletionRepo, string $deletionId)
66 $deleteCount = $deletionRepo->destroy(intval($deletionId));
68 return response()->json(['delete_count' => $deleteCount]);
72 * Load some related details for the deletion listing.
74 protected function listFormatter(Deletion $deletion): void
76 $deletable = $deletion->deletable;
78 if ($deletable instanceof BookChild) {
79 $parent = $deletable->getParent();
80 $parent->setAttribute('type', $parent->getType());
81 $deletable->setRelation('parent', $parent);
84 if ($deletable instanceof Book || $deletable instanceof Chapter) {
85 $countsToLoad = ['pages' => static::withTrashedQuery(...)];
86 if ($deletable instanceof Book) {
87 $countsToLoad['chapters'] = static::withTrashedQuery(...);
89 $deletable->loadCount($countsToLoad);
94 * @param Builder<Chapter|Page> $query
96 protected static function withTrashedQuery(Builder $query): void
98 $query->withTrashed();