EsgHistoryResource.php 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace App\Filament\Resources;
  3. use App\Filament\Resources\EsgHistoryResource\Pages;
  4. use App\Filament\Resources\EsgHistoryResource\RelationManagers;
  5. use App\Models\EsgHistory;
  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. for ($i = $currentYear - 15; $i <= $currentYear + 5; $i++) {
  40. $years[$i] = strval($i) . '年';
  41. }
  42. return $years;
  43. })->columnSpan(1),
  44. Select::make("selected_month")->label("月份")->options(function (){
  45. $months = [];
  46. for($i=1;$i<=12 ;$i++){
  47. $months[$i] = strval($i) . "月";
  48. }
  49. return $months;
  50. })->default(now()->month)->columnSpan(1),
  51. Translate::make()->schema(fn (string $locale) => [
  52. TextInput::make('title')->required($locale == 'zh_TW')->label("標題"),
  53. Textarea::make('description')->required($locale == 'zh_TW')->label("內文"),
  54. ])
  55. ->locales(["zh_TW", "en"])
  56. ->actions([
  57. app(DeepLService::class)->createTranslationAction("Main", ["title"])
  58. ])->columnSpanFull(),
  59. Radio::make("visible")->label("顯示/不顯示")->options([1 => "顯示", 0 => "不顯示"])->inline()->default(1)->columnSpan(2)
  60. ])->columns(3)
  61. ]);
  62. }
  63. public static function table(Table $table): Table
  64. {
  65. return $table
  66. ->columns([
  67. TextColumn::make("selected_year")->label("年份")->alignCenter(),
  68. TextColumn::make("selected_month")->label("月份")->alignCenter(),
  69. TextColumn::make("title")->label("標題"),
  70. ])
  71. ->filters([
  72. SelectFilter::make('selected_year')->label("年份")
  73. ->options(function () {
  74. $currentYear = now()->year;
  75. $years = [];
  76. // 從 10 年前到 5 年後
  77. for ($i = $currentYear - 10; $i <= $currentYear + 10; $i++) {
  78. $years[$i] = strval($i) . '年';
  79. }
  80. return $years;
  81. })
  82. ->attribute('selected_year'),
  83. SelectFilter::make('selected_month')->label("月份")
  84. ->options(function () {
  85. $months = [];
  86. for($i=1;$i<=12 ;$i++){
  87. $months[$i] = strval($i) . "月";
  88. }
  89. return $months;
  90. })
  91. ->attribute('selected_month'),
  92. ])
  93. ->actions([
  94. Tables\Actions\EditAction::make(),
  95. ])
  96. ->bulkActions([
  97. Tables\Actions\BulkActionGroup::make([
  98. Tables\Actions\DeleteBulkAction::make(),
  99. ]),
  100. ])
  101. ->modifyQueryUsing(function (Builder $query) {
  102. // ✅ 正確:多欄位排序
  103. return $query
  104. ->orderBy('selected_year', 'desc')
  105. ->orderBy('selected_month', 'desc');
  106. });
  107. }
  108. public static function getRelations(): array
  109. {
  110. return [
  111. //
  112. ];
  113. }
  114. public static function getPages(): array
  115. {
  116. return [
  117. 'index' => Pages\ListEsgHistories::route('/'),
  118. 'create' => Pages\CreateEsgHistory::route('/create'),
  119. 'edit' => Pages\EditEsgHistory::route('/{record}/edit'),
  120. ];
  121. }
  122. }