Skip to content
This repository has been archived by the owner on Oct 16, 2023. It is now read-only.

Commit

Permalink
Merge pull request #37 from alexjustesen/simplified-resource-table
Browse files Browse the repository at this point in the history
Simplified resource table
  • Loading branch information
alexjustesen authored Jan 11, 2023
2 parents 90c6557 + 0377b0d commit 6d75f41
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 59 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
coverage: none

- name: Install composer dependencies
uses: ramsey/composer-install@v1
uses: ramsey/composer-install@v2

- name: Run PHPStan
run: ./vendor/bin/phpstan --error-format=github
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@
"test-coverage": "vendor/bin/pest coverage"
},
"config": {
"allow-plugins": {
"pestphp/pest-plugin": true,
"phpstan/extension-installer": true
},
"sort-packages": true
},
"extra": {
Expand Down
8 changes: 5 additions & 3 deletions resources/lang/de/activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
'description' => 'Beschreibung',
'attributes' => 'Neue Werte',
'old' => 'Alte Werte',
'subject' => "Subjekt",
'logged_at' => "Änderungszeit",
'has_subject' => "besitzt Subjekt",
'subject' => 'Subjekt',
'logged_at' => 'Änderungszeit',
'log' => 'Log',
'plural_log' => 'Logs',
'has_subject' => 'besitzt Subjekt',
'label' => 'Aktivität',
'plural_label' => 'Aktivitäten',
];
10 changes: 6 additions & 4 deletions resources/lang/en/activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
'description' => 'Description',
'attributes' => 'Attributes',
'old' => 'Old',
'subject' => "Subject",
'logged_at' => "Logged at",
'has_subject' => "Has Subject",
'subject' => 'Subject',
'logged_at' => 'Logged at',
'log' => 'Log',
'plural_log' => 'Logs',
'has_subject' => 'Has Subject',
'label' => 'Activity',
'plural_label' => 'Activity'
'plural_label' => 'Activity',
];
2 changes: 2 additions & 0 deletions resources/lang/es/activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
'old' => 'Valor(es) anterior(es)',
'subject' => 'Asunto',
'logged_at' => 'Registrado el',
'log' => 'Log',
'plural_log' => 'Logs',
'has_subject' => 'Tiene objeto sometido',
'label' => 'Actividad',
'plural_label' => 'Registros de actividad'
Expand Down
2 changes: 0 additions & 2 deletions src/RelationManagers/ActivitiesRelationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
namespace AlexJustesen\FilamentSpatieLaravelActivitylog\RelationManagers;

use AlexJustesen\FilamentSpatieLaravelActivitylog\Resources\ActivityResource;
use Filament\Facades\Filament;
use Filament\Resources\Form;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Resources\Table;
use Filament\Tables;
use Illuminate\Database\Eloquent\Model;

class ActivitiesRelationManager extends RelationManager
Expand Down
99 changes: 50 additions & 49 deletions src/Resources/ActivityResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@

namespace AlexJustesen\FilamentSpatieLaravelActivitylog\Resources;

use AlexJustesen\FilamentSpatieLaravelActivitylog\Contracts\IsActivitySubject;
use AlexJustesen\FilamentSpatieLaravelActivitylog\RelationManagers\ActivitiesRelationManager;
use AlexJustesen\FilamentSpatieLaravelActivitylog\ResourceFinder;
use AlexJustesen\FilamentSpatieLaravelActivitylog\Resources\ActivityResource\Pages;
use Carbon\Carbon;
use Filament\Forms;
use Filament\Resources\Form;
use Filament\Resources\Resource;
use Filament\Resources\Table;
use Filament\Tables;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\HtmlString;
use Livewire\Component;
use Spatie\Activitylog\Models\Activity;

