Files
ntspi-app/app/Filament/Resources/MainSliderResource.php
T
2024-10-28 12:57:16 +05:00

169 lines
7.2 KiB
PHP

<?php
namespace App\Filament\Resources;
use App\Filament\Resources\MainSliderResource\Pages;
use App\Filament\Resources\MainSliderResource\RelationManagers;
use App\Models\Event;
use App\Models\MainSlider;
use App\Models\Page;
use App\Models\Post;
use Filament\Forms;
use Filament\Forms\Components\ColorPicker;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Toggle;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Illuminate\Support\Str;
class MainSliderResource extends Resource
{
public static ?string $label = 'Слайдер';
protected static ?string $pluralLabel = 'Главный слайдер';
protected static ?string $model = MainSlider::class;
protected static ?string $navigationIcon = 'heroicon-o-square-3-stack-3d';
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Section::make('Быстрая настройка слайда')->schema([
Forms\Components\Grid::make()->schema([
Forms\Components\Select::make('model_select')
->name('')
->label('Выбор типа данных')
->options([
'Post' => 'Новость',
'Page' => 'Страница',
'Event' => 'Мероприятие',
'Custom' => 'Кастомная ссылка',
])
->afterStateUpdated(function (string $operation, string|null $state, Forms\Set $set, Forms\Get $get) {
if ($get('model_select') === 'Custom') {
$set('model', null);
$set('title', null);
$set('content', null);
$set('link', null);
};
})->live(onBlur: true),
Forms\Components\Select::make('model')
->label('Поиск данных')
->name('')
->live(onBlur: true)
->searchable()
->live(onBlur: true)
->afterStateUpdated(function (string $operation, string|null $state, Forms\Set $set, Forms\Get $get) {
if ($get('model_select') === 'Post') {
$post = Post::find($state);
$set('title', $post->title);
$relativeUrl = parse_url(route('client.post.show', $post->slug), PHP_URL_PATH);
$set('link', $relativeUrl);
};
if ($get('model_select') === 'Page') {
$page = Page::find($state);
$set('title', $page->title);
$set('link', $page->path);
};
if ($get('model_select') === 'Event') {
$event = Event::find($state);
$set('title', $event->title);
$relativeUrl = parse_url(route('client.event.show', $event->slug), PHP_URL_PATH);
$set('link', $relativeUrl);
};
})
->options(function (Forms\Get $get) {
if ($get('model_select') === 'Post') {
return Post::where('status', '=', 'published')->pluck('title', 'id');
};
if ($get('model_select') === 'Page') {
return Page::where('title', '!=', null)->pluck('title', 'id');
};
if ($get('model_select') === 'Event') {
return Event::all()->pluck('title', 'id');
};
if ($get('model_select') === 'Custom') {
return [];
};
}),
]),
]),
Forms\Components\Section::make('Слайдер')->schema([
Forms\Components\TextInput::make('title')
->label('Заголовок слайда')
->required(),
Forms\Components\Textarea::make('content')
->label('Текст слайда'),
FileUpload::make('image')
->label('Изображение')
->image()
->optimize('webp')
->resize(50)
->disk('public')
->directory('images')
->imageEditor(),
Forms\Components\Grid::make(2)->schema([
Forms\Components\TextInput::make('link')
->label('Ссылка кнопки')
->required(),
Forms\Components\TextInput::make('link_text')
->default('Читать')
->label('Текст кнопки')
->required(),
]),
ColorPicker::make('color_theme')
->label('Цвет текста')
->default('#ffffff')
->required(),
Toggle::make('is_active')->default(true)->label('Активный слайдер')->inline(false),
]),
]);
}
public static function table(Table $table): Table
{
return $table
->reorderable('sort')
->defaultSort('sort')
->columns([
Tables\Columns\TextColumn::make('title'),
Tables\Columns\ToggleColumn::make('is_active')
])
->filters([
//
])
->actions([
Tables\Actions\EditAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}
public static function getRelations(): array
{
return [
//
];
}
public static function getPages(): array
{
return [
'index' => Pages\ListMainSliders::route('/'),
'create' => Pages\CreateMainSlider::route('/create'),
'edit' => Pages\EditMainSlider::route('/{record}/edit'),
];
}
}