| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- <?php
- namespace App\Filament\Resources;
- use App\Filament\Resources\TextbookResource\Pages;
- use App\Filament\Resources\TextbookResource\Schemas\TextbookFormSchema;
- use App\Filament\Resources\TextbookResource\Tables\TextbookTable;
- use App\Filament\Resources\TextbookResource\Actions\DeleteTextbookAction;
- use App\Models\Textbook;
- use App\Services\TextbookApiService;
- use BackedEnum;
- use UnitEnum;
- use Filament\Resources\Resource;
- use Filament\Schemas\Schema;
- use Filament\Tables;
- use Illuminate\Database\Eloquent\Model;
- class TextbookResource extends Resource
- {
- protected static ?string $model = Textbook::class;
- protected static ?string $recordTitleAttribute = 'official_title';
- protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-book-open';
- protected static ?string $navigationLabel = '教材管理';
- protected static UnitEnum|string|null $navigationGroup = '教材管理';
- protected static ?int $navigationSort = 2;
- protected static ?TextbookApiService $apiService = null;
- public static function boot()
- {
- parent::boot();
- static::$apiService = app(TextbookApiService::class);
- }
- protected static function getApiService(): TextbookApiService
- {
- if (!static::$apiService) {
- static::$apiService = app(TextbookApiService::class);
- }
- return static::$apiService;
- }
- public static function form(Schema $schema): Schema
- {
- return TextbookFormSchema::make($schema);
- }
- public static function table(Tables\Table $table): Tables\Table
- {
- return $table
- ->columns([
- \Filament\Tables\Columns\TextColumn::make('id')
- ->label('ID')
- ->sortable(),
- \Filament\Tables\Columns\TextColumn::make('series.name')
- ->label('系列')
- ->searchable(),
- \Filament\Tables\Columns\TextColumn::make('official_title')
- ->label('官方书名')
- ->searchable()
- ->wrap(),
- \Filament\Tables\Columns\TextColumn::make('stage')
- ->label('学段')
- ->formatStateUsing(function ($state): string {
- return match ($state) {
- 'primary' => '小学',
- 'junior' => '初中',
- 'senior' => '高中',
- default => $state,
- };
- })
- ->badge()
- ->color('info'),
- \Filament\Tables\Columns\TextColumn::make('grade')
- ->label('年级')
- ->formatStateUsing(function ($state): string {
- return $state ? "{$state}年级" : '-';
- }),
- \Filament\Tables\Columns\TextColumn::make('semester')
- ->label('学期')
- ->formatStateUsing(function ($state): string {
- return match ($state) {
- 1 => '上学期',
- 2 => '下学期',
- default => '-',
- };
- })
- ->badge()
- ->color('success'),
- \Filament\Tables\Columns\TextColumn::make('naming_scheme')
- ->label('体系')
- ->formatStateUsing(function ($state): string {
- return match ($state) {
- 'new' => '新体系',
- 'old' => '旧体系',
- default => $state,
- };
- })
- ->badge(),
- \Filament\Tables\Columns\TextColumn::make('status')
- ->label('状态')
- ->formatStateUsing(function ($state): string {
- return match ($state) {
- 'draft' => '草稿',
- 'published' => '已发布',
- 'archived' => '已归档',
- default => $state,
- };
- })
- ->badge()
- ->color(function ($state): string {
- return match ($state) {
- 'draft' => 'gray',
- 'published' => 'success',
- 'archived' => 'danger',
- default => 'gray',
- };
- }),
- \Filament\Tables\Columns\TextColumn::make('created_at')
- ->label('创建时间')
- ->dateTime()
- ->sortable()
- ->toggleable(isToggledHiddenByDefault: true),
- \Filament\Tables\Columns\TextColumn::make('updated_at')
- ->label('更新时间')
- ->dateTime()
- ->sortable()
- ->toggleable(isToggledHiddenByDefault: true),
- ])
- ->filters([
- \Filament\Tables\Filters\SelectFilter::make('stage')
- ->label('学段')
- ->options([
- 'primary' => '小学',
- 'junior' => '初中',
- 'senior' => '高中',
- ]),
- \Filament\Tables\Filters\SelectFilter::make('status')
- ->label('状态')
- ->options([
- 'draft' => '草稿',
- 'published' => '已发布',
- 'archived' => '已归档',
- ]),
- ])
- ->actions([
- \Filament\Actions\EditAction::make()
- ->label('编辑')
- ->url(fn($record): string =>
- route('filament.admin.resources.textbooks.edit', ['record' => $record->id])
- ),
- DeleteTextbookAction::make()
- ->label('删除'),
- \Filament\Actions\Action::make('view_catalog')
- ->label('查看目录')
- ->icon('heroicon-o-list-bullet')
- ->url(fn(Model $record): string =>
- route('filament.admin.resources.textbook-catalogs.index', ['tableFilters[textbook_id][value]' => $record->id])
- ),
- ])
- ->bulkActions([
- \Filament\Actions\BulkActionGroup::make([
- \Filament\Actions\DeleteBulkAction::make()
- ->label('批量删除'),
- ]),
- ])
- ->defaultSort('id')
- ->paginated([10, 25, 50, 100]);
- }
- public static function getEloquentQuery(): \Illuminate\Database\Eloquent\Builder
- {
- // 返回空查询,实际数据通过 API 获取
- return parent::getEloquentQuery()->whereRaw('1=0');
- }
- public static function getRecords(): array
- {
- // 从 API 获取教材数据
- $apiService = static::getApiService();
- $result = $apiService->getTextbooks();
- \Log::info('TextbookResource::getRecords called', [
- 'count' => count($result['data'] ?? []),
- 'has_data' => !empty($result['data'])
- ]);
- $records = [];
- foreach ($result['data'] ?? [] as $item) {
- $model = new \App\Models\Textbook($item);
- $model->exists = true;
- $model->id = $item['id'];
- $records[] = $model;
- }
- return $records;
- }
- public static function resolveRecordRouteBinding(int | string $key, ?\Closure $modifyQuery = null): ?\Illuminate\Database\Eloquent\Model
- {
- $record = static::getApiService()->getTextbook((int) $key);
- if (!$record) {
- return null;
- }
- $model = new \App\Models\Textbook($record);
- $model->exists = true;
- $model->id = $record['id'];
- return $model;
- }
- public static function getPages(): array
- {
- return [
- 'index' => Pages\ManageTextbooks::route('/'),
- 'create' => Pages\CreateTextbook::route('/create'),
- 'edit' => Pages\EditTextbook::route('/{record}/edit'),
- ];
- }
- public static function canViewAny(): bool
- {
- // 临时允许所有用户查看,等待权限系统完善
- return true;
- }
- public static function getHeaderActions(): array
- {
- return [
- \Filament\Actions\Action::make('import_excel')
- ->label('Excel导入')
- ->icon('heroicon-o-document-arrow-up')
- ->color('success')
- ->url(fn(): string =>
- route('filament.admin.pages.textbook-excel-import-page')
- ),
- ];
- }
- public static function canCreate(): bool
- {
- // 临时允许所有用户创建,等待权限系统完善
- return true;
- }
- public static function canEdit(Model $record): bool
- {
- // 临时允许所有用户编辑,等待权限系统完善
- return true;
- }
- public static function canDelete(Model $record): bool
- {
- // 临时允许所有用户删除,等待权限系统完善
- return true;
- }
- public static function canDeleteAny(): bool
- {
- // 临时允许所有用户批量删除,等待权限系统完善
- return true;
- }
- }
|