|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of MetaModels/attribute_checkbox. |
| 5 | + * |
| 6 | + * (c) 2012-2024 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/attribute_checkbox |
| 14 | + * @author Ingolf Steinhardt <info@e-spin.de> |
| 15 | + * @copyright 2012-2024 The MetaModels team. |
| 16 | + * @license https://github.com/MetaModels/attribute_checkbox/blob/master/LICENSE LGPL-3.0-or-later |
| 17 | + * @filesource |
| 18 | + */ |
| 19 | + |
| 20 | +declare(strict_types=1); |
| 21 | + |
| 22 | +namespace MetaModels\AttributeCheckboxBundle\Translator; |
| 23 | + |
| 24 | +use MetaModels\Attribute\IAttribute; |
| 25 | +use MetaModels\AttributeCheckboxBundle\Attribute\Checkbox; |
| 26 | +use MetaModels\CoreBundle\Translator\LoaderInterface; |
| 27 | +use MetaModels\IMetaModel; |
| 28 | +use MetaModels\ITranslatedMetaModel; |
| 29 | +use MetaModels\ViewCombination\InputScreenInformationBuilder; |
| 30 | +use Symfony\Component\Translation\MessageCatalogue; |
| 31 | +use Symfony\Component\Translation\TranslatorBagInterface; |
| 32 | + |
| 33 | +use function array_map; |
| 34 | +use function in_array; |
| 35 | +use function is_array; |
| 36 | + |
| 37 | +final class TranslationLoader implements LoaderInterface |
| 38 | +{ |
| 39 | + public function __construct( |
| 40 | + private readonly TranslatorBagInterface $baseTranslator, |
| 41 | + private readonly InputScreenInformationBuilder $builder, |
| 42 | + ) { |
| 43 | + } |
| 44 | + |
| 45 | + public function load(IMetaModel $metaModel, string $locale): MessageCatalogue |
| 46 | + { |
| 47 | + /** |
| 48 | + * @psalm-suppress DeprecatedMethod |
| 49 | + * @psalm-suppress TooManyArguments |
| 50 | + */ |
| 51 | + if ($metaModel instanceof ITranslatedMetaModel) { |
| 52 | + $metaModel->selectLanguage($locale); |
| 53 | + $mainLanguage = $metaModel->getMainLanguage(); |
| 54 | + } elseif ($metaModel->isTranslated(false)) { |
| 55 | + $mainLanguage = $metaModel->getFallbackLanguage() ?? 'en'; |
| 56 | + } else { |
| 57 | + // Untranslated MetaModel. |
| 58 | + $mainLanguage = 'en'; |
| 59 | + } |
| 60 | + $domain = $metaModel->getTableName(); |
| 61 | + |
| 62 | + $catalog = new MessageCatalogue($locale); |
| 63 | + |
| 64 | + foreach ($this->builder->fetchAllInputScreensForTable($domain) as $inputScreen) { |
| 65 | + foreach ( |
| 66 | + $this->loadInputScreen($metaModel, $inputScreen, $locale, $mainLanguage, $domain) |
| 67 | + ->all($domain) as $key => $value |
| 68 | + ) { |
| 69 | + $catalog->set($key, $value, $domain); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + return $catalog; |
| 74 | + } |
| 75 | + |
| 76 | + private function loadInputScreen( |
| 77 | + IMetaModel $metaModel, |
| 78 | + array $inputScreen, |
| 79 | + string $locale, |
| 80 | + string $mainLanguage, |
| 81 | + string $domain |
| 82 | + ): MessageCatalogue { |
| 83 | + $allProps = $inputScreen['properties']; |
| 84 | + $properties = array_map( |
| 85 | + static fn(array $property): ?string => ($property['col_name'] ?? null), |
| 86 | + $allProps |
| 87 | + ); |
| 88 | + $catalog = new MessageCatalogue($locale); |
| 89 | + |
| 90 | + // Load tl_metamodel_item catalogue. |
| 91 | + $base = $this->baseTranslator->getCatalogue($locale); |
| 92 | + |
| 93 | + $prefix = 'inputscreen.' . $inputScreen['meta']['id'] . '.'; |
| 94 | + foreach ($metaModel->getAttributes() as $attribute) { |
| 95 | + if (!$this->wantToAdd($attribute, $properties)) { |
| 96 | + continue; |
| 97 | + } |
| 98 | + |
| 99 | + if ($attribute->get('check_listview') === '1') { |
| 100 | + $commandName = 'listviewtoggle_' . $attribute->getColName(); |
| 101 | + } else { |
| 102 | + $commandName = 'publishtoggle_' . $attribute->getColName(); |
| 103 | + } |
| 104 | + |
| 105 | + $values = ['%name%' => $this->getName($attribute, $locale, $mainLanguage)]; |
| 106 | + |
| 107 | + $catalog->set( |
| 108 | + $prefix . $commandName . '.label', |
| 109 | + strtr($base->get('toggle.label', 'metamodel_checkbox'), $values), |
| 110 | + $domain |
| 111 | + ); |
| 112 | + $catalog->set( |
| 113 | + $prefix . $commandName . '.description', |
| 114 | + strtr($base->get('toggle.description', 'metamodel_checkbox'), $values), |
| 115 | + $domain |
| 116 | + ); |
| 117 | + } |
| 118 | + |
| 119 | + return $catalog; |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Test if we want to add an operation for the attribute. |
| 124 | + * |
| 125 | + * @param IAttribute $attribute The attribute to test. |
| 126 | + * @param array $properties The property names in the input screen. |
| 127 | + * |
| 128 | + * @return bool |
| 129 | + */ |
| 130 | + private function wantToAdd(IAttribute $attribute, array $properties): bool |
| 131 | + { |
| 132 | + return ($attribute instanceof Checkbox) |
| 133 | + && (($attribute->get('check_publish') === '1') || ($attribute->get('check_listview') === '1')) |
| 134 | + && (in_array($attribute->getColName(), $properties, true)); |
| 135 | + } |
| 136 | + |
| 137 | + private function getName(IAttribute $attribute, string $locale, string $mainLanguage): string |
| 138 | + { |
| 139 | + $colName = $attribute->getColName(); |
| 140 | + $name = $attribute->get('name') ?? $colName; |
| 141 | + |
| 142 | + return $this->extractLangString($name, $locale, $mainLanguage) ?? $colName; |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * @param string|array<string, string> $value The value. |
| 147 | + * @param string $locale The locale. |
| 148 | + * @param string $mainLocale The fallback language. |
| 149 | + * |
| 150 | + * @return string|null |
| 151 | + * |
| 152 | + * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
| 153 | + */ |
| 154 | + private function extractLangString(string|array $value, string $locale, string $mainLocale): ?string |
| 155 | + { |
| 156 | + if (!is_array($value)) { |
| 157 | + return $value; |
| 158 | + } |
| 159 | + |
| 160 | + $fallback = null; |
| 161 | + foreach ($value as $langCode => $string) { |
| 162 | + if ($locale === $langCode && '' !== $string) { |
| 163 | + return $string; |
| 164 | + } |
| 165 | + if ($mainLocale === $langCode && '' !== $string) { |
| 166 | + $fallback = $string; |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + if ('en' === $locale && null === $fallback && (null !== $default = ($value['default'] ?? $value[''] ?? null))) { |
| 171 | + return $default; |
| 172 | + } |
| 173 | + |
| 174 | + return $fallback; |
| 175 | + } |
| 176 | +} |
0 commit comments