<?php declare(strict_types=1);
namespace TemplaidInvoices\Subscriber;
use CoeWishlistSw6\Core\Content\Wishlist\Aggregate\WishlistNoteEntity;
use CoeWishlistSw6\Storefront\Page\Wishlist\WishlistPageLoadedEvent;
use Shopware\Core\Content\Property\PropertyGroupEntity;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Page\GenericPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use TemplaidInvoices\Core\Content\Wishlist\WishlistItemEntity;
use TemplaidInvoices\Service\SyncService;
use TemplaidInvoices\Service\WishlistService;
use TemplaidInvoices\Storefront\Page\Account\TemplaidInvoicesPageLoadedEvent;
use TemplaidInvoices\Storefront\Page\Account\TemplaidOrdersPageLoadedEvent;
use TemplaidInvoices\Storefront\Page\Account\TemplaidPackingSlipsPageLoadedEvent;
class PageSubscriber implements EventSubscriberInterface
{
private WishlistService $wishlistService;
private SyncService $syncService;
private EntityRepository $propertyGroupRepository;
private SystemConfigService $configService;
public function __construct(
WishlistService $wishlistService,
SyncService $syncService,
EntityRepository $propertyGroupRepository,
SystemConfigService $configService
)
{
$this->wishlistService = $wishlistService;
$this->syncService = $syncService;
$this->propertyGroupRepository = $propertyGroupRepository;
$this->configService = $configService;
}
/**
* @return array|string[]
*/
public static function getSubscribedEvents()
{
return [
WishlistPageLoadedEvent::class => 'onWishlistPageLoaded',
GenericPageLoadedEvent::class => 'onAccountPageLoadedEvent',
TemplaidInvoicesPageLoadedEvent::class => 'onInvoicePageLoadedEvent',
TemplaidPackingSlipsPageLoadedEvent::class => 'onPackingSlipPageLoadedEvent',
TemplaidOrdersPageLoadedEvent::class => 'onOrderPageLoadedEvent',
];
}
/**
* @param TemplaidOrdersPageLoadedEvent $event
*/
public function onOrderPageLoadedEvent(TemplaidOrdersPageLoadedEvent $event): void
{
if (!$event->getSalesChannelContext()->getCustomer()) {
return;
}
$this->syncService->syncOrders($event->getSalesChannelContext()->getCustomerId(), $event->getSalesChannelContext()->getCustomer()->getCustomerNumber(), $event->getSalesChannelContext());
}
/**
* @param TemplaidInvoicesPageLoadedEvent $event
*/
public function onInvoicePageLoadedEvent(TemplaidInvoicesPageLoadedEvent $event): void
{
if (!$event->getSalesChannelContext()->getCustomer()) {
return;
}
$this->syncService->syncInvoices($event->getSalesChannelContext()->getCustomerId(), $event->getSalesChannelContext()->getCustomer()->getCustomerNumber(), $event->getSalesChannelContext());
}
/**
* @param TemplaidPackingSlipsPageLoadedEvent $event
*/
public function onPackingSlipPageLoadedEvent(TemplaidPackingSlipsPageLoadedEvent $event): void
{
if (!$event->getSalesChannelContext()->getCustomer()) {
return;
}
$this->syncService->syncInvoices($event->getSalesChannelContext()->getCustomerId(), $event->getSalesChannelContext()->getCustomer()->getCustomerNumber(), $event->getSalesChannelContext());
}
/**
* @param GenericPageLoadedEvent $event
*/
public function onAccountPageLoadedEvent(GenericPageLoadedEvent $event): void
{
if (!$event->getSalesChannelContext()->getCustomer() || !str_contains($event->getRequest()->getPathInfo(), '/account')) {
return;
}
$lists = $this->wishlistService->getCustomerLists($event->getSalesChannelContext());
$event->getPage()->addArrayExtension('assortmentLists', $lists);
if ($assortmentPropertyIds = $this->configService->get('TemplaidInvoices.config.assortmentProperties')) {
$criteria = new Criteria($assortmentPropertyIds);
$properties = $this->propertyGroupRepository->search($criteria, $event->getContext())->map(function (PropertyGroupEntity $property) {
return [
'id' => $property->getId(),
'label' => $property->getName()
];
});
$event->getPage()->addArrayExtension('assortmentProperties', $properties);
}
}
/**
* @param WishlistPageLoadedEvent $event
*/
public function onWishlistPageLoaded(WishlistPageLoadedEvent $event): void
{
if (!$event->getSalesChannelContext()->getCustomer()) {
return;
}
$sortedItems = $this->wishlistService->getSortedWishlistItems($event->getSalesChannelContext()->getCustomerId(), $event->getContext());
$sortedIds = array_values($sortedItems->map(function (WishlistItemEntity $item) {
return $item->getLineId();
}));
$sortedNotes = $event->getPage()->getActiveList()->getNotes();
$sortedNotes->sort(function (WishlistNoteEntity $a, WishlistNoteEntity $b) use ($sortedIds) {
$positionA = array_search($a->getId(), $sortedIds) ?: 0;
$positionB = array_search($b->getId(), $sortedIds) ?: 0;
return $positionA <=> $positionB;
});
}
}