diff --git a/contao/templates/be_dummy_copier.html5 b/contao/templates/be_dummy_copier.html5 index 622685e..2a4e96a 100644 --- a/contao/templates/be_dummy_copier.html5 +++ b/contao/templates/be_dummy_copier.html5 @@ -164,6 +164,8 @@ 'moduleMap' => $this->result->moduleMap, 'newsArchiveMap' => $this->result->newsArchiveMap, 'calendarMap' => $this->result->calendarMap, + 'newsItemMap' => $this->result->newsItemMap, + 'eventMap' => $this->result->eventMap, 'notes' => $this->result->notes, ], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES), ENT_QUOTES, 'UTF-8'); ?> diff --git a/src/Service/DummyCopier.php b/src/Service/DummyCopier.php index 16e2b8b..d134a2f 100644 --- a/src/Service/DummyCopier.php +++ b/src/Service/DummyCopier.php @@ -327,11 +327,71 @@ final class DummyCopier $updates['cal_calendar'] = $this->remapSerializedIds((string) ($row['cal_calendar'] ?? ''), $result->calendarMap); } + if (array_key_exists('news_readerModule', $row)) { + $oldReaderModuleId = (int) ($row['news_readerModule'] ?? 0); + + if ($oldReaderModuleId > 0 && isset($result->moduleMap[$oldReaderModuleId])) { + $updates['news_readerModule'] = $result->moduleMap[$oldReaderModuleId]; + } + } + + if (array_key_exists('cal_readerModule', $row)) { + $oldReaderModuleId = (int) ($row['cal_readerModule'] ?? 0); + + if ($oldReaderModuleId > 0 && isset($result->moduleMap[$oldReaderModuleId])) { + $updates['cal_readerModule'] = $result->moduleMap[$oldReaderModuleId]; + } + } + if ($updates !== []) { $updates['tstamp'] = time(); $this->connection->update('tl_module', $updates, ['id' => $newModuleId]); } } + + $this->rewriteNewsItemReferences($result); + } + + private function rewriteNewsItemReferences(DummyCopyResult $result): void + { + if ($result->newsItemMap === []) { + return; + } + + foreach ($result->newsItemMap as $oldNewsId => $newNewsId) { + $sourceRow = $this->fetchRow('tl_news', $oldNewsId); + + if ($sourceRow === null || !array_key_exists('related', $sourceRow)) { + continue; + } + + $related = StringUtil::deserialize((string) ($sourceRow['related'] ?? ''), true); + + if ($related === []) { + continue; + } + + $mappedRelated = []; + + foreach ($related as $relatedIdValue) { + $relatedId = (int) $relatedIdValue; + + if ($relatedId < 1) { + continue; + } + + $mappedRelated[] = (string) ($result->newsItemMap[$relatedId] ?? $relatedId); + } + + $this->connection->update( + 'tl_news', + [ + 'related' => StringUtil::serialize($mappedRelated), + 'tstamp' => time(), + ], + ['id' => $newNewsId] + ); + } } /** @@ -391,7 +451,8 @@ final class DummyCopier $newsRow['jumpTo'] = $result->pageMap[(int) $newsRow['jumpTo']]; } - $this->insertRow('tl_news', $newsRow); + $newNewsId = $this->insertRow('tl_news', $newsRow); + $result->newsItemMap[(int) $newsId] = $newNewsId; $result->copiedNewsItems++; } } @@ -456,7 +517,8 @@ final class DummyCopier $eventRow['jumpTo'] = $result->pageMap[(int) $eventRow['jumpTo']]; } - $this->insertRow('tl_calendar_events', $eventRow); + $newEventId = $this->insertRow('tl_calendar_events', $eventRow); + $result->eventMap[(int) $eventId] = $newEventId; $result->copiedEvents++; } } diff --git a/src/Service/DummyCopyResult.php b/src/Service/DummyCopyResult.php index 8e66c06..f645851 100644 --- a/src/Service/DummyCopyResult.php +++ b/src/Service/DummyCopyResult.php @@ -30,6 +30,12 @@ final class DummyCopyResult /** @var array */ public array $calendarMap = []; + /** @var array */ + public array $newsItemMap = []; + + /** @var array */ + public array $eventMap = []; + /** @var array */ public array $notes = [];