119 lines
4.1 KiB
PHP
119 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Resources;
|
|
|
|
use App\Admin\Resources\CategoryResource\Pages;
|
|
use App\Models\Category;
|
|
use Filament\Forms;
|
|
use Filament\Forms\Form;
|
|
use Filament\Forms\Get;
|
|
use Filament\Forms\Set;
|
|
use Filament\Notifications\Notification;
|
|
use Filament\Resources\Resource;
|
|
use Filament\Tables;
|
|
use Filament\Tables\Actions\DeleteBulkAction;
|
|
use Filament\Tables\Table;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Support\Str;
|
|
|
|
class CategoryResource extends Resource
|
|
{
|
|
protected static ?string $model = Category::class;
|
|
|
|
protected static ?string $navigationGroup = 'Administration';
|
|
|
|
protected static ?string $navigationIcon = 'ri-folder-6-line';
|
|
|
|
protected static ?string $activeNavigationIcon = 'ri-folder-6-fill';
|
|
|
|
protected static ?int $navigationSort = 1;
|
|
|
|
public static function form(Form $form): Form
|
|
{
|
|
return $form
|
|
->schema([
|
|
Forms\Components\TextInput::make('name')
|
|
->required()
|
|
->maxLength(255)
|
|
->live(onBlur: true)
|
|
->afterStateUpdated(function (Get $get, Set $set, ?string $old, ?string $state) {
|
|
if (($get('slug') ?? '') !== Str::slug($old)) {
|
|
return;
|
|
}
|
|
|
|
$set('slug', Str::slug($state));
|
|
}),
|
|
Forms\Components\TextInput::make('slug')
|
|
->required(),
|
|
Forms\Components\RichEditor::make('description')
|
|
->required(),
|
|
Forms\Components\Select::make('parent_id')
|
|
->relationship('parent', 'name')
|
|
->searchable()
|
|
->preload()
|
|
->label('Parent Category')
|
|
// Disallow having same category as it's own parent
|
|
->disableOptionWhen(fn (string $value, ?Category $record): bool => $record && (int) $value === $record->id),
|
|
Forms\Components\FileUpload::make('image')->label('Image')->nullable()->acceptedFileTypes(['image/*'])->columnSpanFull(),
|
|
])->columns(2);
|
|
}
|
|
|
|
public static function table(Table $table): Table
|
|
{
|
|
return $table
|
|
->columns([
|
|
Tables\Columns\TextColumn::make('name')
|
|
->searchable()
|
|
->sortable(),
|
|
Tables\Columns\TextColumn::make('slug')
|
|
->searchable()
|
|
->sortable(),
|
|
])
|
|
->filters([
|
|
//
|
|
])
|
|
->actions([
|
|
Tables\Actions\EditAction::make(),
|
|
])
|
|
->bulkActions([
|
|
Tables\Actions\BulkActionGroup::make([
|
|
Tables\Actions\DeleteBulkAction::make()->before(function (DeleteBulkAction $action) {
|
|
foreach ($action->getRecords() as $record) {
|
|
if ($record->products()->exists() || $record->children()->exists()) {
|
|
Notification::make()
|
|
->title('Cannot delete category')
|
|
->body('The category has products or children categories.')
|
|
->duration(5000)
|
|
->icon('ri-error-warning-line')
|
|
->danger()
|
|
->send();
|
|
$action->cancel();
|
|
}
|
|
}
|
|
}),
|
|
]),
|
|
])
|
|
->defaultSort(function (Builder $query): Builder {
|
|
return $query
|
|
->orderBy('sort', 'asc');
|
|
})
|
|
->reorderable('sort');
|
|
}
|
|
|
|
public static function getRelations(): array
|
|
{
|
|
return [
|
|
//
|
|
];
|
|
}
|
|
|
|
public static function getPages(): array
|
|
{
|
|
return [
|
|
'index' => Pages\ListCategories::route('/'),
|
|
'create' => Pages\CreateCategory::route('/create'),
|
|
'edit' => Pages\EditCategory::route('/{record}/edit'),
|
|
];
|
|
}
|
|
}
|