BannerResource.php 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace App\Filament\Resources;
  3. use App\Filament\Resources\BannerResource\Pages;
  4. use App\Models\Banner;
  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\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\IconColumn;
  16. use Filament\Tables\Columns\ImageColumn;
  17. use Filament\Tables\Columns\TextColumn;
  18. use Filament\Tables\Table;
  19. use SolutionForest\FilamentTranslateField\Forms\Component\Translate;
  20. class BannerResource extends Resource
  21. {
  22. protected static ?string $model = Banner::class;
  23. protected static ?int $navigationSort = 1;
  24. protected static ?string $navigationIcon = 'heroicon-o-photo';
  25. protected static ?string $navigationGroup = '其他設定';
  26. // protected static ?string $navigationGroup = '上稿內容管理';
  27. protected static ?string $navigationLabel = '首頁 Banner 管理';
  28. protected static ?string $modelLabel = '首頁 Banner 管理';
  29. // 預設排序
  30. protected static function booted()
  31. {
  32. static::addGlobalScope('ordered', function ($query) {
  33. $query->orderBy('order')->orderBy('id');
  34. });
  35. }
  36. public static function form(Form $form): Form
  37. {
  38. return $form
  39. ->schema([
  40. //
  41. Section::make('')->schema([
  42. Group::make()->schema([
  43. Radio::make('type')->label('')->default(1)->options([1 => '圖片', 2 => '影片'])->reactive(),
  44. Translate::make()->schema(fn (string $locale) => [
  45. Textarea::make('title')
  46. ->required(fn ($get) => $locale == 'zh_TW' && $get('../../type') == 1)
  47. ->label('大字標題'),
  48. Textarea::make('content')
  49. ->required(fn ($get) => $locale == 'zh_TW' && $get('../../type') == 1)
  50. ->label('小字標題'),
  51. ])
  52. ->locales(['zh_TW', 'en'])
  53. ->actions([
  54. app(DeepLService::class)->createTranslationAction('Main', ['title', 'content']),
  55. ])->columnSpanFull()
  56. ->visible(fn ($get) => $get('type') == 1),
  57. Group::make()->schema([
  58. FileUpload::make('img_url')->label('圖片')->directory('banners')->columnSpan(1)
  59. ->image()
  60. ->optimize('webp')
  61. ->acceptedFileTypes(['image/jpeg', 'image/jpg', 'image/png', 'image/webp'])
  62. ->required(fn ($get) => $get('type') == 1)
  63. ->imageEditor()
  64. ->visible(fn ($get) => $get('type') == 1),
  65. FileUpload::make('mobile_img')->label('手機板圖片')->directory('banners')->columnSpan(1)
  66. ->image()
  67. ->optimize('webp')
  68. ->acceptedFileTypes(['image/jpeg', 'image/jpg', 'image/png', 'image/webp'])
  69. ->required(fn ($get) => $get('type') == 1)
  70. ->imageEditor()
  71. ->visible(fn ($get) => $get('type') == 1),
  72. Translate::make()->schema(fn (string $locale) => [
  73. TextInput::make('img_alt')
  74. ->required(fn ($get) => $locale == 'zh_TW' && $get('../../type') == 1)
  75. ->label('圖片註釋'),
  76. ])
  77. ->locales(['zh_TW', 'en'])
  78. ->actions([
  79. app(DeepLService::class)->createTranslationAction('Main', ['img_alt']),
  80. ])->columnSpan(1)
  81. ->visible(fn ($get) => $get('../type') == 1),
  82. ])->columnSpanFull(),
  83. TextInput::make('video_url')->label('影片網址')->url()->columnSpanFull()
  84. ->required(fn ($get) => $get('type') == 2)
  85. ->visible(fn ($get) => $get('type') == 2),
  86. Radio::make('visible')->label('顯示/不顯示')->options([1 => '顯示', 0 => '不顯示'])->inline()->default(1)->columnSpan(2),
  87. ])->columns(4)->columnSpanFull(),
  88. ]),
  89. ]);
  90. }
  91. public static function table(Table $table): Table
  92. {
  93. return $table
  94. ->columns([
  95. IconColumn::make('type')->label('類別')
  96. ->icon(fn (string $state): string => match ($state) {
  97. '1' => 'heroicon-o-photo',
  98. '2' => 'heroicon-o-film',
  99. }),
  100. ImageColumn::make('img_url')->label('圖片')
  101. ->getStateUsing(fn ($record) => $record->type == 1 ? $record->img_url : null),
  102. TextColumn::make('title')->label('標題')
  103. ->getStateUsing(fn ($record) => $record->type == 1 ? $record->title : null),
  104. TextColumn::make('video_url')->label('影片網址')
  105. ->formatStateUsing(fn ($record) => $record->type == 2 ? $record->video_url : null),
  106. ])
  107. ->filters([
  108. //
  109. ])
  110. ->actions([
  111. Tables\Actions\EditAction::make(),
  112. ])
  113. ->bulkActions([
  114. Tables\Actions\BulkActionGroup::make([
  115. Tables\Actions\DeleteBulkAction::make(),
  116. ]),
  117. ])
  118. ->reorderable('order')
  119. ->defaultSort('order');
  120. }
  121. public static function getRelations(): array
  122. {
  123. return [
  124. //
  125. ];
  126. }
  127. public static function getPages(): array
  128. {
  129. return [
  130. 'index' => Pages\ListBanners::route('/'),
  131. 'create' => Pages\CreateBanner::route('/create'),
  132. 'edit' => Pages\EditBanner::route('/{record}/edit'),
  133. ];
  134. }
  135. }