123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. <?php
  2. namespace App\Filament\Resources;
  3. use App\Filament\Resources\EsgResource\Pages;
  4. use App\Models\Esg;
  5. use Filament\Forms\Components\Fieldset;
  6. use Filament\Forms\Components\FileUpload;
  7. use Filament\Forms\Components\Grid;
  8. use Filament\Forms\Components\Group;
  9. use Filament\Forms\Components\Placeholder;
  10. use Filament\Forms\Components\Radio;
  11. use Filament\Forms\Components\Repeater;
  12. use Filament\Forms\Components\RichEditor;
  13. use Filament\Forms\Components\Section;
  14. use Filament\Forms\Components\Select;
  15. use Filament\Forms\Components\Tabs;
  16. use Filament\Forms\Components\Tabs\Tab;
  17. use Filament\Forms\Components\Textarea;
  18. use Filament\Forms\Components\TextInput;
  19. use Filament\Forms\Components\Toggle;
  20. use Filament\Forms\Form;
  21. use Filament\Forms\Get;
  22. use Filament\Resources\Resource;
  23. use Filament\Tables;
  24. use Filament\Tables\Columns\TextColumn;
  25. use Filament\Tables\Table;
  26. use Illuminate\Database\Eloquent\Builder;
  27. use Illuminate\Database\Eloquent\SoftDeletingScope;
  28. use Malzariey\FilamentLexicalEditor\Enums\ToolbarItem;
  29. use Malzariey\FilamentLexicalEditor\FilamentLexicalEditor;
  30. use SolutionForest\FilamentTranslateField\Forms\Component\Translate;
  31. use Str;
  32. class EsgResource extends Resource
  33. {
  34. protected static ?string $model = Esg::class;
  35. protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
  36. protected static ?string $modelLabel = "ESG 管理";
  37. protected static ?string $navigationGroup = 'ESG 上稿內容管理';
  38. protected static ?string $navigationLabel = "ESG 管理";
  39. public static function form(Form $form): Form
  40. {
  41. $editor_toolbar= [
  42. ToolbarItem::UNDO, ToolbarItem::REDO, ToolbarItem::NORMAL,
  43. ToolbarItem::H2, ToolbarItem::H3, ToolbarItem::H4, ToolbarItem::H5,
  44. ToolbarItem::BULLET, ToolbarItem::NUMBERED, ToolbarItem::FONT_SIZE,
  45. ToolbarItem::BOLD, ToolbarItem::ITALIC, ToolbarItem::UNDERLINE,
  46. ToolbarItem::LINK, ToolbarItem::TEXT_COLOR, ToolbarItem::BACKGROUND_COLOR,
  47. ToolbarItem::SUBSCRIPT, ToolbarItem::LOWERCASE, ToolbarItem::DIVIDER,
  48. ToolbarItem::UPPERCASE, ToolbarItem::CLEAR, ToolbarItem::HR
  49. ];
  50. return $form
  51. ->schema([
  52. //
  53. Tabs::make("")->tabs([
  54. Tab::make('基本資訊')->schema([
  55. TextInput::make('keyword')->label("關聯字詞")->required()
  56. ->validationMessages([
  57. 'required' => '請填寫關聯字詞',
  58. ]),
  59. Translate::make()->schema(fn (string $locale) => [
  60. TextInput::make('title')
  61. ->label("標題")
  62. ->columnSpan(1)
  63. ->required($locale == "zh_TW")
  64. ->validationMessages([
  65. 'required' => '請填寫標題',
  66. ])
  67. ])->locales(["zh_TW", "en"])
  68. ->columnSpanFull()->columns(3),
  69. ]),
  70. Tab::make('Banner 設定')->schema([
  71. FileUpload::make('banner_pc')->label("Banner (PC)")
  72. ->disk("public")
  73. // ->helperText('建議寬高限制為:1280*720px,檔案大小限制為1M以下')->maxSize('1024')
  74. ->directory("esg")->required()
  75. ->validationMessages([
  76. 'required' => '請上傳圖片',
  77. ]),
  78. FileUpload::make('banner_mobile')->label("Banner (Mobile)")
  79. ->disk("public")
  80. // ->helperText('建議寬高限制為:600*896px,檔案大小限制為1M以下')->maxSize('1024')
  81. ->directory("esg")->required()
  82. ->validationMessages([
  83. 'required' => '請上傳圖片',
  84. ]),
  85. Translate::make()->schema(fn (string $locale) => [
  86. TextInput::make('banner_alt')
  87. ->label("Banner 圖片註釋")
  88. ->required($locale == "zh_TW")
  89. ->validationMessages([
  90. 'required' => '請填寫圖片註釋',
  91. ]),
  92. Textarea::make("description")->rows(5)->columnSpanFull()->label("短文"),
  93. ])->locales(["zh_TW", "en"])
  94. ->columnSpanFull(),
  95. ]),
  96. Tab::make("段落設計")->schema([
  97. //1: 純文字 2:區塊文字 3:表格 4:影片 or 圖片 5:左右圖文
  98. Repeater::make("paragraphs")->label("")->schema([
  99. TextInput::make('item_key')
  100. ->default(fn () => Str::random())
  101. ->hidden()
  102. ->afterStateHydrated(function (TextInput $component, $state) {
  103. if (empty($state)) {
  104. $component->state(Str::random());
  105. }
  106. }),
  107. Radio::make("type")->options([
  108. 1 => "純文字",
  109. 2 => "區塊文字",
  110. 3 => "表格",
  111. 4 => "圖片",
  112. 5 => "影片",
  113. ])->label("")->default(1)->Live(),
  114. Group::make()->schema([
  115. Section::make('純文字設定')->schema([
  116. FilamentLexicalEditor::make('content.text_content_tw')
  117. ->label('中文')
  118. ->id(fn ($get) => "text_content_tw_" . $get('item_key') . "_" . uniqid())
  119. ->enabledToolbars($editor_toolbar)
  120. ->required()
  121. ->columnSpanFull(),
  122. FilamentLexicalEditor::make('content.text_content_en')
  123. ->label('English')
  124. ->id(fn ($get) => "text_content_en_" . $get('item_key') . "_" . uniqid())
  125. ->enabledToolbars($editor_toolbar)
  126. ->columnSpanFull(),
  127. ]),
  128. ])->visible(fn (Get $get):bool => $get("type") == 1),
  129. Group::make()->schema([
  130. Section::make('區塊文字設定')->schema([
  131. // 文字區塊重複器
  132. Repeater::make("content.text_blocks")->schema([
  133. TextInput::make('block_item_key')
  134. ->default(fn () => Str::random())
  135. ->hidden()
  136. ->afterStateHydrated(function (TextInput $component, $state) {
  137. if (empty($state)) {
  138. $component->state(Str::random());
  139. }
  140. }),
  141. FilamentLexicalEditor::make('block_content_tw')
  142. ->label('繁體中文內容')
  143. ->id(fn ($get) => "block_content_tw_" . $get('block_item_key') . "_" . uniqid())
  144. ->enabledToolbars($editor_toolbar)
  145. ->required()
  146. ->live(onBlur: true)
  147. ->columnSpanFull()
  148. ->basicColors([
  149. '#000000',
  150. '#333333',
  151. '#666666',
  152. '#999999',
  153. '#ffffff',
  154. ]),
  155. FilamentLexicalEditor::make('block_content_en')
  156. ->label('English Content')
  157. ->id(fn ($get) => "block_content_en_" . $get('block_item_key') . "_" . uniqid())
  158. ->enabledToolbars($editor_toolbar)
  159. ->columnSpanFull(),
  160. ])
  161. ->label("")
  162. ->addActionLabel('增加區塊')
  163. ->orderColumn('order')
  164. ->reorderableWithButtons()
  165. ->cloneable()
  166. ->minItems(1)
  167. ->maxItems(20)
  168. ->defaultItems(1),
  169. ]),
  170. ])->visible(fn (Get $get):bool => $get('type') == 2),
  171. Group::make()->schema([
  172. Section::make('表格設定')->schema([
  173. Radio::make('content.is_card')
  174. ->label('手機板卡片顯示')
  175. ->options([
  176. '1' => '是', // 改為數字鍵值
  177. '0' => '否'
  178. ])
  179. ->default('0')
  180. ->inline(),
  181. // 表格資料
  182. Placeholder::make('table_builder_info')->label("")->content('請先設定表格欄數,然後填入表格資料'),
  183. Grid::make(3)->schema([
  184. Radio::make('content.column_count')
  185. ->label('欄數')
  186. ->options([
  187. 2 => '2欄', // 改為數字鍵值
  188. 3 => '3欄',
  189. 4 => '4欄'
  190. ])
  191. ->default(2)
  192. ->inline()
  193. ->live()
  194. ->reactive(), // ✅ 添加 reactive()
  195. ]),
  196. Translate::make()->schema(fn (string $locale) => [
  197. Grid::make(2) // 改用 Grid 來支援動態 columns
  198. ->schema([
  199. TextInput::make('content.head1')->label('標題第1欄')->required()
  200. ->validationMessages([
  201. 'required' => '請填寫',
  202. ]),
  203. Radio::make('content.head_align1')
  204. ->label('對齊')
  205. ->options([
  206. 1 => '置左', // 改為數字鍵值
  207. 2 => '置中',
  208. 3 => '置右'
  209. ])
  210. ->default(1),
  211. TextInput::make('content.head2')->label('標題第2欄')->required()
  212. ->validationMessages([
  213. 'required' => '請填寫',
  214. ]),
  215. Radio::make('content.head_align2')
  216. ->label('對齊')
  217. ->options([
  218. 1 => '置左', // 改為數字鍵值
  219. 2 => '置中',
  220. 3 => '置右'
  221. ])
  222. ->default(1),
  223. TextInput::make('content.head3')->label('標題第3欄')
  224. ->visible(fn (Get $get) => intval($get('content.column_count')) >= 3)->columnSpan(1),
  225. Radio::make('content.head_align3')
  226. ->label('對齊')
  227. ->options([
  228. 1 => '置左', // 改為數字鍵值
  229. 2 => '置中',
  230. 3 => '置右'
  231. ])
  232. ->default(1)
  233. ->visible(fn (Get $get) => intval($get('content.column_count')) >= 3)->columnSpan(1),
  234. TextInput::make('content.head4')->label('標題第4欄')
  235. ->visible(fn (Get $get) => intval($get('content.column_count')) >= 4)->columnSpan(1),
  236. Radio::make('content.head_align4')
  237. ->label('對齊')
  238. ->options([
  239. 1 => '置左', // 改為數字鍵值
  240. 2 => '置中',
  241. 3 => '置右'
  242. ])
  243. ->default(1)
  244. ->visible(fn (Get $get) => intval($get('content.column_count')) >= 4)->columnSpan(1),
  245. ])
  246. ->reactive() // 加入響應式
  247. ])->locales(["zh_TW", "en"]),
  248. Repeater::make('content.simple_table_rows')
  249. ->label('表格資料')
  250. ->schema([
  251. TextInput::make('simple_table_rows_key')
  252. ->default(fn () => Str::random())
  253. ->hidden()
  254. ->afterStateHydrated(function (TextInput $component, $state) {
  255. if (empty($state)) {
  256. $component->state(Str::random());
  257. }
  258. }),
  259. Grid::make(1) // 改用 Grid 來支援動態 columns
  260. ->schema([
  261. Section::make("第一欄")->schema([
  262. Fieldset::make('中文')->schema([
  263. Radio::make('align1_tw')
  264. ->label('對齊')
  265. ->options([
  266. 1 => '置左', // 改為數字鍵值
  267. 2 => '置中',
  268. 3 => '置右'
  269. ])
  270. ->default(1)->inline(),
  271. FilamentLexicalEditor::make('col1_tw')
  272. ->id(fn ($get) => "col1_tw_" . $get('simple_table_rows_key') . "_" . uniqid())
  273. ->enabledToolbars($editor_toolbar)
  274. ->required()
  275. ->columnSpanFull(),
  276. ]),
  277. Fieldset::make('英文')->schema([
  278. Radio::make('align1_en')
  279. ->label('對齊')
  280. ->options([
  281. 1 => '置左', // 改為數字鍵值
  282. 2 => '置中',
  283. 3 => '置右'
  284. ])
  285. ->default(1)->inline(),
  286. FilamentLexicalEditor::make('col1_en')
  287. ->id(fn ($get) => "col1_en_" . $get('simple_table_rows_key') . "_" . uniqid())
  288. ->enabledToolbars($editor_toolbar)
  289. ->columnSpanFull(),
  290. ]),
  291. ]),
  292. Section::make("第二欄")->schema([
  293. Fieldset::make('中文')->schema([
  294. Radio::make('align2_tw')
  295. ->label('對齊')
  296. ->options([
  297. 1 => '置左', // 改為數字鍵值
  298. 2 => '置中',
  299. 3 => '置右'
  300. ])
  301. ->default(1)->inline(),
  302. FilamentLexicalEditor::make('col2_tw')
  303. ->id(fn ($get) => "col2_tw_" . $get('simple_table_rows_key') . "_" . uniqid())
  304. ->enabledToolbars($editor_toolbar)
  305. ->required()
  306. ->columnSpanFull(),
  307. ]),
  308. Fieldset::make('英文')->schema([
  309. Radio::make('align2_en')
  310. ->label('對齊')
  311. ->options([
  312. 1 => '置左', // 改為數字鍵值
  313. 2 => '置中',
  314. 3 => '置右'
  315. ])
  316. ->default(1)->inline(),
  317. FilamentLexicalEditor::make('col2_en')
  318. ->id(fn ($get) => "col2_en_" . $get('simple_table_rows_key') . "_" . uniqid())
  319. ->enabledToolbars($editor_toolbar)
  320. ->columnSpanFull(),
  321. ]),
  322. ]),
  323. Section::make("第三欄")->schema([
  324. Fieldset::make('中文')->schema([
  325. Radio::make('align3_tw')
  326. ->label('對齊')
  327. ->options([
  328. 1 => '置左', // 改為數字鍵值
  329. 2 => '置中',
  330. 3 => '置右'
  331. ])
  332. ->default(1),
  333. FilamentLexicalEditor::make('col3_tw')
  334. ->id(fn ($get) => "col3_tw_" . $get('simple_table_rows_key') . "_" . uniqid())
  335. ->enabledToolbars($editor_toolbar)
  336. ->required()
  337. ->columnSpanFull(),
  338. ]),
  339. Fieldset::make('英文')->schema([
  340. Radio::make('align3_en')
  341. ->label('對齊')
  342. ->options([
  343. 1 => '置左', // 改為數字鍵值
  344. 2 => '置中',
  345. 3 => '置右'
  346. ])
  347. ->default(1)->inline(),
  348. FilamentLexicalEditor::make('col3_en')
  349. ->id(fn ($get) => "col3_en_" . $get('simple_table_rows_key') . "_" . uniqid())
  350. ->enabledToolbars($editor_toolbar)
  351. ->columnSpanFull(),
  352. ]),
  353. ])->visible(fn (Get $get) => intval($get('../../../content.column_count')) >= 3),
  354. Section::make("第四欄")->schema([
  355. Fieldset::make('中文')->schema([
  356. Radio::make('align4_tw')
  357. ->label('對齊')
  358. ->options([
  359. 1 => '置左', // 改為數字鍵值
  360. 2 => '置中',
  361. 3 => '置右'
  362. ])
  363. ->default(1)->inline(),
  364. FilamentLexicalEditor::make('col4_tw')
  365. ->id(fn ($get) => "col4_tw_" . $get('simple_table_rows_key') . "_" . uniqid())
  366. ->enabledToolbars($editor_toolbar)
  367. ->required()
  368. ->columnSpanFull(),
  369. ]),
  370. Fieldset::make('英文')->schema([
  371. Radio::make('align4_en')
  372. ->label('對齊')
  373. ->options([
  374. 1 => '置左', // 改為數字鍵值
  375. 2 => '置中',
  376. 3 => '置右'
  377. ])
  378. ->default(1)->inline(),
  379. FilamentLexicalEditor::make('col4_en')
  380. ->id(fn ($get) => "col4_en_" . $get('simple_table_rows_key') . "_" . uniqid())
  381. ->enabledToolbars($editor_toolbar)
  382. ->columnSpanFull(),
  383. ]),
  384. ])->visible(fn (Get $get) => intval($get('../../../content.column_count')) >= 4),
  385. ])
  386. ->reactive() // 加入響應式
  387. ])
  388. ->id(fn ($get) => "simple_table_" . $get('simple_table_rows_key'))
  389. ->addActionLabel('新增列')
  390. ->reorderableWithButtons()
  391. ->minItems(1)
  392. ]),
  393. ])->visible(fn (Get $get):bool => $get('type') == 3),
  394. Group::make()->schema([
  395. Section::make('')->schema([
  396. Repeater::make("content.multiple_images")->schema([
  397. TextInput::make('para_img_item_key')
  398. ->default(fn () => Str::random())
  399. ->hidden()
  400. ->afterStateHydrated(function (TextInput $component, $state) {
  401. if (empty($state)) {
  402. $component->state(Str::random());
  403. }
  404. }),
  405. Translate::make()->schema(fn (string $locale) => [
  406. TextInput::make('image_alt')->label("圖片註文")
  407. ])->locales(["zh_TW", "en"])
  408. ->id(fn ($get) => "para_img_mul_" . $get('para_img_item_key')),
  409. FileUpload::make('image_url')->label("")->disk("public")
  410. // ->helperText('建議寬高限制為:1080*675px,檔案大小限制為1M以下')->maxSize('1024')
  411. ->directory("esg/paragraphPhoto")
  412. ->maxFiles(10)
  413. ->required()
  414. ->validationMessages([
  415. 'required' => '請上傳圖片',
  416. ]),
  417. ])
  418. ->addActionLabel('新增')
  419. ->label("")
  420. ->orderColumn('order')
  421. ])
  422. ])->visible(fn (Get $get):bool => $get("type") == 4),
  423. Group::make()->schema([
  424. Section::make("")->schema([
  425. FileUpload::make('content.video_img')->label("影片底圖")
  426. ->disk("public")
  427. ->directory("esg/paragraphVideo"),
  428. TextInput::make('content.link')->label("網址")->required()
  429. ->validationMessages([
  430. 'required' => '請附上連結',
  431. ]),
  432. Translate::make()->schema(fn (string $locale) => [
  433. TextInput::make('content.video_alt')->label("影片註文"),
  434. ])->locales(["zh_TW", "en"])
  435. ]),
  436. ])->visible(fn (Get $get):bool => $get("type") == 5),
  437. ])
  438. ->relationship("paragraphs")
  439. ->addActionLabel('新增段落')
  440. ->collapsible()
  441. ->reorderableWithButtons()
  442. ->orderColumn('order')
  443. ->cloneable()
  444. ->mutateRelationshipDataBeforeFillUsing(function (array $data): array {
  445. switch($data["type"]){
  446. case "1":
  447. if (!empty($data["content"]['text_content'])) {
  448. $content = is_string($data["content"]['text_content'])
  449. ? json_decode($data["content"]['text_content'], true)
  450. : $data["content"]['text_content'];
  451. if (is_array($content)) {
  452. $data["content"]['text_content_tw'] = $content['zh_TW'] ?? '';
  453. $data["content"]['text_content_en'] = $content['en'] ?? '';
  454. }
  455. }
  456. break;
  457. case "2":
  458. if (!empty($data["content"]['text_blocks']) && count($data["content"]['text_blocks']) > 0) {
  459. foreach ($data["content"]['text_blocks'] as $block_index => $block){
  460. $content = is_string($block['block_content'])
  461. ? json_decode($block['block_content'], true)
  462. : $block['block_content'];
  463. if (is_array($content)) {
  464. $data["content"]['text_blocks'][$block_index]["block_content_tw"] = $content['zh_TW'] ?? '';
  465. $data["content"]['text_blocks'][$block_index]["block_content_en"] = $content['en'] ?? '';
  466. }
  467. }
  468. }
  469. break;
  470. case "3":
  471. if (!empty($data["content"]['simple_table_rows']) && count($data["content"]['simple_table_rows']) > 0) {
  472. foreach ($data["content"]['simple_table_rows'] as $row_key => $row) {
  473. $flatRow = [];
  474. foreach ($row as $key => $value) {
  475. if (is_array($value) && isset($value['zh_TW'], $value['en'])) {
  476. // 多语言字段,分离为两个字段
  477. $flatRow[$key . '_tw'] = $value['zh_TW'];
  478. $flatRow[$key . '_en'] = $value['en'];
  479. } else {
  480. // 非多语言字段,保持原样
  481. $flatRow[$key] = $value;
  482. }
  483. }
  484. $data["content"]['simple_table_rows'][$row_key] = $flatRow;
  485. }
  486. }
  487. break;
  488. }
  489. return $data;
  490. })
  491. ->mutateRelationshipDataBeforeSaveUsing(function (array $data): array {
  492. switch($data["type"]){
  493. case "1":
  494. $data["content"]["text_content"] = ["zh_TW" => $data["content"]["text_content_tw"] ?? "" , "en" => $data["content"]["text_content_en"] ?? ""];
  495. // 移除臨時的分離欄位,只保留合併的 JSON 欄位
  496. unset($data["content"]["text_content_tw"]);
  497. unset($data["content"]["text_content_en"]);
  498. // 清理 HTML 標籤(如果需要)
  499. foreach ($data['content']['text_content'] as $locale => $content) {
  500. $data['content']['text_content'][$locale] = stripslashes($content);
  501. }
  502. break;
  503. case "2":
  504. // 確保有 text_blocks
  505. if (!isset($data['content']['text_blocks'])) {
  506. $data['content']['text_blocks'] = [];
  507. }
  508. // 處理每個文字區塊
  509. foreach ($data['content']['text_blocks'] as $index => &$block) {
  510. // 確保區塊有必要的欄位
  511. $block['block_item_key'] = $block['block_item_key'] ?? Str::random();
  512. $block['order'] = $block['order'] ?? ($index + 1);
  513. $block["block_content"] = ["zh_TW" => $block["block_content_tw"] ?? "" , "en" => $block["block_content_en"] ?? ""];
  514. // 移除臨時的分離欄位,只保留合併的 JSON 欄位
  515. unset($block["block_content_tw"]);
  516. unset($block["block_content_en"]);
  517. // 清理區塊內容
  518. if (isset($block['block_content'])) {
  519. foreach ($block['block_content'] as $locale => $content) {
  520. $block['block_content'][$locale] = stripslashes($content);
  521. }
  522. }
  523. }
  524. break;
  525. case "3":
  526. // 確保欄數設定
  527. $columnCount = $data['content']['column_count'] ?? 2;
  528. // 確保表頭存在
  529. if (!isset($data['content']['headers'])) {
  530. $data['content']['headers'] = ['zh_TW' => [], 'en' => []];
  531. }
  532. // 確保表格資料存在
  533. if (!isset($data['content']['simple_table_rows'])) {
  534. $data['content']['simple_table_rows'] = [];
  535. }
  536. // 處理表格資料,確保欄數一致
  537. foreach ($data['content']['simple_table_rows'] as $rowIndex => &$row) {
  538. // 確保每行都有正確的欄數
  539. for ($i = 1; $i <= $columnCount; $i++) {
  540. $row["col{$i}"] = ["zh_TW" => $row["col{$i}_tw"] ?? "" , "en" => $row["col{$i}_en"] ?? ""];
  541. foreach ($row["col{$i}"] as $locale => $content) {
  542. $row["col{$i}"][$locale] = $content;
  543. }
  544. unset($row["col{$i}_tw"]);
  545. unset($row["col{$i}_en"]);
  546. $row["align{$i}"] = ["zh_TW" => $row["align{$i}_tw"] ?? "" , "en" => $row["align{$i}_en"] ?? ""];
  547. foreach ($row["align{$i}"] as $locale => $content) {
  548. $row["align{$i}"][$locale] = $content;
  549. }
  550. unset($row["align{$i}_tw"]);
  551. unset($row["align{$i}_en"]);
  552. }
  553. // 移除多餘的欄位
  554. for ($i = $columnCount + 1; $i <= 4; $i++) {
  555. unset($row["col{$i}_tw"]);
  556. unset($row["col{$i}_en"]);
  557. unset($row["align{$i}_tw"]);
  558. unset($row["align{$i}_en"]);
  559. }
  560. }
  561. break;
  562. }
  563. return $data;
  564. })
  565. ->mutateRelationshipDataBeforeCreateUsing(function (array $data): array {
  566. switch($data["type"]){
  567. case "1":
  568. $data["content"]["text_content"] = ["zh_TW" => $data["content"]["text_content_tw"] ?? "" , "en" => $data["content"]["text_content_en"] ?? ""];
  569. // 移除臨時的分離欄位,只保留合併的 JSON 欄位
  570. unset($data["content"]["text_content_tw"]);
  571. unset($data["content"]["text_content_en"]);
  572. // 清理 HTML 標籤(如果需要)
  573. foreach ($data['content']['text_content'] as $locale => $content) {
  574. $data['content']['text_content'][$locale] = stripslashes($content);
  575. }
  576. break;
  577. case "2":
  578. // 確保有 text_blocks
  579. if (!isset($data['content']['text_blocks'])) {
  580. $data['content']['text_blocks'] = [];
  581. }
  582. // 處理每個文字區塊
  583. foreach ($data['content']['text_blocks'] as $index => &$block) {
  584. // 確保區塊有必要的欄位
  585. $block['block_item_key'] = $block['block_item_key'] ?? Str::random();
  586. $block['order'] = $block['order'] ?? ($index + 1);
  587. $block["block_content"] = ["zh_TW" => $block["block_content_tw"] ?? "" , "en" => $block["block_content_en"] ?? ""];
  588. // 移除臨時的分離欄位,只保留合併的 JSON 欄位
  589. unset($block["block_content_tw"]);
  590. unset($block["block_content_en"]);
  591. // 清理區塊內容
  592. if (isset($block['block_content'])) {
  593. foreach ($block['block_content'] as $locale => $content) {
  594. $block['block_content'][$locale] = stripslashes($content);
  595. }
  596. }
  597. }
  598. break;
  599. case "3":
  600. // 確保欄數設定
  601. $columnCount = $data['content']['column_count'] ?? 2;
  602. // 確保表頭存在
  603. if (!isset($data['content']['headers'])) {
  604. $data['content']['headers'] = ['zh_TW' => [], 'en' => []];
  605. }
  606. // 確保表格資料存在
  607. if (!isset($data['content']['simple_table_rows'])) {
  608. $data['content']['simple_table_rows'] = [];
  609. }
  610. // 處理表格資料,確保欄數一致
  611. foreach ($data['content']['simple_table_rows'] as $rowIndex => &$row) {
  612. // 確保每行都有正確的欄數
  613. for ($i = 1; $i <= $columnCount; $i++) {
  614. $row["col{$i}"] = ["zh_TW" => $row["col{$i}_tw"] ?? "" , "en" => $row["col{$i}_en"] ?? ""];
  615. foreach ($row["col{$i}"] as $locale => $content) {
  616. $row["col{$i}"][$locale] = $content;
  617. }
  618. unset($row["col{$i}_tw"]);
  619. unset($row["col{$i}_en"]);
  620. $row["align{$i}"] = ["zh_TW" => $row["align{$i}_tw"] ?? "" , "en" => $row["align{$i}_en"] ?? ""];
  621. foreach ($row["align{$i}"] as $locale => $content) {
  622. $row["align{$i}"][$locale] = $content;
  623. }
  624. unset($row["align{$i}_tw"]);
  625. unset($row["align{$i}_en"]);
  626. }
  627. // 移除多餘的欄位
  628. for ($i = $columnCount + 1; $i <= 4; $i++) {
  629. unset($row["col{$i}_tw"]);
  630. unset($row["col{$i}_en"]);
  631. unset($row["align{$i}_tw"]);
  632. unset($row["align{$i}_en"]);
  633. }
  634. }
  635. break;
  636. }
  637. \Log::info($data);
  638. return $data;
  639. })
  640. ])->columnSpanFull(),
  641. ])
  642. ->columnSpanFull()
  643. ]);
  644. }
  645. public static function table(Table $table): Table
  646. {
  647. return $table
  648. ->columns([
  649. //
  650. TextColumn::make("title")->label("標題"),
  651. ])
  652. ->filters([
  653. //
  654. ])
  655. ->actions([
  656. Tables\Actions\EditAction::make(),
  657. ]);
  658. }
  659. public static function getRelations(): array
  660. {
  661. return [
  662. //
  663. ];
  664. }
  665. public static function getPages(): array
  666. {
  667. return [
  668. 'index' => Pages\ListEsg::route('/'),
  669. 'create' => Pages\CreateEsg::route('/create'),
  670. 'edit' => Pages\EditEsg::route('/{record}/edit'),
  671. ];
  672. }
  673. }