2022-04-06 16:57:18 -04:00
|
|
|
<?php
|
|
|
|
|
2023-05-17 12:56:55 -04:00
|
|
|
namespace BookStack\Entities\Controllers;
|
2022-04-06 16:57:18 -04:00
|
|
|
|
2022-04-25 12:54:59 -04:00
|
|
|
use BookStack\Entities\Models\Book;
|
|
|
|
use BookStack\Entities\Models\BookChild;
|
|
|
|
use BookStack\Entities\Models\Chapter;
|
2022-04-06 16:57:18 -04:00
|
|
|
use BookStack\Entities\Models\Deletion;
|
|
|
|
use BookStack\Entities\Repos\DeletionRepo;
|
2023-05-18 15:53:39 -04:00
|
|
|
use BookStack\Http\ApiController;
|
2022-04-20 16:58:16 -04:00
|
|
|
use Closure;
|
2022-04-25 12:54:59 -04:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2022-04-06 16:57:18 -04:00
|
|
|
|
|
|
|
class RecycleBinApiController extends ApiController
|
|
|
|
{
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->middleware(function ($request, $next) {
|
|
|
|
$this->checkPermission('settings-manage');
|
|
|
|
$this->checkPermission('restrictions-manage-all');
|
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-24 04:16:45 -04:00
|
|
|
/**
|
|
|
|
* Get a top-level listing of the items in the recycle bin.
|
2022-04-25 12:54:59 -04:00
|
|
|
* The "deletable" property will reflect the main item deleted.
|
|
|
|
* For books and chapters, counts of child pages/chapters will
|
|
|
|
* be loaded within this "deletable" data.
|
|
|
|
* For chapters & pages, the parent item will be loaded within this "deletable" data.
|
|
|
|
* Requires permission to manage both system settings and permissions.
|
2022-04-24 04:16:45 -04:00
|
|
|
*/
|
2022-04-06 16:57:18 -04:00
|
|
|
public function list()
|
|
|
|
{
|
2022-04-24 04:16:45 -04:00
|
|
|
return $this->apiListingResponse(Deletion::query()->with('deletable'), [
|
2022-04-20 16:58:16 -04:00
|
|
|
'id',
|
|
|
|
'deleted_by',
|
2022-04-06 16:57:18 -04:00
|
|
|
'created_at',
|
|
|
|
'updated_at',
|
|
|
|
'deletable_type',
|
2022-04-20 16:58:16 -04:00
|
|
|
'deletable_id',
|
|
|
|
], [Closure::fromCallable([$this, 'listFormatter'])]);
|
2022-04-06 16:57:18 -04:00
|
|
|
}
|
|
|
|
|
2022-04-24 04:16:45 -04:00
|
|
|
/**
|
|
|
|
* Restore a single deletion from the recycle bin.
|
2022-04-25 12:54:59 -04:00
|
|
|
* Requires permission to manage both system settings and permissions.
|
2022-04-24 04:16:45 -04:00
|
|
|
*/
|
2022-04-25 12:54:59 -04:00
|
|
|
public function restore(DeletionRepo $deletionRepo, string $deletionId)
|
2022-04-06 16:57:18 -04:00
|
|
|
{
|
2022-04-25 12:54:59 -04:00
|
|
|
$restoreCount = $deletionRepo->restore(intval($deletionId));
|
2022-04-20 16:58:16 -04:00
|
|
|
|
2022-04-06 16:57:18 -04:00
|
|
|
return response()->json(['restore_count' => $restoreCount]);
|
|
|
|
}
|
|
|
|
|
2022-04-24 04:16:45 -04:00
|
|
|
/**
|
|
|
|
* Remove a single deletion from the recycle bin.
|
|
|
|
* Use this endpoint carefully as it will entirely remove the underlying deleted items from the system.
|
2022-04-25 12:54:59 -04:00
|
|
|
* Requires permission to manage both system settings and permissions.
|
2022-04-24 04:16:45 -04:00
|
|
|
*/
|
2022-04-25 12:54:59 -04:00
|
|
|
public function destroy(DeletionRepo $deletionRepo, string $deletionId)
|
2022-04-06 16:57:18 -04:00
|
|
|
{
|
2022-04-25 12:54:59 -04:00
|
|
|
$deleteCount = $deletionRepo->destroy(intval($deletionId));
|
2022-04-20 16:58:16 -04:00
|
|
|
|
2022-04-06 16:57:18 -04:00
|
|
|
return response()->json(['delete_count' => $deleteCount]);
|
|
|
|
}
|
2022-04-20 16:58:16 -04:00
|
|
|
|
2022-04-25 12:54:59 -04:00
|
|
|
/**
|
|
|
|
* Load some related details for the deletion listing.
|
|
|
|
*/
|
2022-04-20 16:58:16 -04:00
|
|
|
protected function listFormatter(Deletion $deletion)
|
|
|
|
{
|
|
|
|
$deletable = $deletion->deletable;
|
2022-04-25 13:42:31 -04:00
|
|
|
$withTrashedQuery = fn (Builder $query) => $query->withTrashed();
|
2022-04-20 16:58:16 -04:00
|
|
|
|
2022-04-25 12:54:59 -04:00
|
|
|
if ($deletable instanceof BookChild) {
|
|
|
|
$parent = $deletable->getParent();
|
|
|
|
$parent->setAttribute('type', $parent->getType());
|
|
|
|
$deletable->setRelation('parent', $parent);
|
2022-04-20 16:58:16 -04:00
|
|
|
}
|
|
|
|
|
2022-04-25 12:54:59 -04:00
|
|
|
if ($deletable instanceof Book || $deletable instanceof Chapter) {
|
|
|
|
$countsToLoad = ['pages' => $withTrashedQuery];
|
|
|
|
if ($deletable instanceof Book) {
|
|
|
|
$countsToLoad['chapters'] = $withTrashedQuery;
|
|
|
|
}
|
|
|
|
$deletable->loadCount($countsToLoad);
|
2022-04-20 16:58:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|