EsgResource.php 47KB

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 Filament\Forms\Components\Hidden;
  32. use Str;
  33. class EsgResource extends Resource
  34. {
  35. protected static ?string $model = Esg::class;
  36. protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
  37. protected static ?string $modelLabel = "ESG 管理";
  38. protected static ?string $navigationGroup = 'ESG 上稿內容管理';
  39. protected static ?string $navigationLabel = "ESG 管理";
  40. public static function form(Form $form): Form
  41. {
  42. $editor_toolbar= [
  43. ToolbarItem::UNDO, ToolbarItem::REDO, ToolbarItem::NORMAL,
  44. ToolbarItem::H2, ToolbarItem::H3, ToolbarItem::H4, ToolbarItem::H5,
  45. ToolbarItem::BULLET, ToolbarItem::NUMBERED, ToolbarItem::FONT_SIZE,
  46. ToolbarItem::BOLD, ToolbarItem::ITALIC, ToolbarItem::UNDERLINE,
  47. ToolbarItem::LINK, ToolbarItem::TEXT_COLOR, ToolbarItem::BACKGROUND_COLOR,
  48. ToolbarItem::SUBSCRIPT, ToolbarItem::LOWERCASE, ToolbarItem::DIVIDER,
  49. ToolbarItem::UPPERCASE, ToolbarItem::CLEAR, ToolbarItem::HR, ToolbarItem::FONT_FAMILY
  50. ];
  51. return $form
  52. ->schema([
  53. //
  54. Tabs::make("")->tabs([
  55. Tab::make('基本資訊')->schema([
  56. TextInput::make('keyword')->label("關聯字詞")->required()
  57. ->validationMessages([
  58. 'required' => '請填寫關聯字詞',
  59. ]),
  60. Translate::make()->schema(fn (string $locale) => [
  61. TextInput::make('title')
  62. ->label("標題")
  63. ->columnSpan(1)
  64. ->required($locale == "zh_TW")
  65. ->validationMessages([
  66. 'required' => '請填寫標題',
  67. ])
  68. ])->locales(["zh_TW", "en"])
  69. ->columnSpanFull()->columns(3),
  70. ]),
  71. Tab::make('Banner 設定')->schema([
  72. FileUpload::make('banner_pc')->label("Banner (PC)")
  73. ->disk("public")
  74. ->helperText('桌機版建議上傳圖片比例 4 : 1,寬1920px 高480px,檔案大小限制為1MB以下')
  75. // ->helperText('建議寬高限制為:1280*720px,檔案大小限制為1M以下')->maxSize('1024')
  76. ->directory("esg")->required()
  77. ->validationMessages([
  78. 'required' => '請上傳圖片',
  79. ]),
  80. FileUpload::make('banner_mobile')->label("Banner (Mobile)")
  81. ->disk("public")
  82. ->helperText('手機版建議上傳圖片比例 4 : 1,寬1920px 高480px,檔案大小限制為1MB以下')
  83. // ->helperText('建議寬高限制為:600*896px,檔案大小限制為1M以下')->maxSize('1024')
  84. ->directory("esg")->required()
  85. ->validationMessages([
  86. 'required' => '請上傳圖片',
  87. ]),
  88. Translate::make()->schema(fn (string $locale) => [
  89. TextInput::make('banner_alt')
  90. ->label("Banner 圖片註釋")
  91. ->required($locale == "zh_TW")
  92. ->validationMessages([
  93. 'required' => '請填寫圖片註釋',
  94. ]),
  95. Textarea::make("description")->rows(5)->columnSpanFull()->label("短文"),
  96. ])->locales(["zh_TW", "en"])
  97. ->columnSpanFull(),
  98. ]),
  99. Tab::make("段落設計")->schema([
  100. //1: 純文字 2:區塊文字 3:表格 4:影片 or 圖片 5:左右圖文
  101. Repeater::make("paragraphs")->label("")->schema([
  102. TextInput::make('item_key')
  103. ->default(fn () => Str::random())
  104. ->hidden()
  105. ->afterStateHydrated(function (TextInput $component, $state) {
  106. if (empty($state)) {
  107. $component->state(Str::random());
  108. }
  109. }),
  110. Radio::make("type")->options([
  111. 1 => "純文字",
  112. 2 => "區塊文字",
  113. 3 => "表格",
  114. 4 => "圖片",
  115. 5 => "影片",
  116. ])->label("")->default(1)->Live(),
  117. Group::make()->schema([
  118. Section::make('純文字設定')->schema([
  119. FilamentLexicalEditor::make('content.text_content_tw')
  120. ->label('中文')
  121. ->label(new \Illuminate\Support\HtmlString('中文  <a href="' . asset('images/tool-bar.png') . '", target="_blank">工具列說明</a>'))
  122. ->id(fn ($get) => "text_content_tw_" . $get('item_key') . "_" . uniqid())
  123. ->enabledToolbars($editor_toolbar)
  124. ->required()
  125. ->columnSpanFull(),
  126. FilamentLexicalEditor::make('content.text_content_en')
  127. ->label('English')
  128. ->id(fn ($get) => "text_content_en_" . $get('item_key') . "_" . uniqid())
  129. ->enabledToolbars($editor_toolbar)
  130. ->columnSpanFull(),
  131. ]),
  132. ])->visible(fn (Get $get):bool => $get("type") == 1),
  133. Group::make()->schema([
  134. Section::make('區塊文字設定')->schema([
  135. // 文字區塊重複器
  136. Repeater::make("content.text_blocks")->schema([
  137. TextInput::make('block_item_key')
  138. ->default(fn () => Str::random())
  139. ->hidden()
  140. ->afterStateHydrated(function (TextInput $component, $state) {
  141. if (empty($state)) {
  142. $component->state(Str::random());
  143. }
  144. }),
  145. FilamentLexicalEditor::make('block_content_tw')
  146. // ->label('繁體中文內容')
  147. ->label(new \Illuminate\Support\HtmlString('繁體中文內容  <a href="' . asset('images/tool-bar.png') . '", target="_blank">工具列說明</a>'))
  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. Hidden::make('content.is_card')
  172. ->default('0'),
  173. // 表格資料
  174. Placeholder::make('table_builder_info')->label("")->content('請先設定表格欄數,然後填入表格資料'),
  175. Grid::make(3)->schema([
  176. Radio::make('content.column_count')
  177. ->label('欄數')
  178. ->options([
  179. 2 => '2欄', // 改為數字鍵值
  180. 3 => '3欄',
  181. 4 => '4欄'
  182. ])
  183. ->default(2)
  184. ->inline()
  185. ->live()
  186. ->reactive(), // ✅ 添加 reactive()
  187. ]),
  188. Translate::make()->schema(fn (string $locale) => [
  189. Grid::make(2) // 改用 Grid 來支援動態 columns
  190. ->schema([
  191. TextInput::make('content.head1')->label('標題第1欄')->required()
  192. ->validationMessages([
  193. 'required' => '請填寫',
  194. ]),
  195. Radio::make('content.head_align1')
  196. ->label('對齊')
  197. ->options([
  198. 1 => '置左', // 改為數字鍵值
  199. 2 => '置中',
  200. 3 => '置右'
  201. ])
  202. ->default(1),
  203. TextInput::make('content.head2')->label('標題第2欄')->required()
  204. ->validationMessages([
  205. 'required' => '請填寫',
  206. ]),
  207. Radio::make('content.head_align2')
  208. ->label('對齊')
  209. ->options([
  210. 1 => '置左', // 改為數字鍵值
  211. 2 => '置中',
  212. 3 => '置右'
  213. ])
  214. ->default(1),
  215. TextInput::make('content.head3')->label('標題第3欄')
  216. ->visible(fn (Get $get) => intval($get('content.column_count')) >= 3)->columnSpan(1),
  217. Radio::make('content.head_align3')
  218. ->label('對齊')
  219. ->options([
  220. 1 => '置左', // 改為數字鍵值
  221. 2 => '置中',
  222. 3 => '置右'
  223. ])
  224. ->default(1)
  225. ->visible(fn (Get $get) => intval($get('content.column_count')) >= 3)->columnSpan(1),
  226. TextInput::make('content.head4')->label('標題第4欄')
  227. ->visible(fn (Get $get) => intval($get('content.column_count')) >= 4)->columnSpan(1),
  228. Radio::make('content.head_align4')
  229. ->label('對齊')
  230. ->options([
  231. 1 => '置左', // 改為數字鍵值
  232. 2 => '置中',
  233. 3 => '置右'
  234. ])
  235. ->default(1)
  236. ->visible(fn (Get $get) => intval($get('content.column_count')) >= 4)->columnSpan(1),
  237. ])
  238. ->reactive() // 加入響應式
  239. ])->locales(["zh_TW", "en"]),
  240. Repeater::make('content.simple_table_rows')
  241. ->label('表格資料')
  242. ->schema([
  243. TextInput::make('simple_table_rows_key')
  244. ->default(fn () => Str::random())
  245. ->hidden()
  246. ->afterStateHydrated(function (TextInput $component, $state) {
  247. if (empty($state)) {
  248. $component->state(Str::random());
  249. }
  250. }),
  251. Grid::make(1) // 改用 Grid 來支援動態 columns
  252. ->schema([
  253. Section::make("第一欄")->schema([
  254. Fieldset::make('中文')->schema([
  255. Radio::make('align1_tw')
  256. ->label('對齊')
  257. ->options([
  258. 1 => '置左', // 改為數字鍵值
  259. 2 => '置中',
  260. 3 => '置右'
  261. ])
  262. ->default(1)->inline(),
  263. FilamentLexicalEditor::make('col1_tw')
  264. ->id(fn ($get) => "col1_tw_" . $get('simple_table_rows_key') . "_" . uniqid())
  265. ->label(new \Illuminate\Support\HtmlString('<a href="' . asset('images/tool-bar.png') . '", target="_blank">工具列說明</a>'))
  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. ->label(new \Illuminate\Support\HtmlString('<a href="' . asset('images/tool-bar.png') . '", target="_blank">工具列說明</a>'))
  298. ->enabledToolbars($editor_toolbar)
  299. ->required()
  300. ->columnSpanFull(),
  301. ]),
  302. Fieldset::make('英文')->schema([
  303. Radio::make('align2_en')
  304. ->label('對齊')
  305. ->options([
  306. 1 => '置左', // 改為數字鍵值
  307. 2 => '置中',
  308. 3 => '置右'
  309. ])
  310. ->default(1)->inline(),
  311. FilamentLexicalEditor::make('col2_en')
  312. ->id(fn ($get) => "col2_en_" . $get('simple_table_rows_key') . "_" . uniqid())
  313. ->enabledToolbars($editor_toolbar)
  314. ->columnSpanFull(),
  315. ]),
  316. ]),
  317. Section::make("第三欄")->schema([
  318. Fieldset::make('中文')->schema([
  319. Radio::make('align3_tw')
  320. ->label('對齊')
  321. ->options([
  322. 1 => '置左', // 改為數字鍵值
  323. 2 => '置中',
  324. 3 => '置右'
  325. ])
  326. ->default(1),
  327. FilamentLexicalEditor::make('col3_tw')
  328. ->id(fn ($get) => "col3_tw_" . $get('simple_table_rows_key') . "_" . uniqid())
  329. ->label(new \Illuminate\Support\HtmlString('<a href="' . asset('images/tool-bar.png') . '", target="_blank">工具列說明</a>'))
  330. ->enabledToolbars($editor_toolbar)
  331. ->required()
  332. ->columnSpanFull(),
  333. ]),
  334. Fieldset::make('英文')->schema([
  335. Radio::make('align3_en')
  336. ->label('對齊')
  337. ->options([
  338. 1 => '置左', // 改為數字鍵值
  339. 2 => '置中',
  340. 3 => '置右'
  341. ])
  342. ->default(1)->inline(),
  343. FilamentLexicalEditor::make('col3_en')
  344. ->id(fn ($get) => "col3_en_" . $get('simple_table_rows_key') . "_" . uniqid())
  345. ->enabledToolbars($editor_toolbar)
  346. ->columnSpanFull(),
  347. ]),
  348. ])->visible(fn (Get $get) => intval($get('../../../content.column_count')) >= 3),
  349. Section::make("第四欄")->schema([
  350. Fieldset::make('中文')->schema([
  351. Radio::make('align4_tw')
  352. ->label('對齊')
  353. ->options([
  354. 1 => '置左', // 改為數字鍵值
  355. 2 => '置中',
  356. 3 => '置右'
  357. ])
  358. ->default(1)->inline(),
  359. FilamentLexicalEditor::make('col4_tw')
  360. ->id(fn ($get) => "col4_tw_" . $get('simple_table_rows_key') . "_" . uniqid())
  361. ->label(new \Illuminate\Support\HtmlString('<a href="' . asset('images/tool-bar.png') . '", target="_blank">工具列說明</a>'))
  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('檔案大小限制為1MB以下')
  407. // ->helperText('建議寬高限制為:1080*675px,檔案大小限制為1M以下')->maxSize('1024')
  408. ->directory("esg/paragraphPhoto")
  409. ->maxFiles(10)
  410. ->required()
  411. ->validationMessages([
  412. 'required' => '請上傳圖片',
  413. ]),
  414. ])
  415. ->addActionLabel('新增')
  416. ->label("")
  417. ->orderColumn('order')
  418. ])
  419. ])->visible(fn (Get $get):bool => $get("type") == 4),
  420. Group::make()->schema([
  421. Section::make("")->schema([
  422. FileUpload::make('content.video_img')->label("影片底圖")
  423. ->disk("public")
  424. ->helperText('檔案大小限制為1MB以下')
  425. ->directory("esg/paragraphVideo"),
  426. Translate::make()->schema(fn (string $locale) => [
  427. TextInput::make('content.link')->label("網址")->required()
  428. ->validationMessages([
  429. 'required' => '請附上連結',
  430. ])
  431. ])->locales(["zh_TW", "en"]),
  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. }