|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tapp\FilamentActivity\Filament\Resources; |
| 6 | + |
| 7 | +use BackedEnum; |
| 8 | +use Filament\Infolists\Components\TextEntry; |
| 9 | +use Filament\Resources\Resource; |
| 10 | +use Filament\Schemas\Components\Grid; |
| 11 | +use Filament\Schemas\Components\Section; |
| 12 | +use Filament\Schemas\Schema; |
| 13 | +use Filament\Tables\Columns\TextColumn; |
| 14 | +use Filament\Tables\Table; |
| 15 | +use Tapp\FilamentActivity\Filament\Resources\ActivityResource\Pages\ListActivities; |
| 16 | +use Tapp\FilamentActivity\Filament\Resources\ActivityResource\Pages\ViewActivity; |
| 17 | +use Tapp\FilamentActivity\Models\Activity; |
| 18 | + |
| 19 | +class ActivityResource extends Resource |
| 20 | +{ |
| 21 | + protected static ?string $model = Activity::class; |
| 22 | + |
| 23 | + protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-bolt'; |
| 24 | + |
| 25 | + protected static ?int $navigationSort = 40; |
| 26 | + |
| 27 | + public static function getNavigationLabel(): string |
| 28 | + { |
| 29 | + return __('Activity'); |
| 30 | + } |
| 31 | + |
| 32 | + public static function getNavigationGroup(): ?string |
| 33 | + { |
| 34 | + return __('Community'); |
| 35 | + } |
| 36 | + |
| 37 | + public static function isScopedToTenant(): bool |
| 38 | + { |
| 39 | + return config('filament-activity.tenant.enabled', false); |
| 40 | + } |
| 41 | + |
| 42 | + public static function getTenantOwnershipRelationshipName(): string |
| 43 | + { |
| 44 | + return 'tenant'; |
| 45 | + } |
| 46 | + |
| 47 | + public static function table(Table $table): Table |
| 48 | + { |
| 49 | + return $table |
| 50 | + ->defaultSort('occurred_at', 'desc') |
| 51 | + ->columns([ |
| 52 | + TextColumn::make('event') |
| 53 | + ->label(__('Event')) |
| 54 | + ->badge() |
| 55 | + ->sortable() |
| 56 | + ->searchable(), |
| 57 | + TextColumn::make('title') |
| 58 | + ->label(__('Title')) |
| 59 | + ->searchable() |
| 60 | + ->limit(60), |
| 61 | + TextColumn::make('actor.name') |
| 62 | + ->label(__('Actor')) |
| 63 | + ->placeholder(__('System')) |
| 64 | + ->sortable(), |
| 65 | + TextColumn::make('subject_type') |
| 66 | + ->label(__('Subject')) |
| 67 | + ->formatStateUsing(fn (?string $state): string => $state ? class_basename($state) : '') |
| 68 | + ->sortable(), |
| 69 | + TextColumn::make('occurred_at') |
| 70 | + ->label(__('Occurred')) |
| 71 | + ->since() |
| 72 | + ->sortable(), |
| 73 | + ]); |
| 74 | + } |
| 75 | + |
| 76 | + public static function infolist(Schema $schema): Schema |
| 77 | + { |
| 78 | + return $schema |
| 79 | + ->components([ |
| 80 | + Grid::make([ |
| 81 | + 'default' => 1, |
| 82 | + 'md' => 3, |
| 83 | + ]) |
| 84 | + ->columnSpanFull() |
| 85 | + ->schema([ |
| 86 | + Section::make(__('Activity')) |
| 87 | + ->columnSpan(2) |
| 88 | + ->schema([ |
| 89 | + TextEntry::make('event')->label(__('Event'))->badge(), |
| 90 | + TextEntry::make('title')->label(__('Title'))->placeholder('-'), |
| 91 | + TextEntry::make('summary')->label(__('Summary'))->placeholder('-')->columnSpanFull(), |
| 92 | + TextEntry::make('metadata') |
| 93 | + ->label(__('Metadata')) |
| 94 | + ->formatStateUsing(fn (?array $state): string => $state ? json_encode($state, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR) : '-') |
| 95 | + ->columnSpanFull(), |
| 96 | + ]), |
| 97 | + Section::make(__('Context')) |
| 98 | + ->columnSpan(1) |
| 99 | + ->schema([ |
| 100 | + TextEntry::make('actor.name')->label(__('Actor'))->placeholder(__('System')), |
| 101 | + TextEntry::make('subject_type')->label(__('Subject'))->formatStateUsing(fn (?string $state): string => $state ? class_basename($state) : '-'), |
| 102 | + TextEntry::make('parent_type')->label(__('Parent'))->formatStateUsing(fn (?string $state): string => $state ? class_basename($state) : '-'), |
| 103 | + TextEntry::make('occurred_at')->label(__('Occurred'))->dateTime(), |
| 104 | + ]), |
| 105 | + ]), |
| 106 | + ]); |
| 107 | + } |
| 108 | + |
| 109 | + public static function getPages(): array |
| 110 | + { |
| 111 | + return [ |
| 112 | + 'index' => ListActivities::route('/'), |
| 113 | + 'view' => ViewActivity::route('/{record}'), |
| 114 | + ]; |
| 115 | + } |
| 116 | +} |
0 commit comments