BannerResource.php 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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\ImageColumn;
  16. use Filament\Tables\Columns\TextColumn;
  17. use Filament\Tables\Columns\IconColumn;
  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. ->acceptedFileTypes(['image/jpeg', 'image/jpg', 'image/png', 'image/webp'])
  60. ->required(fn($get) => $get('type') == 1)
  61. ->imageEditor()
  62. ->visible(fn($get) => $get('type') == 1),
  63. FileUpload::make('mobile_img')->label('手機板圖片')->directory('banners')->columnSpan(1)
  64. ->acceptedFileTypes(['image/jpeg', 'image/jpg', 'image/png', 'image/webp'])
  65. ->required(fn($get) => $get('type') == 1)
  66. ->imageEditor()
  67. ->visible(fn($get) => $get('type') == 1),
  68. Translate::make()->schema(fn(string $locale) => [
  69. TextInput::make('img_alt')
  70. ->required(fn($get) => $locale == 'zh_TW' && $get('../../type') == 1)
  71. ->label('圖片註釋'),
  72. ])
  73. ->locales(['zh_TW', 'en'])
  74. ->actions([
  75. app(DeepLService::class)->createTranslationAction('Main', ['img_alt']),
  76. ])->columnSpan(1)
  77. ->visible(fn($get) => $get('../type') == 1),
  78. ])->columnSpanFull(),
  79. TextInput::make('video_url')->label('影片網址')->url()->columnSpanFull()
  80. ->required(fn($get) => $get('type') == 2)
  81. ->visible(fn($get) => $get('type') == 2),
  82. Radio::make('visible')->label('顯示/不顯示')->options([1 => '顯示', 0 => '不顯示'])->inline()->default(1)->columnSpan(2),
  83. ])->columns(4)->columnSpanFull(),
  84. ]),
  85. ]);
  86. }
  87. public static function table(Table $table): Table
  88. {
  89. return $table
  90. ->columns([
  91. IconColumn::make('type')->label('類別')
  92. ->icon(fn (string $state): string => match ($state) {
  93. '1' => 'heroicon-o-photo',
  94. '2' => 'heroicon-o-film',
  95. }),
  96. ImageColumn::make('img_url')->label('圖片')
  97. ->visible(fn ($record): bool => $record->type == 1),
  98. TextColumn::make('title')->label('標題')
  99. ->visible(fn ($record): bool => $record->type == 1),
  100. TextColumn::make('video_url')->label('影片網址')
  101. ->visible(fn ($record): bool => $record->type == 2),
  102. ])
  103. ->filters([
  104. //
  105. ])
  106. ->actions([
  107. Tables\Actions\EditAction::make(),
  108. ])
  109. ->bulkActions([
  110. Tables\Actions\BulkActionGroup::make([
  111. Tables\Actions\DeleteBulkAction::make(),
  112. ]),
  113. ])
  114. ->reorderable('order')
  115. ->defaultSort('order');
  116. }
  117. public static function getRelations(): array
  118. {
  119. return [
  120. //
  121. ];
  122. }
  123. public static function getPages(): array
  124. {
  125. return [
  126. 'index' => Pages\ListBanners::route('/'),
  127. 'create' => Pages\CreateBanner::route('/create'),
  128. 'edit' => Pages\EditBanner::route('/{record}/edit'),
  129. ];
  130. }
  131. }