HistoryResource.php 5.7KB

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