2025-04-24 18:15:47 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Livewire\Admin;
|
|
|
|
|
|
|
|
use Spatie\Permission\Models\Role;
|
2025-05-13 10:59:22 +08:00
|
|
|
use Spatie\Permission\Models\Permission;
|
2025-04-24 18:15:47 +08:00
|
|
|
use Illuminate\Support\Carbon;
|
2025-05-13 10:59:22 +08:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2025-04-24 18:15:47 +08:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
use PowerComponents\LivewirePowerGrid\Button;
|
|
|
|
use PowerComponents\LivewirePowerGrid\Column;
|
|
|
|
use PowerComponents\LivewirePowerGrid\Facades\Filter;
|
|
|
|
use PowerComponents\LivewirePowerGrid\Facades\PowerGrid;
|
|
|
|
use PowerComponents\LivewirePowerGrid\PowerGridFields;
|
|
|
|
use PowerComponents\LivewirePowerGrid\PowerGridComponent;
|
2025-05-13 10:59:22 +08:00
|
|
|
use Livewire\Attributes\On;
|
|
|
|
use WireUi\Traits\WireUiActions;
|
2025-04-24 18:15:47 +08:00
|
|
|
|
|
|
|
final class RoleTable extends PowerGridComponent
|
|
|
|
{
|
2025-05-13 10:59:22 +08:00
|
|
|
use WireUiActions;
|
|
|
|
|
2025-04-24 18:15:47 +08:00
|
|
|
public string $tableName = 'role-table';
|
2025-05-13 10:59:22 +08:00
|
|
|
public bool $canCreate;
|
|
|
|
public bool $canEdit;
|
|
|
|
public bool $canDelect;
|
2025-04-24 18:15:47 +08:00
|
|
|
|
|
|
|
public function boot(): void
|
|
|
|
{
|
|
|
|
config(['livewire-powergrid.filter' => 'outside']);
|
2025-05-13 10:59:22 +08:00
|
|
|
//權限設定
|
|
|
|
$this->canCreate = Auth::user()?->can('role-edit') ?? false;
|
|
|
|
$this->canEdit = Auth::user()?->can('role-edit') ?? false;
|
|
|
|
$this->canDelect = Auth::user()?->can('role-delete') ?? false;
|
2025-04-24 18:15:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function setUp(): array
|
|
|
|
{
|
2025-05-13 10:59:22 +08:00
|
|
|
if($this->canDelect){
|
|
|
|
$this->showCheckBox();
|
|
|
|
}
|
|
|
|
$actions = [];
|
|
|
|
$header =PowerGrid::header();
|
|
|
|
if($this->canCreate){
|
|
|
|
$header->includeViewOnTop('livewire.admin.role-header');
|
|
|
|
}
|
|
|
|
$actions[]=$header;
|
|
|
|
$actions[]=PowerGrid::footer()
|
|
|
|
->showPerPage()
|
|
|
|
->showRecordCount();
|
|
|
|
return $actions;
|
2025-04-24 18:15:47 +08:00
|
|
|
}
|
2025-05-13 10:59:22 +08:00
|
|
|
public function header(): array
|
|
|
|
{
|
|
|
|
$actions = [];
|
|
|
|
if ($this->canDelect) {
|
|
|
|
$actions[]=Button::add('bulk-delete')
|
|
|
|
->slot('Bulk delete (<span x-text="window.pgBulkActions.count(\'' . $this->tableName . '\')"></span>)')
|
|
|
|
->icon('solid-trash',['id' => 'my-custom-icon-id', 'class' => 'font-bold'])
|
|
|
|
->class('inline-flex items-center gap-1 px-3 py-1 rounded ')
|
|
|
|
->dispatch('bulkDelete.' . $this->tableName, []);
|
|
|
|
}
|
|
|
|
return $actions;
|
|
|
|
}
|
2025-04-24 18:15:47 +08:00
|
|
|
|
|
|
|
public function datasource(): Builder
|
|
|
|
{
|
|
|
|
//dd(Role::with('permissions'));
|
|
|
|
return Role::with('permissions');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function relationSearch(): array
|
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function fields(): PowerGridFields
|
|
|
|
{
|
2025-05-13 10:59:22 +08:00
|
|
|
$allPermissions = Permission::pluck('name')->sort()->values();
|
2025-04-24 18:15:47 +08:00
|
|
|
return PowerGrid::fields()
|
|
|
|
->add('id')
|
|
|
|
->add('name')
|
2025-05-13 10:59:22 +08:00
|
|
|
->add('permissions_list', function (Role $model) use ($allPermissions) {
|
|
|
|
$rolePermissions = $model->permissions->pluck('name')->sort()->values();
|
|
|
|
|
|
|
|
if ($rolePermissions->count() === $allPermissions->count() && $rolePermissions->values()->all() === $allPermissions->values()->all()) {
|
|
|
|
return 'all';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rolePermissions->implode(', ');
|
|
|
|
})
|
|
|
|
->add('created_at_formatted', fn (Role $model) => Carbon::parse($model->created_at)->format('Y-m-d H:i:s'));
|
2025-04-24 18:15:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function columns(): array
|
|
|
|
{
|
2025-05-13 10:59:22 +08:00
|
|
|
$column=[];
|
|
|
|
$column[]=Column::make(__('roles.no'), 'id')->sortable()->searchable();
|
|
|
|
$column[]=Column::make(__('roles.name'), 'name')->sortable()->searchable()
|
|
|
|
->editOnClick(
|
|
|
|
hasPermission: $this->canEdit,
|
|
|
|
dataField: 'name',
|
|
|
|
fallback: 'N/A',
|
|
|
|
saveOnMouseOut: true
|
|
|
|
);
|
|
|
|
$column[]=Column::make(__('roles.permissions'), 'permissions_list');
|
|
|
|
$column[]=Column::make('Created at', 'created_at_formatted', 'created_at')->sortable();
|
|
|
|
$column[]=Column::action('Action');
|
|
|
|
return $column;
|
|
|
|
}
|
|
|
|
#[On('bulkDelete.{tableName}')]
|
|
|
|
public function bulkDelete(): void
|
|
|
|
{
|
|
|
|
if ($this->canDelect) {
|
|
|
|
$this->js('alert(window.pgBulkActions.get(\'' . $this->tableName . '\'))');
|
|
|
|
if($this->checkboxValues){
|
|
|
|
Role::destroy($this->checkboxValues);
|
|
|
|
$this->js('window.pgBulkActions.clearAll()'); // clear the count on the interface.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[On('onUpdatedEditable')]
|
|
|
|
public function onUpdatedEditable($id, $field, $value): void
|
|
|
|
{
|
|
|
|
if ($field === 'name' && $this->canEdit) {
|
|
|
|
$this->noUpdated($id,$field,$value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private function noUpdated($id,$field,$value){
|
|
|
|
$role = Role::find($id);
|
|
|
|
if ($role) {
|
|
|
|
$role->{$field} = $value;
|
|
|
|
$role->save(); // 明確觸發 saving
|
|
|
|
}
|
|
|
|
$this->notification()->send([
|
|
|
|
'icon' => 'success',
|
|
|
|
'title' => $id.'.'.__('roles.'.$field).':'.$value,
|
|
|
|
'description' => '已經寫入',
|
|
|
|
]);
|
2025-04-24 18:15:47 +08:00
|
|
|
}
|
|
|
|
public function filters(): array
|
|
|
|
{
|
|
|
|
return [
|
2025-05-13 10:59:22 +08:00
|
|
|
Filter::inputText('name')->placeholder(__('roles.name')),
|
2025-04-24 18:15:47 +08:00
|
|
|
Filter::datetimepicker('created_at'),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function actions(Role $row): array
|
|
|
|
{
|
2025-05-13 10:59:22 +08:00
|
|
|
$actions = [];
|
|
|
|
if ($this->canEdit) {
|
|
|
|
$actions[] =Button::add('edit')
|
2025-04-29 14:39:36 +08:00
|
|
|
->slot(__('roles.edit'))
|
2025-04-24 18:15:47 +08:00
|
|
|
->icon('solid-pencil-square')
|
|
|
|
->class('inline-flex items-center gap-1 px-3 py-1 rounded ')
|
2025-05-13 10:59:22 +08:00
|
|
|
->dispatchTo('admin.role-form', 'openEditRoleModal', ['id' => $row->id]);
|
|
|
|
}
|
|
|
|
if($this->canDelect){
|
|
|
|
$actions[] =Button::add('delete')
|
|
|
|
->slot(__('roles.delete'))
|
2025-04-24 18:15:47 +08:00
|
|
|
->icon('solid-trash')
|
|
|
|
->class('inline-flex items-center gap-1 px-3 py-1 rounded ')
|
2025-05-13 10:59:22 +08:00
|
|
|
->dispatchTo('admin.role-form', 'deleteRole', ['id' => $row->id]);
|
|
|
|
}
|
|
|
|
return $actions;
|
2025-04-24 18:15:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
public function actionRules($row): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
// Hide button edit for ID 1
|
|
|
|
Rule::button('edit')
|
|
|
|
->when(fn($row) => $row->id === 1)
|
|
|
|
->hide(),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
}
|