class ActivityResource extends Resource
Expand All @@ -40,39 +36,39 @@ public static function form(Form $form): Form
->label(__('filament-spatie-activitylog::activity.causer_type'))
->columnSpan([
'default' => 2,
'sm' => 1
'sm' => 1,
]),
Forms\Components\TextInput::make('causer_id')
->label(__('filament-spatie-activitylog::activity.causer_id'))
->columnSpan([
'default' => 2,
'sm' => 1
'sm' => 1,
]),
Forms\Components\TextInput::make('subject_type')
->label(__('filament-spatie-activitylog::activity.subject_type'))
->columnSpan([
'default' => 2,
'sm' => 1
'sm' => 1,
]),
Forms\Components\TextInput::make('subject_id')
->label(__('filament-spatie-activitylog::activity.subject_id'))
->columnSpan([
'default' => 2,
'sm' => 1
'sm' => 1,
]),
Forms\Components\TextInput::make('description')
->label(__('filament-spatie-activitylog::activity.description'))->columnSpan(2),
Forms\Components\KeyValue::make('properties.attributes')
->label(__('filament-spatie-activitylog::activity.attributes'))
->columnSpan([
'default' => 2,
'sm' => 1
'sm' => 1,
]),
Forms\Components\KeyValue::make('properties.old')
->label(__('filament-spatie-activitylog::activity.old'))
->columnSpan([
'default' => 2,
'sm' => 1
'sm' => 1,
]),
]);
}
Expand All @@ -84,55 +80,60 @@ public static function table(Table $table): Table
Tables\Columns\TextColumn::make('id')
->label('ID')
->sortable(),
Tables\Columns\TextColumn::make('subject_type')
->label(__('filament-spatie-activitylog::activity.subject'))
->searchable(),
Tables\Columns\TextColumn::make('description')
->label(__('filament-spatie-activitylog::activity.description'))
->searchable(),
Tables\Columns\TextColumn::make('subject.name')
->label(__('filament-spatie-activitylog::activity.subject'))
->hidden(function (Component $livewire) {
return method_exists($livewire, 'hideSubjectColumn')
? call_user_func([$livewire, 'hideSubjectColumn'])
: $livewire instanceof ActivitiesRelationManager;
})
->getStateUsing(function (Activity $record) {
if (! $record->subject || ! $record->subject instanceof IsActivitySubject) {
return new HtmlString('—');
}

/** @var \AlexJustesen\FilamentSpatieLaravelActivitylog\Contracts\IsActivitySubject */
$subject = $record->subject;

return $subject->getActivitySubjectDescription($record);
Tables\Columns\TextColumn::make('log_name')
->label(__('filament-spatie-activitylog::activity.log')),
Tables\Columns\TextColumn::make('created_at')
->label(__('filament-spatie-activitylog::activity.logged_at'))
->dateTime(),
])
->filters([
Tables\Filters\SelectFilter::make('event')
->multiple()
->options([
'created' => 'Created',
'updated' => 'Updated',
'deleted' => 'Deleted',
]),
Tables\Filters\Filter::make('created_at')
->form([
Forms\Components\DatePicker::make('logged_from')
->label('Logged from'),
Forms\Components\DatePicker::make('logged_until')
->label('Logged until'),
])
->query(function (Builder $query, array $data): Builder {
return $query
->when(
$data['logged_from'],
fn (Builder $query, $date): Builder => $query->whereDate('created_at', '>=', $date),
)
->when(
$data['logged_until'],
fn (Builder $query, $date): Builder => $query->whereDate('created_at', '<=', $date),
);
})
->url(function (Activity $record) {
if (! $record->subject || ! $record->subject instanceof IsActivitySubject) {
return;
}
->indicateUsing(function (array $data): array {
$indicators = [];

/** @var class-string<\Filament\Resources\Resource> */
$resource = ResourceFinder::find($record->subject::class);

if (! $resource) {
return;
if ($data['from'] ?? null) {
$indicators['from'] = 'Created from ' . Carbon::parse($data['from'])->toFormattedDateString();
}

if (! $resource::hasPage('edit')) {
return;
if ($data['until'] ?? null) {
$indicators['until'] = 'Created until ' . Carbon::parse($data['until'])->toFormattedDateString();
}

return $resource::getUrl('edit', ['record' => $record->subject]) ?? null;
}, shouldOpenInNewTab: true),
Tables\Columns\TextColumn::make('created_at')
->label(__('filament-spatie-activitylog::activity.logged_at'))
->dateTime()
->sortable(),
])
->filters([
Tables\Filters\Filter::make('has_subject') ->label(__('filament-spatie-activitylog::activity.has_subject'))
->query(fn (Builder $query) => $query->has('subject')),
return $indicators;
}),
])
->bulkActions([])
->defaultSort('created_at', 'DESC');
->defaultSort('id', 'DESC');
}

protected static function getNavigationGroup(): ?string
Expand Down
1 change: 1 addition & 0 deletions src/Resources/ActivityResource/Pages/ListActivities.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
class ListActivities extends ListRecords
{
protected static string $resource = ActivityResource::class;

protected function getTableRecordsPerPageSelectOptions(): array
{
return config('filament-spatie-laravel-activitylog.paginate') ?? parent::getTableRecordsPerPageSelectOptions();
Expand Down

0 comments on commit 6d75f41

Please sign in to comment.