From ededf40c7fcd11d0ba368df17ca70e00ded55876 Mon Sep 17 00:00:00 2001 From: Faizal Nugraha Date: Tue, 17 Sep 2024 15:53:31 +0700 Subject: [PATCH] Activity Resource --- app/Filament/Resources/ActivityResource.php | 54 +++++++++++++++++++ .../ActivityResource/Pages/ListActivities.php | 19 +++++++ app/Models/Activity.php | 49 +++++++++++++++++ 3 files changed, 122 insertions(+) create mode 100644 app/Filament/Resources/ActivityResource.php create mode 100644 app/Filament/Resources/ActivityResource/Pages/ListActivities.php diff --git a/app/Filament/Resources/ActivityResource.php b/app/Filament/Resources/ActivityResource.php new file mode 100644 index 0000000..0abbf1c --- /dev/null +++ b/app/Filament/Resources/ActivityResource.php @@ -0,0 +1,54 @@ +heading(__('Activity Logs')) + ->description(__('A log of all activity in the system.')) + ->columns([ + Stack::make([ + Tables\Columns\TextColumn::make('description_formatted'), + Tables\Columns\TextColumn::make('created_at') + ->dateTime() + ->color('warning'), + ]), + ]) + ->filters([ + // + ]); + } + + public static function getPages(): array + { + return [ + 'index' => Pages\ListActivities::route('/'), + ]; + } +} diff --git a/app/Filament/Resources/ActivityResource/Pages/ListActivities.php b/app/Filament/Resources/ActivityResource/Pages/ListActivities.php new file mode 100644 index 0000000..ae94dac --- /dev/null +++ b/app/Filament/Resources/ActivityResource/Pages/ListActivities.php @@ -0,0 +1,19 @@ +simplePaginate(($this->getTableRecordsPerPage() === 'all') ? $query->count() : $this->getTableRecordsPerPage()); + } +} diff --git a/app/Models/Activity.php b/app/Models/Activity.php index fec9283..aa63865 100644 --- a/app/Models/Activity.php +++ b/app/Models/Activity.php @@ -5,7 +5,10 @@ use App\Models\Scopes\OrderByIdDesc; use Illuminate\Database\Eloquent\Attributes\ScopedBy; use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\Pivot; +use Illuminate\Support\HtmlString; use Spatie\Activitylog\Models\Activity as ModelsActivity; #[ScopedBy(OrderByIdDesc::class)] @@ -21,4 +24,50 @@ public function scopeForSubject(Builder $query, Model $subject): Builder } }); } + + /** + * Get the activity's description formatted. + */ + protected function descriptionFormatted(): Attribute + { + return Attribute::make( + get: function () { + if ($this->subject instanceof Pivot) { + return new HtmlString(sprintf( + '%s %s was %s to %s %s by %s', + match ($this->subject_type) { + RoleUser::class => 'Role', + PermissionRole::class => 'Permission', + }, + match ($this->subject_type) { + RoleUser::class => $this->subject->role->name, + PermissionRole::class => $this->subject->permission->label, + }, + match ($this->event) { + 'created' => 'attached', + 'deleted' => 'detached', + default => $this->event, + }, + match ($this->subject_type) { + RoleUser::class => 'User', + PermissionRole::class => 'Role', + }, + match ($this->subject_type) { + RoleUser::class => $this->subject->user->name, + PermissionRole::class => $this->subject->role->name, + }, + $this->causer?->name ? "{$this->causer->name}" : '' . __('System') . '', + )); + } + + return new HtmlString(sprintf( + '%s %s was %s by %s', + str(class_basename($this->subject))->headline(), + $this->subject->name, + $this->event, + $this->causer?->name ? "{$this->causer->name}" : '' . __('System') . '', + )); + }, + ); + } }