NewsResource.php 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php
  2. namespace App\Filament\Resources;
  3. use App\Filament\Resources\NewsResource\Pages;
  4. use App\Models\News;
  5. use App\Models\NewsCategory;
  6. use Filament\Forms\Components\Actions\Action;
  7. use Filament\Forms\Components\Group;
  8. use Filament\Forms\Components\Repeater;
  9. use Filament\Forms\Components\RichEditor;
  10. use Filament\Forms\Components\TextInput;
  11. use Filament\Forms\Components\Textarea;
  12. use Filament\Forms\Components\Select;
  13. use Filament\Forms\Components\Radio;
  14. use Filament\Forms\Components\Section;
  15. use Filament\Forms\Components\Toggle;
  16. use Filament\Forms\Components\DatePicker;
  17. use Filament\Forms\Components\FileUpload;
  18. use Filament\Forms\Get;
  19. use Filament\Forms\Form;
  20. use Filament\Resources\Resource;
  21. use Filament\Tables;
  22. use Filament\Tables\Columns\IconColumn;
  23. use Filament\Tables\Columns\ImageColumn;
  24. use Filament\Tables\Columns\TextColumn;
  25. use Filament\Tables\Filters\SelectFilter;
  26. use Filament\Tables\Table;
  27. use Illuminate\Database\Eloquent\Builder;
  28. use Illuminate\Support\Facades\DB;
  29. use Illuminate\Support\Str;
  30. use SolutionForest\FilamentTranslateField\Forms\Component\Translate;
  31. use RalphJSmit\Filament\SEO\SEO;
  32. class NewsResource extends Resource
  33. {
  34. protected static ?string $model = News::class;
  35. protected static ?string $modelLabel = "文章管理";
  36. protected static ?string $navigationIcon = 'heroicon-o-newspaper';
  37. protected static ?string $navigationGroup = '上稿內容管理';
  38. protected static ?string $navigationLabel = "文章管理";
  39. public static function form(Form $form): Form
  40. {
  41. return $form
  42. ->schema([
  43. //
  44. Section::make("新增 文章")->schema([
  45. Group::make()->schema([
  46. Select::make("news_category_id")
  47. ->options(NewsCategory::get()->pluck("name","id"))
  48. ->label("文章分類")
  49. ->columnSpan(1)
  50. ->native(false)
  51. ->Live(),
  52. ])->columnSpanFull()->columns(2),
  53. Group::make()->schema([
  54. DatePicker::make('post_date')
  55. ->label("發布日期")
  56. ->closeOnDateSelection(),
  57. ])->columnSpanFull()->columns(2),
  58. FileUpload::make('news_img_pc')->label("列表圖(desktop)")
  59. ->disk("public")
  60. // ->helperText('建議寬高限制為:1280*720px,檔案大小限制為1M以下')->maxSize('1024')
  61. ->directory("news"),
  62. FileUpload::make('news_img_mobile')->label("列表圖(mobile)")
  63. ->disk("public")
  64. // ->helperText('建議寬高限制為:600*896px,檔案大小限制為1M以下')->maxSize('1024')
  65. ->directory("news"),
  66. Translate::make()->schema(fn (string $locale) => [
  67. TextInput::make('title')
  68. ->label("標題")
  69. ->columnSpan(1),
  70. TextInput::make('written_by')
  71. ->label("發佈者")->columnSpan(1),
  72. Textarea::make("description")->rows(5)->columnSpanFull()->label("短文"),
  73. ])->locales(["zh_TW", "en"])
  74. ->id("main")
  75. ->columnSpanFull()->columns(3),
  76. TextInput::make('order')->label("排序")->integer()->default(0),
  77. ])->columns(3),
  78. Section::make("SEO")->schema([
  79. // SEO::make(),
  80. Translate::make()->schema(fn (string $locale) => [
  81. TextInput::make('meta_title')->label("SEO 標題")->columnSpan(1),
  82. Textarea::make("meta_keyword")->rows(5)->columnSpanFull()->label("SEO 關鍵字"),
  83. Textarea::make("meta_description")->rows(5)->columnSpanFull()->label("SEO 短文"),
  84. ])
  85. ->locales(["zh_TW", "en"])
  86. ->id("seo"),
  87. FileUpload::make('meta_img')->label("放大預覽圖")->disk("public")
  88. // ->helperText('建議寬高限制為:1200*630px,檔案大小限制為1M以下')->maxSize('1024')
  89. ->directory("seo/news"),
  90. ])->columnSpanFull(),
  91. Section::make("文章內容")->schema([
  92. Repeater::make("paragraphs")->schema([
  93. TextInput::make('item_key')
  94. ->default(fn () => Str::random())
  95. ->hidden()
  96. ->afterStateHydrated(function (TextInput $component, $state) {
  97. if (empty($state)) {
  98. $component->state(Str::random());
  99. }
  100. }),
  101. Radio::make("paragraph_type")->options([
  102. 1 => "圖片",
  103. 2 => "文字",
  104. 3 => "影音"
  105. ])->label("")->default(1)->Live(),
  106. Group::make()->schema([
  107. Translate::make()->schema(fn (string $locale) => [
  108. TextInput::make('image_alt')->label("圖片註文")->columnSpan(1),
  109. ])->locales(["zh_TW", "en"])
  110. ->id(fn ($get) => "para_img_" . $get('item_key')),
  111. FileUpload::make('image_url')->label("")->disk("public")
  112. // ->helperText('建議寬高限制為:1080*675px,檔案大小限制為1M以下')->maxSize('1024')
  113. ->directory("news/paragraph"),
  114. TextInput::make('image_link')->label("外連網址")->columnSpanFull()->url(),
  115. ])->visible(fn (Get $get):bool => $get("paragraph_type") == 1),
  116. Group::make()->schema([
  117. Translate::make()->schema(fn (string $locale) => [
  118. RichEditor::make('text_content')
  119. ->toolbarButtons([
  120. 'blockquote',
  121. 'bold',
  122. 'bulletList',
  123. 'h2',
  124. 'h3',
  125. 'italic',
  126. 'link',
  127. 'orderedList',
  128. 'redo',
  129. 'strike',
  130. 'underline',
  131. 'undo',
  132. ])
  133. ->disableToolbarButtons([
  134. 'blockquote',
  135. 'strike',
  136. 'attachFiles',
  137. ])
  138. ->fileAttachmentsDirectory('attachments')
  139. ->fileAttachmentsVisibility('private'),
  140. ])->locales(["zh_TW", "en"])
  141. ->id(fn ($get) => "para_text_" . $get('item_key')),
  142. ])->visible(fn (Get $get):bool => $get("paragraph_type") == 2),
  143. Group::make()->schema([
  144. Section::make("")->schema([
  145. FileUpload::make('video_img')->label("影片底圖")
  146. ->disk("public")
  147. ->directory("news/paragraph/video"),
  148. // ->helperText('建議寬高限制為:2000*720px,出血寬度720px,主要圖像範圍為:1280*720px,檔案大小限制為1M以下')->maxSize('1024'),
  149. Radio::make("video_type")->label("")->options([
  150. 1 => "網址",
  151. 2 => "檔案"
  152. ])->columnSpanFull()->default(1)->Live(),
  153. Group::make()->schema([
  154. TextInput::make('link')->label("網址")->nullable(),
  155. ])->visible(fn (Get $get):bool => $get("video_type") == 1)->columnSpanFull(),
  156. Group::make()->schema([
  157. FileUpload::make('video_url')->label("")->disk("public")->directory("news/paragraph/video")
  158. // ->helperText('建議影片寬高限制為:1920*1080px,出血寬度720px,大小限制為:100M以下')
  159. // ->maxSize(102400)
  160. ->nullable(),
  161. ])->visible(fn (Get $get):bool => $get("video_type") == 2)->columnSpanFull(),
  162. ]),
  163. ])->visible(fn (Get $get):bool => $get("paragraph_type") == 3),
  164. ])
  165. ->relationship("paragraphs")
  166. ->label("段落")
  167. ->collapsible()
  168. ->reorderableWithButtons()
  169. ->orderColumn('order')
  170. ->cloneable()
  171. ]),
  172. Section::make("額外圖片")->schema([
  173. Repeater::make("extra_images")->schema([
  174. FileUpload::make('image_url')->label("")->disk("public")
  175. ->directory("news/extraPhoto")
  176. ->maxFiles(10),
  177. TextInput::make('image_link')->label("外連網址")->columnSpanFull()->url(),
  178. ])
  179. ->helperText('至少需上傳4張圖片才會出現圖片輯')
  180. ->relationship("photos")
  181. ->label("")
  182. ->collapsible()
  183. ->orderColumn('order')
  184. ->defaultItems(0)
  185. ])->columns(5)
  186. ]);
  187. }
  188. public static function table(Table $table): Table
  189. {
  190. return $table
  191. ->columns([
  192. //
  193. TextColumn::make("newsCategory.name")->label("分類")->alignCenter(),
  194. TextColumn::make("title")->label("標題")->alignCenter(),
  195. TextColumn::make("written_by")->label("發佈者")->alignCenter(),
  196. TextColumn::make("post_date")->label("發佈時間")->dateTime('Y/m/d')->alignCenter(),
  197. ImageColumn::make("news_img_pc_url")->label("列表圖")->alignCenter(),
  198. TextColumn::make("list_audit_state")->label("狀態")->badge()
  199. ->color(fn (string $state): string => match ($state) {
  200. '暫存' => 'warning',
  201. '已發佈' => 'success',
  202. }),
  203. IconColumn::make("on_top")->label("置頂")
  204. ->color(fn (string $state): string => match ($state) {
  205. 1 => 'success',
  206. default => ''
  207. })
  208. ->icon(fn (string $state): string => match ($state) {
  209. 1 => 'heroicon-o-check-circle',
  210. default => ''
  211. })
  212. ->action(function ($record): void {
  213. $record->on_top = !$record->on_top;
  214. $record->save();
  215. }),
  216. TextColumn::make("created_at")->label("建立時間")->dateTime('Y/m/d H:i:s')->alignCenter(),
  217. TextColumn::make("updated_at")->label("更新時間")->dateTime()->alignCenter(),
  218. ])
  219. ->filters([
  220. SelectFilter::make('news_category_id')->label("分類")
  221. ->options(NewsCategory::orderBy("order")->pluck("name", "id"))
  222. ->attribute('news_category_id'),
  223. SelectFilter::make('post_date')->label("年份")
  224. ->options(News::select(DB::raw("DATE_FORMAT(post_date, '%Y') as year"))->whereNotNull("post_date")->distinct()->pluck("year","year")->toArray())
  225. ->query(
  226. fn (array $data, Builder $query): Builder =>
  227. $query->when(
  228. $data['value'],
  229. fn (Builder $query, $value): Builder => $query->where('post_date', 'like', $data['value']. "%")
  230. )
  231. ),
  232. SelectFilter::make('visible')->label("狀態")
  233. ->options([
  234. 0 => "暫存",
  235. 1 => "已發佈",
  236. ])
  237. ->query(
  238. fn (array $data, Builder $query): Builder =>
  239. $query->when(
  240. $data['value'],
  241. fn (Builder $query, $value): Builder => $query->where('visible', $data['value'])
  242. )
  243. ),
  244. ])
  245. ->actions([
  246. Tables\Actions\EditAction::make(),
  247. Tables\Actions\DeleteAction::make(),
  248. \Filament\Tables\Actions\Action::make("audit")
  249. ->label(fn ($record) => match ($record->visible) {
  250. 0 => '發佈',
  251. 1 => '下架',
  252. })
  253. ->color(fn ($record) => match ($record->visible) {
  254. 0 => 'warning',
  255. 1 => 'gray',
  256. })
  257. ->icon(fn ($record) => match ($record->visible) {
  258. 0 => 'heroicon-m-chevron-double-up',
  259. 1 => 'heroicon-m-chevron-double-down',
  260. })
  261. ->action(function ($record): void {
  262. $record->visible = !$record->visible;
  263. $record->save();
  264. })
  265. ->outlined()
  266. ->requiresConfirmation(),
  267. ])
  268. ->bulkActions([
  269. Tables\Actions\BulkActionGroup::make([
  270. Tables\Actions\DeleteBulkAction::make(),
  271. ]),
  272. ])
  273. ->defaultSort('order', 'desc')
  274. ->defaultSort('created_at', 'desc');
  275. }
  276. public static function getRelations(): array
  277. {
  278. return [
  279. //
  280. ];
  281. }
  282. public static function getPages(): array
  283. {
  284. return [
  285. 'index' => Pages\ListNews::route('/'),
  286. 'create' => Pages\CreateNews::route('/create'),
  287. 'edit' => Pages\EditNews::route('/{record}/edit'),
  288. 'view' => Pages\ViewNews::route('/{record}/view'),
  289. ];
  290. }
  291. }