Skip to content

Commit

Permalink
model activity widget
Browse files Browse the repository at this point in the history
  • Loading branch information
fzldn committed Sep 20, 2024
1 parent ba488bb commit ee36ac6
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 14 deletions.
10 changes: 4 additions & 6 deletions app/Filament/Resources/ActivityResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
use App\Models\User;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Infolists;
use Filament\Resources\Resource;
use Filament\Support\Enums\FontFamily;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
Expand Down Expand Up @@ -64,12 +62,12 @@ public static function table(Table $table): Table
)
->searchable(),
Tables\Filters\Filter::make('from')
->form([Forms\Components\DatePicker::make('from')])
->form([Forms\Components\DateTimePicker::make('from')])
->query(function (Builder $query, array $data): Builder {
return $query
->when(
$data['from'],
fn(Builder $query, $date): Builder => $query->whereDate('created_at', '>=', $date),
fn(Builder $query, $date): Builder => $query->where('created_at', '>=', $date),
);
})
->indicateUsing(function ($data) {
Expand All @@ -80,12 +78,12 @@ public static function table(Table $table): Table
return __('From: :date', ['date' => $data['from']]);
}),
Tables\Filters\Filter::make('to')
->form([Forms\Components\DatePicker::make('to')])
->form([Forms\Components\DateTimePicker::make('to')])
->query(function (Builder $query, array $data): Builder {
return $query
->when(
$data['to'],
fn(Builder $query, $date): Builder => $query->whereDate('created_at', '<=', $date),
fn(Builder $query, $date): Builder => $query->where('created_at', '<=', $date),
);
})
->indicateUsing(function ($data) {
Expand Down
68 changes: 68 additions & 0 deletions app/Filament/Resources/ActivityResource/Widgets/ModelActivity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace App\Filament\Resources\ActivityResource\Widgets;

use App\Models\Activity;
use Filament\Tables;
use Filament\Tables\Table;
use Filament\Widgets\TableWidget as BaseWidget;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Pivot;

class ModelActivity extends BaseWidget
{
public ?Model $causer = null;
public ?Model $subject = null;

public function getHeading(): ?string
{
if ($this->causer) {
return __('User Activity');
}

if ($this->subject) {
return str(class_basename($this->subject))
->headline()
->append(__(' History'));
}

return null;
}

public function table(Table $table): Table
{
$query = Activity::query()
->when($this->causer, function ($q, $causer) {
$q->causedBy($causer);
})
->when($this->subject, function ($q, $subject) {
$q->forSubject($subject);
});

return $table
->heading($this->getHeading())
->query($query)
->columns([
Tables\Columns\Layout\Split::make([
Tables\Columns\TextColumn::make('description')->html(),
Tables\Columns\Layout\Split::make([
Tables\Columns\TextColumn::make('created_at')
->since()
->dateTimeTooltip()
->badge()
->color('warning')
->grow(false),
])
->grow(false),
])
->from('md'),
Tables\Columns\Layout\Panel::make([
Tables\Columns\ViewColumn::make('properties')
->view('filament.tables.columns.activity-properties'),
])
->hidden(fn(Model $model) => (new $model->subject_type) instanceof Pivot)
->extraAttributes(['class' => 'overflow-x-auto'])
->collapsible(),
]);
}
}
8 changes: 8 additions & 0 deletions app/Filament/Resources/RoleResource/Pages/EditRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Filament\Resources\RoleResource\Pages;

use App\Filament\Resources\ActivityResource;
use App\Filament\Resources\RoleResource;
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;
Expand All @@ -17,4 +18,11 @@ protected function getHeaderActions(): array
Actions\DeleteAction::make(),
];
}

protected function getFooterWidgets(): array
{
return [
ActivityResource\Widgets\ModelActivity::make(['subject' => $this->record]),
];
}
}
8 changes: 8 additions & 0 deletions app/Filament/Resources/RoleResource/Pages/ViewRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Filament\Resources\RoleResource\Pages;

use App\Filament\Resources\ActivityResource;
use App\Filament\Resources\RoleResource;
use Filament\Actions;
use Filament\Resources\Pages\ViewRecord;
Expand All @@ -17,4 +18,11 @@ protected function getHeaderActions(): array
Actions\DeleteAction::make(),
];
}

protected function getFooterWidgets(): array
{
return [
ActivityResource\Widgets\ModelActivity::make(['subject' => $this->record]),
];
}
}
9 changes: 9 additions & 0 deletions app/Filament/Resources/UserResource/Pages/EditUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Filament\Resources\UserResource\Pages;

use App\Filament\Resources\ActivityResource;
use App\Filament\Resources\UserResource;
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;
Expand All @@ -17,4 +18,12 @@ protected function getHeaderActions(): array
Actions\DeleteAction::make(),
];
}

protected function getFooterWidgets(): array
{
return [
ActivityResource\Widgets\ModelActivity::make(['causer' => $this->record]),
ActivityResource\Widgets\ModelActivity::make(['subject' => $this->record]),
];
}
}
9 changes: 9 additions & 0 deletions app/Filament/Resources/UserResource/Pages/ViewUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Filament\Resources\UserResource\Pages;

use App\Filament\Resources\ActivityResource;
use App\Filament\Resources\UserResource;
use Filament\Actions;
use Filament\Actions\Modal\Actions\Action;
Expand All @@ -18,4 +19,12 @@ protected function getHeaderActions(): array
Actions\DeleteAction::make(),
];
}

protected function getFooterWidgets(): array
{
return [
ActivityResource\Widgets\ModelActivity::make(['causer' => $this->record]),
ActivityResource\Widgets\ModelActivity::make(['subject' => $this->record]),
];
}
}
13 changes: 10 additions & 3 deletions app/Models/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\Relation;
use Spatie\Permission\Models\Role as ModelsRole;

class Role extends ModelsRole
Expand All @@ -16,9 +17,15 @@ class Role extends ModelsRole

public function logIncludes(Builder $query): Builder
{
return $query->whereHasMorph('subject', [PermissionRole::class], function ($q) {
$q->where(config('permission.column_names.role_pivot_key'), $this->getKey());
});
return $query
->where('subject_type', Relation::getMorphAlias(PermissionRole::class))
->where(function ($q) {
$rolePivotKey = config('permission.column_names.role_pivot_key') ?? 'role_id';

$q
->where("properties->attributes->{$rolePivotKey}", $this->getKey())
->orWhere("properties->old->{$rolePivotKey}", $this->getKey());
});
}

public function isSuperAdmin(): bool
Expand Down
15 changes: 10 additions & 5 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Spatie\Permission\Traits\HasRoles;
Expand Down Expand Up @@ -76,11 +77,15 @@ public function logExcept(): array

public function logIncludes(Builder $query): Builder
{
return $query->whereHasMorph('subject', [RoleUser::class], function ($q) {
$q
->where('model_type', $this->getMorphClass())
->where(config('permission.column_names.model_morph_key'), $this->getKey());
});
return $query
->where('subject_type', Relation::getMorphAlias(RoleUser::class))
->where(function ($q) {
$modelMorphKey = config('permission.column_names.model_morph_key');

$q
->where("properties->attributes->{$modelMorphKey}", $this->getKey())
->orWhere("properties->old->{$modelMorphKey}", $this->getKey());
});
}

/**
Expand Down

0 comments on commit ee36ac6

Please sign in to comment.