HistoryResource.php 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. ->maxImageWidth(1920)
  62. ->acceptedFileTypes(['image/jpeg', 'image/jpg', 'image/png', 'image/webp'])->imageEditor()
  63. ->columnSpanFull(),
  64. ])->columnSpanFull(),
  65. Radio::make('visible')->label('顯示/不顯示')->options([1 => '顯示', 0 => '不顯示'])->inline()->default(1)->columnSpan(2),
  66. ])->columns(3),
  67. ]);
  68. }
  69. public static function table(Table $table): Table
  70. {
  71. return $table
  72. ->columns([
  73. TextColumn::make('selected_year')->label('年份')->alignCenter(),
  74. TextColumn::make('selected_month')->label('月份')->alignCenter(),
  75. TextColumn::make('title')->label('標題'),
  76. ImageColumn::make('img_url')->label('圖片')->disk(config('filesystem.default'))->alignCenter(),
  77. ])
  78. ->filters([
  79. SelectFilter::make('selected_year')->label('年份')
  80. ->options(function () {
  81. $currentYear = now()->year;
  82. $years = [];
  83. // 從 10 年前到 5 年後
  84. for ($i = $currentYear - 10; $i <= $currentYear + 10; $i++) {
  85. $years[$i] = strval($i).'年';
  86. }
  87. return $years;
  88. })
  89. ->attribute('selected_year'),
  90. SelectFilter::make('selected_month')->label('月份')
  91. ->options(function () {
  92. $months = [];
  93. for ($i = 1; $i <= 12; $i++) {
  94. $months[$i] = strval($i).'月';
  95. }
  96. return $months;
  97. })
  98. ->attribute('selected_month'),
  99. ])
  100. ->actions([
  101. Tables\Actions\EditAction::make(),
  102. ])
  103. ->bulkActions([
  104. Tables\Actions\BulkActionGroup::make([
  105. Tables\Actions\DeleteBulkAction::make(),
  106. ]),
  107. ])
  108. ->modifyQueryUsing(function (Builder $query) {
  109. // ✅ 正確:多欄位排序
  110. return $query
  111. ->orderBy('selected_year', 'desc')
  112. ->orderBy('selected_month', 'desc');
  113. });
  114. }
  115. public static function getRelations(): array
  116. {
  117. return [
  118. //
  119. ];
  120. }
  121. public static function getPages(): array
  122. {
  123. return [
  124. 'index' => Pages\ListHistories::route('/'),
  125. 'create' => Pages\CreateHistory::route('/create'),
  126. 'edit' => Pages\EditHistory::route('/{record}/edit'),
  127. ];
  128. }
  129. }