AlbumResource.php 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. namespace App\Filament\Resources;
  3. use App\Filament\Resources\AlbumResource\Pages;
  4. use App\Models\Album;
  5. use App\Models\AlbumCategory;
  6. use App\Service\DeepLService;
  7. use Filament\Forms\Components\DatePicker;
  8. use Filament\Forms\Components\FileUpload;
  9. use Filament\Forms\Components\Group;
  10. use Filament\Forms\Components\Radio;
  11. use Filament\Forms\Components\Section;
  12. use Filament\Forms\Components\Select;
  13. use Filament\Forms\Components\TextInput;
  14. use Filament\Forms\Components\Toggle;
  15. use Filament\Forms\Form;
  16. use Filament\Forms\Get;
  17. use Filament\Resources\Resource;
  18. use Filament\Tables;
  19. use Filament\Tables\Columns\ImageColumn;
  20. use Filament\Tables\Columns\TextColumn;
  21. use Filament\Tables\Filters\SelectFilter;
  22. use Filament\Tables\Table;
  23. use Illuminate\Database\Eloquent\Builder;
  24. use Illuminate\Support\Facades\DB;
  25. use SolutionForest\FilamentTranslateField\Forms\Component\Translate;
  26. class AlbumResource extends Resource
  27. {
  28. protected static ?string $model = Album::class;
  29. protected static ?string $modelLabel = '影音管理';
  30. protected static ?string $navigationIcon = 'heroicon-o-photo';
  31. protected static ?string $navigationGroup = '最新消息';
  32. protected static ?string $navigationLabel = '影音管理';
  33. public static function form(Form $form): Form
  34. {
  35. return $form
  36. ->schema([
  37. Section::make('新增 影音')->schema([
  38. Group::make()->schema([
  39. Select::make('album_category_id')
  40. ->options(AlbumCategory::orderBy('order')->get()->pluck('name', 'id'))
  41. ->label('影音分類')
  42. ->required()
  43. ->columnSpan(1)
  44. ->native(false)
  45. ->live(),
  46. ])->columnSpanFull()->columns(2),
  47. Group::make()->schema([
  48. DatePicker::make('post_date')
  49. ->label('發布日期')
  50. ->closeOnDateSelection(),
  51. ])->columnSpanFull()->columns(2),
  52. FileUpload::make('news_banner')->label('列表大圖')
  53. ->directory('album/img')
  54. ->acceptedFileTypes(['image/jpeg', 'image/jpg', 'image/png', 'image/webp'])
  55. ->helperText('建議寬高限制為:2000*720px,出血寬度720px,主要圖像範圍為:1280*720px,檔案大小限制為1M以下')->maxSize('1024'),
  56. FileUpload::make('news_img_pc')->label('列表圖(desktop)')
  57. ->directory('album/img')
  58. ->acceptedFileTypes(['image/jpeg', 'image/jpg', 'image/png', 'image/webp'])
  59. ->helperText('建議寬高限制為:1280*720px,檔案大小限制為1M以下')->maxSize('1024'),
  60. FileUpload::make('news_img_mobile')->label('列表圖(mobile)')
  61. ->directory('album/img')
  62. ->acceptedFileTypes(['image/jpeg', 'image/jpg', 'image/png', 'image/webp'])
  63. ->helperText('建議寬高限制為:600x896px,檔案大小限制為1M以下')->maxSize('1024'),
  64. Translate::make()->schema(fn (string $locale) => [
  65. TextInput::make('title')
  66. ->label('標題')
  67. ->columnSpan(1),
  68. ])
  69. ->locales(['zh_TW', 'en', 'jp'])
  70. ->actions([
  71. app(DeepLService::class)->createTranslationAction('Main', ['title']),
  72. ])
  73. ->id('main')->columnSpanFull()->columns(3),
  74. Section::make('')->schema([
  75. Radio::make('upload_type')->label('')->options([
  76. 1 => '網址',
  77. 2 => '檔案',
  78. ])->columnSpanFull()->default(1)->live(),
  79. Group::make()->schema([
  80. TextInput::make('link_video')->label('網址')->nullable(),
  81. ])->visible(fn (Get $get): bool => $get('upload_type') == 1)->columnSpanFull(),
  82. Group::make()->schema([
  83. FileUpload::make('link_upload')->label('')->directory('album/video')
  84. ->acceptedFileTypes(['video/mp4', 'video/mpeg', 'video/quicktime', 'video/x-msvideo', 'video/x-ms-wmv'])
  85. ->helperText('建議影片寬高限制為:1920*1080px,出血寬度720px,大小限制為:100M以下')
  86. ->maxSize(102400)->nullable(),
  87. ])->visible(fn (Get $get): bool => $get('upload_type') == 2)->columnSpanFull(),
  88. ])->columnSpanFull(),
  89. Toggle::make('on_top')->inline()->label('置頂輪播')->columnSpanFull(),
  90. Toggle::make('homepage_top')->inline()->label('在首頁輪播')->columnSpanFull(),
  91. TextInput::make('order')->label('排序')->integer()->default(0),
  92. ])->columns(3),
  93. ]);
  94. }
  95. public static function table(Table $table): Table
  96. {
  97. return $table
  98. ->columns([
  99. //
  100. TextColumn::make('albumCategory.name')->label('分類')->alignCenter(),
  101. TextColumn::make('title')->label('標題')->alignCenter(),
  102. TextColumn::make('post_date')->date()->alignCenter(),
  103. ImageColumn::make('news_img_pc')->alignCenter(),
  104. TextColumn::make('list_audit_state')->label('狀態')->badge()
  105. ->color(fn (string $state): string => match ($state) {
  106. '暫存' => 'warning',
  107. '已發佈' => 'success',
  108. }),
  109. TextColumn::make('created_at')->label('建立時間')->dateTime()->alignCenter(),
  110. TextColumn::make('updated_at')->label('更新時間')->dateTime()->alignCenter(),
  111. ])
  112. ->filters([
  113. SelectFilter::make('post_date')->label('年份')
  114. ->options(Album::select(DB::raw("DATE_FORMAT(post_date, '%Y') as year"))->whereNotNull('post_date')->distinct()->pluck('year', 'year')->toArray())
  115. ->query(
  116. fn (array $data, Builder $query): Builder => $query->when(
  117. $data['value'],
  118. fn (Builder $query, $value): Builder => $query->where('post_date', 'like', $data['value'].'%')
  119. )
  120. ),
  121. SelectFilter::make('album_category_id')->label('分類')
  122. ->relationship('albumCategory', 'name')
  123. ->getOptionLabelFromRecordUsing(fn ($record, $livewire) => $record->getTranslation('name', 'zh_TW')),
  124. SelectFilter::make('visible')->label('狀態')
  125. ->options([
  126. 0 => '暫存',
  127. 1 => '已發佈',
  128. ])
  129. ->query(
  130. fn (array $data, Builder $query): Builder => $query->when(
  131. $data['value'],
  132. fn (Builder $query, $value): Builder => $query->where('visible', $data['value'])
  133. )
  134. ),
  135. ])
  136. ->actions([
  137. Tables\Actions\EditAction::make(),
  138. Tables\Actions\DeleteAction::make(),
  139. \Filament\Tables\Actions\Action::make('audit')
  140. ->label(fn ($record) => match ($record->visible) {
  141. 0 => '發佈',
  142. 1 => '下架',
  143. })
  144. ->color(fn ($record) => match ($record->visible) {
  145. 0 => 'warning',
  146. 1 => 'gray',
  147. })
  148. ->icon(fn ($record) => match ($record->visible) {
  149. 0 => 'heroicon-m-chevron-double-up',
  150. 1 => 'heroicon-m-chevron-double-down',
  151. })
  152. ->action(function ($record) {
  153. $record->visible = ! $record->visible;
  154. $record->save();
  155. })
  156. ->outlined()
  157. ->requiresConfirmation(),
  158. ])
  159. ->bulkActions([
  160. Tables\Actions\BulkActionGroup::make([
  161. Tables\Actions\DeleteBulkAction::make(),
  162. ]),
  163. ])
  164. ->defaultSort('created_at', 'desc');
  165. }
  166. public static function getRelations(): array
  167. {
  168. return [
  169. //
  170. ];
  171. }
  172. public static function getPages(): array
  173. {
  174. return [
  175. 'index' => Pages\ListAlbums::route('/'),
  176. 'create' => Pages\CreateAlbum::route('/create'),
  177. 'edit' => Pages\EditAlbum::route('/{record}/edit'),
  178. 'view' => Pages\ViewAlbum::route('/{record}/view'),
  179. ];
  180. }
  181. }