HistoryResource.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace App\Filament\Resources;
  3. use App\Filament\Resources\HistoryResource\Pages;
  4. use App\Models\History;
  5. use App\Service\DeepLService;
  6. use Filament\Forms\Components\FileUpload;
  7. use Filament\Forms\Components\Group;
  8. use Filament\Forms\Components\Radio;
  9. use Filament\Forms\Components\Section;
  10. use Filament\Forms\Components\Select;
  11. use Filament\Forms\Components\TextInput;
  12. use Filament\Forms\Form;
  13. use Filament\Resources\Resource;
  14. use Filament\Tables;
  15. use Filament\Tables\Columns\ImageColumn;
  16. use Filament\Tables\Columns\TextColumn;
  17. use Filament\Tables\Filters\SelectFilter;
  18. use Filament\Tables\Table;
  19. use Illuminate\Database\Eloquent\Builder;
  20. use SolutionForest\FilamentTranslateField\Forms\Component\Translate;
  21. class HistoryResource extends Resource
  22. {
  23. protected static ?string $model = History::class;
  24. protected static ?string $navigationIcon = 'heroicon-o-list-bullet';
  25. protected static ?string $navigationLabel = '宏匯里程管理';
  26. protected static ?string $navigationGroup = '關於宏匯';
  27. protected static ?string $modelLabel = '宏匯里程管理';
  28. public ?string $tableSortColumn = null;
  29. public ?string $tableSortDirection = null;
  30. public static function form(Form $form): Form
  31. {
  32. return $form
  33. ->schema([
  34. Section::make('')->schema([
  35. Select::make('selected_year')->label('年份')->options(function () {
  36. $currentYear = now()->year;
  37. $years = [];
  38. for ($i = $currentYear - 15; $i <= $currentYear + 5; $i++) {
  39. $years[$i] = strval($i).'年';
  40. }
  41. return $years;
  42. })->columnSpan(1),
  43. Select::make('selected_month')->label('月份')->options(function () {
  44. $months = [];
  45. for ($i = 1; $i <= 12; $i++) {
  46. $months[$i] = strval($i).'月';
  47. }
  48. return $months;
  49. })->default(now()->month)->columnSpan(1),
  50. Translate::make()->schema(fn (string $locale) => [
  51. TextInput::make('title')->required($locale == 'zh_TW')->label('標題'),
  52. ])
  53. ->locales(['zh_TW', 'en'])
  54. ->actions([
  55. app(DeepLService::class)->createTranslationAction('Main', ['title']),
  56. ])->columnSpanFull(),
  57. Group::make()->schema([
  58. FileUpload::make('img_url')->label('圖片')->directory('histories')
  59. ->image()
  60. ->optimize('webp')
  61. ->acceptedFileTypes(['image/jpeg', 'image/jpg', 'image/png', 'image/webp'])->imageEditor()
  62. ->columnSpanFull(),
  63. ])->columnSpanFull(),
  64. Radio::make('visible')->label('顯示/不顯示')->options([1 => '顯示', 0 => '不顯示'])->inline()->default(1)->columnSpan(2),
  65. ])->columns(3),
  66. ]);
  67. }
  68. public static function table(Table $table): Table
  69. {
  70. return $table
  71. ->columns([
  72. TextColumn::make('selected_year')->label('年份')->alignCenter(),
  73. TextColumn::make('selected_month')->label('月份')->alignCenter(),
  74. TextColumn::make('title')->label('標題'),
  75. ImageColumn::make('img_url')->label('圖片')->disk(config('filesystem.default'))->alignCenter(),
  76. ])
  77. ->filters([
  78. SelectFilter::make('selected_year')->label('年份')
  79. ->options(function () {
  80. $currentYear = now()->year;
  81. $years = [];
  82. // 從 10 年前到 5 年後
  83. for ($i = $currentYear - 10; $i <= $currentYear + 10; $i++) {
  84. $years[$i] = strval($i).'年';
  85. }
  86. return $years;
  87. })
  88. ->attribute('selected_year'),
  89. SelectFilter::make('selected_month')->label('月份')
  90. ->options(function () {
  91. $months = [];
  92. for ($i = 1; $i <= 12; $i++) {
  93. $months[$i] = strval($i).'月';
  94. }
  95. return $months;
  96. })
  97. ->attribute('selected_month'),
  98. ])
  99. ->actions([
  100. Tables\Actions\EditAction::make(),
  101. ])
  102. ->bulkActions([
  103. Tables\Actions\BulkActionGroup::make([
  104. Tables\Actions\DeleteBulkAction::make(),
  105. ]),
  106. ])
  107. ->modifyQueryUsing(function (Builder $query) {
  108. // ✅ 正確:多欄位排序
  109. return $query
  110. ->orderBy('selected_year', 'desc')
  111. ->orderBy('selected_month', 'desc');
  112. });
  113. }
  114. public static function getRelations(): array
  115. {
  116. return [
  117. //
  118. ];
  119. }
  120. public static function getPages(): array
  121. {
  122. return [
  123. 'index' => Pages\ListHistories::route('/'),
  124. 'create' => Pages\CreateHistory::route('/create'),
  125. 'edit' => Pages\EditHistory::route('/{record}/edit'),
  126. ];
  127. }
  128. }