|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace App\Filament\Resources; |
| 6 | + |
| 7 | +use AmidEsfahani\FilamentTinyEditor\TinyEditor; |
| 8 | +use App\Enums\PageStatusEnum; |
| 9 | +use App\Filament\Resources\PageResource\Pages\CreatePage; |
| 10 | +use App\Filament\Resources\PageResource\Pages\EditPage; |
| 11 | +use App\Filament\Resources\PageResource\Pages\ListPages; |
| 12 | +use App\Models\Page as PageModel; |
| 13 | +use Filament\Actions\BulkActionGroup; |
| 14 | +use Filament\Actions\DeleteBulkAction; |
| 15 | +use Filament\Actions\EditAction; |
| 16 | +use Filament\Forms\Components\DateTimePicker; |
| 17 | +use Filament\Forms\Components\FileUpload; |
| 18 | +use Filament\Forms\Components\Select; |
| 19 | +use Filament\Forms\Components\TextInput; |
| 20 | +use Filament\Forms\Components\Toggle; |
| 21 | +use Filament\Resources\Resource; |
| 22 | +use Filament\Schemas\Components\Fieldset; |
| 23 | +use Filament\Schemas\Components\Utilities\Get; |
| 24 | +use Filament\Schemas\Components\Utilities\Set; |
| 25 | +use Filament\Schemas\Schema; |
| 26 | +use Filament\Tables\Columns\IconColumn; |
| 27 | +use Filament\Tables\Columns\TextColumn; |
| 28 | +use Filament\Tables\Table; |
| 29 | +use Illuminate\Support\Str; |
| 30 | + |
| 31 | +class PageResource extends Resource |
| 32 | +{ |
| 33 | + protected static ?string $model = PageModel::class; |
| 34 | + |
| 35 | + protected static string | \UnitEnum | null $navigationGroup = 'Content'; |
| 36 | + |
| 37 | + protected static string | \BackedEnum | null $navigationIcon = 'heroicon-o-document-text'; |
| 38 | + |
| 39 | + protected static ?int $navigationSort = 6; |
| 40 | + |
| 41 | + public static function form(Schema $schema): Schema |
| 42 | + { |
| 43 | + return $schema |
| 44 | + ->components([ |
| 45 | + TextInput::make('heading') |
| 46 | + ->required() |
| 47 | + ->maxLength(255) |
| 48 | + ->live(onBlur: true) |
| 49 | + ->afterStateUpdated(function (string $operation, ?string $state, Set $set): void { |
| 50 | + if ($operation === 'create') { |
| 51 | + $set('slug', Str::slug((string) $state)); |
| 52 | + } |
| 53 | + }) |
| 54 | + ->hintIcon('heroicon-o-information-circle') |
| 55 | + ->hintIconTooltip('The page title shown to visitors, e.g. "About Us".'), |
| 56 | + TextInput::make('slug') |
| 57 | + ->disabled() |
| 58 | + ->dehydrated() |
| 59 | + ->required() |
| 60 | + ->maxLength(255) |
| 61 | + ->unique(PageModel::class, 'slug', ignoreRecord: true) |
| 62 | + ->hintIcon('heroicon-o-information-circle') |
| 63 | + ->hintIconTooltip('Auto-generated from the heading. Used as the URL path, e.g. /about-us.'), |
| 64 | + TinyEditor::make('content') |
| 65 | + ->nullable() |
| 66 | + ->columnSpanFull() |
| 67 | + ->hintIcon('heroicon-o-information-circle') |
| 68 | + ->hintIconTooltip('The main body content of the page, edited with the rich text editor.'), |
| 69 | + TextInput::make('title') |
| 70 | + ->maxLength(255) |
| 71 | + ->hintIcon('heroicon-o-information-circle') |
| 72 | + ->hintIconTooltip('SEO page title shown in the browser tab and search results. Leave blank to use the heading.'), |
| 73 | + TextInput::make('description') |
| 74 | + ->maxLength(255) |
| 75 | + ->hintIcon('heroicon-o-information-circle') |
| 76 | + ->hintIconTooltip('Meta description shown in search engine results. Aim for 150-160 characters.'), |
| 77 | + Toggle::make('no_index') |
| 78 | + ->hintIcon('heroicon-o-information-circle') |
| 79 | + ->hintIconTooltip('When enabled, search engines are instructed not to index this page.'), |
| 80 | + TextInput::make('canonical') |
| 81 | + ->maxLength(255) |
| 82 | + ->url() |
| 83 | + ->hintIcon('heroicon-o-information-circle') |
| 84 | + ->hintIconTooltip('Canonical URL to prevent duplicate content issues. Leave blank unless this page should point to another URL.'), |
| 85 | + Select::make('status') |
| 86 | + ->required() |
| 87 | + ->live() |
| 88 | + ->options(PageStatusEnum::options()) |
| 89 | + ->default(PageStatusEnum::PUBLISHED->value) |
| 90 | + ->hintIcon('heroicon-o-information-circle') |
| 91 | + ->hintIconTooltip('Published: visible on the frontend. Draft: hidden. Scheduled: published on the date set below. Deleted: removed from listings.'), |
| 92 | + DateTimePicker::make('published_at') |
| 93 | + ->nullable() |
| 94 | + ->required(fn (Get $get): bool => (int) $get('status') === PageStatusEnum::SCHEDULED->value) |
| 95 | + ->hidden(fn (Get $get): bool => (int) $get('status') !== PageStatusEnum::SCHEDULED->value) |
| 96 | + ->hintIcon('heroicon-o-information-circle') |
| 97 | + ->hintIconTooltip('The date and time this page will be made public. Only applies when status is Scheduled.'), |
| 98 | + Fieldset::make('Image') |
| 99 | + ->relationship('image') |
| 100 | + ->schema([ |
| 101 | + FileUpload::make('path') |
| 102 | + ->image() |
| 103 | + ->nullable() |
| 104 | + ->columnSpanFull(), |
| 105 | + TextInput::make('alt_text') |
| 106 | + ->nullable() |
| 107 | + ->maxLength(255), |
| 108 | + ]) |
| 109 | + ->mutateRelationshipDataBeforeSaveUsing(function (array $data, PageModel $record): array { |
| 110 | + if (empty($data['path'])) { |
| 111 | + $record->image?->delete(); |
| 112 | + } |
| 113 | + |
| 114 | + return $data; |
| 115 | + }) |
| 116 | + ->columnSpanFull(), |
| 117 | + ]); |
| 118 | + } |
| 119 | + |
| 120 | + public static function table(Table $table): Table |
| 121 | + { |
| 122 | + return $table |
| 123 | + ->columns([ |
| 124 | + TextColumn::make('heading') |
| 125 | + ->limit(30) |
| 126 | + ->wrap() |
| 127 | + ->searchable(), |
| 128 | + TextColumn::make('slug') |
| 129 | + ->limit(30) |
| 130 | + ->wrap() |
| 131 | + ->searchable(), |
| 132 | + IconColumn::make('no_index') |
| 133 | + ->boolean(), |
| 134 | + TextColumn::make('status') |
| 135 | + ->getStateUsing(fn (PageModel $record): string => $record->status->label()) |
| 136 | + ->color(fn (PageModel $record): string => $record->status->color()) |
| 137 | + ->sortable(), |
| 138 | + TextColumn::make('published_at') |
| 139 | + ->dateTime() |
| 140 | + ->sortable() |
| 141 | + ->toggleable(isToggledHiddenByDefault: true), |
| 142 | + TextColumn::make('created_at') |
| 143 | + ->dateTime() |
| 144 | + ->sortable() |
| 145 | + ->toggleable(isToggledHiddenByDefault: true), |
| 146 | + TextColumn::make('updated_at') |
| 147 | + ->dateTime() |
| 148 | + ->sortable() |
| 149 | + ->toggleable(isToggledHiddenByDefault: true), |
| 150 | + ]) |
| 151 | + ->filters([ |
| 152 | + // |
| 153 | + ]) |
| 154 | + ->recordActions([ |
| 155 | + EditAction::make(), |
| 156 | + ]) |
| 157 | + ->toolbarActions([ |
| 158 | + BulkActionGroup::make([ |
| 159 | + DeleteBulkAction::make(), |
| 160 | + ]), |
| 161 | + ]); |
| 162 | + } |
| 163 | + |
| 164 | + public static function getRelations(): array |
| 165 | + { |
| 166 | + return []; |
| 167 | + } |
| 168 | + |
| 169 | + public static function getPages(): array |
| 170 | + { |
| 171 | + return [ |
| 172 | + 'index' => ListPages::route('/'), |
| 173 | + 'create' => CreatePage::route('/create'), |
| 174 | + 'edit' => EditPage::route('/{record}/edit'), |
| 175 | + ]; |
| 176 | + } |
| 177 | +} |
0 commit comments