vendor/marketingfactory/destisuite/src/DestiSuiteBundle/Controller/ExperienceController.php line 270

Open in your IDE?
  1. <?php
  2. namespace DestiSuiteBundle\Controller;
  3. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  4. use Pimcore\Controller\FrontendController;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  8. use DestiSuiteBundle\Controller\BaseController;
  9. use Knp\Component\Pager\Paginator;
  10. use DestiSuiteBundle\Model\DataObject\Experience;
  11. use DestiSuiteBundle\Model\DataObject\ExperienceCategory;
  12. use Pimcore\Model\DataObject\Location;
  13. use DestiSuiteBundle\Repository\ExperienceRepository;
  14. use DestiSuiteBundle\Repository\ExperienceCategoryRepository;
  15. class ExperienceController extends BaseController
  16. {
  17.     
  18.     /**
  19.      * @Template
  20.      * @param Request $request
  21.      * @return array
  22.      */
  23.     public function indexAction(Request $request)
  24.     {
  25.         $multisiteOwner $this->document->getProperty('multisiteOwner');
  26.         
  27.         $view $request->get('view');
  28.         $locale $request->get('_locale');
  29.         
  30.         $locationList ExperienceRepository::getUsedLocations($locale$multisiteOwnertrue);
  31.         
  32.         $redirect $this->checkFilterRequestedNotAvailable($request$locationList);
  33.         if (!empty($redirect)) {
  34.             return $redirect;
  35.         }
  36.         $dataFrom null;
  37.         $dataTo null;
  38.         $explicitDateSearch false;
  39.         if ($dateTmp $request->get('date')) {
  40.             $dateTmp explode('-'$dateTmp);
  41.             $since null;
  42.             $until null;
  43.             if (!empty($dateTmp[0]) && !empty($dateTmp[1])) {
  44.                 $since explode('.'trim($dateTmp[0]));
  45.                 $until explode('.'trim($dateTmp[1]));
  46.             }
  47.             if (!empty($since) && count($since) == && !empty($until) && count($until) == 3) {
  48.                 $dataFrom = new \DateTime($since[2].'-'.$since[1].'-'.$since[0]);
  49.                 $dataTo = new \DateTime($until[2].'-'.$until[1].'-'.$until[0]);
  50.                 $explicitDateSearch true;
  51.             }
  52.         }
  53.         // datepicker value
  54.         if (empty($dataFrom) || empty($dataTo)) {
  55.             /*$dataFrom = new \DateTime();
  56.             $dataTo = new \DateTime();
  57.             $dataTo->add(new \DateInterval('P7D'));
  58.             
  59.             $dataFrom->setTime(0,0,0);
  60.             $dataTo->setTime(0,0,0);*/
  61.             $date '';
  62.         } else {
  63.             $date $dataFrom->format('d.m.Y') . ' - ' $dataTo->format('d.m.Y');
  64.         }
  65.         
  66.         $paginator null;
  67.         $markerDatas = array();
  68.         
  69.         $locations = array();
  70.         if ($locationIds $request->get('locations')) {
  71.             foreach ($locationIds as $locationId) {
  72.                 $location Location::getById($locationId);
  73.                 if (!empty($location)) {
  74.                     $locations[] = $location;
  75.                     if (!empty($location->getChildren())) {
  76.                         foreach ($location->getChildren() as $c) {
  77.                             $locations[] = $c;
  78.                         }
  79.                     }
  80.                 }
  81.             }
  82.         }
  83.         
  84.         $categories = array();
  85.         $categoryIds = array();
  86.         $searchConsideringChildren false;
  87.         if (!empty($request->get('subcategories'))) {
  88.             $categoryIds array_merge($categoryIds$request->get('subcategories'));
  89.             $searchConsideringChildren true;
  90.         } else if (!empty($request->get('categories'))) {
  91.             $categoryIds array_merge($categoryIds$request->get('categories'));
  92.             $searchConsideringChildren true;
  93.         }
  94.         
  95.         foreach ($categoryIds as $categoryId) {
  96.             $category ExperienceCategory::getById($categoryId);
  97.             if (!empty($category)) {
  98.                 $categories[] = $category;
  99.                 
  100.                 if ($searchConsideringChildren) {
  101.                     foreach($category->getSubcategories() as $c) {
  102.                         $categories[] = $c;
  103.                     }
  104.                 }
  105.             }
  106.         }
  107.         
  108.         
  109.         $filters = array(
  110.             'categories' => $categories,
  111.             'locations' => $locations,
  112.             'dataFrom' => $dataFrom,
  113.             'dataTo' => $dataTo,
  114.             'name' => $request->get('name'),
  115.         );
  116.         $includeVisibleAfterExpiration true;
  117.         if ($explicitDateSearch) {
  118.             $includeVisibleAfterExpiration false// when explicit period searched, do not include expired entities
  119.         }
  120.         
  121.         if ($view == 'map') {
  122.             $eventIds ExperienceRepository::getNextEntities($request->get('_locale'), $multisiteOwner$filtersnulltruefalse$includeVisibleAfterExpiration);
  123.             $markerDatas ExperienceRepository::getMapMarkerData($request->get('_locale'), $eventIds);
  124.         } else {
  125. /*
  126.             $lifetime = 60 * 60;
  127.             $cacheKey = md5('experience_listing_seed');
  128.             if(!$randomSeed = \Pimcore\Cache::load($cacheKey)) {
  129.                 $randomSeed = time();
  130.                 \Pimcore\Cache::save(
  131.                     $randomSeed,
  132.                     $cacheKey,
  133.                     ["seed"],
  134.                     $lifetime
  135.                 );
  136.             }
  137. */
  138.             $entityList ExperienceRepository::getNextEntities($request->get('_locale'), $multisiteOwner$filtersnullfalsefalse$includeVisibleAfterExpiration);
  139.             
  140.             $paginator \Pimcore::getContainer()->get(\Knp\Component\Pager\PaginatorInterface::class);
  141.             $paginator $paginator->paginate// TODO GF -- PASS LIST OBJ, to implement real pagination 
  142.                 $entityList,
  143.                 $request->get('page'1),
  144.                 9
  145.             );
  146.         }
  147.         
  148.         return [
  149.             'isSubmitted' => !empty($request->getQueryString()),
  150.             
  151.             'locationList' => $locationList,
  152.             // 'categoryList' => $categoryList,
  153.             'experienceList' => $paginator,
  154.             
  155.             'date' => $date,
  156.             'viewType' => $view,
  157.             'markerDatas' => $markerDatas
  158.         ];
  159.     }
  160.     
  161.     private function checkFilterRequestedNotAvailable($request$locationList)
  162.     {
  163.         $valid true;
  164.         
  165.         if ($locationIds $request->get('locations')) {
  166.             foreach ($locationIds as $locationId) {
  167.                 $found false;
  168.                 foreach ($locationList as $locationObj) {
  169.                     if ($locationObj->getId() == $locationId) {
  170.                         $found true;
  171.                     }
  172.                 }
  173.                 if (!$found) {
  174.                     $valid false;
  175.                 }
  176.             }
  177.         }
  178.         
  179.         $categoryList ExperienceCategoryRepository::getUsedCategories($request->get('_locale'), $this->document->getProperty('multisiteOwner'));
  180.         $categoryIds = array();
  181.         if (!empty($request->get('subcategories'))) {
  182.             $categoryIds array_merge($categoryIds$request->get('subcategories'));
  183.         } else if (!empty($request->get('categories'))) {
  184.             $categoryIds array_merge($categoryIds$request->get('categories'));
  185.         }
  186.         
  187.         if ($categoryIds) {
  188.             foreach ($categoryIds as $categoryId) {
  189.                 $found false;
  190.                 foreach ($categoryList as $categoryObj) {
  191.                     if ($categoryObj->getId() == $categoryId) {
  192.                         $found true;
  193.                     }
  194.                 }
  195.                 if (!$found) {
  196.                     $valid false;
  197.                 }
  198.             }
  199.         }
  200.         
  201.         if (!$valid) {
  202.             $url null;
  203.             if ($this->document->getController() == 'DestiSuiteBundle\Controller\Experience::indexAction') {
  204.                 $url $this->document->getFullPath();
  205.             }
  206.             if (!empty($url)) {
  207.                 return $this->redirect($url302);
  208.             }
  209.         }
  210.     }
  211.     
  212.     /**
  213.      * @Template
  214.      * @Route("/{_locale}/ajax-experience", name="experience-ajax-detail")
  215.      */
  216.     public function ajaxdetailAction(Request $request)
  217.     {
  218.         $translateUrls = array();
  219.         
  220.         foreach (\Pimcore\Tool::getValidLanguages() as $lang) {
  221.             $translateUrls[$lang] = $this->get('router')->generate('experience-ajax-detail', ['_locale' => $lang]);
  222.         }
  223.         
  224.         return [
  225.             'bodyClass' => 'ds-body-obj-detail',
  226.             'translateUrls' => $translateUrls
  227.         ];
  228.     }
  229.     
  230.     /**
  231.      * @Route("/{_locale}/ajax-experience-url/{id}", name="experience-ajax-detail-url")
  232.      */
  233.     public function ajaxdetailurlAction(Request $request)
  234.     {
  235.         $entity Experience::getById($request->get('id'));
  236.         if (empty($entity)) {
  237.             throw new NotFoundHttpException('Not found.');
  238.         }
  239.         
  240.         $url \Pimcore::getContainer()->get('DestiSuiteBundle\LinkGenerator\ExperienceLinkGenerator')->generate($entity);
  241.         $languageSwitch \Pimcore::getContainer()->get('DestiSuiteBundle\LinkGenerator\ExperienceLinkGenerator')->languageSwitchUrls($entity);
  242.         
  243.         $url $this->addDsBasicAuth([$url])[0];
  244.         
  245.         return $this->json([
  246.             'url' => $url,
  247.             'languageSwitch' => $languageSwitch,
  248.             'url_relative' => \Pimcore::getContainer()->get('DestiSuiteBundle\LinkGenerator\ExperienceLinkGenerator')->generate($entity, [], true),
  249.             'title' => !empty($entity->getMetaTitle()) ? $entity->getMetaTitle() : $entity->getTitle(),
  250.         ]);
  251.     }
  252.     
  253.     
  254.     /**
  255.      * @Template
  256.      * @Route("/{_locale}/experience/{slug}_{id}", name="experience-detail")
  257.      */
  258.     public function detailAction(Request $request)
  259.     {
  260.         $this->addResponseHeader('Access-Control-Allow-Origin''*'false$request);
  261.         $this->addResponseHeader('Access-Control-Allow-Headers'"origin, x-requested-with, content-type, authorization"false$request);
  262.         // $this->addResponseHeader('Access-Control-Allow-Methods', "GET,POST,OPTIONS,DELETE,PUT", false, $request);
  263.         
  264.         $multisiteOwner $this->document->getProperty('multisiteOwner');
  265.         
  266.         $experience Experience::getById($request->get('id'));
  267.         if (empty($experience)) {
  268.             throw new NotFoundHttpException('Experience not found.');
  269.         }
  270.         
  271.         if (empty($experience->isVisible($multisiteOwner)) && empty($this->verifyPreviewRequest($request$experience))) {
  272.             throw new NotFoundHttpException('Experience not found.');
  273.         }
  274.         
  275.         if ($this->verifyPreviewRequest($request$experience)) {
  276.             $this->setObjPreviewLocale($experience);
  277.         }
  278.         
  279.         $correlatedEntities $experience->getCorrelatedEvents($request->get('_locale'), $multisiteOwner);
  280.                 
  281.         return [
  282.             'bodyClass' => 'ds-body-obj-detail',
  283.             'experience' => $experience,
  284.             'translateUrls' => \Pimcore::getContainer()->get('DestiSuiteBundle\LinkGenerator\ExperienceLinkGenerator')->languageSwitchUrls($experience),
  285.             'correlatedEntities' => $correlatedEntities,
  286.             'bundleType' => Experience::getDs_bundle_type()
  287.         ];
  288.     }
  289.     
  290.     /**
  291.      * @Template
  292.      * @Route("/{_locale}/map_popup/experience/{id}", name="experience-map-popup")
  293.      */
  294.     public function mappopupAction(Request $request$id)
  295.     {   
  296.         $entity Experience::getById($id);
  297.         
  298.         return [
  299.             'entity' => $entity
  300.         ];
  301.     }
  302.     
  303.     /**
  304.      * @Template
  305.      * @Route("/{_locale}/experience-cart", name="experience-cart")
  306.      */
  307.     public function cartAction(Request $request)
  308.     {   
  309.         
  310.         
  311.     }
  312.     
  313.     /**
  314.      * @Template
  315.      * @Route("/{_locale}/experience/testtosc5/{slug}_{id}", name="experience-detail-testtosc5")
  316.      */
  317.     public function testtosc5detailAction(Request $request)
  318.     {
  319.         $this->checkBasicAuth();
  320.         
  321.         $multisiteOwner $this->document->getProperty('multisiteOwner');
  322.         
  323.         $experience Experience::getById($request->get('id'));
  324.         if (empty($experience)) {
  325.             throw new NotFoundHttpException('Experience not found.');
  326.         }
  327.         
  328.         if (empty($experience->isVisible($multisiteOwner)) && empty($this->verifyPreviewRequest($request$experience))) {
  329.             throw new NotFoundHttpException('Experience not found.');
  330.         }
  331.         
  332.         if ($this->verifyPreviewRequest($request$experience)) {
  333.             $this->setObjPreviewLocale($experience);
  334.         }
  335.         
  336.         $correlatedEntities $experience->getCorrelatedEvents($request->get('_locale'), $multisiteOwner);
  337.                 
  338.         return [
  339.             'experience' => $experience,
  340.             'translateUrls' => \Pimcore::getContainer()->get('DestiSuiteBundle\LinkGenerator\ExperienceLinkGenerator')->languageSwitchUrls($experience),
  341.             'correlatedEntities' => $correlatedEntities,
  342.         ];
  343.     }
  344.     
  345.     /**
  346.      * @Template
  347.      * @Route("/{_locale}/testtosc5-experiences", name="experiences-testtosc5")
  348.      */
  349.     public function testtosc5landingAction(Request $request)
  350.     {
  351.         if (!$this->editmode) {
  352.             $this->checkBasicAuth();
  353.         }
  354.     }
  355.     
  356.     private function checkBasicAuth()
  357.     {
  358.         if (!isset($_SERVER['PHP_AUTH_USER']) || !($_SERVER['PHP_AUTH_USER'] == 'mf' && $_SERVER['PHP_AUTH_PW'] == 'dev')) {
  359.             header('WWW-Authenticate: Basic realm="My Realm"');
  360.             header('HTTP/1.0 401 Unauthorized');
  361.             echo 'Unauthorized';
  362.             exit;
  363.         }
  364.     }
  365. }