custom/plugins/NetiNextAccessManager/src/Subscriber/AddressEditingSubscriber.php line 38

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace NetInventors\NetiNextAccessManager\Subscriber;
  4. use NetInventors\NetiNextAccessManager\Extension\Content\Customer\Aggregate\CustomerAttribute\CustomerAttributeEntity;
  5. use NetInventors\NetiNextAccessManager\Service\PluginConfig;
  6. use Shopware\Core\Checkout\Customer\CustomerEntity;
  7. use Shopware\Storefront\Page\Account\Overview\AccountOverviewPageLoadedEvent;
  8. use Shopware\Storefront\Page\Account\Profile\AccountProfilePageLoadedEvent;
  9. use Shopware\Storefront\Page\Address\Detail\AddressDetailPageLoadedEvent;
  10. use Shopware\Storefront\Page\Address\Listing\AddressListingPageLoadedEvent;
  11. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  12. use Shopware\Storefront\Page\PageLoadedEvent;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class AddressEditingSubscriber implements EventSubscriberInterface
  15. {
  16.     private PluginConfig $pluginConfig;
  17.     public function __construct(PluginConfig $pluginConfig)
  18.     {
  19.         $this->pluginConfig $pluginConfig;
  20.     }
  21.     public static function getSubscribedEvents(): array
  22.     {
  23.         return [
  24.             AccountOverviewPageLoadedEvent::class => 'onAccountOverviewLoaded',
  25.             CheckoutConfirmPageLoadedEvent::class => 'onAccountOverviewLoaded',
  26.             AddressListingPageLoadedEvent::class  => 'onAccountOverviewLoaded',
  27.             AddressDetailPageLoadedEvent::class   => 'onAccountOverviewLoaded',
  28.             AccountProfilePageLoadedEvent::class  => 'onAccountProfilePageLoaded',
  29.         ];
  30.     }
  31.     public function onAccountOverviewLoaded(PageLoadedEvent $event): void
  32.     {
  33.         if (!$this->pluginConfig->isActive()) {
  34.             return;
  35.         }
  36.         $page     $event->getPage();
  37.         $customer $event->getSalesChannelContext()->getCustomer();
  38.         if (null === $customer || null === $customer->getExtension('netiAccessManagerCustomerAttribute')) {
  39.             return;
  40.         }
  41.         $customerGroupId $customer->getGroupId();
  42.         /** @var CustomerAttributeEntity $extension */
  43.         $extension $customer->getExtension('netiAccessManagerCustomerAttribute');
  44.         $page->assign([
  45.             'amBlockShippingAddress' => $this->isEditingBlocked(
  46.                 $customerGroupId,
  47.                 $extension->getEditShippingAddress(),
  48.                 'shipping'
  49.             ),
  50.             'amBlockBillingAddress'  => $this->isEditingBlocked(
  51.                 $customerGroupId,
  52.                 $extension->getEditBillingAddress(),
  53.                 'billing'
  54.             ),
  55.         ]);
  56.     }
  57.     public function onAccountProfilePageLoaded(AccountProfilePageLoadedEvent $event): void
  58.     {
  59.         if (!$this->pluginConfig->isActive()) {
  60.             return;
  61.         }
  62.         $blockedGroups $this->pluginConfig->getBlockedEmailEditGroups();
  63.         $customer      $event->getSalesChannelContext()->getCustomer();
  64.         if (!$customer instanceof CustomerEntity || !in_array($customer->getGroupId(), $blockedGroupstrue)) {
  65.             return;
  66.         }
  67.         $event->getPage()->assign([ 'amBlockMailAddress' => true ]);
  68.     }
  69.     public function isEditingBlocked(string $customerGroupIdstring $customerAttributeConfigstring $mode): bool
  70.     {
  71.         $blockedGroups = [];
  72.         if ('shipping' === $mode) {
  73.             $blockedGroups $this->pluginConfig->getBlockedShippingAddressEditGroups();
  74.         }
  75.         if ('billing' === $mode) {
  76.             $blockedGroups $this->pluginConfig->getBlockedBillingAddressEditGroups();
  77.         }
  78.         if ($customerAttributeConfig === CustomerAttributeEntity::ADDRESS_EDIT_INHERITED) {
  79.             return in_array($customerGroupId$blockedGroupstrue);
  80.         }
  81.         return $customerAttributeConfig === CustomerAttributeEntity::ADDRESS_EDIT_PROHIBITED;
  82.     }
  83. }