Files
nexusphp/app/Filament/Resources/System/ExamResource.php

190 lines
8.7 KiB
PHP
Raw Normal View History

2022-06-27 01:39:01 +08:00
<?php
namespace App\Filament\Resources\System;
use App\Filament\OptionsTrait;
2022-06-27 01:39:01 +08:00
use App\Filament\Resources\System\ExamResource\Pages;
use App\Filament\Resources\System\ExamResource\RelationManagers;
use App\Models\Exam;
use App\Repositories\ExamRepository;
2022-06-27 01:39:01 +08:00
use App\Repositories\UserRepository;
use Filament\Forms;
use Filament\Resources\Form;
use Filament\Resources\Resource;
use Filament\Resources\Table;
use Filament\Tables;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
2022-07-02 15:08:23 +08:00
use Illuminate\Validation\Rule;
2022-06-27 01:39:01 +08:00
class ExamResource extends Resource
{
use OptionsTrait;
2022-06-27 01:39:01 +08:00
protected static ?string $model = Exam::class;
protected static ?string $navigationIcon = 'heroicon-o-exclamation';
protected static ?string $navigationGroup = 'System';
2022-07-02 15:08:23 +08:00
protected static ?int $navigationSort = 1;
2022-07-02 15:08:23 +08:00
const IS_DISCOVERED_OPTIONS = ['0' => 'No', '1' => 'Yes'];
2022-06-27 01:39:01 +08:00
protected static function getNavigationLabel(): string
{
return __('admin.sidebar.exams_list');
}
2022-06-29 17:00:15 +08:00
public static function getBreadcrumb(): string
{
return self::getNavigationLabel();
}
2022-06-27 01:39:01 +08:00
public static function form(Form $form): Form
{
$userRep = new UserRepository();
return $form
->schema([
2022-07-02 15:08:23 +08:00
Forms\Components\Section::make(__('label.exam.section_base_info'))->schema([
Forms\Components\TextInput::make('name')->required()->columnSpan(['sm' => 2])->label(__('label.name')),
2024-05-18 14:53:30 +08:00
Forms\Components\Select::make('type')
->required()->columnSpan(['sm' => 2])
->label(__('exam.type'))
->options(Exam::listTypeOptions())
->helperText(__('exam.type_help'))
->reactive()
,
Forms\Components\TextInput::make('success_reward_bonus')
2024-06-07 02:51:05 +08:00
->required()
2024-05-18 14:53:30 +08:00
->label(__('exam.success_reward_bonus'))
->hidden(fn (\Closure $get) => $get('type') != Exam::TYPE_TASK)
,
Forms\Components\TextInput::make('fail_deduct_bonus')
2024-06-07 02:51:05 +08:00
->required()
2024-05-18 14:53:30 +08:00
->label(__('exam.fail_deduct_bonus'))
->hidden(fn (\Closure $get) => $get('type') != Exam::TYPE_TASK)
,
2022-07-02 15:08:23 +08:00
Forms\Components\Repeater::make('indexes')->schema([
Forms\Components\Select::make('index')
->options(Exam::listIndex(true))
->label(__('label.exam.index_required_label'))
->rules([Rule::in(array_keys(Exam::$indexes))])
->required(),
Forms\Components\TextInput::make('require_value')
->label(__('label.exam.index_required_value'))
->placeholder(__('label.exam.index_placeholder'))
->integer()
->required(),
Forms\Components\Hidden::make('checked')->default(true),
])
->label(__('label.exam.index_formatted'))
->required(),
Forms\Components\Radio::make('status')
2022-07-02 15:08:23 +08:00
->options(self::getEnableDisableOptions())
->inline()
2022-07-02 15:08:23 +08:00
->required()
->label(__('label.status'))
->columnSpan(['sm' => 2]),
Forms\Components\Radio::make('is_discovered')
->options(self::IS_DISCOVERED_OPTIONS)
->label(__('label.exam.is_discovered'))
->inline()
2022-07-02 15:08:23 +08:00
->required()
->columnSpan(['sm' => 2]),
2022-07-02 15:08:23 +08:00
Forms\Components\TextInput::make('priority')
->columnSpan(['sm' => 2])
->integer()
->label(__("label.priority"))
->helperText(__('label.exam.priority_help')),
2022-06-27 01:39:01 +08:00
])->columns(2),
2022-07-02 15:08:23 +08:00
Forms\Components\Section::make(__('label.exam.section_time'))->schema([
Forms\Components\DateTimePicker::make('begin')->label(__('label.begin')),
2022-09-20 18:47:33 +08:00
Forms\Components\DateTimePicker::make('end')->label(__('label.end')),
Forms\Components\TextInput::make('duration')
->integer()
->columnSpan(['sm' => 2])
->label(__('label.duration'))
2022-07-02 15:08:23 +08:00
->helperText(__('label.exam.duration_help')),
2024-04-13 13:55:32 +08:00
Forms\Components\Select::make('recurring')
->options(Exam::listRecurringOptions())
->label(__('exam.recurring'))
->helperText(__('exam.recurring_help'))
->columnSpan(['sm' => 2])
,
2022-06-27 01:39:01 +08:00
])->columns(2),
2022-07-02 15:08:23 +08:00
Forms\Components\Section::make(__('label.exam.section_target_user'))->schema([
Forms\Components\CheckboxList::make('filters.classes')
->options($userRep->listClass())->columnSpan(['sm' => 2])
->columns(4)
->label(__('label.user.class')),
Forms\Components\DateTimePicker::make('filters.register_time_range.0')->label(__("label.exam.register_time_range.begin")),
Forms\Components\DateTimePicker::make('filters.register_time_range.1')->label(__("label.exam.register_time_range.end")),
Forms\Components\TextInput::make('filters.register_days_range.0')->numeric()->label(__("label.exam.register_days_range.begin")),
Forms\Components\TextInput::make('filters.register_days_range.1')->numeric()->label(__("label.exam.register_days_range.end")),
2022-07-02 15:08:23 +08:00
Forms\Components\CheckboxList::make('filters.donate_status')
->options(self::$yesOrNo)
->label(__('label.exam.donated')),
2022-06-27 01:39:01 +08:00
])->columns(2),
2022-07-02 15:08:23 +08:00
Forms\Components\Textarea::make('description')->columnSpan(['sm' => 2])->label(__('label.description')),
2022-06-27 01:39:01 +08:00
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
2022-07-02 16:36:44 +08:00
Tables\Columns\TextColumn::make('id')->sortable(),
Tables\Columns\TextColumn::make('name')->searchable()->label(__('label.name')),
2024-05-18 14:53:30 +08:00
Tables\Columns\TextColumn::make('typeText')->label(__('exam.type')),
Tables\Columns\TextColumn::make('indexFormatted')->label(__('label.exam.index_formatted'))->html(),
Tables\Columns\TextColumn::make('begin')->label(__('label.begin')),
2022-09-20 18:47:33 +08:00
Tables\Columns\TextColumn::make('end')->label(__('label.end')),
Tables\Columns\TextColumn::make('durationText')->label(__('label.duration')),
2024-04-13 13:55:32 +08:00
Tables\Columns\TextColumn::make('recurringText')->label(__('exam.recurring')),
Tables\Columns\TextColumn::make('filterFormatted')->label(__('label.exam.filter_formatted'))->html(),
Tables\Columns\BooleanColumn::make('is_discovered')->label(__('label.exam.is_discovered')),
Tables\Columns\TextColumn::make('priority')->label(__('label.priority')),
Tables\Columns\TextColumn::make('statusText')->label(__('label.status')),
2022-06-27 01:39:01 +08:00
])
2022-07-02 16:36:44 +08:00
->defaultSort('id', 'desc')
2022-06-27 01:39:01 +08:00
->filters([
2024-05-18 14:53:30 +08:00
Tables\Filters\SelectFilter::make('type')->options(Exam::listTypeOptions())->label(__("exam.type")),
Tables\Filters\SelectFilter::make('is_discovered')->options(self::IS_DISCOVERED_OPTIONS)->label(__("label.exam.is_discovered")),
2022-07-02 15:08:23 +08:00
Tables\Filters\SelectFilter::make('status')->options(self::getEnableDisableOptions())->label(__("label.status")),
2022-06-27 01:39:01 +08:00
])
->actions([
2022-07-02 15:08:23 +08:00
Tables\Actions\EditAction::make(),
Tables\Actions\DeleteAction::make()->using(function ($record) {
$rep = new ExamRepository();
$rep->delete($record->id);
}),
2022-06-27 01:39:01 +08:00
])
->bulkActions([
// Tables\Actions\DeleteBulkAction::make(),
2022-06-27 01:39:01 +08:00
]);
}
public static function getRelations(): array
{
return [
//
];
}
public static function getPages(): array
{
return [
'index' => Pages\ListExams::route('/'),
2022-07-02 15:08:23 +08:00
'create' => Pages\CreateExam::route('/create'),
'edit' => Pages\EditExam::route('/{record}/edit'),
2022-06-27 01:39:01 +08:00
];
}
}