AlbumResource.php 9.0KB

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