'outside']);
//權限設定
$this->canDownload=true;
}
public function setUp(): array
{
if($this->canDownload ){
$this->showCheckBox();
}
$actions = [];
if($this->canDownload){
$actions[]=PowerGrid::exportable(fileName: $this->tableName.'-file')
->type(Exportable::TYPE_XLS, Exportable::TYPE_CSV);
}
$header = PowerGrid::header()
->withoutLoading()
->showToggleColumns();
//->showSoftDeletes()
//->showSearchInput()
$actions[]=$header;
$actions[]=PowerGrid::footer()->showPerPage()->showRecordCount();
return $actions;
}
public function datasource(): Builder
{
return Activity::with(['causer'])->latest();
}
public function relationSearch(): array
{
return [];
}
public function fields(): PowerGridFields
{
return PowerGrid::fields()
->add('id')
->add('created_at_formatted', fn (Activity $model) => Carbon::parse($model->created_at)->format('Y-m-d H:i:s'))
->add('causer_name', fn (Activity $model) => optional($model->causer)->name)
->add('subject_type_label', fn (Activity $model) => class_basename($model->subject_type))
->add('subject_type')
->add('subject_id')
->add('description')
->add('changes_str', function (Activity $model) {
$old = $model->properties['old'] ?? [];
$new = $model->properties['attributes'] ?? [];
$changes = [];
foreach ($new as $key => $newValue) {
if (in_array($key, ['updated_at', 'created_at'])) continue;
$oldValue = $old[$key] ?? '(空)';
if ($newValue != $oldValue) {
$changes[] = "{$key}: {$oldValue} → {$newValue}";
}
}
//dd(implode('
', $changes));
return implode('
', $changes);
})
;
}
public function columns(): array
{
$column=[];
$column[]=Column::make('時間', 'created_at_formatted', 'created_at')->sortable()->searchable();
$column[]=Column::make('操作者', 'causer_name')->sortable()->searchable()->bodyAttribute('whitespace-nowrap');
$column[]=Column::make('模型', 'subject_type_label')->sortable()->searchable();
$column[]=Column::make('模型 ID', 'subject_id')->sortable()->searchable();
$column[]=Column::make('動作', 'description')->sortable()->searchable();
$column[]=Column::make('變更內容', 'changes_str')->sortable(false)->searchable(false)->bodyAttribute('whitespace-normal text-sm text-gray-700');
return $column;
}
public function filters(): array
{
return [
Filter::datetimepicker('created_at'),
Filter::inputText('causer_name')->placeholder('操作者'),
Filter::inputText('subject_type_label')->placeholder('模型'),
Filter::number('subject_id'),
Filter::inputText('description')->placeholder('動作'),
];
}
}