EsgResource.php 46KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  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('檔案大小限制為1MB以下')
  74. // ->helperText('建議寬高限制為:1280*720px,檔案大小限制為1M以下')->maxSize('1024')
  75. ->directory("esg")->required()
  76. ->validationMessages([
  77. 'required' => '請上傳圖片',
  78. ]),
  79. FileUpload::make('banner_mobile')->label("Banner (Mobile)")
  80. ->disk("public")
  81. ->helperText('檔案大小限制為1MB以下')
  82. // ->helperText('建議寬高限制為:600*896px,檔案大小限制為1M以下')->maxSize('1024')
  83. ->directory("esg")->required()
  84. ->validationMessages([
  85. 'required' => '請上傳圖片',
  86. ]),
  87. Translate::make()->schema(fn (string $locale) => [
  88. TextInput::make('banner_alt')
  89. ->label("Banner 圖片註釋")
  90. ->required($locale == "zh_TW")
  91. ->validationMessages([
  92. 'required' => '請填寫圖片註釋',
  93. ]),
  94. Textarea::make("description")->rows(5)->columnSpanFull()->label("短文"),
  95. ])->locales(["zh_TW", "en"])
  96. ->columnSpanFull(),
  97. ]),
  98. Tab::make("段落設計")->schema([
  99. //1: 純文字 2:區塊文字 3:表格 4:影片 or 圖片 5:左右圖文
  100. Repeater::make("paragraphs")->label("")->schema([
  101. TextInput::make('item_key')
  102. ->default(fn () => Str::random())
  103. ->hidden()
  104. ->afterStateHydrated(function (TextInput $component, $state) {
  105. if (empty($state)) {
  106. $component->state(Str::random());
  107. }
  108. }),
  109. Radio::make("type")->options([
  110. 1 => "純文字",
  111. 2 => "區塊文字",
  112. 3 => "表格",
  113. 4 => "圖片",
  114. 5 => "影片",
  115. ])->label("")->default(1)->Live(),
  116. Group::make()->schema([
  117. Section::make('純文字設定')->schema([
  118. FilamentLexicalEditor::make('content.text_content_tw')
  119. ->label('中文')
  120. ->label(new \Illuminate\Support\HtmlString('
  121. 中文<br> <img src="' . asset('images/your-image.png') . '" alt="label image" class="h-6 w-auto" />
  122. '))
  123. ->id(fn ($get) => "text_content_tw_" . $get('item_key') . "_" . uniqid())
  124. ->enabledToolbars($editor_toolbar)
  125. ->required()
  126. ->columnSpanFull(),
  127. FilamentLexicalEditor::make('content.text_content_en')
  128. ->label('English')
  129. ->id(fn ($get) => "text_content_en_" . $get('item_key') . "_" . uniqid())
  130. ->enabledToolbars($editor_toolbar)
  131. ->columnSpanFull(),
  132. ]),
  133. ])->visible(fn (Get $get):bool => $get("type") == 1),
  134. Group::make()->schema([
  135. Section::make('區塊文字設定')->schema([
  136. // 文字區塊重複器
  137. Repeater::make("content.text_blocks")->schema([
  138. TextInput::make('block_item_key')
  139. ->default(fn () => Str::random())
  140. ->hidden()
  141. ->afterStateHydrated(function (TextInput $component, $state) {
  142. if (empty($state)) {
  143. $component->state(Str::random());
  144. }
  145. }),
  146. FilamentLexicalEditor::make('block_content_tw')
  147. ->label('繁體中文內容')
  148. ->id(fn ($get) => "block_content_tw_" . $get('block_item_key') . "_" . uniqid())
  149. ->enabledToolbars($editor_toolbar)
  150. ->required()
  151. ->live(onBlur: true)
  152. ->columnSpanFull(),
  153. FilamentLexicalEditor::make('block_content_en')
  154. ->label('English Content')
  155. ->id(fn ($get) => "block_content_en_" . $get('block_item_key') . "_" . uniqid())
  156. ->enabledToolbars($editor_toolbar)
  157. ->columnSpanFull(),
  158. ])
  159. ->label("")
  160. ->addActionLabel('增加區塊')
  161. ->orderColumn('order')
  162. ->reorderableWithButtons()
  163. ->cloneable()
  164. ->minItems(1)
  165. ->maxItems(20)
  166. ->defaultItems(1),
  167. ]),
  168. ])->visible(fn (Get $get):bool => $get('type') == 2),
  169. Group::make()->schema([
  170. Section::make('表格設定')->schema([
  171. Radio::make('content.is_card')
  172. ->label('手機板卡片顯示')
  173. ->options([
  174. '1' => '是', // 改為數字鍵值
  175. '0' => '否'
  176. ])
  177. ->default('0')
  178. ->inline(),
  179. // 表格資料
  180. Placeholder::make('table_builder_info')->label("")->content('請先設定表格欄數,然後填入表格資料'),
  181. Grid::make(3)->schema([
  182. Radio::make('content.column_count')
  183. ->label('欄數')
  184. ->options([
  185. 2 => '2欄', // 改為數字鍵值
  186. 3 => '3欄',
  187. 4 => '4欄'
  188. ])
  189. ->default(2)
  190. ->inline()
  191. ->live()
  192. ->reactive(), // ✅ 添加 reactive()
  193. ]),
  194. Translate::make()->schema(fn (string $locale) => [
  195. Grid::make(2) // 改用 Grid 來支援動態 columns
  196. ->schema([
  197. TextInput::make('content.head1')->label('標題第1欄')->required()
  198. ->validationMessages([
  199. 'required' => '請填寫',
  200. ]),
  201. Radio::make('content.head_align1')
  202. ->label('對齊')
  203. ->options([
  204. 1 => '置左', // 改為數字鍵值
  205. 2 => '置中',
  206. 3 => '置右'
  207. ])
  208. ->default(1),
  209. TextInput::make('content.head2')->label('標題第2欄')->required()
  210. ->validationMessages([
  211. 'required' => '請填寫',
  212. ]),
  213. Radio::make('content.head_align2')
  214. ->label('對齊')
  215. ->options([
  216. 1 => '置左', // 改為數字鍵值
  217. 2 => '置中',
  218. 3 => '置右'
  219. ])
  220. ->default(1),
  221. TextInput::make('content.head3')->label('標題第3欄')
  222. ->visible(fn (Get $get) => intval($get('content.column_count')) >= 3)->columnSpan(1),
  223. Radio::make('content.head_align3')
  224. ->label('對齊')
  225. ->options([
  226. 1 => '置左', // 改為數字鍵值
  227. 2 => '置中',
  228. 3 => '置右'
  229. ])
  230. ->default(1)
  231. ->visible(fn (Get $get) => intval($get('content.column_count')) >= 3)->columnSpan(1),
  232. TextInput::make('content.head4')->label('標題第4欄')
  233. ->visible(fn (Get $get) => intval($get('content.column_count')) >= 4)->columnSpan(1),
  234. Radio::make('content.head_align4')
  235. ->label('對齊')
  236. ->options([
  237. 1 => '置左', // 改為數字鍵值
  238. 2 => '置中',
  239. 3 => '置右'
  240. ])
  241. ->default(1)
  242. ->visible(fn (Get $get) => intval($get('content.column_count')) >= 4)->columnSpan(1),
  243. ])
  244. ->reactive() // 加入響應式
  245. ])->locales(["zh_TW", "en"]),
  246. Repeater::make('content.simple_table_rows')
  247. ->label('表格資料')
  248. ->schema([
  249. TextInput::make('simple_table_rows_key')
  250. ->default(fn () => Str::random())
  251. ->hidden()
  252. ->afterStateHydrated(function (TextInput $component, $state) {
  253. if (empty($state)) {
  254. $component->state(Str::random());
  255. }
  256. }),
  257. Grid::make(1) // 改用 Grid 來支援動態 columns
  258. ->schema([
  259. Section::make("第一欄")->schema([
  260. Fieldset::make('中文')->schema([
  261. Radio::make('align1_tw')
  262. ->label('對齊')
  263. ->options([
  264. 1 => '置左', // 改為數字鍵值
  265. 2 => '置中',
  266. 3 => '置右'
  267. ])
  268. ->default(1)->inline(),
  269. FilamentLexicalEditor::make('col1_tw')
  270. ->id(fn ($get) => "col1_tw_" . $get('simple_table_rows_key') . "_" . uniqid())
  271. ->enabledToolbars($editor_toolbar)
  272. ->required()
  273. ->columnSpanFull(),
  274. ]),
  275. Fieldset::make('英文')->schema([
  276. Radio::make('align1_en')
  277. ->label('對齊')
  278. ->options([
  279. 1 => '置左', // 改為數字鍵值
  280. 2 => '置中',
  281. 3 => '置右'
  282. ])
  283. ->default(1)->inline(),
  284. FilamentLexicalEditor::make('col1_en')
  285. ->id(fn ($get) => "col1_en_" . $get('simple_table_rows_key') . "_" . uniqid())
  286. ->enabledToolbars($editor_toolbar)
  287. ->columnSpanFull(),
  288. ]),
  289. ]),
  290. Section::make("第二欄")->schema([
  291. Fieldset::make('中文')->schema([
  292. Radio::make('align2_tw')
  293. ->label('對齊')
  294. ->options([
  295. 1 => '置左', // 改為數字鍵值
  296. 2 => '置中',
  297. 3 => '置右'
  298. ])
  299. ->default(1)->inline(),
  300. FilamentLexicalEditor::make('col2_tw')
  301. ->id(fn ($get) => "col2_tw_" . $get('simple_table_rows_key') . "_" . uniqid())
  302. ->enabledToolbars($editor_toolbar)
  303. ->required()
  304. ->columnSpanFull(),
  305. ]),
  306. Fieldset::make('英文')->schema([
  307. Radio::make('align2_en')
  308. ->label('對齊')
  309. ->options([
  310. 1 => '置左', // 改為數字鍵值
  311. 2 => '置中',
  312. 3 => '置右'
  313. ])
  314. ->default(1)->inline(),
  315. FilamentLexicalEditor::make('col2_en')
  316. ->id(fn ($get) => "col2_en_" . $get('simple_table_rows_key') . "_" . uniqid())
  317. ->enabledToolbars($editor_toolbar)
  318. ->columnSpanFull(),
  319. ]),
  320. ]),
  321. Section::make("第三欄")->schema([
  322. Fieldset::make('中文')->schema([
  323. Radio::make('align3_tw')
  324. ->label('對齊')
  325. ->options([
  326. 1 => '置左', // 改為數字鍵值
  327. 2 => '置中',
  328. 3 => '置右'
  329. ])
  330. ->default(1),
  331. FilamentLexicalEditor::make('col3_tw')
  332. ->id(fn ($get) => "col3_tw_" . $get('simple_table_rows_key') . "_" . uniqid())
  333. ->enabledToolbars($editor_toolbar)
  334. ->required()
  335. ->columnSpanFull(),
  336. ]),
  337. Fieldset::make('英文')->schema([
  338. Radio::make('align3_en')
  339. ->label('對齊')
  340. ->options([
  341. 1 => '置左', // 改為數字鍵值
  342. 2 => '置中',
  343. 3 => '置右'
  344. ])
  345. ->default(1)->inline(),
  346. FilamentLexicalEditor::make('col3_en')
  347. ->id(fn ($get) => "col3_en_" . $get('simple_table_rows_key') . "_" . uniqid())
  348. ->enabledToolbars($editor_toolbar)
  349. ->columnSpanFull(),
  350. ]),
  351. ])->visible(fn (Get $get) => intval($get('../../../content.column_count')) >= 3),
  352. Section::make("第四欄")->schema([
  353. Fieldset::make('中文')->schema([
  354. Radio::make('align4_tw')
  355. ->label('對齊')
  356. ->options([
  357. 1 => '置左', // 改為數字鍵值
  358. 2 => '置中',
  359. 3 => '置右'
  360. ])
  361. ->default(1)->inline(),
  362. FilamentLexicalEditor::make('col4_tw')
  363. ->id(fn ($get) => "col4_tw_" . $get('simple_table_rows_key') . "_" . uniqid())
  364. ->enabledToolbars($editor_toolbar)
  365. ->required()
  366. ->columnSpanFull(),
  367. ]),
  368. Fieldset::make('英文')->schema([
  369. Radio::make('align4_en')
  370. ->label('對齊')
  371. ->options([
  372. 1 => '置左', // 改為數字鍵值
  373. 2 => '置中',
  374. 3 => '置右'
  375. ])
  376. ->default(1)->inline(),
  377. FilamentLexicalEditor::make('col4_en')
  378. ->id(fn ($get) => "col4_en_" . $get('simple_table_rows_key') . "_" . uniqid())
  379. ->enabledToolbars($editor_toolbar)
  380. ->columnSpanFull(),
  381. ]),
  382. ])->visible(fn (Get $get) => intval($get('../../../content.column_count')) >= 4),
  383. ])
  384. ->reactive() // 加入響應式
  385. ])
  386. ->id(fn ($get) => "simple_table_" . $get('simple_table_rows_key'))
  387. ->addActionLabel('新增列')
  388. ->reorderableWithButtons()
  389. ->minItems(1)
  390. ]),
  391. ])->visible(fn (Get $get):bool => $get('type') == 3),
  392. Group::make()->schema([
  393. Section::make('')->schema([
  394. Repeater::make("content.multiple_images")->schema([
  395. TextInput::make('para_img_item_key')
  396. ->default(fn () => Str::random())
  397. ->hidden()
  398. ->afterStateHydrated(function (TextInput $component, $state) {
  399. if (empty($state)) {
  400. $component->state(Str::random());
  401. }
  402. }),
  403. Translate::make()->schema(fn (string $locale) => [
  404. TextInput::make('image_alt')->label("圖片註文")
  405. ])->locales(["zh_TW", "en"])
  406. ->id(fn ($get) => "para_img_mul_" . $get('para_img_item_key')),
  407. FileUpload::make('image_url')->label("")->disk("public")
  408. ->helperText('檔案大小限制為1MB以下')
  409. // ->helperText('建議寬高限制為:1080*675px,檔案大小限制為1M以下')->maxSize('1024')
  410. ->directory("esg/paragraphPhoto")
  411. ->maxFiles(10)
  412. ->required()
  413. ->validationMessages([
  414. 'required' => '請上傳圖片',
  415. ]),
  416. ])
  417. ->addActionLabel('新增')
  418. ->label("")
  419. ->orderColumn('order')
  420. ])
  421. ])->visible(fn (Get $get):bool => $get("type") == 4),
  422. Group::make()->schema([
  423. Section::make("")->schema([
  424. FileUpload::make('content.video_img')->label("影片底圖")
  425. ->disk("public")
  426. ->helperText('檔案大小限制為1MB以下')
  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. }