EsgResource.php 45KB

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