|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Diglin\AutoCustomerGroupBundle\EventListener; |
| 6 | + |
| 7 | +use Doctrine\Persistence\ManagerRegistry; |
| 8 | +use Diglin\AutoCustomerGroupBundle\DependencyInjection\AutoCustomerGroupExtension; |
| 9 | +use Diglin\AutoCustomerGroupBundle\DependencyInjection\Configuration; |
| 10 | +use Oro\Bundle\ConfigBundle\Config\ConfigManager; |
| 11 | +use Oro\Bundle\ConfigBundle\Event\ConfigSettingsUpdateEvent; |
| 12 | +use Oro\Bundle\CustomerBundle\Entity\CustomerGroup; |
| 13 | + |
| 14 | +class SystemConfigListener |
| 15 | +{ |
| 16 | + protected ManagerRegistry $registry; |
| 17 | + |
| 18 | + protected string $ownerClass; |
| 19 | + |
| 20 | + public function __construct(ManagerRegistry $registry) |
| 21 | + { |
| 22 | + $this->registry = $registry; |
| 23 | + } |
| 24 | + |
| 25 | + public function onFormPreSetData(ConfigSettingsUpdateEvent $event): void |
| 26 | + { |
| 27 | + $settingsKey = implode(ConfigManager::SECTION_VIEW_SEPARATOR, [AutoCustomerGroupExtension::ALIAS, Configuration::ASSIGNMENT_CUSTOMER_GROUP]); |
| 28 | + $settings = $event->getSettings(); |
| 29 | + if (is_array($settings) |
| 30 | + && !empty($settings[$settingsKey]['value']) |
| 31 | + ) { |
| 32 | + $settings[$settingsKey]['value'] = $this->registry |
| 33 | + ->getManagerForClass(CustomerGroup::class) |
| 34 | + ->find(CustomerGroup::class, $settings[$settingsKey]['value']); |
| 35 | + $event->setSettings($settings); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + public function onSettingsSaveBefore(ConfigSettingsUpdateEvent $event): void |
| 40 | + { |
| 41 | + $settings = $event->getSettings(); |
| 42 | + |
| 43 | + if (!array_key_exists('value', $settings)) { |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + if (!is_a($settings['value'], CustomerGroup::class)) { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + /** @var object $owner */ |
| 52 | + $owner = $settings['value']; |
| 53 | + $settings['value'] = $owner->getId(); |
| 54 | + $event->setSettings($settings); |
| 55 | + } |
| 56 | +} |
0 commit comments