EsgResource.php 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. <?php
  2. namespace App\Filament\Resources;
  3. use App\Filament\Resources\EsgResource\Pages;
  4. use App\Models\Esg;
  5. use Filament\Forms\Components\FileUpload;
  6. use Filament\Forms\Components\Grid;
  7. use Filament\Forms\Components\Group;
  8. use Filament\Forms\Components\Placeholder;
  9. use Filament\Forms\Components\Radio;
  10. use Filament\Forms\Components\Repeater;
  11. use Filament\Forms\Components\RichEditor;
  12. use Filament\Forms\Components\Section;
  13. use Filament\Forms\Components\Select;
  14. use Filament\Forms\Components\Tabs;
  15. use Filament\Forms\Components\Tabs\Tab;
  16. use Filament\Forms\Components\Textarea;
  17. use Filament\Forms\Components\TextInput;
  18. use Filament\Forms\Components\Toggle;
  19. use Filament\Forms\Form;
  20. use Filament\Forms\Get;
  21. use Filament\Resources\Resource;
  22. use Filament\Tables;
  23. use Filament\Tables\Columns\TextColumn;
  24. use Filament\Tables\Table;
  25. use Illuminate\Database\Eloquent\Builder;
  26. use Illuminate\Database\Eloquent\SoftDeletingScope;
  27. use SolutionForest\FilamentTranslateField\Forms\Component\Translate;
  28. use Str;
  29. class EsgResource extends Resource
  30. {
  31. protected static ?string $model = Esg::class;
  32. protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
  33. protected static ?string $modelLabel = "ESG 管理";
  34. protected static ?string $navigationGroup = 'ESG 上稿內容管理';
  35. protected static ?string $navigationLabel = "ESG 管理";
  36. public static function form(Form $form): Form
  37. {
  38. return $form
  39. ->schema([
  40. //
  41. Tabs::make("")->tabs([
  42. Tab::make('基本資訊')->schema([
  43. TextInput::make('keyword')->label("關聯字詞")->required()
  44. ->validationMessages([
  45. 'required' => '請填寫關聯字詞',
  46. ]),
  47. Translate::make()->schema(fn (string $locale) => [
  48. TextInput::make('title')
  49. ->label("標題")
  50. ->columnSpan(1)
  51. ->required($locale == "zh_TW")
  52. ->validationMessages([
  53. 'required' => '請填寫標題',
  54. ])
  55. ])->locales(["zh_TW", "en"])
  56. ->columnSpanFull()->columns(3),
  57. ]),
  58. Tab::make('Banner 設定')->schema([
  59. FileUpload::make('banner_pc')->label("Banner (PC)")
  60. ->disk("public")
  61. // ->helperText('建議寬高限制為:1280*720px,檔案大小限制為1M以下')->maxSize('1024')
  62. ->directory("esg")->required()
  63. ->validationMessages([
  64. 'required' => '請上傳圖片',
  65. ]),
  66. FileUpload::make('banner_mobile')->label("Banner (Mobile)")
  67. ->disk("public")
  68. // ->helperText('建議寬高限制為:600*896px,檔案大小限制為1M以下')->maxSize('1024')
  69. ->directory("esg")->required()
  70. ->validationMessages([
  71. 'required' => '請上傳圖片',
  72. ]),
  73. Translate::make()->schema(fn (string $locale) => [
  74. TextInput::make('banner_alt')
  75. ->label("Banner 圖片註釋")
  76. ->required($locale == "zh_TW")
  77. ->validationMessages([
  78. 'required' => '請填寫圖片註釋',
  79. ]),
  80. Textarea::make("description")->rows(5)->columnSpanFull()->label("短文"),
  81. ])->locales(["zh_TW", "en"])
  82. ->columnSpanFull(),
  83. ]),
  84. Tab::make("段落設計")->schema([
  85. //1: 純文字 2:區塊文字 3:表格 4:影片 or 圖片 5:左右圖文
  86. Repeater::make("paragraphs")->schema([
  87. TextInput::make('item_key')
  88. ->default(fn () => Str::random())
  89. ->hidden()
  90. ->afterStateHydrated(function (TextInput $component, $state) {
  91. if (empty($state)) {
  92. $component->state(Str::random());
  93. }
  94. }),
  95. Radio::make("type")->options([
  96. 1 => "純文字",
  97. 2 => "區塊文字",
  98. 3 => "表格",
  99. 4 => "圖片",
  100. 5 => "影片",
  101. ])->label("")->default(1)->Live(),
  102. Group::make()->schema([
  103. Translate::make()->schema(fn (string $locale) => [
  104. RichEditor::make('content.text_content')
  105. ->fileAttachmentsDirectory('attachments')
  106. ->fileAttachmentsVisibility('private')
  107. ->disableToolbarButtons(['attachFiles'])
  108. ->required($locale == "zh_TW")
  109. ->validationMessages([
  110. 'required' => '請填寫文字內容',
  111. ]),
  112. ])
  113. ->locales(["zh_TW", "en"])
  114. ->id(fn ($get) => "para_text_" . $get('item_key')),
  115. ])->visible(fn (Get $get):bool => $get("type") == 1),
  116. Group::make()->schema([
  117. Section::make('區塊文字設定')->schema([
  118. // // 佈局設定
  119. // Radio::make('layout_type')->label('佈局方式')->options([
  120. // 'vertical' => '垂直排列',
  121. // 'horizontal' => '水平排列',
  122. // 'grid' => '網格排列',
  123. // ])->default('vertical')->Live(),
  124. // 文字區塊重複器
  125. Repeater::make("content.text_blocks")->schema([
  126. TextInput::make('block_item_key')
  127. ->default(fn () => Str::random())
  128. ->hidden()
  129. ->afterStateHydrated(function (TextInput $component, $state) {
  130. if (empty($state)) {
  131. $component->state(Str::random());
  132. }
  133. }),
  134. // 多語系內容
  135. Translate::make()->schema(fn (string $locale) => [
  136. // TextInput::make('block_title')
  137. // ->label("區塊標題")
  138. // ->maxLength(200)
  139. // ->visible(fn (Get $get) => in_array($get('block_type'), ['title', 'subtitle', 'highlight'])),
  140. RichEditor::make('block_content')
  141. ->disableToolbarButtons(['attachFiles'])
  142. ->required($locale == "zh_TW")
  143. ->validationMessages([
  144. 'required' => '請填寫文字內容',
  145. ]),
  146. ])
  147. ->locales(["zh_TW", "en"])
  148. ->id(fn ($get) => "text_block_" . $get('block_item_key')),
  149. ])
  150. ->label("")
  151. ->addActionLabel('增加區塊')
  152. ->orderColumn('order')
  153. ->reorderableWithButtons()
  154. ->cloneable()
  155. ->minItems(1)
  156. ->maxItems(20)
  157. ->defaultItems(1),
  158. ]),
  159. ])->visible(fn (Get $get):bool => $get('type') == 2),
  160. Group::make()->schema([
  161. Section::make('表格設定')->schema([
  162. Radio::make('content.is_card')
  163. ->label('手機板卡片顯示')
  164. ->options([
  165. 1 => '是', // 改為數字鍵值
  166. 0 => '否'
  167. ])
  168. ->default(2)
  169. ->inline(),
  170. // 表格基本資訊
  171. // Translate::make()->schema(fn (string $locale) => [
  172. // TextInput::make('content.table_title')
  173. // ->label("表格標題")
  174. // ->maxLength(200),
  175. // Textarea::make('content.table_description')
  176. // ->label("表格說明")
  177. // ->rows(3)
  178. // ->maxLength(500),
  179. // ])->locales(["zh_TW", "en"])
  180. // ->id(fn ($get) => "table_info_" . $get('item_key')),
  181. // 表格資料
  182. Placeholder::make('table_builder_info')
  183. ->content('請先設定表格欄數,然後填入表格資料'),
  184. Grid::make(3)->schema([
  185. Radio::make('content.column_count')
  186. ->label('欄數')
  187. ->options([
  188. 2 => '2欄', // 改為數字鍵值
  189. 3 => '3欄',
  190. 4 => '4欄'
  191. ])
  192. ->default(2)
  193. ->inline()
  194. ->live()
  195. ->reactive(), // ✅ 添加 reactive()
  196. // Radio::make('content.is_card')
  197. // ->label('是否卡片呈現')
  198. // ->options([
  199. // 1 => '是', // 改為數字鍵值
  200. // 0 => '否',
  201. // ])
  202. // ->default(1)
  203. // ->inline()
  204. // ->required()
  205. ]),
  206. Translate::make()->schema(fn (string $locale) => [
  207. Grid::make(2) // 改用 Grid 來支援動態 columns
  208. ->schema([
  209. TextInput::make('content.head1')->label('標題第1欄')->required()
  210. ->validationMessages([
  211. 'required' => '請填寫',
  212. ]),
  213. Radio::make('content.head_align1')
  214. ->label('對齊')
  215. ->options([
  216. 1 => '置左', // 改為數字鍵值
  217. 2 => '置中',
  218. 3 => '置右'
  219. ])
  220. ->default(1),
  221. TextInput::make('content.head2')->label('標題第2欄')->required()
  222. ->validationMessages([
  223. 'required' => '請填寫',
  224. ]),
  225. Radio::make('content.head_align2')
  226. ->label('對齊')
  227. ->options([
  228. 1 => '置左', // 改為數字鍵值
  229. 2 => '置中',
  230. 3 => '置右'
  231. ])
  232. ->default(1),
  233. TextInput::make('content.head3')->label('標題第3欄')
  234. ->visible(fn (Get $get) => intval($get('content.column_count')) >= 3)->columnSpan(1),
  235. Radio::make('content.head_align3')
  236. ->label('對齊')
  237. ->options([
  238. 1 => '置左', // 改為數字鍵值
  239. 2 => '置中',
  240. 3 => '置右'
  241. ])
  242. ->default(1)
  243. ->visible(fn (Get $get) => intval($get('content.column_count')) >= 3)->columnSpan(1),
  244. TextInput::make('content.head4')->label('標題第4欄')
  245. ->visible(fn (Get $get) => intval($get('content.column_count')) >= 4)->columnSpan(1),
  246. Radio::make('content.head_align4')
  247. ->label('對齊')
  248. ->options([
  249. 1 => '置左', // 改為數字鍵值
  250. 2 => '置中',
  251. 3 => '置右'
  252. ])
  253. ->default(1)
  254. ->visible(fn (Get $get) => intval($get('content.column_count')) >= 4)->columnSpan(1),
  255. ])
  256. ->reactive() // 加入響應式
  257. ])->locales(["zh_TW", "en"]),
  258. Repeater::make('content.simple_table_rows')
  259. ->label('表格資料')
  260. ->schema([
  261. TextInput::make('simple_table_rows_key')
  262. ->default(fn () => Str::random())
  263. ->hidden()
  264. ->afterStateHydrated(function (TextInput $component, $state) {
  265. if (empty($state)) {
  266. $component->state(Str::random());
  267. }
  268. }),
  269. Translate::make()->schema(fn (string $locale) => [
  270. Grid::make(1) // 改用 Grid 來支援動態 columns
  271. ->schema([
  272. Radio::make('align1')
  273. ->label('對齊')
  274. ->options([
  275. 1 => '置左', // 改為數字鍵值
  276. 2 => '置中',
  277. 3 => '置右'
  278. ])
  279. ->default(1),
  280. RichEditor::make('col1')
  281. ->label('第1欄')
  282. ->disableToolbarButtons(['attachFiles'])
  283. ->required($locale == "zh_TW")
  284. ->validationMessages([
  285. 'required' => '請填寫',
  286. ]),
  287. // TextInput::make('col2')->label('第2欄')->required(),
  288. Radio::make('align2')
  289. ->label('對齊')
  290. ->options([
  291. 1 => '置左', // 改為數字鍵值
  292. 2 => '置中',
  293. 3 => '置右'
  294. ])
  295. ->default(1),
  296. RichEditor::make('col2')
  297. ->label('第2欄')
  298. ->disableToolbarButtons(['attachFiles'])
  299. ->required($locale == "zh_TW")
  300. ->validationMessages([
  301. 'required' => '請填寫',
  302. ]),
  303. Radio::make('align3')
  304. ->label('對齊')
  305. ->options([
  306. 1 => '置左', // 改為數字鍵值
  307. 2 => '置中',
  308. 3 => '置右'
  309. ])
  310. ->default(1)
  311. ->visible(fn (Get $get) => intval($get('../../../content.column_count')) >= 3),
  312. RichEditor::make('col3')
  313. ->label('第3欄')
  314. ->disableToolbarButtons(['attachFiles'])
  315. ->visible(fn (Get $get) => intval($get('../../../content.column_count')) >= 3)
  316. ->required($locale == "zh_TW" && fn (Get $get)=> intval($get("../../../content.column_count")) >= 3)
  317. ->validationMessages([
  318. 'required' => '請填寫',
  319. ]),
  320. Radio::make('align4')
  321. ->label('對齊')
  322. ->options([
  323. 1 => '置左', // 改為數字鍵值
  324. 2 => '置中',
  325. 3 => '置右'
  326. ])
  327. ->default(1)
  328. ->visible(fn (Get $get) => intval($get('../../../content.column_count')) >= 4),
  329. RichEditor::make('col4')
  330. ->label('第4欄')
  331. ->disableToolbarButtons(['attachFiles'])
  332. ->visible(fn (Get $get) => intval($get('../../../content.column_count')) >= 4)
  333. ->required($locale == "zh_TW" && fn (Get $get)=> intval($get("../../../content.column_count")) >= 4)
  334. ->validationMessages([
  335. 'required' => '請填寫',
  336. ]),
  337. ])
  338. ->reactive() // 加入響應式
  339. ])->locales(["zh_TW", "en"]),
  340. ])
  341. ->id(fn ($get) => "simple_table_" . $get('simple_table_key'))
  342. ->addActionLabel('新增列')
  343. ->reorderableWithButtons()
  344. ->minItems(1)
  345. ]),
  346. ])->visible(fn (Get $get):bool => $get('type') == 3),
  347. Group::make()->schema([
  348. Section::make('')->schema([
  349. Repeater::make("content.multiple_images")->schema([
  350. TextInput::make('para_img_item_key')
  351. ->default(fn () => Str::random())
  352. ->hidden()
  353. ->afterStateHydrated(function (TextInput $component, $state) {
  354. if (empty($state)) {
  355. $component->state(Str::random());
  356. }
  357. }),
  358. Translate::make()->schema(fn (string $locale) => [
  359. TextInput::make('image_alt')->label("圖片註文")
  360. ])->locales(["zh_TW", "en"])
  361. ->id(fn ($get) => "para_img_mul_" . $get('para_img_item_key')),
  362. FileUpload::make('image_url')->label("")->disk("public")
  363. // ->helperText('建議寬高限制為:1080*675px,檔案大小限制為1M以下')->maxSize('1024')
  364. ->directory("esg/paragraphPhoto")
  365. ->maxFiles(10)
  366. ->required()
  367. ->validationMessages([
  368. 'required' => '請上傳圖片',
  369. ]),
  370. ])
  371. ->addActionLabel('新增')
  372. ->label("")
  373. ->orderColumn('order')
  374. ])
  375. ])->visible(fn (Get $get):bool => $get("type") == 4),
  376. Group::make()->schema([
  377. Section::make("")->schema([
  378. FileUpload::make('content.video_img')->label("影片底圖")
  379. ->disk("public")
  380. ->directory("esg/paragraphVideo")
  381. ->helperText('建議寬高限制為:2000*720px,出血寬度720px,主要圖像範圍為:1280*720px,檔案大小限制為1M以下')->maxSize('1024'),
  382. TextInput::make('content.link')->label("網址")->required()
  383. ->validationMessages([
  384. 'required' => '請附上連結',
  385. ]),
  386. Translate::make()->schema(fn (string $locale) => [
  387. TextInput::make('content.video_alt')->label("影片註文"),
  388. ])->locales(["zh_TW", "en"])
  389. ]),
  390. ])->visible(fn (Get $get):bool => $get("type") == 5),
  391. ])
  392. ->relationship("paragraphs")
  393. ->addActionLabel('新增段落')
  394. ->collapsible()
  395. ->reorderableWithButtons()
  396. ->orderColumn('order')
  397. ->cloneable()
  398. ])->columnSpanFull(),
  399. ])
  400. ->columnSpanFull()
  401. ]);
  402. }
  403. public static function table(Table $table): Table
  404. {
  405. return $table
  406. ->columns([
  407. //
  408. TextColumn::make("title")->label("標題"),
  409. ])
  410. ->filters([
  411. //
  412. ])
  413. ->actions([
  414. Tables\Actions\EditAction::make(),
  415. ])
  416. ->bulkActions([
  417. Tables\Actions\BulkActionGroup::make([
  418. Tables\Actions\DeleteBulkAction::make(),
  419. ]),
  420. ]);
  421. }
  422. public static function getRelations(): array
  423. {
  424. return [
  425. //
  426. ];
  427. }
  428. public static function getPages(): array
  429. {
  430. return [
  431. 'index' => Pages\ListEsg::route('/'),
  432. 'create' => Pages\CreateEsg::route('/create'),
  433. 'edit' => Pages\EditEsg::route('/{record}/edit'),
  434. ];
  435. }
  436. }