|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of MetaModels/attribute_translatedfile. |
| 5 | + * |
| 6 | + * (c) 2012-2023 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 Ingolf Steinhardt <[email protected]> |
| 15 | + * @copyright 2012-2023 The MetaModels team. |
| 16 | + * @license https://github.com/MetaModels/attribute_translatedfile/blob/master/LICENSE LGPL-3.0-or-later |
| 17 | + * @filesource |
| 18 | + */ |
| 19 | + |
| 20 | +declare(strict_types=1); |
| 21 | + |
| 22 | +namespace MetaModels\AttributeTranslatedFileBundle\EventListener\DcGeneral\Table\DcaSetting; |
| 23 | + |
| 24 | +use ContaoCommunityAlliance\DcGeneral\Contao\RequestScopeDeterminator; |
| 25 | +use ContaoCommunityAlliance\DcGeneral\Contao\View\Contao2BackendView\Event\GetPropertyOptionsEvent; |
| 26 | +use Doctrine\DBAL\Connection; |
| 27 | +use MetaModels\CoreBundle\EventListener\DcGeneral\Table\DcaSetting\AbstractListener; |
| 28 | +use MetaModels\IFactory; |
| 29 | + |
| 30 | +/** |
| 31 | + * Add the options for the file widget mode. |
| 32 | + */ |
| 33 | +class FileWidgetModeOptions extends AbstractListener |
| 34 | +{ |
| 35 | + /** |
| 36 | + * Invoke the event. |
| 37 | + * |
| 38 | + * @param GetPropertyOptionsEvent $event The event. |
| 39 | + * |
| 40 | + * @return void |
| 41 | + */ |
| 42 | + public function __invoke(GetPropertyOptionsEvent $event): void |
| 43 | + { |
| 44 | + if (('file_widgetMode' !== $event->getPropertyName()) |
| 45 | + || (false === $this->wantToHandle($event)) |
| 46 | + || (false === $this->isAttributeTranslatedFile($event)) |
| 47 | + ) { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + $this->addOptions($event); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Add the options. |
| 56 | + * |
| 57 | + * @param GetPropertyOptionsEvent $event The event. |
| 58 | + * |
| 59 | + * @return void |
| 60 | + */ |
| 61 | + private function addOptions(GetPropertyOptionsEvent $event): void |
| 62 | + { |
| 63 | + $addOptions = ['downloads', 'gallery']; |
| 64 | + |
| 65 | + $event->setOptions(\array_values(\array_unique(\array_merge($event->getOptions(), $addOptions)))); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * If used attribute type of file. |
| 70 | + * |
| 71 | + * @param GetPropertyOptionsEvent $event The event. |
| 72 | + * |
| 73 | + * @return bool |
| 74 | + */ |
| 75 | + private function isAttributeTranslatedFile(GetPropertyOptionsEvent $event): bool |
| 76 | + { |
| 77 | + $builder = $this->connection->createQueryBuilder(); |
| 78 | + $builder |
| 79 | + ->select('t.type') |
| 80 | + ->from('tl_metamodel_attribute', 't') |
| 81 | + ->where($builder->expr()->eq('t.id', ':id')) |
| 82 | + ->setParameter('id', $event->getModel()->getProperty('attr_id')); |
| 83 | + |
| 84 | + $statement = $builder->execute(); |
| 85 | + if (0 === $statement->columnCount()) { |
| 86 | + return false; |
| 87 | + } |
| 88 | + |
| 89 | + $result = $statement->fetch(\PDO::FETCH_OBJ); |
| 90 | + return 'translatedfile' === $result->type; |
| 91 | + } |
| 92 | +} |
0 commit comments