Files
ai-stylegallery/app/Filament/Resources/Galleries/GalleryResource.php

109 lines
3.0 KiB
PHP

<?php
namespace App\Filament\Resources\Galleries;
use App\Filament\Resources\Galleries\Pages\CreateGallery;
use App\Filament\Resources\Galleries\Pages\EditGallery;
use App\Filament\Resources\Galleries\Pages\ListGalleries;
use App\Filament\Resources\Galleries\Schemas\GalleryForm;
use App\Filament\Resources\Galleries\Tables\GalleriesTable;
use App\Models\Gallery;
use BackedEnum;
use Filament\Resources\Resource;
use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;
use Filament\Tables\Table;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use UnitEnum;
class GalleryResource extends Resource
{
protected static ?string $model = Gallery::class;
protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedRectangleStack;
protected static UnitEnum|string|null $navigationGroup = 'Content';
public static function getNavigationGroup(): ?string
{
return __('filament.navigation.groups.content');
}
public static function form(Schema $schema): Schema
{
return GalleryForm::configure($schema);
}
public static function table(Table $table): Table
{
return GalleriesTable::configure($table);
}
public static function getRelations(): array
{
return [
//
];
}
public static function getPages(): array
{
return [
'index' => ListGalleries::route('/'),
'create' => CreateGallery::route('/create'),
'edit' => EditGallery::route('/{record}/edit'),
];
}
public static function mutateFormDataBeforeCreate(array $data): array
{
$data = self::mutatePassword($data);
$data = self::mutateSparkbooth($data, true);
$data['slug'] = $data['slug'] ?: Str::uuid()->toString();
return $data;
}
public static function mutateFormDataBeforeSave(array $data): array
{
$data = self::mutatePassword($data);
$data = self::mutateSparkbooth($data);
$data['slug'] = $data['slug'] ?: Str::uuid()->toString();
return $data;
}
private static function mutatePassword(array $data): array
{
$password = $data['password'] ?? null;
unset($data['password']);
if (! empty($password)) {
$data['password_hash'] = Hash::make($password);
}
return $data;
}
private static function mutateSparkbooth(array $data, bool $isCreate = false): array
{
if (array_key_exists('sparkbooth_username', $data)) {
$data['sparkbooth_username'] = $data['sparkbooth_username']
? Str::of($data['sparkbooth_username'])->lower()->trim()->value()
: null;
}
$password = $data['sparkbooth_password'] ?? null;
unset($data['sparkbooth_password']);
if (! empty($password)) {
$data['sparkbooth_password'] = $password;
} elseif ($isCreate && empty($password)) {
$data['sparkbooth_password'] = Str::random(24);
}
return $data;
}
}