EsgHistoryResource.php 5.0KB

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