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.
167 lines
3.9 KiB
167 lines
3.9 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;
|
|
|
|
/**
|
|
* Number of items per page for pagination.
|
|
*/
|
|
public int $perPage = 10;
|
|
|
|
/**
|
|
* Pagination theme (bootstrap or tailwind).
|
|
*/
|
|
protected string $paginationTheme = 'bootstrap';
|
|
|
|
/**
|
|
* Current UI mode: 'index' (list view) or 'form' (create/edit view).
|
|
*/
|
|
public string $mode = 'index';
|
|
|
|
/**
|
|
* ID of the permission currently being edited.
|
|
* Null when creating a new permission.
|
|
*/
|
|
public ?int $editingId = null;
|
|
|
|
/**
|
|
* Current search query string.
|
|
*/
|
|
public string $search = '';
|
|
|
|
/**
|
|
* Permission name used in the create/edit form.
|
|
*/
|
|
public string $name = '';
|
|
|
|
/**
|
|
* Handle internal Livewire event to switch back to the index (list) view.
|
|
*/
|
|
#[On('permissionSaved')]
|
|
public function showIndex(): void
|
|
{
|
|
$this->mode = 'index';
|
|
$this->resetPage();
|
|
$this->name = '';
|
|
$this->editingId = null;
|
|
}
|
|
|
|
/**
|
|
* Reset pagination whenever the search term changes.
|
|
*
|
|
* @param string $value
|
|
* @return void
|
|
*/
|
|
public function updatedSearch(string $value): void
|
|
{
|
|
$this->resetPage();
|
|
}
|
|
|
|
/**
|
|
* Switch to the form view for creating or editing a permission.
|
|
*
|
|
* @param int|null $id Permission ID to edit, or null to create new.
|
|
* @return void
|
|
*/
|
|
public function showForm(?int $id = null): void
|
|
{
|
|
$this->mode = 'form';
|
|
$this->editingId = $id;
|
|
$this->name = $id
|
|
? Permission::findOrFail($id)->name
|
|
: '';
|
|
}
|
|
|
|
/**
|
|
* Delete a permission by its ID.
|
|
*
|
|
* @param int $id
|
|
* @return void
|
|
*/
|
|
public function delete(int $id): void
|
|
{
|
|
Permission::findOrFail($id)->delete();
|
|
session()->flash('message', 'Permission deleted.');
|
|
$this->resetPage();
|
|
}
|
|
|
|
/**
|
|
* Validate and save the permission (create or update).
|
|
*
|
|
* @return void
|
|
*/
|
|
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.');
|
|
|
|
// Trigger internal Livewire event to return to the list view
|
|
$this->dispatch('permissionSaved');
|
|
}
|
|
|
|
/**
|
|
* Compute dynamic browser title based on current mode and editing state.
|
|
*
|
|
* @return string
|
|
*/
|
|
protected function computeTitle(): string
|
|
{
|
|
if ($this->mode === 'index') {
|
|
return 'Permissions';
|
|
}
|
|
|
|
if ($this->mode === 'form' && $this->editingId) {
|
|
return "Edit Permission: {$this->name}";
|
|
}
|
|
|
|
return 'Create Permission';
|
|
}
|
|
|
|
/**
|
|
* Expose computed title as a Livewire property.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getTitleProperty(): string
|
|
{
|
|
return $this->computeTitle();
|
|
}
|
|
|
|
/**
|
|
* Render the component view with paginated permissions and dynamic title.
|
|
*
|
|
* @return \Illuminate\View\View
|
|
*/
|
|
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,
|
|
// Blade can now access {{ $title }} via the computed property
|
|
'title' => $this->title,
|
|
]);
|
|
}
|
|
}
|
|
|