<?php
/**
* Created by Elements.at New Media Solutions GmbH
*
*/
namespace App\EventListener;
use Carbon\Carbon;
use Pimcore\Http\Request\Resolver\DocumentResolver;
use Pimcore\Model\DataObject\PresseBundleText;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use Symfony\Component\HttpFoundation\RequestStack;
class PresseBundleListener implements EventSubscriberInterface
{
public function __construct(private readonly RequestStack $requestStack, private readonly DocumentResolver $documentResolver)
{
}
public function onPresseTexteListing(GenericEvent $event): void
{
$request = $this->requestStack->getCurrentRequest();
$document = $this->documentResolver->getDocument();
$presseTexteListing = $event->getArguments()['presseTexteListing'];
if ($presseTexteListing instanceof PresseBundleText\Listing) {
if($document && $document->getProperty('resort') != null && !$request->get('resort')){
$presseTexteListing->addConditionParam("resorts LIKE :resort", ['resort' => '%' . $document->getProperty('resort') . '%']);
}
if ($request->get('resort') && $request->get('resort') != 'all') {
$presseTexteListing->addConditionParam('resorts LIKE "%' . $request->get('resort') . '%"');
}
if ($request->get('keyword')) {
$presseTexteListing->addConditionParam('subHeadline LIKE :keyword OR name LIKE :keyword OR shortDescription LIKE :keyword', ['keyword' => '%' . $request->get('keyword') . '%']);
}
$currentDate = Carbon::now()->timestamp;
$presseTexteListing->addConditionParam('showFrom <= :currentDate OR showFrom IS NULL', ['currentDate' => $currentDate]);
$currentDatePlusOneDay = Carbon::now()->subDay()->timestamp;
$presseTexteListing->addConditionParam('showTo >= :currentDatePlusOneDay OR showTo IS NULL', ['currentDatePlusOneDay' => $currentDatePlusOneDay]);
$presseTexteListing->setOrderKey('publishDate');
$presseTexteListing->setOrder('DESC');
}
}
public static function getSubscribedEvents(): array
{
return [
'presseTexteListing' => 'onPresseTexteListing',
];
}
}