3 namespace BookStack\Http\Controllers\Api;
5 use BookStack\Entities\Models\Deletion;
6 use BookStack\Entities\Repos\DeletionRepo;
9 class RecycleBinApiController extends ApiController
11 protected $fieldsToExpose = [
12 'id', 'deleted_by', 'created_at', 'updated_at', 'deletable_type', 'deletable_id',
15 public function __construct()
17 $this->middleware(function ($request, $next) {
18 $this->checkPermission('settings-manage');
19 $this->checkPermission('restrictions-manage-all');
21 return $next($request);
26 * Get a top-level listing of the items in the recycle bin.
27 * Requires the permission to manage settings and restrictions.
29 public function list()
31 return $this->apiListingResponse(Deletion::query()->with('deletable'), [
38 ], [Closure::fromCallable([$this, 'listFormatter'])]);
42 * Restore a single deletion from the recycle bin.
43 * You must provide the deletion id, not the id of the corresponding deleted item.
45 public function restore(DeletionRepo $deletionRepo, string $id)
47 $restoreCount = $deletionRepo->restore((int) $id);
49 return response()->json(['restore_count' => $restoreCount]);
53 * Remove a single deletion from the recycle bin.
54 * Use this endpoint carefully as it will entirely remove the underlying deleted items from the system.
55 * You must provide the deletion id, not the id of the corresponding deleted item.
57 public function destroy(DeletionRepo $deletionRepo, string $id)
59 $deleteCount = $deletionRepo->destroy((int) $id);
61 return response()->json(['delete_count' => $deleteCount]);
64 protected function listFormatter(Deletion $deletion)
66 $deletion->makeVisible($this->fieldsToExpose);
67 $deletion->makeHidden('deletable');
69 $deletable = $deletion->deletable;
70 $isBook = $deletion->deletable_type === "BookStack\Book";
75 $chapterCount = $deletable->chapters()->withTrashed()->count();
76 $children['BookStack\Chapter'] = $chapterCount;
79 if ($isBook || $deletion->deletable_type === "BookStack\Chapter") {
80 $pageCount = $deletable->pages()->withTrashed()->count();
81 $children['BookStack\Page'] = $pageCount;
84 $parentEntity = $deletable->getParent();
88 $parent['type'] = $parentEntity->getMorphClass();
89 $parent['id'] = $parentEntity->getKey();
92 $deletion->setAttribute('parent', $parent);
93 $deletion->setAttribute('children', $children);