<?php
namespace App\Controller;
use App\Service\CanonicalRedirectHelper;
use App\Twig\LinkGenerator;
use Knp\Component\Pager\PaginatorInterface;
use Pimcore\Document\Editable\Exception\NotFoundException;
use Pimcore\Model\DataObject\Community;
use Pimcore\Model\DataObject\ConventionPartner;
use Pimcore\Model\DataObject\DemiInfrastructure;
use Pimcore\Model\DataObject\DemiInfrastructureTopic;
use Pimcore\Model\DataObject\Region;
use Pimcore\Model\WebsiteSetting;
use Pimcore\Translation\Translator;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Elements\Bundle\CmsToolsBundle\Tool\Helper\FunctionsHelper;
class CommunityController extends AbstractController
{
/**
* @var LinkGenerator
*/
private $linkGenerator;
public function __construct( LinkGenerator $linkGenerator)
{
$this->linkGenerator = $linkGenerator;
}
/**
*@Route("/{_locale}/community/overview", name="community_overview")
*/
public function overviewAction(Request $request, PaginatorInterface $paginator, Translator $translator)
{
$communityListing = new Community\Listing();
$notStyriaCommunity = WebsiteSetting::getByName('notStyriaCommunityFolder')->getData();
if($this->getDocumentEditable('checkbox', 'hideRegionSelect')->isChecked()) {
$region = $this->document->getProperty('region');
$communityListing->addConditionParam('o_path LIKE "%' . $region->getFullPath() . '%"');
} else {
if($regionId = $request->get('region')) {
if($regionId == 'otherRegion'){
$communityListing->addConditionParam('o_path LIKE "%' . $notStyriaCommunity->getFullPath() . '%"');
}else {
$region = Region::getById($regionId);
$communityListing->addConditionParam('o_path LIKE "%' . $region->getFullPath() . '%"');
}
} else { //exclude not styria communities
$communityListing->addConditionParam('o_path not LIKE "%' . $notStyriaCommunity->getFullPath() . '%"');
}
}
if($q = $request->get('keyword')) {
$communityListing->addConditionParam('name like :q OR shortdescription like :q', ['q' => '%' . $q . '%']);
}
$communityListing->setOrderKey('nameLocalized');
$paginator = $paginator->paginate($communityListing, $request->get('page',1),12);
$paginator->setPageRange(3);
//map
if ($request->isXmlHttpRequest() && $request->get('pois', false)) {
return $this->getMapdata($paginator);
}
if($request->get('ajax') && $request->isXmlHttpRequest()) {
return $this->json([
"success" => true,
// "html" => $this->renderView('Community/Includes/places-paging.html.twig', ['paginator' => $paginator])
"content" => [
'result-main' => $this->renderView('Community/Includes/places-list.html.twig', [
'paginator' => $paginator,
]),
'result-paging' => $this->renderView('Navigation/paging.html.twig', [
'styleModifier' => 'justify-content-center',
'ajaxClass' => 'js-ajax-form-map__link',
'paginator' => $paginator
]),
],
]);
}
$regionSelect = $this->getRegionSelectData($regionId ?? null, $translator);
return $this->renderTemplate('Community/overview.html.twig', [
'regionSelect' => $regionSelect,
'paginator' => $paginator,
]);
}
public function detailAction(Request $request, Translator $translator, CanonicalRedirectHelper $redirectHelper)
{
$community = Community::getById($request->get('id',0));
if(!$community){
throw new NotFoundException("requested object doesn't exist.");
}
if (CanonicalRedirectHelper::ENABLE_CANONICAL_REDIRECT && $redirect = $redirectHelper->canonicalRedirect($community)) {
return $redirect;
}
$demiInfrastructure = $this->getDemiInfrastructureTeasers($community, $translator);
return $this->renderTemplate('Community/detail.html.twig', [
'community' => $community,
'demiInfrastructure' => $demiInfrastructure
]);
}
private function getRegionSelectData($location = null, Translator $translator = null) {
$regions = new Region\Listing();
$regions->setOrderKey('sorting');
$regionsSelect = [[
"label" => "",
"value" => "",
"disabled" => true,
"selected" => !$location,
"class"=> "sr-only"
]];
foreach ($regions as $region) {
$regionsSelect[] = [
"label" => $region->getName(),
"value" => $region->getId(),
"class" => "",
"selected" => $location && $location == $region->getId(),
];
}
$regionsSelect[] = [
"label" => $translator->trans('community.Gemeinden Außerhalb'),
"value" => 'otherRegion',
"class" => "",
"selected" => $location && $location == 'otherRegion',
];
return $regionsSelect;
}
public function getMapData($paginator) {
$geoData = [];
foreach ($paginator as $community){
if($community) {
$geo = $community->getGeoposition();
if($geo) {
$geoData[] = [
"id" => $community->getId(),
"lat" => $geo->getLatitude(),
"lng" => $geo->getLongitude(),
"detailInfoBoxUrl" => $this->generateUrl('community-interactive-map-info-box',[
'id' => $community->getId(),
])
];
}
}
}
return new JsonResponse([
"success" => true,
"pois" => $geoData
]);
}
/**
* @Route("/{_locale}/community-interactive-map-info-box", name="community-interactive-map-info-box")
*/
public function getMapInfoBox(Request $request, LinkGenerator $linkGenerator) {
$community = Community::getById($request->get('id', null));
$document = $this->document;
$locale = $document->getProperty('language') ?? $request->getLocale();
if($community) {
return $this->json([
'success' => true,
'html' => $this->renderView('Convention/Includes/interactive-map-info-box.html.twig', [
'image' => $community->getTeaserimage(),
'subtitle' => '',
'title' => $community->getNameLocalized($locale) ?? $community->getName(),
'wysiwyg' => $community->getShortdescription($locale),
'link' => $linkGenerator->generate($community, ['document' => $document]),
'id' => $request->get('id')
])
]);
}
}
private function getDemiInfrastructureTeasers(Community $community, Translator $translator, $numOfTeaser = 9) : array {
if($community->getDetailRelatedExcursions() && !empty($community->getDetailRelatedExcursions())) {
$demiInfrastructures = $community->getDetailRelatedExcursions();
} else {
$demiInfrastructures = DemiInfrastructure::getList();
$demiInfrastructures->addConditionParam('name is not null and name != ""');
if($community->getDetailRadiusExcursion()) {
$functionsHelper = new FunctionsHelper();
$distance = $functionsHelper->getGeoDistanceQuery($community->getGeoposition(), 'position');
$demiInfrastructures->addConditionParam('(' . $distance . ') <= :detailRadius', [ 'detailRadius' => $community->getDetailRadiusExcursion()]);
}
$topics = [];
if(($infraTypes = $community->getDetailRadiusTypesExcursion()) && !empty($infraTypes)) {
foreach($infraTypes as $type) {
foreach($type->getChildren() as $child) {
if($child instanceof DemiInfrastructureTopic) {
$topics[] = $child->getId();
}
}
}
}
if(($infraTopics = $community->getDetailRadiusTopicsExcursion()) && !empty($infraTopics)) {
foreach ($infraTopics as $topic) {
$topics[] = $topic->getId();
}
}
if(!empty($topics)) {
$topics = array_unique($topics);
$subcondition = [];
foreach ($topics as $topic) {
$subcondition[] = 'topics LIKE "%,' . $topic . ',%"';
$subcondition[] = 'subTopics LIKE "%,' . $topic . ',%"';
}
$demiInfrastructures->addConditionParam('(' . implode(' OR ', $subcondition) . ')');
}
// $demiInfrastructures->setOrderKey(["RAND()"]);
$demiInfrastructures->setOrderKey("RAND()", false);
$demiInfrastructures->setLimit(9);
}
//prepare img-teasers
$count = 0;
$teaserdata = [];
foreach ($demiInfrastructures as $infra) {
$teaserdata[] = [
"title" => $infra->getName(),
"href" => $this->linkGenerator->generate($infra),
"image" => $infra->getFirstImage(),
// "badgeText" => $infra->get(),
"isInNav" => false,
"isSearchTeaser" => false,
"ratio" => null
];
$count++;
if($count >= $numOfTeaser) break;
}
return $teaserdata;
}
}