EsgResource.php 45KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  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, ToolbarItem::FONT_FAMILY
  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. ->label(new \Illuminate\Support\HtmlString('
  119. 中文<br> <img src="' . asset('images/your-image.png') . '" alt="label image" class="h-6 w-auto" />
  120. '))
  121. ->id(fn ($get) => "text_content_tw_" . $get('item_key') . "_" . uniqid())
  122. ->enabledToolbars($editor_toolbar)
  123. ->required()
  124. ->columnSpanFull(),
  125. FilamentLexicalEditor::make('content.text_content_en')
  126. ->label('English')
  127. ->id(fn ($get) => "text_content_en_" . $get('item_key') . "_" . uniqid())
  128. ->enabledToolbars($editor_toolbar)
  129. ->columnSpanFull(),
  130. ]),
  131. ])->visible(fn (Get $get):bool => $get("type") == 1),
  132. Group::make()->schema([
  133. Section::make('區塊文字設定')->schema([
  134. // 文字區塊重複器
  135. Repeater::make("content.text_blocks")->schema([
  136. TextInput::make('block_item_key')
  137. ->default(fn () => Str::random())
  138. ->hidden()
  139. ->afterStateHydrated(function (TextInput $component, $state) {
  140. if (empty($state)) {
  141. $component->state(Str::random());
  142. }
  143. }),
  144. FilamentLexicalEditor::make('block_content_tw')
  145. ->label('繁體中文內容')
  146. ->id(fn ($get) => "block_content_tw_" . $get('block_item_key') . "_" . uniqid())
  147. ->enabledToolbars($editor_toolbar)
  148. ->required()
  149. ->live(onBlur: true)
  150. ->columnSpanFull(),
  151. FilamentLexicalEditor::make('block_content_en')
  152. ->label('English Content')
  153. ->id(fn ($get) => "block_content_en_" . $get('block_item_key') . "_" . uniqid())
  154. ->enabledToolbars($editor_toolbar)
  155. ->columnSpanFull(),
  156. ])
  157. ->label("")
  158. ->addActionLabel('增加區塊')
  159. ->orderColumn('order')
  160. ->reorderableWithButtons()
  161. ->cloneable()
  162. ->minItems(1)
  163. ->maxItems(20)
  164. ->defaultItems(1),
  165. ]),
  166. ])->visible(fn (Get $get):bool => $get('type') == 2),
  167. Group::make()->schema([
  168. Section::make('表格設定')->schema([
  169. Radio::make('content.is_card')
  170. ->label('手機板卡片顯示')
  171. ->options([
  172. '1' => '是', // 改為數字鍵值
  173. '0' => '否'
  174. ])
  175. ->default('0')
  176. ->inline(),
  177. // 表格資料
  178. Placeholder::make('table_builder_info')->label("")->content('請先設定表格欄數,然後填入表格資料'),
  179. Grid::make(3)->schema([
  180. Radio::make('content.column_count')
  181. ->label('欄數')
  182. ->options([
  183. 2 => '2欄', // 改為數字鍵值
  184. 3 => '3欄',
  185. 4 => '4欄'
  186. ])
  187. ->default(2)
  188. ->inline()
  189. ->live()
  190. ->reactive(), // ✅ 添加 reactive()
  191. ]),
  192. Translate::make()->schema(fn (string $locale) => [
  193. Grid::make(2) // 改用 Grid 來支援動態 columns
  194. ->schema([
  195. TextInput::make('content.head1')->label('標題第1欄')->required()
  196. ->validationMessages([
  197. 'required' => '請填寫',
  198. ]),
  199. Radio::make('content.head_align1')
  200. ->label('對齊')
  201. ->options([
  202. 1 => '置左', // 改為數字鍵值
  203. 2 => '置中',
  204. 3 => '置右'
  205. ])
  206. ->default(1),
  207. TextInput::make('content.head2')->label('標題第2欄')->required()
  208. ->validationMessages([
  209. 'required' => '請填寫',
  210. ]),
  211. Radio::make('content.head_align2')
  212. ->label('對齊')
  213. ->options([
  214. 1 => '置左', // 改為數字鍵值
  215. 2 => '置中',
  216. 3 => '置右'
  217. ])
  218. ->default(1),
  219. TextInput::make('content.head3')->label('標題第3欄')
  220. ->visible(fn (Get $get) => intval($get('content.column_count')) >= 3)->columnSpan(1),
  221. Radio::make('content.head_align3')
  222. ->label('對齊')
  223. ->options([
  224. 1 => '置左', // 改為數字鍵值
  225. 2 => '置中',
  226. 3 => '置右'
  227. ])
  228. ->default(1)
  229. ->visible(fn (Get $get) => intval($get('content.column_count')) >= 3)->columnSpan(1),
  230. TextInput::make('content.head4')->label('標題第4欄')
  231. ->visible(fn (Get $get) => intval($get('content.column_count')) >= 4)->columnSpan(1),
  232. Radio::make('content.head_align4')
  233. ->label('對齊')
  234. ->options([
  235. 1 => '置左', // 改為數字鍵值
  236. 2 => '置中',
  237. 3 => '置右'
  238. ])
  239. ->default(1)
  240. ->visible(fn (Get $get) => intval($get('content.column_count')) >= 4)->columnSpan(1),
  241. ])
  242. ->reactive() // 加入響應式
  243. ])->locales(["zh_TW", "en"]),
  244. Repeater::make('content.simple_table_rows')
  245. ->label('表格資料')
  246. ->schema([
  247. TextInput::make('simple_table_rows_key')
  248. ->default(fn () => Str::random())
  249. ->hidden()
  250. ->afterStateHydrated(function (TextInput $component, $state) {
  251. if (empty($state)) {
  252. $component->state(Str::random());
  253. }
  254. }),
  255. Grid::make(1) // 改用 Grid 來支援動態 columns
  256. ->schema([
  257. Section::make("第一欄")->schema([
  258. Fieldset::make('中文')->schema([
  259. Radio::make('align1_tw')
  260. ->label('對齊')
  261. ->options([
  262. 1 => '置左', // 改為數字鍵值
  263. 2 => '置中',
  264. 3 => '置右'
  265. ])
  266. ->default(1)->inline(),
  267. FilamentLexicalEditor::make('col1_tw')
  268. ->id(fn ($get) => "col1_tw_" . $get('simple_table_rows_key') . "_" . uniqid())
  269. ->enabledToolbars($editor_toolbar)
  270. ->required()
  271. ->columnSpanFull(),
  272. ]),
  273. Fieldset::make('英文')->schema([
  274. Radio::make('align1_en')
  275. ->label('對齊')
  276. ->options([
  277. 1 => '置左', // 改為數字鍵值
  278. 2 => '置中',
  279. 3 => '置右'
  280. ])
  281. ->default(1)->inline(),
  282. FilamentLexicalEditor::make('col1_en')
  283. ->id(fn ($get) => "col1_en_" . $get('simple_table_rows_key') . "_" . uniqid())
  284. ->enabledToolbars($editor_toolbar)
  285. ->columnSpanFull(),
  286. ]),
  287. ]),
  288. Section::make("第二欄")->schema([
  289. Fieldset::make('中文')->schema([
  290. Radio::make('align2_tw')
  291. ->label('對齊')
  292. ->options([
  293. 1 => '置左', // 改為數字鍵值
  294. 2 => '置中',
  295. 3 => '置右'
  296. ])
  297. ->default(1)->inline(),
  298. FilamentLexicalEditor::make('col2_tw')
  299. ->id(fn ($get) => "col2_tw_" . $get('simple_table_rows_key') . "_" . uniqid())
  300. ->enabledToolbars($editor_toolbar)
  301. ->required()
  302. ->columnSpanFull(),
  303. ]),
  304. Fieldset::make('英文')->schema([
  305. Radio::make('align2_en')
  306. ->label('對齊')
  307. ->options([
  308. 1 => '置左', // 改為數字鍵值
  309. 2 => '置中',
  310. 3 => '置右'
  311. ])
  312. ->default(1)->inline(),
  313. FilamentLexicalEditor::make('col2_en')
  314. ->id(fn ($get) => "col2_en_" . $get('simple_table_rows_key') . "_" . uniqid())
  315. ->enabledToolbars($editor_toolbar)
  316. ->columnSpanFull(),
  317. ]),
  318. ]),
  319. Section::make("第三欄")->schema([
  320. Fieldset::make('中文')->schema([
  321. Radio::make('align3_tw')
  322. ->label('對齊')
  323. ->options([
  324. 1 => '置左', // 改為數字鍵值
  325. 2 => '置中',
  326. 3 => '置右'
  327. ])
  328. ->default(1),
  329. FilamentLexicalEditor::make('col3_tw')
  330. ->id(fn ($get) => "col3_tw_" . $get('simple_table_rows_key') . "_" . uniqid())
  331. ->enabledToolbars($editor_toolbar)
  332. ->required()
  333. ->columnSpanFull(),
  334. ]),
  335. Fieldset::make('英文')->schema([
  336. Radio::make('align3_en')
  337. ->label('對齊')
  338. ->options([
  339. 1 => '置左', // 改為數字鍵值
  340. 2 => '置中',
  341. 3 => '置右'
  342. ])
  343. ->default(1)->inline(),
  344. FilamentLexicalEditor::make('col3_en')
  345. ->id(fn ($get) => "col3_en_" . $get('simple_table_rows_key') . "_" . uniqid())
  346. ->enabledToolbars($editor_toolbar)
  347. ->columnSpanFull(),
  348. ]),
  349. ])->visible(fn (Get $get) => intval($get('../../../content.column_count')) >= 3),
  350. Section::make("第四欄")->schema([
  351. Fieldset::make('中文')->schema([
  352. Radio::make('align4_tw')
  353. ->label('對齊')
  354. ->options([
  355. 1 => '置左', // 改為數字鍵值
  356. 2 => '置中',
  357. 3 => '置右'
  358. ])
  359. ->default(1)->inline(),
  360. FilamentLexicalEditor::make('col4_tw')
  361. ->id(fn ($get) => "col4_tw_" . $get('simple_table_rows_key') . "_" . uniqid())
  362. ->enabledToolbars($editor_toolbar)
  363. ->required()
  364. ->columnSpanFull(),
  365. ]),
  366. Fieldset::make('英文')->schema([
  367. Radio::make('align4_en')
  368. ->label('對齊')
  369. ->options([
  370. 1 => '置左', // 改為數字鍵值
  371. 2 => '置中',
  372. 3 => '置右'
  373. ])
  374. ->default(1)->inline(),
  375. FilamentLexicalEditor::make('col4_en')
  376. ->id(fn ($get) => "col4_en_" . $get('simple_table_rows_key') . "_" . uniqid())
  377. ->enabledToolbars($editor_toolbar)
  378. ->columnSpanFull(),
  379. ]),
  380. ])->visible(fn (Get $get) => intval($get('../../../content.column_count')) >= 4),
  381. ])
  382. ->reactive() // 加入響應式
  383. ])
  384. ->id(fn ($get) => "simple_table_" . $get('simple_table_rows_key'))
  385. ->addActionLabel('新增列')
  386. ->reorderableWithButtons()
  387. ->minItems(1)
  388. ]),
  389. ])->visible(fn (Get $get):bool => $get('type') == 3),
  390. Group::make()->schema([
  391. Section::make('')->schema([
  392. Repeater::make("content.multiple_images")->schema([
  393. TextInput::make('para_img_item_key')
  394. ->default(fn () => Str::random())
  395. ->hidden()
  396. ->afterStateHydrated(function (TextInput $component, $state) {
  397. if (empty($state)) {
  398. $component->state(Str::random());
  399. }
  400. }),
  401. Translate::make()->schema(fn (string $locale) => [
  402. TextInput::make('image_alt')->label("圖片註文")
  403. ])->locales(["zh_TW", "en"])
  404. ->id(fn ($get) => "para_img_mul_" . $get('para_img_item_key')),
  405. FileUpload::make('image_url')->label("")->disk("public")
  406. // ->helperText('建議寬高限制為:1080*675px,檔案大小限制為1M以下')->maxSize('1024')
  407. ->directory("esg/paragraphPhoto")
  408. ->maxFiles(10)
  409. ->required()
  410. ->validationMessages([
  411. 'required' => '請上傳圖片',
  412. ]),
  413. ])
  414. ->addActionLabel('新增')
  415. ->label("")
  416. ->orderColumn('order')
  417. ])
  418. ])->visible(fn (Get $get):bool => $get("type") == 4),
  419. Group::make()->schema([
  420. Section::make("")->schema([
  421. FileUpload::make('content.video_img')->label("影片底圖")
  422. ->disk("public")
  423. ->directory("esg/paragraphVideo"),
  424. TextInput::make('content.link')->label("網址")->required()
  425. ->validationMessages([
  426. 'required' => '請附上連結',
  427. ]),
  428. Translate::make()->schema(fn (string $locale) => [
  429. TextInput::make('content.video_alt')->label("影片註文"),
  430. ])->locales(["zh_TW", "en"])
  431. ]),
  432. ])->visible(fn (Get $get):bool => $get("type") == 5),
  433. ])
  434. ->relationship("paragraphs")
  435. ->addActionLabel('新增段落')
  436. ->collapsible()
  437. ->reorderableWithButtons()
  438. ->orderColumn('order')
  439. ->cloneable()
  440. ->mutateRelationshipDataBeforeFillUsing(function (array $data): array {
  441. switch($data["type"]){
  442. case "1":
  443. if (!empty($data["content"]['text_content'])) {
  444. $content = is_string($data["content"]['text_content'])
  445. ? json_decode($data["content"]['text_content'], true)
  446. : $data["content"]['text_content'];
  447. if (is_array($content)) {
  448. $data["content"]['text_content_tw'] = $content['zh_TW'] ?? '';
  449. $data["content"]['text_content_en'] = $content['en'] ?? '';
  450. }
  451. }
  452. break;
  453. case "2":
  454. if (!empty($data["content"]['text_blocks']) && count($data["content"]['text_blocks']) > 0) {
  455. foreach ($data["content"]['text_blocks'] as $block_index => $block){
  456. $content = is_string($block['block_content'])
  457. ? json_decode($block['block_content'], true)
  458. : $block['block_content'];
  459. if (is_array($content)) {
  460. $data["content"]['text_blocks'][$block_index]["block_content_tw"] = $content['zh_TW'] ?? '';
  461. $data["content"]['text_blocks'][$block_index]["block_content_en"] = $content['en'] ?? '';
  462. }
  463. }
  464. }
  465. break;
  466. case "3":
  467. if (!empty($data["content"]['simple_table_rows']) && count($data["content"]['simple_table_rows']) > 0) {
  468. foreach ($data["content"]['simple_table_rows'] as $row_key => $row) {
  469. $flatRow = [];
  470. foreach ($row as $key => $value) {
  471. if (is_array($value) && isset($value['zh_TW'], $value['en'])) {
  472. // 多语言字段,分离为两个字段
  473. $flatRow[$key . '_tw'] = $value['zh_TW'];
  474. $flatRow[$key . '_en'] = $value['en'];
  475. } else {
  476. // 非多语言字段,保持原样
  477. $flatRow[$key] = $value;
  478. }
  479. }
  480. $data["content"]['simple_table_rows'][$row_key] = $flatRow;
  481. }
  482. }
  483. break;
  484. }
  485. return $data;
  486. })
  487. ->mutateRelationshipDataBeforeSaveUsing(function (array $data): array {
  488. switch($data["type"]){
  489. case "1":
  490. $data["content"]["text_content"] = ["zh_TW" => $data["content"]["text_content_tw"] ?? "" , "en" => $data["content"]["text_content_en"] ?? ""];
  491. // 移除臨時的分離欄位,只保留合併的 JSON 欄位
  492. unset($data["content"]["text_content_tw"]);
  493. unset($data["content"]["text_content_en"]);
  494. // 清理 HTML 標籤(如果需要)
  495. foreach ($data['content']['text_content'] as $locale => $content) {
  496. $data['content']['text_content'][$locale] = stripslashes($content);
  497. }
  498. break;
  499. case "2":
  500. // 確保有 text_blocks
  501. if (!isset($data['content']['text_blocks'])) {
  502. $data['content']['text_blocks'] = [];
  503. }
  504. // 處理每個文字區塊
  505. foreach ($data['content']['text_blocks'] as $index => &$block) {
  506. // 確保區塊有必要的欄位
  507. $block['block_item_key'] = $block['block_item_key'] ?? Str::random();
  508. $block['order'] = $block['order'] ?? ($index + 1);
  509. $block["block_content"] = ["zh_TW" => $block["block_content_tw"] ?? "" , "en" => $block["block_content_en"] ?? ""];
  510. // 移除臨時的分離欄位,只保留合併的 JSON 欄位
  511. unset($block["block_content_tw"]);
  512. unset($block["block_content_en"]);
  513. // 清理區塊內容
  514. if (isset($block['block_content'])) {
  515. foreach ($block['block_content'] as $locale => $content) {
  516. $block['block_content'][$locale] = stripslashes($content);
  517. }
  518. }
  519. }
  520. break;
  521. case "3":
  522. // 確保欄數設定
  523. $columnCount = $data['content']['column_count'] ?? 2;
  524. // 確保表頭存在
  525. if (!isset($data['content']['headers'])) {
  526. $data['content']['headers'] = ['zh_TW' => [], 'en' => []];
  527. }
  528. // 確保表格資料存在
  529. if (!isset($data['content']['simple_table_rows'])) {
  530. $data['content']['simple_table_rows'] = [];
  531. }
  532. // 處理表格資料,確保欄數一致
  533. foreach ($data['content']['simple_table_rows'] as $rowIndex => &$row) {
  534. // 確保每行都有正確的欄數
  535. for ($i = 1; $i <= $columnCount; $i++) {
  536. $row["col{$i}"] = ["zh_TW" => $row["col{$i}_tw"] ?? "" , "en" => $row["col{$i}_en"] ?? ""];
  537. foreach ($row["col{$i}"] as $locale => $content) {
  538. $row["col{$i}"][$locale] = $content;
  539. }
  540. unset($row["col{$i}_tw"]);
  541. unset($row["col{$i}_en"]);
  542. $row["align{$i}"] = ["zh_TW" => $row["align{$i}_tw"] ?? "" , "en" => $row["align{$i}_en"] ?? ""];
  543. foreach ($row["align{$i}"] as $locale => $content) {
  544. $row["align{$i}"][$locale] = $content;
  545. }
  546. unset($row["align{$i}_tw"]);
  547. unset($row["align{$i}_en"]);
  548. }
  549. // 移除多餘的欄位
  550. for ($i = $columnCount + 1; $i <= 4; $i++) {
  551. unset($row["col{$i}_tw"]);
  552. unset($row["col{$i}_en"]);
  553. unset($row["align{$i}_tw"]);
  554. unset($row["align{$i}_en"]);
  555. }
  556. }
  557. break;
  558. }
  559. return $data;
  560. })
  561. ->mutateRelationshipDataBeforeCreateUsing(function (array $data): array {
  562. switch($data["type"]){
  563. case "1":
  564. $data["content"]["text_content"] = ["zh_TW" => $data["content"]["text_content_tw"] ?? "" , "en" => $data["content"]["text_content_en"] ?? ""];
  565. // 移除臨時的分離欄位,只保留合併的 JSON 欄位
  566. unset($data["content"]["text_content_tw"]);
  567. unset($data["content"]["text_content_en"]);
  568. // 清理 HTML 標籤(如果需要)
  569. foreach ($data['content']['text_content'] as $locale => $content) {
  570. $data['content']['text_content'][$locale] = stripslashes($content);
  571. }
  572. break;
  573. case "2":
  574. // 確保有 text_blocks
  575. if (!isset($data['content']['text_blocks'])) {
  576. $data['content']['text_blocks'] = [];
  577. }
  578. // 處理每個文字區塊
  579. foreach ($data['content']['text_blocks'] as $index => &$block) {
  580. // 確保區塊有必要的欄位
  581. $block['block_item_key'] = $block['block_item_key'] ?? Str::random();
  582. $block['order'] = $block['order'] ?? ($index + 1);
  583. $block["block_content"] = ["zh_TW" => $block["block_content_tw"] ?? "" , "en" => $block["block_content_en"] ?? ""];
  584. // 移除臨時的分離欄位,只保留合併的 JSON 欄位
  585. unset($block["block_content_tw"]);
  586. unset($block["block_content_en"]);
  587. // 清理區塊內容
  588. if (isset($block['block_content'])) {
  589. foreach ($block['block_content'] as $locale => $content) {
  590. $block['block_content'][$locale] = stripslashes($content);
  591. }
  592. }
  593. }
  594. break;
  595. case "3":
  596. // 確保欄數設定
  597. $columnCount = $data['content']['column_count'] ?? 2;
  598. // 確保表頭存在
  599. if (!isset($data['content']['headers'])) {
  600. $data['content']['headers'] = ['zh_TW' => [], 'en' => []];
  601. }
  602. // 確保表格資料存在
  603. if (!isset($data['content']['simple_table_rows'])) {
  604. $data['content']['simple_table_rows'] = [];
  605. }
  606. // 處理表格資料,確保欄數一致
  607. foreach ($data['content']['simple_table_rows'] as $rowIndex => &$row) {
  608. // 確保每行都有正確的欄數
  609. for ($i = 1; $i <= $columnCount; $i++) {
  610. $row["col{$i}"] = ["zh_TW" => $row["col{$i}_tw"] ?? "" , "en" => $row["col{$i}_en"] ?? ""];
  611. foreach ($row["col{$i}"] as $locale => $content) {
  612. $row["col{$i}"][$locale] = $content;
  613. }
  614. unset($row["col{$i}_tw"]);
  615. unset($row["col{$i}_en"]);
  616. $row["align{$i}"] = ["zh_TW" => $row["align{$i}_tw"] ?? "" , "en" => $row["align{$i}_en"] ?? ""];
  617. foreach ($row["align{$i}"] as $locale => $content) {
  618. $row["align{$i}"][$locale] = $content;
  619. }
  620. unset($row["align{$i}_tw"]);
  621. unset($row["align{$i}_en"]);
  622. }
  623. // 移除多餘的欄位
  624. for ($i = $columnCount + 1; $i <= 4; $i++) {
  625. unset($row["col{$i}_tw"]);
  626. unset($row["col{$i}_en"]);
  627. unset($row["align{$i}_tw"]);
  628. unset($row["align{$i}_en"]);
  629. }
  630. }
  631. break;
  632. }
  633. \Log::info($data);
  634. return $data;
  635. })
  636. ])->columnSpanFull(),
  637. ])
  638. ->columnSpanFull()
  639. ]);
  640. }
  641. public static function table(Table $table): Table
  642. {
  643. return $table
  644. ->columns([
  645. //
  646. TextColumn::make("title")->label("標題"),
  647. ])
  648. ->filters([
  649. //
  650. ])
  651. ->actions([
  652. Tables\Actions\EditAction::make(),
  653. ]);
  654. }
  655. public static function getRelations(): array
  656. {
  657. return [
  658. //
  659. ];
  660. }
  661. public static function getPages(): array
  662. {
  663. return [
  664. 'index' => Pages\ListEsg::route('/'),
  665. 'create' => Pages\CreateEsg::route('/create'),
  666. 'edit' => Pages\EditEsg::route('/{record}/edit'),
  667. ];
  668. }
  669. }