BannerResource.php 5.4KB

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