Files
ai-stylegallery/app/Filament/Resources/StyleResource.php

131 lines
5.2 KiB
PHP

<?php
namespace App\Filament\Resources;
use App\Filament\Resources\StyleResource\Pages;
use App\Models\Style;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Select;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Columns\ImageColumn;
use Filament\Forms\Components\Toggle;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Filters\SelectFilter;
class StyleResource extends Resource
{
protected static ?string $model = Style::class;
protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
public static function form(Form $form): Form
{
return $form
->schema([
TextInput::make('title')
->label(__('filament.resource.style.form.title'))
->required()
->maxLength(255),
Toggle::make('enabled')
->label(__('filament.resource.style.form.enabled'))
->default(true),
Textarea::make('prompt')
->label(__('filament.resource.style.form.prompt'))
->required()
->rows(5),
Textarea::make('description')
->label(__('filament.resource.style.form.description'))
->required()
->rows(5),
FileUpload::make('preview_image')
->label(__('filament.resource.style.form.preview_image'))
->required()
->image()
->disk('public')
->directory('style_previews'),
Textarea::make('parameters')
->label(__('filament.resource.style.form.parameters'))
->nullable()
->rows(15)
->json()
->helperText(__('filament.resource.style.form.parameters_help')),
Select::make('ai_model_id')
->relationship('aiModel', 'name')
->label(__('filament.resource.style.form.ai_model'))
->required(),
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
TextColumn::make('title')->label(__('filament.resource.style.table.title'))->searchable()->sortable(),
IconColumn::make('enabled')
->label(__('filament.resource.style.table.enabled'))
->boolean(),
TextColumn::make('aiModel.name')->label(__('filament.resource.style.table.ai_model'))->searchable()->sortable(),
ImageColumn::make('preview_image')->label(__('filament.resource.style.table.preview_image'))->disk('public'),
])
->filters([
SelectFilter::make('ai_model')
->relationship('aiModel', 'name')
->label(__('filament.resource.style.table.ai_model')),
])
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\Action::make('duplicate')
->label(__('filament.resource.style.action.duplicate'))
->icon('heroicon-o-document-duplicate')
->action(function (\App\Models\Style $record) {
$newStyle = $record->replicate();
$newStyle->title = $record->title . ' (Kopie)';
$newStyle->save();
return redirect()->to(StyleResource::getUrl('edit', ['record' => $newStyle->id]));
}),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
Tables\Actions\BulkAction::make('enable')
->label(__('filament.resource.style.action.enable_selected'))
->icon('heroicon-o-check-circle')
->action(function (\Illuminate\Support\Collection $records) {
$records->each->update(['enabled' => true]);
}),
Tables\Actions\BulkAction::make('disable')
->label(__('filament.resource.style.action.disable_selected'))
->icon('heroicon-o-x-circle')
->action(function (\Illuminate\Support\Collection $records) {
$records->each->update(['enabled' => false]);
}),
]),
])
->emptyStateActions([
Tables\Actions\CreateAction::make(),
]);
}
public static function getRelations(): array
{
return [
//
];
}
public static function getPages(): array
{
return [
'index' => Pages\ListStyles::route('/'),
'create' => Pages\CreateStyle::route('/create'),
'edit' => Pages\EditStyle::route('/{record}/edit'),
];
}
}