EgsHistoryResource.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace App\Filament\Resources;
  3. use App\Filament\Resources\EgsHistoryResource\Pages;
  4. use App\Filament\Resources\EgsHistoryResource\RelationManagers;
  5. use App\Models\EgsHistory;
  6. use Filament\Forms;
  7. use Filament\Forms\Components\Radio;
  8. use Filament\Forms\Components\Section;
  9. use Filament\Forms\Components\Select;
  10. use Filament\Forms\Components\Textarea;
  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\TextColumn;
  16. use Filament\Tables\Filters\SelectFilter;
  17. use Filament\Tables\Table;
  18. use Illuminate\Database\Eloquent\Builder;
  19. use Illuminate\Database\Eloquent\SoftDeletingScope;
  20. use SolutionForest\FilamentTranslateField\Forms\Component\Translate;
  21. use App\Service\DeepLService;
  22. class EgsHistoryResource extends Resource
  23. {
  24. protected static ?string $model = EgsHistory::class;
  25. protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
  26. public static function form(Form $form): Form
  27. {
  28. return $form
  29. ->schema([
  30. Section::make("")->schema([
  31. Select::make("selected_year")->label("年份")->options(function () {
  32. $currentYear = now()->year;
  33. $years = [];
  34. // 從 10 年前到 5 年後
  35. for ($i = $currentYear - 10; $i <= $currentYear; $i++) {
  36. $years[$i] = strval($i) . '年';
  37. }
  38. return $years;
  39. })->columnSpan(1),
  40. Select::make("selected_month")->label("月份")->options(function (){
  41. $months = [];
  42. for($i=1;$i<=12 ;$i++){
  43. $months[$i] = strval($i) . "月";
  44. }
  45. return $months;
  46. })->default(now()->month)->columnSpan(1),
  47. Translate::make()->schema(fn (string $locale) => [
  48. TextInput::make('title')->required($locale == 'zh_TW')->label("標題"),
  49. Textarea::make('description')->required($locale == 'zh_TW')->label("內文"),
  50. ])
  51. ->locales(["zh_TW", "en"])
  52. ->actions([
  53. app(DeepLService::class)->createTranslationAction("Main", ["title"])
  54. ])->columnSpanFull(),
  55. Radio::make("visible")->label("顯示/不顯示")->options([1 => "顯示", 0 => "不顯示"])->inline()->default(1)->columnSpan(2)
  56. ])->columns(3)
  57. ]);
  58. }
  59. public static function table(Table $table): Table
  60. {
  61. return $table
  62. ->columns([
  63. TextColumn::make("selected_year")->label("年份")->alignCenter(),
  64. TextColumn::make("selected_month")->label("月份")->alignCenter(),
  65. TextColumn::make("title")->label("標題"),
  66. ])
  67. ->filters([
  68. SelectFilter::make('selected_year')->label("年份")
  69. ->options(function () {
  70. $currentYear = now()->year;
  71. $years = [];
  72. // 從 10 年前到 5 年後
  73. for ($i = $currentYear - 10; $i <= $currentYear + 10; $i++) {
  74. $years[$i] = strval($i) . '年';
  75. }
  76. return $years;
  77. })
  78. ->attribute('selected_year'),
  79. SelectFilter::make('selected_month')->label("月份")
  80. ->options(function () {
  81. $months = [];
  82. for($i=1;$i<=12 ;$i++){
  83. $months[$i] = strval($i) . "月";
  84. }
  85. return $months;
  86. })
  87. ->attribute('selected_month'),
  88. ])
  89. ->actions([
  90. Tables\Actions\EditAction::make(),
  91. ])
  92. ->bulkActions([
  93. Tables\Actions\BulkActionGroup::make([
  94. Tables\Actions\DeleteBulkAction::make(),
  95. ]),
  96. ])
  97. ->modifyQueryUsing(function (Builder $query) {
  98. // ✅ 正確:多欄位排序
  99. return $query
  100. ->orderBy('selected_year', 'desc')
  101. ->orderBy('selected_month', 'desc');
  102. });
  103. }
  104. public static function getRelations(): array
  105. {
  106. return [
  107. //
  108. ];
  109. }
  110. public static function getPages(): array
  111. {
  112. return [
  113. 'index' => Pages\ListEgsHistories::route('/'),
  114. 'create' => Pages\CreateEgsHistory::route('/create'),
  115. 'edit' => Pages\EditEgsHistory::route('/{record}/edit'),
  116. ];
  117. }
  118. }