HistoryResource.php 5.4KB

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