|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of MetaModels/attribute_translatedfile. |
| 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_translatedfile |
| 14 | + * @author Sven Baumann <[email protected]> |
| 15 | + * @author Stefan Heimes <[email protected]> |
| 16 | + * @author Christian Schiffler <[email protected]> |
| 17 | + * @author Ingolf Steinhardt <[email protected]> |
| 18 | + * @copyright 2012-2024 The MetaModels team. |
| 19 | + * @license https://github.com/MetaModels/attribute_translatedfile/blob/master/LICENSE LGPL-3.0-or-later |
| 20 | + * @filesource |
| 21 | + */ |
| 22 | + |
| 23 | +namespace MetaModels\AttributeTranslatedFileBundle\EventListener; |
| 24 | + |
| 25 | +use Contao\CoreBundle\Image\ImageSizes; |
| 26 | +use ContaoCommunityAlliance\DcGeneral\Contao\View\Contao2BackendView\Event\GetPropertyOptionsEvent; |
| 27 | +use Symfony\Contracts\Translation\TranslatorInterface; |
| 28 | + |
| 29 | +/** |
| 30 | + * Get the options for the image size. |
| 31 | + */ |
| 32 | +class ImageSizeOptionsProvider |
| 33 | +{ |
| 34 | + public function __construct( |
| 35 | + private readonly ImageSizes $imageSizes, |
| 36 | + private readonly TranslatorInterface $translator, |
| 37 | + ) { |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Add the options. |
| 42 | + * |
| 43 | + * @param GetPropertyOptionsEvent $event The event. |
| 44 | + * |
| 45 | + * @return void |
| 46 | + */ |
| 47 | + public function addOptions(GetPropertyOptionsEvent $event): void |
| 48 | + { |
| 49 | + $options = $this->imageSizes->getAllOptions(); |
| 50 | + $optionsFull = []; |
| 51 | + foreach ($options as $section => $sizeNames) { |
| 52 | + $optionsFull[$this->translateSizeName($section)] = $this->translateSection($section, $sizeNames); |
| 53 | + } |
| 54 | + |
| 55 | + $event->setOptions($optionsFull); |
| 56 | + } |
| 57 | + |
| 58 | + private function translateSection(string $section, array $sizeNames): array |
| 59 | + { |
| 60 | + if (!\in_array($section, ['relative', 'exact'], true)) { |
| 61 | + return $sizeNames; |
| 62 | + } |
| 63 | + |
| 64 | + $optionSection = []; |
| 65 | + foreach ($sizeNames as $currentLabel) { |
| 66 | + $optionSection[$currentLabel] = $this->translateSizeName($currentLabel); |
| 67 | + } |
| 68 | + return $optionSection; |
| 69 | + } |
| 70 | + |
| 71 | + private function translateSizeName(string $sizeName): string |
| 72 | + { |
| 73 | + $key = 'MSC.' . $sizeName . '.0'; |
| 74 | + if ($key !== $label = $this->translator->trans($key, [], 'contao_default')) { |
| 75 | + return $label; |
| 76 | + } |
| 77 | + $key = 'MSC.' . $sizeName; |
| 78 | + if ($key !== $label = $this->translator->trans($key, [], 'contao_default')) { |
| 79 | + return $label; |
| 80 | + } |
| 81 | + |
| 82 | + return $sizeName; |
| 83 | + } |
| 84 | +} |
0 commit comments