src/Elements/Bundle/DemiFrontendBundle/Controller/EventsController.php line 297

Open in your IDE?
  1. <?php
  2. /**
  3.  * Elements DeMI
  4.  *
  5.  * This source file is available under the elements DeMI license version 1
  6.  *
  7.  *  @copyright  Copyright (c) elements.at New Media Solutions GmbH (https://www.elements.at/)
  8.  *  @license    elements DeMI Lizenz Version 1 (https://www.elements.at/de/demi-lizenz)
  9.  */
  10. namespace Elements\Bundle\DemiFrontendBundle\Controller;
  11. use Carbon\Carbon;
  12. use DateTime;
  13. use Elements\Bundle\CmsToolsBundle\Tool\Helper\ElementsCustomDateFormat;
  14. use Elements\Bundle\DemiFrontendBundle\Service\DemiUrl;
  15. use Elements\Demi\Frontend\Service\Configuration;
  16. use Elements\Bundle\DemiFrontendBundle\Service\ConnectedEntryProvider;
  17. use Elements\Demi\Frontend\Service\EventSearchFrontend;
  18. use Elements\Demi\Frontend\Service\EventsHelper;
  19. use Elements\Bundle\DemiFrontendBundle\Service\PaginatorHelper;
  20. use Elements\Demi\Helper;
  21. use Elements\Demi\Model\District;
  22. use Elements\Demi\Model\Event;
  23. use Elements\Demi\Model\Region;
  24. use Elements\Demi\Model\Town;
  25. use Pimcore\Model\DataObject;
  26. use Pimcore\Model\DataObject\AbstractObject;
  27. use Pimcore\Model\DataObject\DemiFacilityGroup;
  28. use Pimcore\Model\DataObject\DemiFilterObject;
  29. use Pimcore\Model\DataObject\DemiSerialEvent;
  30. use Pimcore\Model\DataObject\Fieldcollection;
  31. use Pimcore\Model\Document;
  32. use Pimcore\Model\Document\Editable\Areablock;
  33. use Symfony\Component\HttpFoundation\Request;
  34. use Symfony\Component\HttpFoundation\Response;
  35. use Symfony\Component\HttpKernel\Exception\GoneHttpException;
  36. use Symfony\Component\Routing\Annotation\Route;
  37. class EventsController extends AbstractDemiController
  38. {
  39.     protected bool $isArchive false;
  40.     public function __construct(protected EventSearchFrontend $searchParams,
  41.                                 protected EventsHelper $eventsHelper,
  42.                                 protected ConnectedEntryProvider $connectedEntryProvider,
  43.                                 ElementsCustomDateFormat $elementsCustomDateFormat,
  44.                                 protected PaginatorHelper $paginatorHelper,
  45.                                 Configuration $configuration,
  46.                                 protected DemiUrl $demiUrl)
  47.     {
  48.         $this->setConfiguration($configuration);
  49.         $this->setElementsCustomDateFormat($elementsCustomDateFormat);
  50.     }
  51.     /**
  52.      * @Route ("{path}/eventlist")
  53.      */
  54.     public function overviewAction(Request $request): Response
  55.     {
  56.         $this->addResponseHeader('X-Robots-Tag''noindex,nofollow');
  57.         $viewParams['isAjax'] = $request->isXmlHttpRequest();
  58.         $editablePrefix $this->findEditablePrefix($this->document);
  59.         $this->setParams($request$editablePrefix);
  60.         $this->searchParams->setLocale($request->getLocale());
  61.         $this->eventsHelper->applyCustomFilters($this->searchParams$request);
  62.         $this->eventsHelper->applyCustomSorting($this->searchParams$request);
  63.         $this->searchParams->setVisibility($this->document->getEditable($editablePrefix 'event-visibility')?->getData() ?? [] );
  64.         $searchParams $this->searchParams->getObjectListing();
  65.         $editable $this->document->getEditable($editablePrefix 'perPage');
  66.         $perPage $editable $editable->getData() : 12;
  67.         $editable $this->document->getEditable($editablePrefix.'layout');
  68.         $predefinedTemplate $editable $editable->getData() : 'undefined';
  69.         $fallbackLayout $this->configuration->getEventsLayout();
  70.         $useTemplate null;
  71.         if ($predefinedTemplate !== 'undefined') {
  72.             $useTemplate $predefinedTemplate;
  73.         }
  74.         if ($useTemplate === null) {
  75.             $useTemplate $fallbackLayout;
  76.         }
  77.         if ($request->get('nopaging')) {
  78.             $paginator $this->paginatorHelper->getPaginator($searchParams, -1);
  79.             $paginator->setItemNumberPerPage(-1);
  80.         } else {
  81.             $paginator $this->paginatorHelper->getPaginator($searchParams$perPage, (int)$request->get('page',1));
  82.         }
  83.         $viewParams["paginator"] = $paginator;
  84.         $viewParams['eventNoResultText'] = $this->configuration->getEventNoResultText($request->getLocale());
  85.         $viewParams['teaserTemplate'] = "@ElementsDemiFrontend/Events/includes/teaser-$useTemplate.html.twig";
  86.         $viewParams['fallbackLayout'] = $fallbackLayout;
  87.         $viewParams['useTemplate'] = $useTemplate;
  88.         $viewParams['isTeaserGrid'] = false;
  89.         if ($request->headers->get('format') === 'json') {
  90.             $response = [
  91.                 'status' => 'success',
  92.                 'html' => $this->renderTemplate('@ElementsDemiFrontend/Events/event-result.html.twig'$viewParams)->getContent()
  93.             ];
  94.             return $this->json($response);
  95.         }
  96.         return $this->renderTemplate('@ElementsDemiFrontend/Events/event-result.html.twig'$viewParams);
  97.     }
  98.     protected function setParams(Request $requeststring $editablePrefix): void
  99.     {
  100.         $propagateRecurrent $this->document->getEditable($editablePrefix'propagateRecurrent');
  101.         $this->searchParams->setPropagateRecurrent(!empty($propagateRecurrent) && !$propagateRecurrent->isEmpty() && $propagateRecurrent->isChecked());
  102.         $editable $this->document->getEditable($editablePrefix'regions');
  103.         $predRegions $editable $editable->getData() : null;
  104.         $reqregions $request->get('region');
  105.         if (Helper::checkIfParamIsEmpty($reqregions)) {
  106.             $regions = [];
  107.             if (is_array($reqregions)) {
  108.                 foreach ($reqregions as $region) {
  109.                     $obj AbstractObject::getById($region);
  110.                     if ($obj instanceof DemiFilterObject) {
  111.                         $regions array_merge($regions$obj->getFilterElements());
  112.                     } else {
  113.                         $regions[] = $region;
  114.                     }
  115.                 }
  116.             } else {
  117.                 $obj AbstractObject::getById($reqregions);
  118.                 if ($obj instanceof DemiFilterObject) {
  119.                     $regions array_merge($regions$obj->getFilterElements());
  120.                 } else {
  121.                     $regions[] = (int)$reqregions;
  122.                 }
  123.             }
  124.             $this->searchParams->setRegionsToTowns($regions);
  125.         } elseif (!empty($predRegions)) {
  126.             $townIds $districtIds = [];
  127.             foreach ($predRegions as $region) {
  128.                 if ($region instanceof DemiFilterObject) {
  129.                     foreach ($region->getFilterElements() as $filterElement) {
  130.                         if ($filterElement instanceof Region) {
  131.                             foreach ($filterElement->getChildren() as $town) {
  132.                                 $townIds[] = $town->getId();
  133.                             }
  134.                         } elseif ($filterElement instanceof Town) {
  135.                             $townIds[] = $filterElement->getId();
  136.                         } elseif ($filterElement instanceof District) {
  137.                             $districtIds[] = $filterElement->getId();
  138.                         }
  139.                     }
  140.                 } elseif ($region instanceof Region) {
  141.                     foreach ($region->getChildren() as $town) {
  142.                         $townIds[] = $town->getId();
  143.                     }
  144.                 } elseif ($region instanceof Town) {
  145.                     $townIds[] = $region->getId();
  146.                 } elseif ($region instanceof District) {
  147.                     $districtIds[] = $region->getId();
  148.                 }
  149.             }
  150.             $allRegionFilterIds array_unique(array_merge($townIds$districtIds));
  151.             $this->searchParams->setRegionsToTowns($allRegionFilterIds);
  152.         }
  153.         $editable $this->document->getEditable($editablePrefix'holidayThemes');
  154.         $predefinedHolidayThemes $editable $editable->getData() : null;
  155.         $reqThemes $request->get('holidaytheme');
  156.         if (!empty($reqThemes) && !empty(current($reqThemes))) {
  157.             $themes = [];
  158.             if (is_array($reqThemes)) {
  159.                 foreach ($reqThemes as $reqTheme) {
  160.                     $obj AbstractObject::getById($reqTheme);
  161.                     if ($obj instanceof DemiFilterObject) {
  162.                         $themes array_merge($themes$obj->getFilterElements());
  163.                     } else {
  164.                         $themes[] = $reqTheme;
  165.                     }
  166.                 }
  167.             } else {
  168.                 $obj AbstractObject::getById($reqThemes);
  169.                 if ($obj instanceof DemiFilterObject) {
  170.                     $themes array_merge($themes$obj->getFilterElements());
  171.                 } else {
  172.                     $themes[] = (int)$reqThemes;
  173.                 }
  174.             }
  175.             $this->searchParams->setHolidayThemes($themes);
  176.         } elseif (!empty($predefinedHolidayThemes)) {
  177.             $themes = [];
  178.             if (is_array($predefinedHolidayThemes)) {
  179.                 foreach ($predefinedHolidayThemes as $predTheme) {
  180.                     $obj AbstractObject::getById($predTheme);
  181.                     if ($obj instanceof DemiFilterObject) {
  182.                         $themes array_merge($themes$obj->getFilterElements());
  183.                     } else {
  184.                         $themes[] = $predTheme;
  185.                     }
  186.                 }
  187.             }
  188.             $this->searchParams->setHolidayThemes($themes);
  189.         }
  190.         $editable $this->document->getEditable($editablePrefix 'category');
  191.         $predefinedCategories $editable $editable->getData() : null;
  192.         $reqcats $request->get('category');
  193.         if (Helper::checkIfParamIsEmpty($reqcats)) {
  194.             if (!is_array($reqcats)) {
  195.                 $catobj AbstractObject::getById($reqcats);
  196.                 if ($catobj instanceof DemiFacilityGroup) {
  197.                     $this->searchParams->setFacility([$request->get('category')]);
  198.                 } elseif ($catobj instanceof DemiSerialEvent) {
  199.                     $this->searchParams->addSerialEvent($reqcats);
  200.                 }
  201.             } else {
  202.                 $catobj AbstractObject::getById($reqcats[0]);
  203.                 if ($catobj instanceof DemiFacilityGroup) {
  204.                     $this->searchParams->setFacility($reqcats);
  205.                 } elseif ($catobj instanceof DemiSerialEvent) {
  206.                     $this->searchParams->addSerialEvent($reqcats);
  207.                 }
  208.             }
  209.         } elseif ($predefinedCategories) {
  210.             $catobj AbstractObject::getById($predefinedCategories[0]);
  211.             if ($catobj instanceof DemiFacilityGroup) {
  212.                 $this->searchParams->setFacility($predefinedCategories);
  213.             } elseif ($catobj instanceof DemiSerialEvent) {
  214.                 $this->searchParams->setSerialEvents($predefinedCategories);
  215.             }
  216.         }
  217.         //TODO: Add hidden Flag in Editmode for EventArchive
  218.         if ($request->get('archive')) {
  219.             $this->searchParams->setArchive(true);
  220.             $this->searchParams->setPropagateRecurrent(false);
  221.         } else {
  222.             $datefrom = new Carbon();
  223.             if ($from $request->get('from')) {
  224.                 $this->searchParams->setFromDate($from);
  225.             } else {
  226.                 $this->searchParams->setFromDate($datefrom->toIso8601String());
  227.             }
  228.             $predefinedRangeTo $this->document->getEditable($editablePrefix'rangeTo');
  229.             if ($to $request->get('to')) {
  230.                 $this->searchParams->setToDate($to);
  231.             } elseif ($predefinedRangeTo instanceof Document\Editable\Select && !$predefinedRangeTo->isEmpty()) {
  232.                 $to $this->searchParams->getFromDate()->clone();
  233.                 $to = match ($predefinedRangeTo->getData()) {
  234.                     'oneweek' => $to->addWeek(),
  235.                     'onemonth' => $to->addMonth(),
  236.                     'threemonths' => $to->addMonths(3),
  237.                     'sixmonths' => $to->addMonths(6),
  238.                     default => $to->addYear(),
  239.                 };
  240.                 $dateEndString $this->elementsCustomDateFormat->dateToString($to);
  241.                 $this->searchParams->setToDate($dateEndString);
  242.             } else {
  243.                 $datefrom = new Carbon();
  244.                 $to $datefrom->copy()->addYear();
  245.                 $this->searchParams->setToDate($to->toIso8601String());
  246.             }
  247.         }
  248.         if ($this->configuration->getContentScoreFilterEvent() > 0) {
  249.             $this->searchParams->setContentScore($this->configuration->getContentScoreFilterEvent());
  250.         }
  251.         $predefinedTopEventsOnly $this->document->getEditable($editablePrefix.'top-events-only');
  252.         $predefinedTopEventsUpfront $this->document->getEditable($editablePrefix.'put-topevents-upfront');
  253.         if ($topEvents $request->get('top-events')) {
  254.             $this->searchParams->setTopEventsOnly($topEvents);
  255.         } elseif ($predefinedTopEventsOnly instanceof Document\Editable\Checkbox && $predefinedTopEventsOnly->isChecked()) {
  256.             $this->searchParams->setTopEventsOnly(true);
  257.         }
  258.         if ($predefinedTopEventsUpfront instanceof Document\Editable\Checkbox && $predefinedTopEventsUpfront->isChecked()) {
  259.             $this->searchParams->setTopEventsUpfront(true);
  260.         }
  261.         if ($keyword $request->get('keyword')) {
  262.             $this->searchParams->setFullText($keyword);
  263.         }
  264.     }
  265.     public function detailAction(Request $request) : Response
  266.     {
  267.         $object Event::getById((int)($request->get('id')));
  268.         if (!$object) {
  269.             throw new GoneHttpException("the requested object doesn't exist anymore");
  270.         }
  271.         if($object && !$object->isPublished() && $object->getProperty('feratelId')){
  272.             return $this->forward(self::class. '::redirectViaFeratelIdProperty', [
  273.                 'request' => $request,
  274.                 'object' => $object,
  275.                 'demiUrl' => $this->demiUrl
  276.             ]);
  277.         } elseif ($object && !$object->isPublished() &&  $this->document->getProperty('demi_config') && $this->document->getProperty('demi_config')->getFullPath() == "/deskline/config/Graz"){
  278.             //redirect to event overview - only for Graz
  279.             return $this->redirect($this->configuration->getEventDetailPath(), 301);
  280.         }
  281.         if (!isset($_COOKIE['pimcore_admin_sid']) && !$this->editmode && !$object->isPublished() && !$request->get('pimcore_object_preview')) {
  282.             return $this->forward(self::class. '::unavailableAction', [
  283.                 'request' => $request,
  284.                 'unavailableType' => 'event'
  285.             ]);
  286.         }
  287.         if ($object->getDateEnd()) {
  288.             $endDate = (Carbon::createFromTimestamp($object->getDateEnd()->getTimestamp())->endOfDay());
  289.             $this->addResponseHeader('X-Robots-Tag''unavailable_after: ' $endDate);
  290.         } elseif ($object->getRecurringDates() instanceof Fieldcollection) {
  291.             $endDate null;
  292.             foreach ($object->getRecurringDates()->getItems() as $dates) {
  293.                 if (!$endDate instanceof Carbon || $endDate->lt($dates->getDateTo())) {
  294.                     $endDate $dates->getDateTo();
  295.                 }
  296.             }
  297.             if ($endDate) {
  298.                 $endDateString = (Carbon::createFromTimestamp($endDate->getTimestamp())->endOfDay())->toRfc850String();
  299.                 $this->addResponseHeader('X-Robots-Tag''unavailable_after: ' $endDateString);
  300.             }
  301.         }
  302.         return $this->renderTemplate("@ElementsDemiFrontend/Events/detail.html.twig", [
  303.             'event' => $object,
  304.             'connectedEntries' => $this->connectedEntryProvider->getConnectedEntriesForEvent($object$request),
  305.             'endDate' => $endDate ?? null,
  306.         ]);
  307.     }
  308.     protected function findEditablePrefix(Document $document): string
  309.     {
  310.         if ($document->getEditables() !== null) {
  311.             $areaBlock current(array_filter($document->getEditables(), static function ($editable) {
  312.                 return $editable instanceof Areablock;
  313.             }));
  314.             if ($areaBlock !== null && !empty($areaBlock->getIndices())) {
  315.                 $correctIndex array_filter($areaBlock->getIndices(), function ($index) {
  316.                     if ($index['type'] === 'demi-event-archive') {
  317.                         $this->isArchive true;
  318.                         return true;
  319.                     }
  320.                     return $index['type'] === 'demi-event-list' ||
  321.                         $index['type'] === 'demi-event-teaser-grid';
  322.                 });
  323.                 return $areaBlock->getName() . ':' current($correctIndex)['key'] . '.';
  324.             }
  325.         }
  326.         // Fallback for overriden documents
  327.         if ($document->getContentMasterDocument() !== null) {
  328.             return $this->findEditablePrefix($document->getContentMasterDocument());
  329.         }
  330.         return '';
  331.     }
  332. }