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