Skip to content

Commit

Permalink
Activity Resource
Browse files Browse the repository at this point in the history
  • Loading branch information
fzldn committed Sep 17, 2024
1 parent 4517296 commit ededf40
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 0 deletions.
54 changes: 54 additions & 0 deletions app/Filament/Resources/ActivityResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace App\Filament\Resources;

use App\Filament\Resources\ActivityResource\Pages;
use App\Filament\Resources\ActivityResource\RelationManagers;
use App\Models\Activity;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Columns\Layout\Stack;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;

class ActivityResource extends Resource
{
protected static ?string $model = Activity::class;

protected static ?string $navigationIcon = 'heroicon-o-bars-arrow-down';

protected static ?int $navigationSort = 2;

public static function getNavigationGroup(): ?string
{
return __('Access Management');
}

public static function table(Table $table): Table
{
return $table
->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('/'),
];
}
}
19 changes: 19 additions & 0 deletions app/Filament/Resources/ActivityResource/Pages/ListActivities.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Filament\Resources\ActivityResource\Pages;

use App\Filament\Resources\ActivityResource;
use Filament\Actions;
use Filament\Resources\Pages\ListRecords;
use Illuminate\Contracts\Pagination\Paginator;
use Illuminate\Database\Eloquent\Builder;

class ListActivities extends ListRecords
{
protected static string $resource = ActivityResource::class;

protected function paginateTableQuery(Builder $query): Paginator
{
return $query->simplePaginate(($this->getTableRecordsPerPage() === 'all') ? $query->count() : $this->getTableRecordsPerPage());
}
}
49 changes: 49 additions & 0 deletions app/Models/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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 <strong>%s</strong> was <strong>%s</strong> to %s <strong>%s</strong> 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 ? "<strong>{$this->causer->name}</strong>" : '<em>' . __('System') . '</em>',
));
}

return new HtmlString(sprintf(
'%s <strong>%s</strong> was <strong>%s</strong> by %s',
str(class_basename($this->subject))->headline(),
$this->subject->name,
$this->event,
$this->causer?->name ? "<strong>{$this->causer->name}</strong>" : '<em>' . __('System') . '</em>',
));
},
);
}
}

0 comments on commit ededf40

Please sign in to comment.