123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. Section::make('')->schema([
  108. Repeater::make("multiple_images")->schema([
  109. TextInput::make('para_img_item_key')
  110. ->default(fn () => Str::random())
  111. ->hidden()
  112. ->afterStateHydrated(function (TextInput $component, $state) {
  113. if (empty($state)) {
  114. $component->state(Str::random());
  115. }
  116. }),
  117. Translate::make()->schema(fn (string $locale) => [
  118. TextInput::make('image_alt')->label("圖片註文"),
  119. ])->locales(["zh_TW", "en"])
  120. ->id(fn ($get) => "para_img_mul_" . $get('para_img_item_key')),
  121. FileUpload::make('image_url')->label("")->disk("public")
  122. // ->helperText('建議寬高限制為:1080*675px,檔案大小限制為1M以下')->maxSize('1024')
  123. ->directory("news/extraPhoto")
  124. ->maxFiles(10),
  125. ])
  126. ->relationship("photos")
  127. ->label("")
  128. ->orderColumn('order')
  129. ])
  130. ])->visible(fn (Get $get):bool => $get("paragraph_type") == 1),
  131. Group::make()->schema([
  132. Translate::make()->schema(fn (string $locale) => [
  133. RichEditor::make('text_content')
  134. ->toolbarButtons([
  135. 'blockquote',
  136. 'bold',
  137. 'bulletList',
  138. 'h2',
  139. 'h3',
  140. 'italic',
  141. 'link',
  142. 'orderedList',
  143. 'redo',
  144. 'strike',
  145. 'underline',
  146. 'undo',
  147. ])
  148. ->disableToolbarButtons([
  149. 'blockquote',
  150. 'strike',
  151. 'attachFiles',
  152. ])
  153. ->fileAttachmentsDirectory('attachments')
  154. ->fileAttachmentsVisibility('private'),
  155. ])
  156. ->locales(["zh_TW", "en"])
  157. ->id(fn ($get) => "para_text_" . $get('item_key')),
  158. ])->visible(fn (Get $get):bool => $get("paragraph_type") == 2),
  159. Group::make()->schema([
  160. Section::make("")->schema([
  161. FileUpload::make('video_img')->label("影片底圖")
  162. ->disk("public")
  163. ->directory("news/paragraph/video"),
  164. // ->helperText('建議寬高限制為:2000*720px,出血寬度720px,主要圖像範圍為:1280*720px,檔案大小限制為1M以下')->maxSize('1024'),
  165. Translate::make()->schema(fn (string $locale) => [
  166. TextInput::make('video_img_alt')->label("圖片註文")->columnSpan(1),
  167. ])->locales(["zh_TW", "en"])
  168. ->id(fn ($get) => "para_video_img_alt_" . $get('item_key')),
  169. Radio::make("video_type")->label("")->options([
  170. 1 => "網址",
  171. 2 => "檔案"
  172. ])->columnSpanFull()->default(1)->Live(),
  173. Group::make()->schema([
  174. TextInput::make('link')->label("網址")->nullable(),
  175. ])->visible(fn (Get $get):bool => $get("video_type") == 1)->columnSpanFull(),
  176. Group::make()->schema([
  177. FileUpload::make('video_url')->label("")->disk("public")->directory("news/paragraph/video")
  178. // ->helperText('建議影片寬高限制為:1920*1080px,出血寬度720px,大小限制為:100M以下')
  179. // ->maxSize(102400)
  180. ->nullable(),
  181. ])->visible(fn (Get $get):bool => $get("video_type") == 2)->columnSpanFull(),
  182. ]),
  183. ])->visible(fn (Get $get):bool => $get("paragraph_type") == 3),
  184. ])
  185. ->relationship("paragraphs")
  186. ->label("段落")
  187. ->collapsible()
  188. ->reorderableWithButtons()
  189. ->orderColumn('order')
  190. ->cloneable()
  191. ]),
  192. ]);
  193. }
  194. public static function table(Table $table): Table
  195. {
  196. return $table
  197. ->columns([
  198. //
  199. TextColumn::make("newsCategory.name")->label("分類")->alignCenter(),
  200. TextColumn::make("title")->label("標題")->alignCenter(),
  201. TextColumn::make("written_by")->label("發佈者")->alignCenter(),
  202. TextColumn::make("post_date")->label("發佈時間")->dateTime('Y/m/d')->alignCenter(),
  203. ImageColumn::make("news_img_pc_url")->label("列表圖")->alignCenter(),
  204. TextColumn::make("list_audit_state")->label("狀態")->badge()
  205. ->color(fn (string $state): string => match ($state) {
  206. '暫存' => 'warning',
  207. '已發佈' => 'success',
  208. }),
  209. IconColumn::make("on_top")->label("置頂")
  210. ->color(fn (string $state): string => match ($state) {
  211. 1 => 'success',
  212. default => ''
  213. })
  214. ->icon(fn (string $state): string => match ($state) {
  215. 1 => 'heroicon-o-check-circle',
  216. default => ''
  217. })
  218. ->action(function ($record): void {
  219. $record->on_top = !$record->on_top;
  220. $record->save();
  221. }),
  222. TextColumn::make("created_at")->label("建立時間")->dateTime('Y/m/d H:i:s')->alignCenter(),
  223. TextColumn::make("updated_at")->label("更新時間")->dateTime()->alignCenter(),
  224. ])
  225. ->filters([
  226. SelectFilter::make('news_category_id')->label("分類")
  227. ->options(NewsCategory::orderBy("order")->pluck("name", "id"))
  228. ->attribute('news_category_id'),
  229. SelectFilter::make('post_date')->label("年份")
  230. ->options(News::select(DB::raw("DATE_FORMAT(post_date, '%Y') as year"))->whereNotNull("post_date")->distinct()->pluck("year","year")->toArray())
  231. ->query(
  232. fn (array $data, Builder $query): Builder =>
  233. $query->when(
  234. $data['value'],
  235. fn (Builder $query, $value): Builder => $query->where('post_date', 'like', $data['value']. "%")
  236. )
  237. ),
  238. SelectFilter::make('visible')->label("狀態")
  239. ->options([
  240. 0 => "暫存",
  241. 1 => "已發佈",
  242. ])
  243. ->query(
  244. fn (array $data, Builder $query): Builder =>
  245. $query->when(
  246. $data['value'],
  247. fn (Builder $query, $value): Builder => $query->where('visible', $data['value'])
  248. )
  249. ),
  250. ])
  251. ->actions([
  252. Tables\Actions\EditAction::make(),
  253. Tables\Actions\DeleteAction::make(),
  254. \Filament\Tables\Actions\Action::make("audit")
  255. ->label(fn ($record) => match ($record->visible) {
  256. 0 => '發佈',
  257. 1 => '下架',
  258. })
  259. ->color(fn ($record) => match ($record->visible) {
  260. 0 => 'warning',
  261. 1 => 'gray',
  262. })
  263. ->icon(fn ($record) => match ($record->visible) {
  264. 0 => 'heroicon-m-chevron-double-up',
  265. 1 => 'heroicon-m-chevron-double-down',
  266. })
  267. ->action(function ($record): void {
  268. $record->visible = !$record->visible;
  269. $record->save();
  270. })
  271. ->outlined()
  272. ->requiresConfirmation(),
  273. ])
  274. ->bulkActions([
  275. Tables\Actions\BulkActionGroup::make([
  276. Tables\Actions\DeleteBulkAction::make(),
  277. ]),
  278. ])
  279. ->defaultSort('order', 'desc')
  280. ->defaultSort('created_at', 'desc');
  281. }
  282. public static function getRelations(): array
  283. {
  284. return [
  285. //
  286. ];
  287. }
  288. public static function getPages(): array
  289. {
  290. return [
  291. 'index' => Pages\ListNews::route('/'),
  292. 'create' => Pages\CreateNews::route('/create'),
  293. 'edit' => Pages\EditNews::route('/{record}/edit'),
  294. 'view' => Pages\ViewNews::route('/{record}/view'),
  295. ];
  296. }
  297. }