BookStack/app/Entities/Controllers/RecycleBinController.php

129 lines
3.7 KiB
PHP
Raw Normal View History

2021-06-26 11:23:15 -04:00
<?php
2023-05-17 12:56:55 -04:00
namespace BookStack\Entities\Controllers;
2023-05-17 12:56:55 -04:00
use BookStack\Activity\ActivityType;
use BookStack\Entities\Models\Deletion;
use BookStack\Entities\Models\Entity;
use BookStack\Entities\Repos\DeletionRepo;
use BookStack\Entities\Tools\TrashCan;
use BookStack\Http\Controller;
class RecycleBinController extends Controller
{
protected string $recycleBinBaseUrl = '/settings/recycle-bin';
/**
* On each request to a method of this controller check permissions
* using a middleware closure.
*/
public function __construct()
{
$this->middleware(function ($request, $next) {
$this->checkPermission('settings-manage');
$this->checkPermission('restrictions-manage-all');
2021-06-26 11:23:15 -04:00
return $next($request);
});
}
/**
* Show the top-level listing for the recycle bin.
*/
public function index()
{
$deletions = Deletion::query()->with(['deletable', 'deleter'])->paginate(10);
$this->setPageTitle(trans('settings.recycle_bin'));
2021-06-26 11:23:15 -04:00
return view('settings.recycle-bin.index', [
'deletions' => $deletions,
]);
}
/**
* Show the page to confirm a restore of the deletion of the given id.
*/
public function showRestore(string $id)
{
/** @var Deletion $deletion */
$deletion = Deletion::query()->findOrFail($id);
// Walk the parent chain to find any cascading parent deletions
$currentDeletable = $deletion->deletable;
$searching = true;
while ($searching && $currentDeletable instanceof Entity) {
$parent = $currentDeletable->getParent();
if ($parent && $parent->trashed()) {
$currentDeletable = $parent;
} else {
$searching = false;
}
}
/** @var ?Deletion $parentDeletion */
$parentDeletion = ($currentDeletable === $deletion->deletable) ? null : $currentDeletable->deletions()->first();
return view('settings.recycle-bin.restore', [
2021-06-26 11:23:15 -04:00
'deletion' => $deletion,
'parentDeletion' => $parentDeletion,
]);
}
/**
* Restore the element attached to the given deletion.
2021-06-26 11:23:15 -04:00
*
* @throws \Exception
*/
public function restore(DeletionRepo $deletionRepo, string $id)
{
$restoreCount = $deletionRepo->restore((int) $id);
$this->showSuccessNotification(trans('settings.recycle_bin_restore_notification', ['count' => $restoreCount]));
2021-06-26 11:23:15 -04:00
return redirect($this->recycleBinBaseUrl);
}
/**
* Show the page to confirm a Permanent deletion of the element attached to the deletion of the given id.
*/
public function showDestroy(string $id)
{
/** @var Deletion $deletion */
$deletion = Deletion::query()->findOrFail($id);
return view('settings.recycle-bin.destroy', [
'deletion' => $deletion,
]);
}
/**
* Permanently delete the content associated with the given deletion.
2021-06-26 11:23:15 -04:00
*
* @throws \Exception
*/
public function destroy(DeletionRepo $deletionRepo, string $id)
{
$deleteCount = $deletionRepo->destroy((int) $id);
$this->showSuccessNotification(trans('settings.recycle_bin_destroy_notification', ['count' => $deleteCount]));
2021-06-26 11:23:15 -04:00
return redirect($this->recycleBinBaseUrl);
}
/**
* Empty out the recycle bin.
2021-06-26 11:23:15 -04:00
*
* @throws \Exception
*/
public function empty()
{
$deleteCount = (new TrashCan())->empty();
$this->logActivity(ActivityType::RECYCLE_BIN_EMPTY);
$this->showSuccessNotification(trans('settings.recycle_bin_destroy_notification', ['count' => $deleteCount]));
2021-06-26 11:23:15 -04:00
return redirect($this->recycleBinBaseUrl);
}
}