src/EventListener/PresseBundleListener.php line 23

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by Elements.at New Media Solutions GmbH
  4.  *
  5.  */
  6. namespace App\EventListener;
  7. use Carbon\Carbon;
  8. use Pimcore\Http\Request\Resolver\DocumentResolver;
  9. use Pimcore\Model\DataObject\PresseBundleText;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\EventDispatcher\GenericEvent;
  12. use Symfony\Component\HttpFoundation\RequestStack;
  13. class PresseBundleListener implements EventSubscriberInterface
  14. {
  15.     public function __construct(private readonly RequestStack $requestStack, private readonly DocumentResolver $documentResolver)
  16.     {
  17.     }
  18.     public function onPresseTexteListing(GenericEvent $event): void
  19.     {
  20.         $request $this->requestStack->getCurrentRequest();
  21.         $document $this->documentResolver->getDocument();
  22.         $presseTexteListing $event->getArguments()['presseTexteListing'];
  23.         if ($presseTexteListing instanceof PresseBundleText\Listing) {
  24.             if($document && $document->getProperty('resort') != null && !$request->get('resort')){
  25.                 $presseTexteListing->addConditionParam("resorts LIKE :resort", ['resort' => '%' $document->getProperty('resort') . '%']);
  26.             }
  27.             if ($request->get('resort') && $request->get('resort') != 'all') {
  28.                 $presseTexteListing->addConditionParam('resorts LIKE "%' $request->get('resort') . '%"');
  29.             }
  30.             if ($request->get('keyword')) {
  31.                 $presseTexteListing->addConditionParam('subHeadline LIKE :keyword OR name LIKE :keyword OR shortDescription LIKE :keyword', ['keyword' => '%' $request->get('keyword') . '%']);
  32.             }
  33.             $currentDate Carbon::now()->timestamp;
  34.             $presseTexteListing->addConditionParam('showFrom <= :currentDate OR showFrom IS NULL', ['currentDate' => $currentDate]);
  35.             $currentDatePlusOneDay Carbon::now()->subDay()->timestamp;
  36.             $presseTexteListing->addConditionParam('showTo >= :currentDatePlusOneDay OR showTo IS NULL', ['currentDatePlusOneDay' => $currentDatePlusOneDay]);
  37.             $presseTexteListing->setOrderKey('publishDate');
  38.             $presseTexteListing->setOrder('DESC');
  39.         }
  40.     }
  41.     public static function getSubscribedEvents(): array
  42.     {
  43.         return [
  44.             'presseTexteListing' => 'onPresseTexteListing',
  45.         ];
  46.     }
  47. }