98 lines
3.2 KiB
PHP
98 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Admin;
|
|
|
|
use App\Models\ActivityLog;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use PowerComponents\LivewirePowerGrid\Column;
|
|
use PowerComponents\LivewirePowerGrid\Facades\Filter;
|
|
use PowerComponents\LivewirePowerGrid\Facades\PowerGrid;
|
|
use PowerComponents\LivewirePowerGrid\PowerGridFields;
|
|
use PowerComponents\LivewirePowerGrid\PowerGridComponent;
|
|
use Spatie\Activitylog\Models\Activity;
|
|
|
|
|
|
final class ActivityLogTable extends PowerGridComponent
|
|
{
|
|
public string $tableName = 'activity-log-table';
|
|
|
|
public function setUp(): array
|
|
{
|
|
$this->showCheckBox();
|
|
|
|
return [
|
|
PowerGrid::header()
|
|
->showSearchInput(),
|
|
PowerGrid::footer()
|
|
->showPerPage()
|
|
->showRecordCount(),
|
|
];
|
|
}
|
|
|
|
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('log_name')
|
|
->add('changes', 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[] = "<strong>{$key}</strong>: {$oldValue} → {$newValue}";
|
|
}
|
|
}
|
|
|
|
return implode('<br>', $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')->sortable(false)->searchable(false)->bodyAttribute('whitespace-normal text-sm text-gray-700');
|
|
$column[]=Column::make('Log 名稱', 'log_name')->sortable()->searchable();
|
|
return $column;
|
|
}
|
|
|
|
public function filters(): array
|
|
{
|
|
return [
|
|
Filter::datetimepicker('created_at'),
|
|
];
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|