TextbookCatalogResource.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. namespace App\Filament\Resources;
  3. use App\Filament\Resources\TextbookCatalogResource\Pages;
  4. use App\Models\Textbook;
  5. use App\Models\TextbookCatalog;
  6. use BackedEnum;
  7. use UnitEnum;
  8. use Filament\Resources\Resource;
  9. use Filament\Forms\Components;
  10. use Filament\Schemas\Schema;
  11. use Filament\Tables;
  12. use Filament\Tables\Columns\TextColumn;
  13. use Filament\Tables\Columns\BadgeColumn;
  14. use Filament\Actions\EditAction;
  15. use Filament\Actions\DeleteAction;
  16. use Filament\Actions\Action;
  17. use Illuminate\Database\Eloquent\Model;
  18. class TextbookCatalogResource extends Resource
  19. {
  20. protected static ?string $model = TextbookCatalog::class;
  21. protected static ?string $recordTitleAttribute = 'title';
  22. protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-squares-2x2';
  23. protected static ?string $navigationLabel = '教材目录';
  24. protected static UnitEnum|string|null $navigationGroup = '教材管理';
  25. protected static ?int $navigationSort = 3;
  26. public static function form(Schema $schema): Schema
  27. {
  28. return $schema->components([
  29. Components\Select::make('textbook_id')
  30. ->label('教材')
  31. ->options(
  32. Textbook::query()
  33. ->orderBy('id')
  34. ->pluck('official_title', 'id')
  35. ->toArray()
  36. )
  37. ->searchable()
  38. ->required(),
  39. Components\TextInput::make('title')
  40. ->label('目录标题')
  41. ->required(),
  42. Components\TextInput::make('display_no')
  43. ->label('编号'),
  44. Components\Select::make('node_type')
  45. ->label('类型')
  46. ->options([
  47. 'chapter' => '章',
  48. 'section' => '节',
  49. 'subsection' => '小节',
  50. 'item' => '条目',
  51. 'project' => '项目学习',
  52. 'reading' => '阅读材料',
  53. 'practice' => '综合实践',
  54. 'summary' => '复习',
  55. 'appendix' => '附录',
  56. ])
  57. ->required(),
  58. Components\TextInput::make('depth')
  59. ->label('层级')
  60. ->numeric(),
  61. Components\TextInput::make('sort_order')
  62. ->label('排序')
  63. ->numeric(),
  64. Components\TextInput::make('page_start')
  65. ->label('起始页码')
  66. ->numeric(),
  67. Components\TextInput::make('page_end')
  68. ->label('结束页码')
  69. ->numeric(),
  70. ])->columns(2);
  71. }
  72. public static function table(Tables\Table $table): Tables\Table
  73. {
  74. return $table
  75. ->columns([
  76. TextColumn::make('textbook.series_name')
  77. ->label('教材系列')
  78. ->searchable()
  79. ->wrap(),
  80. TextColumn::make('textbook.official_title')
  81. ->label('教材')
  82. ->searchable()
  83. ->wrap(),
  84. TextColumn::make('title')
  85. ->label('目录标题')
  86. ->searchable()
  87. ->wrap(),
  88. TextColumn::make('display_no')
  89. ->label('编号')
  90. ->searchable(),
  91. BadgeColumn::make('node_type')
  92. ->label('类型')
  93. ->formatStateUsing(fn (string $state): string => match ($state) {
  94. 'chapter' => '章',
  95. 'section' => '节',
  96. 'subsection' => '小节',
  97. 'item' => '条目',
  98. 'project' => '项目学习',
  99. 'reading' => '阅读材料',
  100. 'practice' => '综合实践',
  101. 'summary' => '复习',
  102. 'appendix' => '附录',
  103. default => '其他',
  104. })
  105. ->color('info'),
  106. TextColumn::make('depth')
  107. ->label('层级')
  108. ->sortable(),
  109. TextColumn::make('page_start')
  110. ->label('起始页码')
  111. ->sortable(),
  112. TextColumn::make('page_end')
  113. ->label('结束页码')
  114. ->sortable(),
  115. TextColumn::make('created_at')
  116. ->label('创建时间')
  117. ->dateTime('Y-m-d H:i')
  118. ->sortable()
  119. ->toggleable(),
  120. ])
  121. ->filters([
  122. Tables\Filters\SelectFilter::make('textbook_id')
  123. ->label('教材')
  124. ->options(function () {
  125. return Textbook::query()
  126. ->orderBy('id')
  127. ->pluck('official_title', 'id')
  128. ->toArray();
  129. })
  130. ->searchable()
  131. ->preload(),
  132. Tables\Filters\SelectFilter::make('node_type')
  133. ->label('节点类型')
  134. ->options([
  135. 'chapter' => '章',
  136. 'section' => '节',
  137. 'subsection' => '小节',
  138. 'item' => '条目',
  139. 'project' => '项目学习',
  140. 'reading' => '阅读材料',
  141. 'practice' => '综合实践',
  142. 'summary' => '复习',
  143. 'appendix' => '附录',
  144. 'custom' => '其他',
  145. ]),
  146. ])
  147. ->actions([
  148. EditAction::make()
  149. ->label('编辑'),
  150. Action::make('delete')
  151. ->label('删除')
  152. ->color('danger')
  153. ->icon('heroicon-o-trash')
  154. ->requiresConfirmation()
  155. ->modalHeading('删除教材目录')
  156. ->modalDescription('确定要删除这个教材目录吗?此操作无法撤销。')
  157. ->action(function (Model $record) {
  158. $record->delete();
  159. return redirect()->refresh();
  160. }),
  161. ])
  162. ->paginated([10, 25, 50, 100])
  163. ->poll(null); // 禁用自动刷新
  164. }
  165. public static function getEloquentQuery(): \Illuminate\Database\Eloquent\Builder
  166. {
  167. return parent::getEloquentQuery()
  168. ->with(['textbook', 'textbook.series']);
  169. }
  170. public static function getPages(): array
  171. {
  172. return [
  173. 'index' => Pages\ManageTextbookCatalogs::route('/'),
  174. ];
  175. }
  176. public static function canViewAny(): bool
  177. {
  178. // 临时允许所有用户查看,等待权限系统完善
  179. return true;
  180. }
  181. public static function canCreate(): bool
  182. {
  183. // 临时允许所有用户创建,等待权限系统完善
  184. return true;
  185. }
  186. public static function canEdit(Model $record): bool
  187. {
  188. // 临时允许所有用户编辑,等待权限系统完善
  189. return true;
  190. }
  191. public static function canDelete(Model $record): bool
  192. {
  193. // 临时允许所有用户删除,等待权限系统完善
  194. return true;
  195. }
  196. public static function canDeleteAny(): bool
  197. {
  198. // 临时允许所有用户批量删除,等待权限系统完善
  199. return true;
  200. }
  201. protected static function deleteRecord(Model $record): bool
  202. {
  203. return (bool) $record->delete();
  204. }
  205. }