AlbumResource.php 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. ->directory("album/img")
  65. ->helperText('建議寬高限制為:2000*720px,出血寬度720px,主要圖像範圍為:1280*720px,檔案大小限制為1M以下')->maxSize('1024'),
  66. FileUpload::make('news_img_pc')->label("列表圖(desktop)")
  67. ->directory("album/img")
  68. ->helperText('建議寬高限制為:1280*720px,檔案大小限制為1M以下')->maxSize('1024'),
  69. FileUpload::make('news_img_mobile')->label("列表圖(mobile)")
  70. ->directory("album/img")
  71. ->helperText('建議寬高限制為:600x896px,檔案大小限制為1M以下')->maxSize('1024'),
  72. Translate::make()->schema(fn (string $locale) => [
  73. TextInput::make('title')
  74. ->label("標題")
  75. ->columnSpan(1),
  76. ])
  77. ->locales(["zh_TW", "en", "jp"])
  78. ->actions([
  79. app(DeepLService::class)->createTranslationAction("Main", ["title"])
  80. ])
  81. ->id("main")->columnSpanFull()->columns(3),
  82. Section::make("")->schema([
  83. Radio::make("upload_type")->label("")->options([
  84. 1 => "網址",
  85. 2 => "檔案"
  86. ])->columnSpanFull()->default(1)->Live(),
  87. Group::make()->schema([
  88. TextInput::make('link_video')->label("網址")->nullable(),
  89. ])->visible(fn (Get $get):bool => $get("upload_type") == 1)->columnSpanFull(),
  90. Group::make()->schema([
  91. FileUpload::make('link_upload')->label("")->directory("album/video")
  92. ->helperText('建議影片寬高限制為:1920*1080px,出血寬度720px,大小限制為:100M以下')
  93. ->maxSize(102400)->nullable(),
  94. ])->visible(fn (Get $get):bool => $get("upload_type") == 2)->columnSpanFull(),
  95. ])->columnSpanFull(),
  96. Toggle::make("on_top")->inline()->label("置頂輪播")->columnSpanFull(),
  97. Toggle::make("homepage_top")->inline()->label("在首頁輪播")->columnSpanFull(),
  98. TextInput::make('order')->label("排序")->integer()->default(0),
  99. ])->columns(3),
  100. ]);
  101. }
  102. public static function table(Table $table): Table
  103. {
  104. return $table
  105. ->columns([
  106. //
  107. TextColumn::make("albumCategory.name")->label("分類")->alignCenter(),
  108. TextColumn::make("title")->label("標題")->alignCenter(),
  109. TextColumn::make("post_date")->date()->alignCenter(),
  110. ImageColumn::make("news_img_pc")->alignCenter(),
  111. TextColumn::make("list_audit_state")->label("狀態")->badge()
  112. ->color(fn (string $state): string => match ($state) {
  113. '暫存' => 'warning',
  114. '已發佈' => 'success',
  115. }),
  116. TextColumn::make("created_at")->label("建立時間")->dateTime()->alignCenter(),
  117. TextColumn::make("updated_at")->label("更新時間")->dateTime()->alignCenter(),
  118. ])
  119. ->filters([
  120. SelectFilter::make('post_date')->label("年份")
  121. ->options(Album::select(DB::raw("DATE_FORMAT(post_date, '%Y') as year"))->whereNotNull("post_date")->distinct()->pluck("year","year")->toArray())
  122. ->query(
  123. fn (array $data, Builder $query): Builder =>
  124. $query->when(
  125. $data['value'],
  126. fn (Builder $query, $value): Builder => $query->where('post_date', 'like', $data['value']. "%")
  127. )
  128. ),
  129. SelectFilter::make('album_category_id')->label("分類")
  130. ->relationship('albumCategory', 'name')
  131. ->getOptionLabelFromRecordUsing(fn($record, $livewire) => $record->getTranslation('name', "zh_TW")),
  132. SelectFilter::make('visible')->label("狀態")
  133. ->options([
  134. 0 => "暫存",
  135. 1 => "已發佈",
  136. ])
  137. ->query(
  138. fn (array $data, Builder $query): Builder =>
  139. $query->when(
  140. $data['value'],
  141. fn (Builder $query, $value): Builder => $query->where('visible', $data['value'])
  142. )
  143. ),
  144. ])
  145. ->actions([
  146. Tables\Actions\EditAction::make(),
  147. Tables\Actions\DeleteAction::make(),
  148. \Filament\Tables\Actions\Action::make("audit")
  149. ->label(fn ($record) => match ($record->visible) {
  150. 0 => '發佈',
  151. 1 => '下架',
  152. })
  153. ->color(fn ($record) => match ($record->visible) {
  154. 0 => 'warning',
  155. 1 => 'gray',
  156. })
  157. ->icon(fn ($record) => match ($record->visible) {
  158. 0 => 'heroicon-m-chevron-double-up',
  159. 1 => 'heroicon-m-chevron-double-down',
  160. })
  161. ->action(function ($record) {
  162. $record->visible = !$record->visible;
  163. $record->save();
  164. })
  165. ->outlined()
  166. ->requiresConfirmation(),
  167. ])
  168. ->bulkActions([
  169. Tables\Actions\BulkActionGroup::make([
  170. Tables\Actions\DeleteBulkAction::make(),
  171. ]),
  172. ])
  173. ->defaultSort('order', 'desc')
  174. ->defaultSort('created_at', 'desc');
  175. }
  176. public static function getRelations(): array
  177. {
  178. return [
  179. //
  180. ];
  181. }
  182. public static function getPages(): array
  183. {
  184. return [
  185. 'index' => Pages\ListAlbums::route('/'),
  186. 'create' => Pages\CreateAlbum::route('/create'),
  187. 'edit' => Pages\EditAlbum::route('/{record}/edit'),
  188. 'view' => Pages\ViewAlbum::route('/{record}/view'),
  189. ];
  190. }
  191. }