|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of MetaModels/core. |
| 5 | + * |
| 6 | + * (c) 2012-2026 The MetaModels team. |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + * |
| 11 | + * This project is provided in good faith and hope to be usable by anyone. |
| 12 | + * |
| 13 | + * @package MetaModels/core |
| 14 | + * @author Ingolf Steinhardt <info@e-spin.de> |
| 15 | + * @copyright 2012-2026 The MetaModels team. |
| 16 | + * @license https://github.com/MetaModels/core/blob/master/LICENSE LGPL-3.0-or-later |
| 17 | + * @filesource |
| 18 | + */ |
| 19 | + |
| 20 | +namespace MetaModels\DcGeneral\Events\MetaModel; |
| 21 | + |
| 22 | +use ContaoCommunityAlliance\DcGeneral\Event\PostDuplicateModelEvent; |
| 23 | +use MetaModels\Attribute\ITranslatedWithFallbackControl; |
| 24 | +use MetaModels\IFactory; |
| 25 | +use MetaModels\ITranslatedMetaModel; |
| 26 | + |
| 27 | +/** |
| 28 | + * Copies translated attribute data for all languages when an item is duplicated. |
| 29 | + * |
| 30 | + * The normal DC_General duplicate path only saves data for the currently active language. This listener runs after |
| 31 | + * the copy has been persisted and iterates every language, copying each translated attribute's raw (non-fallback) data |
| 32 | + * from the source item to the new item. |
| 33 | + */ |
| 34 | +final class CopyTranslatedData |
| 35 | +{ |
| 36 | + /** |
| 37 | + * The MetaModels factory. |
| 38 | + * |
| 39 | + * @var IFactory |
| 40 | + */ |
| 41 | + private IFactory $factory; |
| 42 | + |
| 43 | + /** |
| 44 | + * Create a new instance. |
| 45 | + * |
| 46 | + * @param IFactory $factory The factory. |
| 47 | + */ |
| 48 | + public function __construct(IFactory $factory) |
| 49 | + { |
| 50 | + $this->factory = $factory; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Copy translated data for all languages after a model has been duplicated. |
| 55 | + * |
| 56 | + * @param PostDuplicateModelEvent $event The event. |
| 57 | + * |
| 58 | + * @return void |
| 59 | + */ |
| 60 | + public function handle(PostDuplicateModelEvent $event): void |
| 61 | + { |
| 62 | + $sourceId = (string) $event->getSourceModel()->getId(); |
| 63 | + $newId = (string) $event->getModel()->getId(); |
| 64 | + |
| 65 | + if ('' === $sourceId || '' === $newId || $sourceId === $newId) { |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + $metaModel = $this->factory->getMetaModel($event->getModel()->getProviderName()); |
| 70 | + if (!$metaModel instanceof ITranslatedMetaModel) { |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + foreach ($metaModel->getLanguages() as $language) { |
| 75 | + $this->copyLanguage($metaModel, $language, $sourceId, $newId); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Copy all translated attributes for a single language. |
| 81 | + * |
| 82 | + * @param ITranslatedMetaModel $metaModel The MetaModel. |
| 83 | + * @param string $language The language code. |
| 84 | + * @param string $sourceId The source item ID. |
| 85 | + * @param string $newId The new item ID. |
| 86 | + * |
| 87 | + * @return void |
| 88 | + */ |
| 89 | + private function copyLanguage( |
| 90 | + ITranslatedMetaModel $metaModel, |
| 91 | + string $language, |
| 92 | + string $sourceId, |
| 93 | + string $newId |
| 94 | + ): void { |
| 95 | + foreach ($metaModel->getAttributes() as $attribute) { |
| 96 | + if (!$attribute instanceof ITranslatedWithFallbackControl) { |
| 97 | + continue; |
| 98 | + } |
| 99 | + |
| 100 | + $data = $attribute->getTranslatedDataForWithoutFallback([$sourceId], $language); |
| 101 | + if ([] === $data || !isset($data[$sourceId])) { |
| 102 | + continue; |
| 103 | + } |
| 104 | + |
| 105 | + $attribute->copyTranslatedDataFor($data[$sourceId], $newId, $language); |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments