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