123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440 |
- <?php
- // app/Traits/HandlesParagraphs.php
-
- namespace App\Traits;
-
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Support\Str;
-
- trait HandlesEsgParagraphs
- {
- /**
- * 格式化 paragraphs 資料以符合表單結構(載入時使用)
- */
- protected function formatParagraphsForForm(array $paragraphs): array
- {
- $formattedParagraphs = [];
-
- foreach ($paragraphs as $paragraph) {
- \Log::info("格式化段落:", $paragraph);
-
- $formatted = [
- 'id' => $paragraph['id'],
- 'type' => $paragraph['type'],
- 'order' => $paragraph['order'],
- 'content' => $paragraph['content'] ?? [],
- ];
-
- // 根據類型進行特殊格式化
- switch ($paragraph['type']) {
- case 1: // 純文字
- $formatted = $this->formatTextParagraphForForm($formatted);
- break;
-
- case 2: // 區塊文字
- $formatted = $this->formatBlockParagraphForForm($formatted);
- break;
-
- case 3: // 表格
- $formatted = $this->formatTableParagraphForForm($formatted);
- break;
-
- case 4: // 圖片
- $formatted = $this->formatImageParagraphForForm($formatted);
- break;
- }
-
- // 確保有 item_key
- if (!isset($formatted['item_key'])) {
- $formatted['item_key'] = Str::random();
- }
-
- $formattedParagraphs[] = $formatted;
-
- \Log::info("格式化完成:", $formatted);
- }
-
- return $formattedParagraphs;
- }
-
- /**
- * 格式化純文字段落(載入時)
- */
- protected function formatTextParagraphForForm(array $paragraph): array
- {
- // 確保 text_content 結構正確
- if (!isset($paragraph['content']['text_content'])) {
- $paragraph['content']['text_content'] = ['zh_TW' => '', 'en' => ''];
- }
-
- return $paragraph;
- }
-
- /**
- * 格式化區塊文字段落(載入時)
- */
- protected function formatBlockParagraphForForm(array $paragraph): array
- {
- // 確保 text_blocks 結構正確
- if (!isset($paragraph['content']['text_blocks'])) {
- $paragraph['content']['text_blocks'] = [];
- }
-
- // 為每個 text_block 添加必要的欄位
- foreach ($paragraph['content']['text_blocks'] as $index => &$block) {
- if (!isset($block['block_item_key'])) {
- $block['block_item_key'] = Str::random();
- }
- if (!isset($block['order'])) {
- $block['order'] = $index + 1;
- }
- }
-
- return $paragraph;
- }
-
- /**
- * 格式化表格段落(載入時)
- */
- protected function formatTableParagraphForForm(array $paragraph): array
- {
- $content = $paragraph['content'];
-
- if (!isset($content['column_count'])) {
- $content['column_count'] = 2;
- }
-
- if (!isset($content['table_title'])) {
- $content['table_title'] = ['zh_TW' => '', 'en' => ''];
- }
-
- if (!isset($content['table_description'])) {
- $content['table_description'] = ['zh_TW' => '', 'en' => ''];
- }
-
- if (!isset($content['simple_table_rows'])) {
- $content['simple_table_rows'] = [];
- }
-
- // 確保表頭資料結構
- $columnCount = $content['column_count'];
- for ($i = 1; $i <= $columnCount; $i++) {
- if (!isset($content["head{$i}"])) {
- $content["head{$i}"] = ['zh_TW' => '', 'en' => ''];
- }
- }
-
- $paragraph['content'] = $content;
-
- return $paragraph;
- }
-
- /**
- * 格式化圖片段落(載入時)
- */
- protected function formatImageParagraphForForm(array $paragraph): array
- {
- // 確保 multiple_images 結構正確
- if (!isset($paragraph['content']['multiple_images'])) {
- $paragraph['content']['multiple_images'] = [];
- }
-
- // 為每個圖片添加必要的欄位
- foreach ($paragraph['content']['multiple_images'] as $index => &$image) {
- if (!isset($image['para_img_item_key'])) {
- $image['para_img_item_key'] = Str::random();
- }
- if (!isset($image['order'])) {
- $image['order'] = $index + 1;
- }
- }
-
- return $paragraph;
- }
-
- /**
- * 預處理 paragraphs 資料(儲存前使用)
- */
- protected function preprocessParagraphs(array $paragraphs): array
- {
- \Log::info("=== 開始預處理 Paragraphs ===");
-
- foreach ($paragraphs as $index => &$paragraph) {
- \Log::info("處理段落 {$index}:", $paragraph);
-
- // 確保基本欄位存在
- $paragraph['content'] = $paragraph['content'] ?? [];
- $paragraph['order'] = $paragraph['order'] ?? ($index + 1);
-
- // 根據類型進行特殊處理
- switch ($paragraph['type']) {
- case 1: // 純文字
- $paragraph = $this->preprocessTextParagraph($paragraph);
- break;
-
- case 2: // 區塊文字
- $paragraph = $this->preprocessBlockParagraph($paragraph);
- break;
-
- case 3: // 表格
- $paragraph = $this->preprocessTableParagraph($paragraph);
- break;
-
- case 4: // 圖片
- $paragraph = $this->preprocessImageParagraph($paragraph);
- break;
- }
-
- // 添加處理時間戳
- $paragraph['content']['processed_at'] = now()->toISOString();
-
- \Log::info("段落 {$index} 處理完成:", $paragraph);
- }
-
- return $paragraphs;
- }
-
- /**
- * 處理純文字段落(儲存前)
- */
- protected function preprocessTextParagraph(array $paragraph): array
- {
- \Log::info("處理純文字段落");
-
- // 確保文字內容結構正確
- if (!isset($paragraph['content']['text_content'])) {
- $paragraph['content']['text_content'] = ['zh_TW' => '', 'en' => ''];
- }
-
- // 清理 HTML 標籤(如果需要)
- foreach ($paragraph['content']['text_content'] as $locale => $content) {
- $paragraph['content']['text_content'][$locale] = $this->cleanHtml($content);
- }
-
- return $paragraph;
- }
-
- /**
- * 處理區塊文字段落(儲存前)
- */
- protected function preprocessBlockParagraph(array $paragraph): array
- {
- \Log::info("處理區塊文字段落");
-
- // 確保有 text_blocks
- if (!isset($paragraph['content']['text_blocks'])) {
- $paragraph['content']['text_blocks'] = [];
- }
-
- // 處理每個文字區塊
- foreach ($paragraph['content']['text_blocks'] as $index => &$block) {
- // 確保區塊有必要的欄位
- $block['block_item_key'] = $block['block_item_key'] ?? Str::random();
- $block['order'] = $block['order'] ?? ($index + 1);
-
- // 清理區塊內容
- if (isset($block['block_content'])) {
- foreach ($block['block_content'] as $locale => $content) {
- $block['block_content'][$locale] = $this->cleanHtml($content);
- }
- }
- }
-
- return $paragraph;
- }
-
- /**
- * 處理表格段落(儲存前)
- */
- protected function preprocessTableParagraph(array $paragraph): array
- {
- \Log::info("處理表格段落");
-
- // 確保欄數設定
- $columnCount = $paragraph['content']['column_count'] ?? 2;
-
- // 確保表頭存在
- if (!isset($paragraph['content']['headers'])) {
- $paragraph['content']['headers'] = ['zh_TW' => [], 'en' => []];
- }
-
- // 確保表格資料存在
- if (!isset($paragraph['content']['simple_table_rows'])) {
- $paragraph['content']['simple_table_rows'] = [];
- }
-
- // 處理表格資料,確保欄數一致
- foreach ($paragraph['content']['simple_table_rows'] as $rowIndex => &$row) {
- foreach (['zh_TW', 'en'] as $locale) {
- if (!isset($row[$locale])) {
- $row[$locale] = [];
- }
-
- // 確保每行都有正確的欄數
- for ($i = 1; $i <= $columnCount; $i++) {
- if (!isset($row[$locale]["col{$i}"])) {
- $row[$locale]["col{$i}"] = '';
- }
- }
-
- // 移除多餘的欄位
- for ($i = $columnCount + 1; $i <= 4; $i++) {
- unset($row[$locale]["col{$i}"]);
- }
- }
- }
-
- return $paragraph;
- }
-
- /**
- * 處理圖片段落(儲存前)
- */
- protected function preprocessImageParagraph(array $paragraph): array
- {
- \Log::info("處理圖片段落");
-
- // 確保圖片陣列存在
- if (!isset($paragraph['content']['multiple_images'])) {
- $paragraph['content']['multiple_images'] = [];
- }
-
- // 處理每個圖片
- foreach ($paragraph['content']['multiple_images'] as $index => &$image) {
- $image['para_img_item_key'] = $image['para_img_item_key'] ?? Str::random();
- $image['order'] = $image['order'] ?? ($index + 1);
-
- // 驗證圖片檔案是否存在
- if (isset($image['image_url'])) {
- // 這裡可以添加圖片驗證邏輯
- }
- }
-
- return $paragraph;
- }
-
- /**
- * 更新 paragraphs 關聯(編輯時使用)
- */
- protected function updateParagraphs(Model $record, array $paragraphs): void
- {
- \Log::info("=== 開始更新 Paragraphs 關聯 ===");
-
- // 獲取現有的段落 IDs
- $existingIds = collect($paragraphs)
- ->pluck('id')
- ->filter()
- ->values()
- ->toArray();
-
- // 刪除不在列表中的段落
- $record->paragraphs()
- ->whereNotIn('id', $existingIds)
- ->delete();
-
- \Log::info("已刪除不存在的段落");
-
- // 更新或建立段落
- foreach ($paragraphs as $paragraphData) {
- if (isset($paragraphData['id'])) {
- // 更新現有段落
- $paragraph = $record->paragraphs()->find($paragraphData['id']);
- if ($paragraph) {
- $paragraph->update([
- 'type' => $paragraphData['type'],
- 'order' => $paragraphData['order'],
- 'content' => $paragraphData['content'],
- ]);
- \Log::info("更新段落 ID: {$paragraphData['id']}");
- }
- } else {
- // 建立新段落
- $newParagraph = $record->paragraphs()->create([
- 'type' => $paragraphData['type'],
- 'order' => $paragraphData['order'],
- 'content' => $paragraphData['content'],
- ]);
- \Log::info("建立新段落 ID: {$newParagraph->id}");
- }
- }
-
- \Log::info("=== Paragraphs 關聯更新完成 ===");
- }
-
- /**
- * 建立 paragraphs 關聯(新增時使用)
- */
- protected function createParagraphs(Model $record, array $paragraphs): void
- {
- \Log::info("=== 開始建立 Paragraphs 關聯 ===");
-
- foreach ($paragraphs as $paragraphData) {
- $newParagraph = $record->paragraphs()->create([
- 'type' => $paragraphData['type'],
- 'order' => $paragraphData['order'],
- 'content' => $paragraphData['content'],
- ]);
- \Log::info("建立新段落 ID: {$newParagraph->id}");
- }
-
- \Log::info("=== Paragraphs 關聯建立完成 ===");
- }
-
- /**
- * 清理 HTML 內容
- */
- protected function cleanHtml(string $html): string
- {
- // 這裡可以根據需求自定義清理邏輯
- // 例如:移除危險標籤、清理空白等
- return trim($html);
- }
-
- /**
- * 後處理 paragraphs(儲存後使用)
- */
- protected function postProcessParagraphs($paragraphs): void
- {
- foreach ($paragraphs as $paragraph) {
- \Log::info("後處理段落:", [
- 'id' => $paragraph->id,
- 'type' => $paragraph->type,
- 'content_keys' => array_keys($paragraph->content ?? [])
- ]);
-
- // 這裡可以進行:
- // - 快取更新
- // - 搜尋索引更新
- // - 通知發送
- // 等後續處理
- }
- }
-
- /**
- * 獲取並格式化關聯資料(載入時的輔助方法)
- */
- protected function loadAndFormatParagraphs(Model $record): array
- {
- $paragraphs = $record->paragraphs()
- ->orderBy('order')
- ->get()
- ->toArray();
-
- return $this->formatParagraphsForForm($paragraphs);
- }
-
- /**
- * 處理表單狀態中的 paragraphs(儲存時的輔助方法)
- */
- protected function processFormParagraphs(): ?array
- {
- $formState = $this->form->getState();
-
- if (!isset($formState['paragraphs'])) {
- return null;
- }
-
- return $this->preprocessParagraphs($formState['paragraphs']);
- }
- }
|