You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
137 lines
3.1 KiB
137 lines
3.1 KiB
<?php
|
|
|
|
namespace App\Components\Permission;
|
|
|
|
use Livewire\Component;
|
|
use Livewire\Attributes\On;
|
|
use Livewire\WithPagination;
|
|
use Spatie\Permission\Models\Permission;
|
|
|
|
class Manager extends Component
|
|
{
|
|
use WithPagination;
|
|
|
|
/**
|
|
* Các tùy chọn số bản ghi mỗi trang.
|
|
*
|
|
* @var int[]
|
|
*/
|
|
public array $perPageOptions = [5, 10, 25, 50, 100];
|
|
|
|
/**
|
|
* Số bản ghi mỗi trang (mặc định).
|
|
*/
|
|
public int $perPage = 10;
|
|
|
|
/**
|
|
* Giao diện phân trang (bootstrap hoặc tailwind).
|
|
*/
|
|
protected string $paginationTheme = 'bootstrap';
|
|
|
|
/**
|
|
* Chế độ UI: 'index' (danh sách) hoặc 'form' (tạo/sửa).
|
|
*/
|
|
public string $mode = 'index';
|
|
|
|
/**
|
|
* ID của permission đang sửa (null khi tạo mới).
|
|
*/
|
|
public ?int $editingId = null;
|
|
|
|
/**
|
|
* Chuỗi tìm kiếm hiện tại.
|
|
*/
|
|
public string $search = '';
|
|
|
|
/**
|
|
* Tên permission dùng trong form.
|
|
*/
|
|
public string $name = '';
|
|
|
|
/**
|
|
* Khi thay đổi số bản ghi mỗi trang, reset về trang 1.
|
|
*/
|
|
public function updatedPerPage(int $value): void
|
|
{
|
|
$this->resetPage();
|
|
}
|
|
|
|
#[On('permissionSaved')]
|
|
public function showIndex(): void
|
|
{
|
|
$this->mode = 'index';
|
|
$this->resetPage();
|
|
$this->name = '';
|
|
$this->editingId = null;
|
|
}
|
|
|
|
public function updatedSearch(string $value): void
|
|
{
|
|
$this->resetPage();
|
|
}
|
|
|
|
public function showForm(?int $id = null): void
|
|
{
|
|
$this->mode = 'form';
|
|
$this->editingId = $id;
|
|
$this->name = $id
|
|
? Permission::findOrFail($id)->name
|
|
: '';
|
|
}
|
|
|
|
public function delete(int $id): void
|
|
{
|
|
Permission::findOrFail($id)->delete();
|
|
session()->flash('message', 'Permission deleted.');
|
|
$this->resetPage();
|
|
}
|
|
|
|
public function save(): void
|
|
{
|
|
$uniqueRule = 'unique:permissions,name'
|
|
. ($this->editingId ? ',' . $this->editingId : '');
|
|
|
|
$this->validate([
|
|
'name' => "required|string|{$uniqueRule}",
|
|
]);
|
|
|
|
Permission::updateOrCreate(
|
|
['id' => $this->editingId],
|
|
['name' => $this->name]
|
|
);
|
|
|
|
session()->flash('message', 'Permission saved.');
|
|
$this->dispatch('permissionSaved');
|
|
}
|
|
|
|
protected function computeTitle(): string
|
|
{
|
|
if ($this->mode === 'index') {
|
|
return 'Permissions';
|
|
}
|
|
|
|
if ($this->mode === 'form' && $this->editingId) {
|
|
return "Edit Permission: {$this->name}";
|
|
}
|
|
|
|
return 'Create Permission';
|
|
}
|
|
|
|
public function getTitleProperty(): string
|
|
{
|
|
return $this->computeTitle();
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
$permissions = Permission::query()
|
|
->when($this->search, fn($q) => $q->where('name', 'like', "%{$this->search}%"))
|
|
->orderByDesc('id')
|
|
->paginate($this->perPage);
|
|
|
|
return view('components.permission.manager', [
|
|
'permissions' => $permissions,
|
|
'title' => $this->title,
|
|
]);
|
|
}
|
|
}
|
|
|