Files
nexusphp/app/Filament/Resources/Section/CategoryResource.php

146 lines
5.7 KiB
PHP
Raw Normal View History

2022-09-07 17:10:52 +08:00
<?php
2022-10-27 20:21:54 +08:00
namespace App\Filament\Resources\Section;
2022-09-07 17:10:52 +08:00
2022-10-27 20:21:54 +08:00
use App\Filament\Resources\Section\CategoryResource\Pages;
use App\Filament\Resources\Section\CategoryResource\RelationManagers;
use App\Models\Category;
2022-09-13 02:41:10 +08:00
use App\Models\Icon;
2022-11-05 18:43:49 +08:00
use App\Models\NexusModel;
2022-10-27 20:21:54 +08:00
use App\Models\SearchBox;
2023-04-07 02:19:28 +08:00
use App\Models\Torrent;
2022-11-05 18:43:49 +08:00
use App\Repositories\SearchBoxRepository;
use Filament\Facades\Filament;
2022-09-07 17:10:52 +08:00
use Filament\Forms;
2024-12-25 23:09:07 +08:00
use Filament\Forms\Form;
2022-10-27 20:21:54 +08:00
use Filament\Resources\Resource;
2024-12-25 23:09:07 +08:00
use Filament\Tables\Table;
2022-09-07 17:10:52 +08:00
use Filament\Tables;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
2022-11-05 18:43:49 +08:00
use Illuminate\Support\Collection;
2022-09-07 17:10:52 +08:00
2022-10-27 20:21:54 +08:00
class CategoryResource extends Resource
2022-09-07 17:10:52 +08:00
{
2022-10-27 20:21:54 +08:00
protected static ?string $model = Category::class;
2022-09-07 17:10:52 +08:00
2024-12-25 23:09:07 +08:00
protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
2022-09-07 17:10:52 +08:00
2022-10-27 20:21:54 +08:00
protected static ?string $navigationGroup = 'Section';
protected static ?int $navigationSort = 2;
2024-12-25 23:09:07 +08:00
public static function getNavigationLabel(): string
2022-10-27 20:21:54 +08:00
{
return __('admin.sidebar.category');
}
public static function getBreadcrumb(): string
2022-09-07 17:10:52 +08:00
{
2022-10-27 20:21:54 +08:00
return self::getNavigationLabel();
2022-09-07 17:10:52 +08:00
}
public static function form(Form $form): Form
{
return $form
->schema([
2022-10-27 20:21:54 +08:00
Forms\Components\Select::make('mode')
2022-11-05 01:08:04 +08:00
->options(SearchBox::listModeOptions())
2022-10-27 20:21:54 +08:00
->label(__('label.search_box.label'))
2023-04-07 02:19:28 +08:00
->rules([
'required',
function () {
return function ($attribute, $value, $fail) {
//@todo how to get the editing record ?
$exists = Torrent::query()->where('category', $value)->exists();
do_log("check $attribute: $value torrent if exists: $exists");
// if ($exists) {
// $fail("There are torrents belonging to this category that cannot be changed!");
// }
};
}
])
2022-10-27 20:21:54 +08:00
,
Forms\Components\TextInput::make('name')->required()->label(__('label.search_box.taxonomy.name'))->required(),
2022-09-07 17:10:52 +08:00
Forms\Components\TextInput::make('image')
->label(__('label.search_box.taxonomy.image'))
->helperText(__('label.search_box.taxonomy.image_help'))
->required()
2022-09-07 17:10:52 +08:00
,
2022-09-13 02:41:10 +08:00
Forms\Components\Select::make('icon_id')
->options(Icon::query()->pluck('name', 'id')->toArray())
->label(__('label.search_box.taxonomy.icon_id'))
->required()
2022-09-13 02:41:10 +08:00
,
2022-09-07 17:10:52 +08:00
Forms\Components\TextInput::make('class_name')
->label(__('label.search_box.taxonomy.class_name'))
->helperText(__('label.search_box.taxonomy.class_name_help'))
,
Forms\Components\TextInput::make('sort_index')
->default(0)
2023-04-21 00:50:10 +08:00
->label(__('label.priority'))
->helperText(__('label.priority_help'))
2022-09-07 17:10:52 +08:00
,
2022-10-27 20:21:54 +08:00
2022-09-07 17:10:52 +08:00
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('id'),
2022-10-27 20:21:54 +08:00
Tables\Columns\TextColumn::make('search_box.name')->label(__('label.search_box.label')),
Tables\Columns\TextColumn::make('name')->label(__('label.search_box.taxonomy.name'))->searchable(),
2022-09-13 02:41:10 +08:00
Tables\Columns\TextColumn::make('icon.name')->label(__('label.search_box.taxonomy.icon_id')),
2022-09-07 17:10:52 +08:00
Tables\Columns\TextColumn::make('image')->label(__('label.search_box.taxonomy.image')),
Tables\Columns\TextColumn::make('class_name')->label(__('label.search_box.taxonomy.class_name')),
Tables\Columns\TextColumn::make('sort_index')->label(__('label.search_box.taxonomy.sort_index'))->sortable(),
2022-09-07 17:10:52 +08:00
])
2023-04-21 00:50:10 +08:00
->defaultSort('sort_index', 'desc')
2022-09-07 17:10:52 +08:00
->filters([
2022-10-27 20:21:54 +08:00
Tables\Filters\SelectFilter::make('mode')
->options(SearchBox::query()->pluck('name', 'id')->toArray())
->label(__('label.search_box.label'))
,
2022-09-07 17:10:52 +08:00
])
->actions([
2022-10-27 20:21:54 +08:00
Tables\Actions\EditAction::make(),
2022-11-05 18:43:49 +08:00
Tables\Actions\DeleteAction::make()->using(function (NexusModel $record) {
try {
$rep = new SearchBoxRepository();
$rep->deleteCategory($record->id);
} catch (\Exception $exception) {
Filament::notify('danger', $exception->getMessage() ?: class_basename($exception));
}
}),
2022-09-07 17:10:52 +08:00
])
->bulkActions([
2022-11-05 18:43:49 +08:00
Tables\Actions\DeleteBulkAction::make()->using(function (Collection $records) {
try {
$rep = new SearchBoxRepository();
$rep->deleteCategory($records->pluck('id')->toArray());
} catch (\Exception $exception) {
Filament::notify('danger', $exception->getMessage() ?: class_basename($exception));
}
}),
2022-09-07 17:10:52 +08:00
]);
}
2022-10-27 20:21:54 +08:00
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'),
];
}
2022-09-07 17:10:52 +08:00
}