|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Eclipse\Catalogue\Filament\Resources; |
| 4 | + |
| 5 | +use BezhanSalleh\FilamentShield\Contracts\HasShieldPermissions; |
| 6 | +use Eclipse\Catalogue\Filament\Resources\ProductResource\Pages; |
| 7 | +use Eclipse\Catalogue\Models\Product; |
| 8 | +use Filament\Forms\Components\Placeholder; |
| 9 | +use Filament\Forms\Components\RichEditor; |
| 10 | +use Filament\Forms\Components\TextInput; |
| 11 | +use Filament\Forms\Form; |
| 12 | +use Filament\Resources\Concerns\Translatable; |
| 13 | +use Filament\Resources\Resource; |
| 14 | +use Filament\Tables\Actions\BulkActionGroup; |
| 15 | +use Filament\Tables\Actions\DeleteAction; |
| 16 | +use Filament\Tables\Actions\DeleteBulkAction; |
| 17 | +use Filament\Tables\Actions\EditAction; |
| 18 | +use Filament\Tables\Actions\ForceDeleteAction; |
| 19 | +use Filament\Tables\Actions\ForceDeleteBulkAction; |
| 20 | +use Filament\Tables\Actions\RestoreAction; |
| 21 | +use Filament\Tables\Actions\RestoreBulkAction; |
| 22 | +use Filament\Tables\Columns\TextColumn; |
| 23 | +use Filament\Tables\Filters\TrashedFilter; |
| 24 | +use Filament\Tables\Table; |
| 25 | +use Illuminate\Database\Eloquent\Builder; |
| 26 | +use Illuminate\Database\Eloquent\SoftDeletingScope; |
| 27 | + |
| 28 | +class ProductResource extends Resource implements HasShieldPermissions |
| 29 | +{ |
| 30 | + use Translatable; |
| 31 | + |
| 32 | + protected static ?string $model = Product::class; |
| 33 | + |
| 34 | + protected static ?string $slug = 'products'; |
| 35 | + |
| 36 | + protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack'; |
| 37 | + |
| 38 | + protected static ?string $navigationGroup = 'Catalogue'; |
| 39 | + |
| 40 | + public static function form(Form $form): Form |
| 41 | + { |
| 42 | + return $form |
| 43 | + ->schema([ |
| 44 | + TextInput::make('code'), |
| 45 | + |
| 46 | + TextInput::make('barcode') |
| 47 | + ->maxLength(255), |
| 48 | + |
| 49 | + TextInput::make('manufacturers_code') |
| 50 | + ->label('Manufacturer\'s Code') |
| 51 | + ->maxLength(255), |
| 52 | + |
| 53 | + TextInput::make('suppliers_code') |
| 54 | + ->label('Supplier\'s Code') |
| 55 | + ->maxLength(255), |
| 56 | + |
| 57 | + TextInput::make('net_weight') |
| 58 | + ->numeric(), |
| 59 | + |
| 60 | + TextInput::make('gross_weight') |
| 61 | + ->numeric(), |
| 62 | + |
| 63 | + TextInput::make('name'), |
| 64 | + |
| 65 | + TextInput::make('short_description'), |
| 66 | + |
| 67 | + RichEditor::make('description'), |
| 68 | + |
| 69 | + Placeholder::make('created_at') |
| 70 | + ->label('Created Date') |
| 71 | + ->content(fn (?Product $record): string => $record?->created_at?->diffForHumans() ?? '-'), |
| 72 | + |
| 73 | + Placeholder::make('updated_at') |
| 74 | + ->label('Last Modified Date') |
| 75 | + ->content(fn (?Product $record): string => $record?->updated_at?->diffForHumans() ?? '-'), |
| 76 | + ]); |
| 77 | + } |
| 78 | + |
| 79 | + public static function table(Table $table): Table |
| 80 | + { |
| 81 | + return $table |
| 82 | + ->columns([ |
| 83 | + TextColumn::make('id'), |
| 84 | + |
| 85 | + TextColumn::make('name') |
| 86 | + ->toggleable(false), |
| 87 | + |
| 88 | + TextColumn::make('short_description') |
| 89 | + ->words(5), |
| 90 | + |
| 91 | + TextColumn::make('code') |
| 92 | + ->copyable(), |
| 93 | + |
| 94 | + TextColumn::make('barcode'), |
| 95 | + |
| 96 | + TextColumn::make('manufacturers_code'), |
| 97 | + |
| 98 | + TextColumn::make('suppliers_code'), |
| 99 | + |
| 100 | + TextColumn::make('net_weight') |
| 101 | + ->numeric(3) |
| 102 | + ->suffix(' kg'), |
| 103 | + |
| 104 | + TextColumn::make('gross_weight') |
| 105 | + ->numeric(3) |
| 106 | + ->suffix(' kg'), |
| 107 | + ]) |
| 108 | + ->filters([ |
| 109 | + TrashedFilter::make(), |
| 110 | + ]) |
| 111 | + ->actions([ |
| 112 | + EditAction::make(), |
| 113 | + DeleteAction::make(), |
| 114 | + RestoreAction::make(), |
| 115 | + ForceDeleteAction::make(), |
| 116 | + ]) |
| 117 | + ->bulkActions([ |
| 118 | + BulkActionGroup::make([ |
| 119 | + DeleteBulkAction::make(), |
| 120 | + RestoreBulkAction::make(), |
| 121 | + ForceDeleteBulkAction::make(), |
| 122 | + ]), |
| 123 | + ]); |
| 124 | + } |
| 125 | + |
| 126 | + public static function getPages(): array |
| 127 | + { |
| 128 | + return [ |
| 129 | + 'index' => Pages\ListProducts::route('/'), |
| 130 | + 'create' => Pages\CreateProduct::route('/create'), |
| 131 | + 'edit' => Pages\EditProduct::route('/{record}/edit'), |
| 132 | + ]; |
| 133 | + } |
| 134 | + |
| 135 | + public static function getEloquentQuery(): Builder |
| 136 | + { |
| 137 | + return parent::getEloquentQuery() |
| 138 | + ->withoutGlobalScopes([ |
| 139 | + SoftDeletingScope::class, |
| 140 | + ]); |
| 141 | + } |
| 142 | + |
| 143 | + public static function getGloballySearchableAttributes(): array |
| 144 | + { |
| 145 | + return []; |
| 146 | + } |
| 147 | + |
| 148 | + public static function getPermissionPrefixes(): array |
| 149 | + { |
| 150 | + return [ |
| 151 | + 'view_any', |
| 152 | + 'view', |
| 153 | + 'create', |
| 154 | + 'update', |
| 155 | + 'restore', |
| 156 | + 'restore_any', |
| 157 | + 'delete', |
| 158 | + 'delete_any', |
| 159 | + 'force_delete', |
| 160 | + 'force_delete_any', |
| 161 | + ]; |
| 162 | + } |
| 163 | +} |
0 commit comments