BannerResource.php 5.7KB

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