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

157 lines
6.0 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
use Filament\Schemas\Schema;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Actions\EditAction;
use Filament\Actions\DeleteAction;
use Exception;
use Filament\Actions\DeleteBulkAction;
use App\Filament\Resources\Section\CategoryResource\Pages\ListCategories;
use App\Filament\Resources\Section\CategoryResource\Pages\CreateCategory;
use App\Filament\Resources\Section\CategoryResource\Pages\EditCategory;
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;
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
protected static string | \BackedEnum | null $navigationIcon = 'heroicon-o-rectangle-stack';
2022-09-07 17:10:52 +08:00
protected static string | \UnitEnum | null $navigationGroup = 'Section';
2022-10-27 20:21:54 +08:00
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(Schema $schema): Schema
2022-09-07 17:10:52 +08:00
{
return $schema
->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
,
TextInput::make('name')->required()->label(__('label.search_box.taxonomy.name'))->required(),
TextInput::make('image')
2022-09-07 17:10:52 +08:00
->label(__('label.search_box.taxonomy.image'))
->helperText(__('label.search_box.taxonomy.image_help'))
->required()
2022-09-07 17:10:52 +08:00
,
Select::make('icon_id')
2022-09-13 02:41:10 +08:00
->options(Icon::query()->pluck('name', 'id')->toArray())
->label(__('label.search_box.taxonomy.icon_id'))
->required()
2022-09-13 02:41:10 +08:00
,
TextInput::make('class_name')
2022-09-07 17:10:52 +08:00
->label(__('label.search_box.taxonomy.class_name'))
->helperText(__('label.search_box.taxonomy.class_name_help'))
,
TextInput::make('sort_index')
2022-09-07 17:10:52 +08:00
->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([
TextColumn::make('id'),
TextColumn::make('search_box.name')->label(__('label.search_box.label')),
TextColumn::make('name')->label(__('label.search_box.taxonomy.name'))->searchable(),
TextColumn::make('icon.name')->label(__('label.search_box.taxonomy.icon_id')),
TextColumn::make('image')->label(__('label.search_box.taxonomy.image')),
TextColumn::make('class_name')->label(__('label.search_box.taxonomy.class_name')),
TextColumn::make('sort_index')->label(__('label.priority'))->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([
SelectFilter::make('mode')
2022-10-27 20:21:54 +08:00
->options(SearchBox::query()->pluck('name', 'id')->toArray())
->label(__('label.search_box.label'))
,
2022-09-07 17:10:52 +08:00
])
->recordActions([
EditAction::make(),
DeleteAction::make()->using(function (NexusModel $record) {
2022-11-05 18:43:49 +08:00
try {
$rep = new SearchBoxRepository();
$rep->deleteCategory($record->id);
} catch (Exception $exception) {
2022-11-05 18:43:49 +08:00
Filament::notify('danger', $exception->getMessage() ?: class_basename($exception));
}
}),
2022-09-07 17:10:52 +08:00
])
->toolbarActions([
DeleteBulkAction::make()->using(function (Collection $records) {
2022-11-05 18:43:49 +08:00
try {
$rep = new SearchBoxRepository();
$rep->deleteCategory($records->pluck('id')->toArray());
} catch (Exception $exception) {
2022-11-05 18:43:49 +08:00
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' => ListCategories::route('/'),
'create' => CreateCategory::route('/create'),
'edit' => EditCategory::route('/{record}/edit'),
2022-10-27 20:21:54 +08:00
];
}
2022-09-07 17:10:52 +08:00
}