Корзина, массовые действия в списках и история записи в карточке
This commit is contained in:
1 parent
20f3d79552
commit
00bba5abfa
23 files changed
+936
No files matched your search
@@ -0,0 +1,144 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controllers\Admin;
|
||||
|
||||
use Rsgrinko\Proton\Access\AccessDenied;
|
||||
use Rsgrinko\Proton\Access\Viewer;
|
||||
use Rsgrinko\Proton\Database\Model\Model;
|
||||
use Rsgrinko\Proton\Http\Controller;
|
||||
use Rsgrinko\Proton\Http\Request;
|
||||
use Rsgrinko\Proton\Http\Response;
|
||||
use Rsgrinko\Proton\Support\Audit;
|
||||
use Rsgrinko\Proton\Support\Trash;
|
||||
|
||||
/**
|
||||
* Корзина: удалённое, но ещё живое.
|
||||
*
|
||||
* Разделы объявляет реестр (`config/trash.php`), поэтому свой раздел появляется
|
||||
* здесь сам, без правки контроллера. Каждый закрыт своим правом: право видеть
|
||||
* пользователей и право видеть вебхуки — разные вещи и в корзине тоже.
|
||||
*/
|
||||
final class TrashController extends Controller
|
||||
{
|
||||
public function index(Request $request, Viewer $viewer): Response
|
||||
{
|
||||
$kinds = $this->allowed($viewer);
|
||||
|
||||
if ($kinds === []) {
|
||||
throw new AccessDenied('Корзина пуста для вашей роли');
|
||||
}
|
||||
|
||||
$key = (string) $request->query('kind', '');
|
||||
$key = isset($kinds[$key]) ? $key : (string) array_key_first($kinds);
|
||||
$kind = $kinds[$key];
|
||||
|
||||
$page = Trash::query($key)
|
||||
?->orderBy(Model::DELETED_AT, 'desc')
|
||||
->paginate($this->page($request), $this->perPage());
|
||||
|
||||
return $this->view('admin/trash', [
|
||||
'active' => 'trash',
|
||||
'kinds' => $kinds,
|
||||
'counts' => $this->counts($kinds),
|
||||
'kind' => $key,
|
||||
'label' => $kind['label'],
|
||||
'title' => $kind['title'],
|
||||
'page' => $page ?? ['items' => [], 'total' => 0, 'page' => 1, 'pages' => 1, 'per_page' => 0],
|
||||
], 'Корзина');
|
||||
}
|
||||
|
||||
/**
|
||||
* Вернуть запись из корзины.
|
||||
*/
|
||||
public function restore(Request $request, Viewer $viewer): Response
|
||||
{
|
||||
[$kind, $record] = $this->find($request, $viewer);
|
||||
|
||||
$record->restore();
|
||||
|
||||
Audit::action(Trash::kind($kind)['entity'] ?? $kind, $record->id(), 'запись возвращена из корзины');
|
||||
|
||||
$this->flash('Запись возвращена');
|
||||
|
||||
return $this->redirect('admin.trash', ['kind' => $kind]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Добить окончательно. Обратной дороги нет, поэтому отдельное действие
|
||||
* и отдельная запись в журнале.
|
||||
*/
|
||||
public function destroy(Request $request, Viewer $viewer): Response
|
||||
{
|
||||
[$kind, $record] = $this->find($request, $viewer);
|
||||
|
||||
$record->forceDelete();
|
||||
|
||||
Audit::deleted(Trash::kind($kind)['entity'] ?? $kind, $record->id(), 'запись удалена окончательно');
|
||||
|
||||
$this->flash('Запись удалена окончательно');
|
||||
|
||||
return $this->redirect('admin.trash', ['kind' => $kind]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Раздел и запись из запроса — с проверкой права на этот раздел.
|
||||
*
|
||||
* @return array{0: string, 1: Model}
|
||||
*/
|
||||
private function find(Request $request, Viewer $viewer): array
|
||||
{
|
||||
$key = (string) $request->input('kind', '');
|
||||
$kinds = $this->allowed($viewer);
|
||||
|
||||
if (!isset($kinds[$key])) {
|
||||
throw new AccessDenied('Этот раздел корзины вам не доступен');
|
||||
}
|
||||
|
||||
/** @var class-string<Model> $model */
|
||||
$model = $kinds[$key]['model'];
|
||||
|
||||
/** @var Model|null $record */
|
||||
$record = $model::query()->onlyTrashed()->where('id', (int) $request->input('id', 0))->first();
|
||||
|
||||
/** @var Model $found */
|
||||
$found = $this->require($record, 'admin.trash', 'Записи в корзине нет');
|
||||
|
||||
return [$key, $found];
|
||||
}
|
||||
|
||||
/**
|
||||
* Разделы, доступные этому человеку.
|
||||
*
|
||||
* @return array<string, array{model: class-string<Model>, label: string, title: callable, permission: string}>
|
||||
*/
|
||||
private function allowed(Viewer $viewer): array
|
||||
{
|
||||
$allowed = [];
|
||||
|
||||
foreach (Trash::kinds() as $key => $kind) {
|
||||
if ($kind['permission'] === '' || $viewer->can($kind['permission'])) {
|
||||
$allowed[$key] = $kind;
|
||||
}
|
||||
}
|
||||
|
||||
return $allowed;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, array<string, mixed>> $kinds
|
||||
*
|
||||
* @return array<string, int>
|
||||
*/
|
||||
private function counts(array $kinds): array
|
||||
{
|
||||
$counts = [];
|
||||
|
||||
foreach (array_keys($kinds) as $key) {
|
||||
$counts[$key] = Trash::query($key)?->count() ?? 0;
|
||||
}
|
||||
|
||||
return $counts;
|
||||
}
|
||||
}
|
||||
@@ -230,6 +230,76 @@ final class UsersController extends Controller
|
||||
return $this->redirect('admin.users');
|
||||
}
|
||||
|
||||
/**
|
||||
* Массовое действие над отмеченными: включить, выключить, удалить.
|
||||
*
|
||||
* Себя в список не берём ни при каком действии: выключить или удалить
|
||||
* самого себя — верный способ остаться без панели.
|
||||
*/
|
||||
public function bulk(Request $request, User $user): Response
|
||||
{
|
||||
$data = $this->validate($request, [
|
||||
'action' => 'required|in:enable,disable,delete',
|
||||
], ['action' => 'Действие']);
|
||||
|
||||
$ids = array_values(array_filter(array_map('intval', (array) $request->input('ids', []))));
|
||||
$ids = array_values(array_diff($ids, [$user->id()]));
|
||||
|
||||
if ($ids === []) {
|
||||
$this->flash('Не отмечено ни одной записи (себя массовые действия не трогают)', 'error');
|
||||
|
||||
return $this->redirect('admin.users');
|
||||
}
|
||||
|
||||
$action = (string) $data['action'];
|
||||
$done = 0;
|
||||
|
||||
/** @var User $target */
|
||||
foreach (User::query()->whereIn('id', $ids)->get() as $target) {
|
||||
// Последнего активного не выключаем и не удаляем: войти станет некому
|
||||
if ($action !== 'enable' && $target->isActive() && User::query()->where('active', 1)->count() <= 1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
match ($action) {
|
||||
'enable' => $target->forceFill(['active' => 1])->save(),
|
||||
'disable' => $this->disable($target),
|
||||
default => $this->remove($target),
|
||||
};
|
||||
|
||||
$done++;
|
||||
}
|
||||
|
||||
$labels = ['enable' => 'включено', 'disable' => 'выключено', 'delete' => 'удалено'];
|
||||
|
||||
Audit::action('user', 0, 'массовое действие: ' . $labels[$action] . ' ' . $done);
|
||||
|
||||
$this->flash(
|
||||
$done > 0 ? 'Готово, ' . $labels[$action] . ': ' . $done : 'Ничего не изменилось',
|
||||
$done > 0 ? 'ok' : 'error'
|
||||
);
|
||||
|
||||
return $this->redirect('admin.users');
|
||||
}
|
||||
|
||||
/**
|
||||
* Выключить: по старой куке отключённый доходить до страниц не должен.
|
||||
*/
|
||||
private function disable(User $target): void
|
||||
{
|
||||
$target->forceFill(['active' => 0])->save();
|
||||
$target->logoutEverywhere();
|
||||
}
|
||||
|
||||
/**
|
||||
* Удалить мягко — запись уходит в корзину.
|
||||
*/
|
||||
private function remove(User $target): void
|
||||
{
|
||||
$target->logoutEverywhere();
|
||||
$target->delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* Роли для списка: id => название.
|
||||
*
|
||||
|
||||
Reference in new issue
Block a user