|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +/** |
| 5 | + * Pimcore |
| 6 | + * |
| 7 | + * This source file is available under two different licenses: |
| 8 | + * - GNU General Public License version 3 (GPLv3) |
| 9 | + * - Pimcore Commercial License (PCL) |
| 10 | + * Full copyright and license information is available in |
| 11 | + * LICENSE.md which is distributed with this source code. |
| 12 | + * |
| 13 | + * @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) |
| 14 | + * @license http://www.pimcore.org/license GPLv3 and PCL |
| 15 | + */ |
| 16 | + |
| 17 | +namespace Pimcore\Bundle\StudioBackendBundle\Translation\Repository; |
| 18 | + |
| 19 | +use Pimcore\Bundle\StaticResolverBundle\Lib\Tools\AdminResolverInterface; |
| 20 | +use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidLocaleException; |
| 21 | +use Pimcore\Bundle\StudioBackendBundle\Exception\Api\NotFoundException; |
| 22 | +use Pimcore\Bundle\StudioBackendBundle\Translation\Schema\TranslationData; |
| 23 | +use Pimcore\Bundle\StudioBackendBundle\Translation\Service\TranslatorServiceInterface; |
| 24 | +use Pimcore\Model\Translation; |
| 25 | +use Pimcore\Model\Translation\Listing; |
| 26 | +use function in_array; |
| 27 | + |
| 28 | +/** |
| 29 | + * @internal |
| 30 | + */ |
| 31 | +final readonly class TranslationRepository implements TranslationRepositoryInterface |
| 32 | +{ |
| 33 | + public function __construct( |
| 34 | + private AdminResolverInterface $adminResolver, |
| 35 | + ) { |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * {@inheritdoc} |
| 40 | + */ |
| 41 | + public function getAllTranslations(string $locale): array |
| 42 | + { |
| 43 | + $validLanguages = $this->adminResolver->getLanguages(); |
| 44 | + $this->validateLocale($locale, $validLanguages); |
| 45 | + |
| 46 | + $list = $this->getTranslationList(); |
| 47 | + $list->setLanguages($validLanguages); |
| 48 | + $list->load(); |
| 49 | + |
| 50 | + return $list->getTranslations(); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * {@inheritdoc} |
| 55 | + */ |
| 56 | + public function createTranslations(array $translationData, string $locale): void |
| 57 | + { |
| 58 | + $languages = $this->adminResolver->getLanguages(); |
| 59 | + $this->validateLocale($locale, $languages); |
| 60 | + |
| 61 | + /** @var TranslationData $translation */ |
| 62 | + foreach ($translationData as $translation) { |
| 63 | + $t = new Translation(); |
| 64 | + $t->setDomain(TranslatorServiceInterface::DOMAIN); |
| 65 | + $t->setKey($translation->getKey()); |
| 66 | + $t->setType($translation->getType()); |
| 67 | + $t->addTranslation($locale, $translation->getTranslation()); |
| 68 | + $t->setCreationDate(time()); |
| 69 | + $t->setModificationDate(time()); |
| 70 | + if ($this->getTranslationByKey($translation->getKey()) === null) { |
| 71 | + $this->setNewValues($t, $languages, $locale); |
| 72 | + } |
| 73 | + $t->save(); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + public function deleteTranslation(string $key): void |
| 78 | + { |
| 79 | + $translation = $this->getTranslationByKey($key); |
| 80 | + if ($translation === null) { |
| 81 | + throw new NotFoundException('translation', $key, 'key'); |
| 82 | + } |
| 83 | + |
| 84 | + $translation->delete(); |
| 85 | + } |
| 86 | + |
| 87 | + private function getTranslationList(): Listing |
| 88 | + { |
| 89 | + $list = new Translation\Listing(); |
| 90 | + $list->setDomain(TranslatorServiceInterface::DOMAIN); |
| 91 | + $list->setOrder('asc'); |
| 92 | + $list->setOrderKey('translations_' . TranslatorServiceInterface::DOMAIN . '.key', false); |
| 93 | + |
| 94 | + return $list; |
| 95 | + } |
| 96 | + |
| 97 | + private function setNewValues(Translation $translation, array $languages, string $locale): void |
| 98 | + { |
| 99 | + $translation->setCreationDate(time()); |
| 100 | + |
| 101 | + foreach ($languages as $language) { |
| 102 | + if ($language !== $locale) { |
| 103 | + $translation->addTranslation($language, ''); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private function getTranslationByKey(string $key): ?Translation |
| 109 | + { |
| 110 | + $list = $this->getTranslationList(); |
| 111 | + $list->setLimit(1); |
| 112 | + $list->addConditionParam('`key` = ?', $key); |
| 113 | + |
| 114 | + return !empty($list->load()) ? $list->current() : null; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * @throws InvalidLocaleException |
| 119 | + */ |
| 120 | + private function validateLocale(string $locale, array $validLanguages): void |
| 121 | + { |
| 122 | + if (!in_array($locale, $validLanguages, true)) { |
| 123 | + throw new InvalidLocaleException($locale); |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments