src/Controller/CommunityController.php line 38

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Service\CanonicalRedirectHelper;
  4. use App\Twig\LinkGenerator;
  5. use Knp\Component\Pager\PaginatorInterface;
  6. use Pimcore\Document\Editable\Exception\NotFoundException;
  7. use Pimcore\Model\DataObject\Community;
  8. use Pimcore\Model\DataObject\ConventionPartner;
  9. use Pimcore\Model\DataObject\DemiInfrastructure;
  10. use Pimcore\Model\DataObject\DemiInfrastructureTopic;
  11. use Pimcore\Model\DataObject\Region;
  12. use Pimcore\Model\WebsiteSetting;
  13. use Pimcore\Translation\Translator;
  14. use Symfony\Component\HttpFoundation\JsonResponse;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. use Elements\Bundle\CmsToolsBundle\Tool\Helper\FunctionsHelper;
  19. class CommunityController extends AbstractController
  20. {
  21.     /**
  22.      * @var LinkGenerator
  23.      */
  24.     private $linkGenerator;
  25.     public function __construct(  LinkGenerator $linkGenerator)
  26.     {
  27.         $this->linkGenerator $linkGenerator;
  28.     }
  29.     /**
  30.      *@Route("/{_locale}/community/overview", name="community_overview")
  31.      */
  32.     public function overviewAction(Request $requestPaginatorInterface $paginatorTranslator $translator)
  33.     {
  34.         $communityListing = new Community\Listing();
  35.         $notStyriaCommunity WebsiteSetting::getByName('notStyriaCommunityFolder')->getData();
  36.         if($this->getDocumentEditable('checkbox''hideRegionSelect')->isChecked()) {
  37.             $region $this->document->getProperty('region');
  38.             $communityListing->addConditionParam('o_path LIKE "%' $region->getFullPath() . '%"');
  39.         } else {
  40.             if($regionId $request->get('region')) {
  41.                 if($regionId ==  'otherRegion'){
  42.                     $communityListing->addConditionParam('o_path LIKE "%' $notStyriaCommunity->getFullPath() . '%"');
  43.                 }else {
  44.                     $region Region::getById($regionId);
  45.                     $communityListing->addConditionParam('o_path LIKE "%' $region->getFullPath() . '%"');
  46.                 }
  47.             } else { //exclude not styria communities
  48.                 $communityListing->addConditionParam('o_path not LIKE "%' $notStyriaCommunity->getFullPath() . '%"');
  49.             }
  50.         }
  51.         if($q $request->get('keyword')) {
  52.             $communityListing->addConditionParam('name like :q OR shortdescription like :q', ['q' => '%' $q '%']);
  53.         }
  54.         $communityListing->setOrderKey('nameLocalized');
  55.         $paginator $paginator->paginate($communityListing$request->get('page',1),12);
  56.         $paginator->setPageRange(3);
  57.         //map
  58.         if ($request->isXmlHttpRequest() && $request->get('pois'false)) {
  59.             return $this->getMapdata($paginator);
  60.         }
  61.         if($request->get('ajax') && $request->isXmlHttpRequest()) {
  62.             return $this->json([
  63.                 "success" => true,
  64. //                "html" => $this->renderView('Community/Includes/places-paging.html.twig', ['paginator' => $paginator])
  65.                 "content" => [
  66.                     'result-main' => $this->renderView('Community/Includes/places-list.html.twig', [
  67.                         'paginator' => $paginator,
  68.                     ]),
  69.                     'result-paging' => $this->renderView('Navigation/paging.html.twig', [
  70.                         'styleModifier' => 'justify-content-center',
  71.                         'ajaxClass' => 'js-ajax-form-map__link',
  72.                         'paginator' => $paginator
  73.                     ]),
  74.                 ],
  75.             ]);
  76.         }
  77.         $regionSelect $this->getRegionSelectData($regionId ?? null$translator);
  78.         return $this->renderTemplate('Community/overview.html.twig', [
  79.             'regionSelect' => $regionSelect,
  80.             'paginator' => $paginator,
  81.         ]);
  82.     }
  83.     public function detailAction(Request $requestTranslator $translatorCanonicalRedirectHelper $redirectHelper)
  84.     {
  85.         $community Community::getById($request->get('id',0));
  86.         if (!$community || (!isset($_COOKIE['pimcore_admin_sid']) && !$community->isPublished() && !$this->editmode && !$request->get('pimcore_object_preview'))) {
  87.             throw new NotFoundHttpException("The requested object doesn't exist anymore");
  88.         }
  89.         if (CanonicalRedirectHelper::ENABLE_CANONICAL_REDIRECT && $redirect $redirectHelper->canonicalRedirect($community)) {
  90.             return $redirect;
  91.         }
  92.         $demiInfrastructure $this->getDemiInfrastructureTeasers($community$translator);
  93.         return $this->renderTemplate('Community/detail.html.twig', [
  94.             'community' => $community,
  95.             'demiInfrastructure' => $demiInfrastructure
  96.         ]);
  97.     }
  98.     private function getRegionSelectData($location nullTranslator $translator null) {
  99.         $regions = new Region\Listing();
  100.         $regions->setOrderKey('sorting');
  101.         $regionsSelect = [[
  102.             "label" => "",
  103.             "value" =>  "",
  104.             "disabled" => true,
  105.             "selected" => !$location,
  106.             "class"=>  "sr-only"
  107.         ]];
  108.         foreach ($regions as $region) {
  109.             $regionsSelect[] = [
  110.                 "label" => $region->getName(),
  111.                 "value" => $region->getId(),
  112.                 "class" => "",
  113.                 "selected" => $location && $location == $region->getId(),
  114.             ];
  115.         }
  116.         $regionsSelect[] = [
  117.             "label" => $translator->trans('community.Gemeinden Außerhalb'),
  118.             "value" => 'otherRegion',
  119.             "class" => "",
  120.             "selected" => $location && $location == 'otherRegion',
  121.         ];
  122.         return $regionsSelect;
  123.     }
  124.     public function getMapData($paginator) {
  125.         $geoData = [];
  126.         foreach ($paginator as $community){
  127.             if($community) {
  128.                 $geo $community->getGeoposition();
  129.                 if($geo) {
  130.                     $geoData[] = [
  131.                         "id" => $community->getId(),
  132.                         "lat" => $geo->getLatitude(),
  133.                         "lng" => $geo->getLongitude(),
  134.                         "detailInfoBoxUrl" => $this->generateUrl('community-interactive-map-info-box',[
  135.                             'id' => $community->getId(),
  136.                         ])
  137.                     ];
  138.                 }
  139.             }
  140.         }
  141.         return new JsonResponse([
  142.             "success" => true,
  143.             "pois" => $geoData
  144.         ]);
  145.     }
  146.     /**
  147.      * @Route("/{_locale}/community-interactive-map-info-box", name="community-interactive-map-info-box")
  148.      */
  149.     public function getMapInfoBox(Request $requestLinkGenerator $linkGenerator) {
  150.         $community Community::getById($request->get('id'null));
  151.         $document $this->document;
  152.         $locale $document->getProperty('language') ?? $request->getLocale();
  153.         if($community) {
  154.             return $this->json([
  155.                 'success' => true,
  156.                 'html' => $this->renderView('Convention/Includes/interactive-map-info-box.html.twig', [
  157.                     'image' => $community->getTeaserimage(),
  158.                     'subtitle' => '',
  159.                     'title' => $community->getNameLocalized($locale) ?? $community->getName(),
  160.                     'wysiwyg' => $community->getShortdescription($locale),
  161.                     'link' => $linkGenerator->generate($community, ['document' => $document]),
  162.                     'id' => $request->get('id')
  163.                 ])
  164.             ]);
  165.         }
  166.     }
  167.     private function getDemiInfrastructureTeasers(Community $communityTranslator $translator$numOfTeaser 9) : array {
  168.         if($community->getDetailRelatedExcursions() && !empty($community->getDetailRelatedExcursions())) {
  169.             $demiInfrastructures $community->getDetailRelatedExcursions();
  170.         } else {
  171.             $demiInfrastructures DemiInfrastructure::getList();
  172.             $demiInfrastructures->addConditionParam('name is not null and name != ""');
  173.             if($community->getDetailRadiusExcursion()) {
  174.                 $functionsHelper = new FunctionsHelper();
  175.                 $distance $functionsHelper->getGeoDistanceQuery($community->getGeoposition(), 'position');
  176.                 $demiInfrastructures->addConditionParam('(' $distance ') <= :detailRadius', [ 'detailRadius' => $community->getDetailRadiusExcursion()]);
  177.             }
  178.             $topics = [];
  179.             if(($infraTypes $community->getDetailRadiusTypesExcursion()) && !empty($infraTypes)) {
  180.                 foreach($infraTypes as $type) {
  181.                     foreach($type->getChildren() as $child) {
  182.                         if($child instanceof DemiInfrastructureTopic) {
  183.                             $topics[] = $child->getId();
  184.                         }
  185.                     }
  186.                 }
  187.             }
  188.             if(($infraTopics $community->getDetailRadiusTopicsExcursion()) && !empty($infraTopics)) {
  189.                 foreach ($infraTopics as $topic) {
  190.                     $topics[] = $topic->getId();
  191.                 }
  192.             }
  193.             if(!empty($topics)) {
  194.                 $topics array_unique($topics);
  195.                 $subcondition = [];
  196.                 foreach ($topics as $topic) {
  197.                     $subcondition[] = 'topics LIKE "%,' $topic ',%"';
  198.                     $subcondition[] = 'subTopics LIKE "%,' $topic ',%"';
  199.                 }
  200.                 $demiInfrastructures->addConditionParam('(' implode(' OR '$subcondition) . ')');
  201.             }
  202. //            $demiInfrastructures->setOrderKey(["RAND()"]);
  203.             $demiInfrastructures->setOrderKey("RAND()"false);
  204.             $demiInfrastructures->setLimit(9);
  205.         }
  206.         //prepare img-teasers
  207.         $count 0;
  208.         $teaserdata = [];
  209.         foreach ($demiInfrastructures as $infra) {
  210.             $teaserdata[] = [
  211.                 "title" => $infra->getName(),
  212.                 "href" => $this->linkGenerator->generate($infra),
  213.                 "image" => $infra->getFirstImage(),
  214. //                "badgeText" =>  $infra->get(),
  215.                 "isInNav" =>  false,
  216.                 "isSearchTeaser" =>  false,
  217.                 "ratio" =>  null
  218.             ];
  219.             $count++;
  220.             if($count >= $numOfTeaser) break;
  221.         }
  222.         return $teaserdata;
  223.     }
  224. }