<?php declare(strict_types=1);
namespace MoorlFormBuilderRegistration\Subscriber;
use MoorlFormBuilder\Core\Service\FormService;
use Shopware\Core\Checkout\Customer\CustomerEvents;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Event\DataMappingEvent;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Page\Account\Login\AccountLoginPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Register\CheckoutRegisterPageLoadedEvent;
use Shopware\Storefront\Page\PageLoadedEvent;
use Shopware\Storefront\Pagelet\Header\HeaderPageletLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class StorefrontSubscriber implements EventSubscriberInterface
{
/**
* @var SystemConfigService
*/
private $systemConfigService;
/**
* @var EntityRepositoryInterface
*/
private $customerUploadRepository;
/**
* @var FormService
*/
private $formService;
public function __construct(
FormService $formService,
SystemConfigService $systemConfigService,
EntityRepositoryInterface $customerUploadRepository
)
{
$this->formService = $formService;
$this->systemConfigService = $systemConfigService;
$this->customerUploadRepository = $customerUploadRepository;
}
public static function getSubscribedEvents(): array
{
return [
CustomerEvents::MAPPING_REGISTER_CUSTOMER => 'onSignUpMapping',
AccountLoginPageLoadedEvent::class => 'onPageLoaded',
CheckoutRegisterPageLoadedEvent ::class => 'onPageLoaded',
HeaderPageletLoadedEvent::class => 'onHeaderPageletLoaded'
];
}
public function onHeaderPageletLoaded(HeaderPageletLoadedEvent $event): void
{
$salesChannelContext = $event->getSalesChannelContext();
$customer = $salesChannelContext->getCustomer();
if (!$customer) {
return;
}
$customerGroupIds = $this->systemConfigService->get('MoorlFormBuilderRegistration.config.customerGroupIds') ?: [];
if (!in_array($customer->getGroupId(), $customerGroupIds)) {
return;
}
$criteria = new Criteria();
$criteria->addAssociation('media');
$criteria->addFilter(new EqualsFilter('customerId', $customer->getId()));
$uploads = $this->customerUploadRepository->search($criteria, $salesChannelContext->getContext())->getEntities();
$customer->addExtension('DocumentUploads', $uploads);
}
public function onPageLoaded(PageLoadedEvent $event): void
{
$this->formService->setSalesChannelContext($event->getSalesChannelContext());
$this->formService->initFormsByType($event->getContext(), 'customerRegister');
$this->formService->initCurrentForm();
if (!$this->formService->getCurrentForm()) {
return;
}
$event->getPage()->addExtension('MoorlFormBuilderRegistration', $this->formService->getCurrentForm());
}
public function onSignUpMapping(DataMappingEvent $event): void
{
$violations = $this->formService->fire($event->getContext());
if ($violations) {
return;
}
//$data = $event->getInput();
$customer = $event->getOutput();
$customer = array_merge($customer, $this->formService->getCurrentForm()->getPayload());
$event->setOutput($customer);
}
}