254 lines
10 KiB
PHP
254 lines
10 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Resources\Styles;
|
|
|
|
use App\Models\Style;
|
|
use BackedEnum;
|
|
use Filament\Actions\Action;
|
|
use Filament\Actions\Action\Step;
|
|
use Filament\Actions\BulkAction;
|
|
use Filament\Actions\BulkActionGroup;
|
|
use Filament\Actions\CreateAction;
|
|
use Filament\Actions\DeleteBulkAction;
|
|
use Filament\Actions\EditAction;
|
|
use Filament\Forms\Components\FileUpload;
|
|
use Filament\Forms\Components\Select;
|
|
use Filament\Forms\Components\Textarea;
|
|
use Filament\Forms\Components\TextInput;
|
|
use Filament\Forms\Components\Toggle;
|
|
use Filament\Resources\Resource;
|
|
use Filament\Schemas\Components\Grid;
|
|
use Filament\Schemas\Components\Tabs;
|
|
use Filament\Schemas\Components\Tabs\Tab;
|
|
use Filament\Schemas\Schema;
|
|
use Filament\Tables\Columns\IconColumn;
|
|
use Filament\Tables\Columns\ImageColumn;
|
|
use Filament\Tables\Columns\TextColumn;
|
|
use Filament\Tables\Filters\SelectFilter;
|
|
use Filament\Tables\Table;
|
|
use Spatie\SimpleExcel\SimpleExcelReader;
|
|
|
|
class StyleResource extends Resource
|
|
{
|
|
protected static ?string $model = Style::class;
|
|
|
|
protected static BackedEnum|string|null $navigationIcon = 'heroicon-o-rectangle-stack';
|
|
|
|
protected static ?int $navigationSort = -80;
|
|
|
|
public static function getNavigationGroup(): ?string
|
|
{
|
|
return __('filament.navigation.groups.ai_models');
|
|
}
|
|
|
|
public static function form(Schema $schema): Schema
|
|
{
|
|
return $schema
|
|
->columns(1)
|
|
->components([
|
|
Tabs::make('Style Details')
|
|
->tabs([
|
|
Tab::make('General')
|
|
->components([
|
|
Grid::make(2)
|
|
->columnSpanFull()
|
|
->components([
|
|
TextInput::make('title')
|
|
->required()
|
|
->maxLength(255),
|
|
Toggle::make('enabled')
|
|
->default(true),
|
|
]),
|
|
Grid::make(2)
|
|
->columnSpanFull()
|
|
->components([
|
|
Textarea::make('prompt')
|
|
->required()
|
|
->rows(5),
|
|
Textarea::make('description')
|
|
->required()
|
|
->rows(5),
|
|
]),
|
|
Select::make('ai_model_id')
|
|
->relationship('aiModel', 'name')
|
|
->required(),
|
|
FileUpload::make('preview_image')
|
|
->disk('public')
|
|
->directory('style_previews')
|
|
->image()
|
|
->imageEditor()
|
|
->required(),
|
|
]),
|
|
Tab::make('Details')
|
|
->components([
|
|
TextInput::make('sort_order')
|
|
->numeric()
|
|
->default(0),
|
|
Textarea::make('parameters')
|
|
->nullable()
|
|
->rows(15),
|
|
]),
|
|
]),
|
|
]);
|
|
}
|
|
|
|
public static function table(Table $table): Table
|
|
{
|
|
return $table
|
|
->defaultSort('sort_order')
|
|
->columns([
|
|
TextColumn::make('title')->searchable()->sortable(),
|
|
IconColumn::make('enabled')
|
|
->boolean(),
|
|
TextColumn::make('aiModel.name')->searchable()->sortable(),
|
|
ImageColumn::make('preview_image')->disk('public'),
|
|
TextColumn::make('sort_order')->sortable(),
|
|
])
|
|
->filters([
|
|
SelectFilter::make('ai_model')
|
|
->relationship('aiModel', 'name'),
|
|
])
|
|
->deferFilters(false)
|
|
->headerActions([
|
|
Action::make('import_styles')
|
|
->label('Import Styles (CSV)')
|
|
->icon('heroicon-o-arrow-up-on-square-stack')
|
|
->steps([
|
|
Step::make('Datei')
|
|
->schema([
|
|
FileUpload::make('import_file')
|
|
->label('CSV-Datei (Titel,Prompt,[Beschreibung])')
|
|
->acceptedFileTypes([
|
|
'text/csv',
|
|
'text/plain',
|
|
'application/vnd.ms-excel',
|
|
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
])
|
|
->directory('imports/styles')
|
|
->visibility('private')
|
|
->required(),
|
|
]),
|
|
Step::make('Zuordnung')
|
|
->schema([
|
|
Select::make('ai_model_id')
|
|
->label('AI Modell')
|
|
->relationship('aiModel', 'name')
|
|
->required(),
|
|
]),
|
|
])
|
|
->action(function (array $data): void {
|
|
$path = storage_path('app/'.$data['import_file']);
|
|
|
|
if (! file_exists($path)) {
|
|
throw new \RuntimeException('Import-Datei nicht gefunden.');
|
|
}
|
|
|
|
$rows = self::parseImportFile($path);
|
|
|
|
if (empty($rows)) {
|
|
throw new \RuntimeException('Keine gültigen Zeilen gefunden.');
|
|
}
|
|
|
|
$created = 0;
|
|
foreach ($rows as $row) {
|
|
$title = $row['title'] ?? null;
|
|
$prompt = $row['prompt'] ?? null;
|
|
$description = $row['description'] ?? null;
|
|
|
|
if (! $title || ! $prompt) {
|
|
continue;
|
|
}
|
|
|
|
Style::create([
|
|
'title' => $title,
|
|
'prompt' => $prompt,
|
|
'description' => $description ?? '',
|
|
'preview_image' => '', // user must set later
|
|
'ai_model_id' => $data['ai_model_id'],
|
|
'enabled' => true,
|
|
'sort_order' => 0,
|
|
]);
|
|
$created++;
|
|
}
|
|
|
|
if ($created === 0) {
|
|
throw new \RuntimeException('Es wurden keine Styles importiert.');
|
|
}
|
|
|
|
\Filament\Notifications\Notification::make()
|
|
->title('Import abgeschlossen')
|
|
->body($created.' Styles importiert. Bitte Vorschau-Bilder ergänzen.')
|
|
->success()
|
|
->send();
|
|
}),
|
|
])
|
|
->actions([
|
|
EditAction::make(),
|
|
Action::make('duplicate')
|
|
->label('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(\App\Filament\Resources\Styles\StyleResource::getUrl('edit', ['record' => $newStyle->id]));
|
|
}),
|
|
])
|
|
->bulkActions([
|
|
BulkActionGroup::make([
|
|
DeleteBulkAction::make(),
|
|
BulkAction::make('enable')
|
|
->label('Enable Selected')
|
|
->icon('heroicon-o-check-circle')
|
|
->action(function (\Illuminate\Support\Collection $records) {
|
|
$records->each->update(['enabled' => true]);
|
|
}),
|
|
BulkAction::make('disable')
|
|
->label('Disable Selected')
|
|
->icon('heroicon-o-x-circle')
|
|
->action(function (\Illuminate\Support\Collection $records) {
|
|
$records->each->update(['enabled' => false]);
|
|
}),
|
|
]),
|
|
])
|
|
->emptyStateActions([
|
|
CreateAction::make(),
|
|
]);
|
|
}
|
|
|
|
public static function getRelations(): array
|
|
{
|
|
return [
|
|
//
|
|
];
|
|
}
|
|
|
|
protected static function parseImportFile(string $path): array
|
|
{
|
|
return SimpleExcelReader::create($path)->getRows()->map(function (array $row) {
|
|
$normalized = [];
|
|
foreach ($row as $key => $value) {
|
|
$normalized[strtolower(trim($key))] = is_string($value) ? trim($value) : $value;
|
|
}
|
|
|
|
return [
|
|
'title' => $normalized['title'] ?? $normalized['titel'] ?? null,
|
|
'prompt' => $normalized['prompt'] ?? null,
|
|
'description' => $normalized['description'] ?? $normalized['beschreibung'] ?? null,
|
|
];
|
|
})->filter(function (array $row) {
|
|
return ! empty($row['title']) && ! empty($row['prompt']);
|
|
})->values()->all();
|
|
}
|
|
|
|
public static function getPages(): array
|
|
{
|
|
return [
|
|
'index' => Pages\ListStyles::route('/'),
|
|
'create' => Pages\CreateStyle::route('/create'),
|
|
'edit' => Pages\EditStyle::route('/{record}/edit'),
|
|
];
|
|
}
|
|
}
|