custom/plugins/HelmaTheme/src/Subscriber/OnDocumentTemplateRendererParameterEvent.php line 109

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace HelmaTheme\Subscriber;
  4. use Doctrine\DBAL\Connection;
  5. use Shopware\Core\Checkout\Cart\Cart;
  6. use Shopware\Core\Checkout\Cart\Event\CartSavedEvent;
  7. use Shopware\Core\Checkout\Cart\Error\ErrorCollection;
  8. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  9. use Shopware\Core\Checkout\Cart\LineItem\LineItemCollection;
  10. use Shopware\Core\Checkout\Customer\Event\CustomerBeforeLoginEvent;
  11. use Shopware\Core\Checkout\Document\Event\DocumentTemplateRendererParameterEvent;
  12. use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemCollection;
  13. use Shopware\Core\Content\MailTemplate\Service\Event\MailBeforeSentEvent;
  14. use Shopware\Core\Content\Property\PropertyGroupCollection;
  15. use Shopware\Core\Content\Property\PropertyGroupEntity;
  16. use Shopware\Core\Framework\Context;
  17. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  18. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  19. use Shopware\Core\PlatformRequest;
  20. use Shopware\Storefront\Event\StorefrontRenderEvent;
  21. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  22. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  23. use Shopware\Core\Content\Property\Aggregate\PropertyGroupOption\PropertyGroupOptionCollection;
  24. use Shopware\Core\Content\Property\Aggregate\PropertyGroupOption\PropertyGroupOptionEntity;
  25. use Symfony\Component\DependencyInjection\Container;
  26. use Shopware\Core\Checkout\Customer\CustomerCollection;
  27. use Shopware\Core\Checkout\Customer\CustomerEntity;
  28. use Symfony\Component\HttpFoundation\RedirectResponse;
  29. use Symfony\Component\HttpKernel\Event\RequestEvent;
  30. use Symfony\Component\HttpKernel\KernelEvents;
  31. use Twig\Environment;
  32. use Shopware\Core\Content\Media\MediaEntity;
  33. use Shopware\Core\Content\Media\MediaCollection;
  34. use Shopware\Storefront\Framework\Cache\CacheResponseSubscriber;
  35. use Shopware\Storefront\Event\RouteRequest\RouteRequestEvent;
  36. use Symfony\Component\HttpKernel\Event\KernelEvent;
  37. use Dompdf\Dompdf;
  38. use Dompdf\Options;
  39. use Symfony\Component\Validator\Constraints\Length;
  40. class OnDocumentTemplateRendererParameterEvent implements EventSubscriberInterface
  41. {
  42.     /**
  43.      * @var EntityRepositoryInterface
  44.      */
  45.     private $propertyRepository;
  46.     /**
  47.      * @var EntityRepositoryInterface
  48.      */
  49.     private $groupRepository;
  50.     private $container;
  51.     /**
  52.      * @var EntityRepositoryInterface
  53.      */
  54.     private $customerRepository;
  55.     /**
  56.      * @var Environment
  57.      */
  58.     private $twig;
  59.     /**
  60.      * @var EntityRepositoryInterface
  61.      */
  62.     private $mediaRepository;
  63.      /**
  64.      * @var SystemConfigService
  65.      */
  66.     private $systemConfigService;
  67.     public function __construct(
  68.         EntityRepositoryInterface $propertyRepository,
  69.         EntityRepositoryInterface $groupRepository,
  70.         Container $container,
  71.         EntityRepositoryInterface $customerRepository,
  72.         Environment $environment,
  73.         EntityRepositoryInterface $mediaRepository
  74.     ) {
  75.         $this->propertyRepository $propertyRepository;
  76.         $this->groupRepository $groupRepository;
  77.         $this->container $container;
  78.         $this->customerRepository $customerRepository;
  79.         $this->twig $environment;
  80.         $this->mediaRepository $mediaRepository;
  81.     }
  82.     /**
  83.      * @return array<string, string>
  84.      */
  85.     public static function getSubscribedEvents(): array
  86.     {
  87.         // Return the events to listen to as array like this:  <event to listen to> => <method to execute>
  88.         return [
  89.             DocumentTemplateRendererParameterEvent::class => 'on_DocumentTemplateRendererParameterEvent'
  90.         ];
  91.     }
  92.     public function on_DocumentTemplateRendererParameterEvent(DocumentTemplateRendererParameterEvent $event)
  93.     {
  94.         $params $event->getParameters();
  95.         $newParam $params['order'];
  96.         $lineItems $newParam->getLineItems();
  97.         $newLineItems = [];
  98.         foreach ($lineItems as $lineItem) {
  99.             $mediaId $lineItem->getCoverId();
  100.             $criteria = new Criteria();
  101.             $criteria->addFilter(new EqualsFilter('id'$mediaId));
  102.             /** @var MediaCollection<MediaEntity> $images */
  103.             $images $this->mediaRepository->search($criteriaContext::createDefaultContext())->getEntities();
  104.             $mediaArray = [];
  105.             /** @var MediaEntity $media */
  106.             foreach ($images as $media) {
  107.                 $mediaArray[] = $media;
  108.             }
  109.             if ($mediaArray && $mediaArray[0]) {
  110.                 $lineItem->setCoverId($mediaArray[0]->getUrl());
  111.             }
  112.             $newLineItems[] = $lineItem;
  113.         }
  114.         usort($newLineItems, array($this"sort_objects_by_position"));
  115.         $newOrder = new OrderLineItemCollection();
  116.         foreach ($newLineItems as $newLineItem) {
  117.             $newOrder->add($newLineItem);
  118.         }
  119.         $newParam->setLineItems($newOrder);
  120.         $params['order'] = $newParam;
  121.         return $this->twig->render('@Framework/documents/invoice.html.twig'$params);
  122.     }
  123.     private function sort_objects_by_position($a$b)
  124.     {
  125.         if ($a->getPosition() == $b->getPosition()) {
  126.             return 0;
  127.         }
  128.         return ($a->getPosition() < $b->getPosition()) ? -1;
  129.     }
  130. }