custom/plugins/TemplaidInvoices/src/Subscriber/PageSubscriber.php line 57

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace TemplaidInvoices\Subscriber;
  3. use CoeWishlistSw6\Core\Content\Wishlist\Aggregate\WishlistNoteEntity;
  4. use CoeWishlistSw6\Storefront\Page\Wishlist\WishlistPageLoadedEvent;
  5. use Shopware\Core\Content\Property\PropertyGroupEntity;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\System\SystemConfig\SystemConfigService;
  9. use Shopware\Storefront\Page\GenericPageLoadedEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use TemplaidInvoices\Core\Content\Wishlist\WishlistItemEntity;
  12. use TemplaidInvoices\Service\SyncService;
  13. use TemplaidInvoices\Service\WishlistService;
  14. use TemplaidInvoices\Storefront\Page\Account\TemplaidInvoicesPageLoadedEvent;
  15. use TemplaidInvoices\Storefront\Page\Account\TemplaidOrdersPageLoadedEvent;
  16. use TemplaidInvoices\Storefront\Page\Account\TemplaidPackingSlipsPageLoadedEvent;
  17. class PageSubscriber implements EventSubscriberInterface
  18. {
  19.     private WishlistService $wishlistService;
  20.     private SyncService $syncService;
  21.     private EntityRepository $propertyGroupRepository;
  22.     private SystemConfigService $configService;
  23.     public function __construct(
  24.         WishlistService          $wishlistService,
  25.         SyncService              $syncService,
  26.         EntityRepository         $propertyGroupRepository,
  27.         SystemConfigService      $configService
  28.     )
  29.     {
  30.         $this->wishlistService $wishlistService;
  31.         $this->syncService $syncService;
  32.         $this->propertyGroupRepository $propertyGroupRepository;
  33.         $this->configService $configService;
  34.     }
  35.     /**
  36.      * @return array|string[]
  37.      */
  38.     public static function getSubscribedEvents()
  39.     {
  40.         return [
  41.             WishlistPageLoadedEvent::class => 'onWishlistPageLoaded',
  42.             GenericPageLoadedEvent::class => 'onAccountPageLoadedEvent',
  43.             TemplaidInvoicesPageLoadedEvent::class => 'onInvoicePageLoadedEvent',
  44.             TemplaidPackingSlipsPageLoadedEvent::class => 'onPackingSlipPageLoadedEvent',
  45.             TemplaidOrdersPageLoadedEvent::class => 'onOrderPageLoadedEvent',
  46.         ];
  47.     }
  48.     /**
  49.      * @param TemplaidOrdersPageLoadedEvent $event
  50.      */
  51.     public function onOrderPageLoadedEvent(TemplaidOrdersPageLoadedEvent $event): void
  52.     {
  53.         if (!$event->getSalesChannelContext()->getCustomer()) {
  54.             return;
  55.         }
  56.         $this->syncService->syncOrders($event->getSalesChannelContext()->getCustomerId(), $event->getSalesChannelContext()->getCustomer()->getCustomerNumber(), $event->getSalesChannelContext());
  57.     }
  58.     /**
  59.      * @param TemplaidInvoicesPageLoadedEvent $event
  60.      */
  61.     public function onInvoicePageLoadedEvent(TemplaidInvoicesPageLoadedEvent $event): void
  62.     {
  63.         if (!$event->getSalesChannelContext()->getCustomer()) {
  64.             return;
  65.         }
  66.         $this->syncService->syncInvoices($event->getSalesChannelContext()->getCustomerId(), $event->getSalesChannelContext()->getCustomer()->getCustomerNumber(), $event->getSalesChannelContext());
  67.     }
  68.     /**
  69.      * @param TemplaidPackingSlipsPageLoadedEvent $event
  70.      */
  71.     public function onPackingSlipPageLoadedEvent(TemplaidPackingSlipsPageLoadedEvent $event): void
  72.     {
  73.         if (!$event->getSalesChannelContext()->getCustomer()) {
  74.             return;
  75.         }
  76.         $this->syncService->syncInvoices($event->getSalesChannelContext()->getCustomerId(), $event->getSalesChannelContext()->getCustomer()->getCustomerNumber(), $event->getSalesChannelContext());
  77.     }
  78.     /**
  79.      * @param GenericPageLoadedEvent $event
  80.      */
  81.     public function onAccountPageLoadedEvent(GenericPageLoadedEvent $event): void
  82.     {
  83.         if (!$event->getSalesChannelContext()->getCustomer() || !str_contains($event->getRequest()->getPathInfo(), '/account')) {
  84.             return;
  85.         }
  86.         $lists $this->wishlistService->getCustomerLists($event->getSalesChannelContext());
  87.         $event->getPage()->addArrayExtension('assortmentLists'$lists);
  88.         if ($assortmentPropertyIds $this->configService->get('TemplaidInvoices.config.assortmentProperties')) {
  89.             $criteria = new Criteria($assortmentPropertyIds);
  90.             $properties $this->propertyGroupRepository->search($criteria$event->getContext())->map(function (PropertyGroupEntity $property) {
  91.                 return [
  92.                     'id' => $property->getId(),
  93.                     'label' => $property->getName()
  94.                 ];
  95.             });
  96.             $event->getPage()->addArrayExtension('assortmentProperties'$properties);
  97.         }
  98.     }
  99.     /**
  100.      * @param WishlistPageLoadedEvent $event
  101.      */
  102.     public function onWishlistPageLoaded(WishlistPageLoadedEvent $event): void
  103.     {
  104.         if (!$event->getSalesChannelContext()->getCustomer()) {
  105.             return;
  106.         }
  107.         $sortedItems $this->wishlistService->getSortedWishlistItems($event->getSalesChannelContext()->getCustomerId(), $event->getContext());
  108.         $sortedIds array_values($sortedItems->map(function (WishlistItemEntity $item) {
  109.             return $item->getLineId();
  110.         }));
  111.         $sortedNotes $event->getPage()->getActiveList()->getNotes();
  112.         $sortedNotes->sort(function (WishlistNoteEntity $aWishlistNoteEntity $b) use ($sortedIds) {
  113.             $positionA array_search($a->getId(), $sortedIds) ?: 0;
  114.             $positionB array_search($b->getId(), $sortedIds) ?: 0;
  115.             return $positionA <=> $positionB;
  116.         });
  117.     }
  118. }