<?php
declare(strict_types=1);
namespace NetInventors\NetiNextAccessManager\Subscriber;
use NetInventors\NetiNextAccessManager\Extension\Content\Customer\Aggregate\CustomerAttribute\CustomerAttributeEntity;
use NetInventors\NetiNextAccessManager\Service\PluginConfig;
use Shopware\Core\Checkout\Customer\CustomerEntity;
use Shopware\Storefront\Page\Account\Overview\AccountOverviewPageLoadedEvent;
use Shopware\Storefront\Page\Account\Profile\AccountProfilePageLoadedEvent;
use Shopware\Storefront\Page\Address\Detail\AddressDetailPageLoadedEvent;
use Shopware\Storefront\Page\Address\Listing\AddressListingPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
use Shopware\Storefront\Page\PageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class AddressEditingSubscriber implements EventSubscriberInterface
{
private PluginConfig $pluginConfig;
public function __construct(PluginConfig $pluginConfig)
{
$this->pluginConfig = $pluginConfig;
}
public static function getSubscribedEvents(): array
{
return [
AccountOverviewPageLoadedEvent::class => 'onAccountOverviewLoaded',
CheckoutConfirmPageLoadedEvent::class => 'onAccountOverviewLoaded',
AddressListingPageLoadedEvent::class => 'onAccountOverviewLoaded',
AddressDetailPageLoadedEvent::class => 'onAccountOverviewLoaded',
AccountProfilePageLoadedEvent::class => 'onAccountProfilePageLoaded',
];
}
public function onAccountOverviewLoaded(PageLoadedEvent $event): void
{
if (!$this->pluginConfig->isActive()) {
return;
}
$page = $event->getPage();
$customer = $event->getSalesChannelContext()->getCustomer();
if (null === $customer || null === $customer->getExtension('netiAccessManagerCustomerAttribute')) {
return;
}
$customerGroupId = $customer->getGroupId();
/** @var CustomerAttributeEntity $extension */
$extension = $customer->getExtension('netiAccessManagerCustomerAttribute');
$page->assign([
'amBlockShippingAddress' => $this->isEditingBlocked(
$customerGroupId,
$extension->getEditShippingAddress(),
'shipping'
),
'amBlockBillingAddress' => $this->isEditingBlocked(
$customerGroupId,
$extension->getEditBillingAddress(),
'billing'
),
]);
}
public function onAccountProfilePageLoaded(AccountProfilePageLoadedEvent $event): void
{
if (!$this->pluginConfig->isActive()) {
return;
}
$blockedGroups = $this->pluginConfig->getBlockedEmailEditGroups();
$customer = $event->getSalesChannelContext()->getCustomer();
if (!$customer instanceof CustomerEntity || !in_array($customer->getGroupId(), $blockedGroups, true)) {
return;
}
$event->getPage()->assign([ 'amBlockMailAddress' => true ]);
}
public function isEditingBlocked(string $customerGroupId, string $customerAttributeConfig, string $mode): bool
{
$blockedGroups = [];
if ('shipping' === $mode) {
$blockedGroups = $this->pluginConfig->getBlockedShippingAddressEditGroups();
}
if ('billing' === $mode) {
$blockedGroups = $this->pluginConfig->getBlockedBillingAddressEditGroups();
}
if ($customerAttributeConfig === CustomerAttributeEntity::ADDRESS_EDIT_INHERITED) {
return in_array($customerGroupId, $blockedGroups, true);
}
return $customerAttributeConfig === CustomerAttributeEntity::ADDRESS_EDIT_PROHIBITED;
}
}