<?php
namespace App\Controller;
use Knp\Component\Pager\PaginatorInterface;
use Pimcore\Model\DataObject\AbstractObject;
use Pimcore\Model\DataObject\Community;
use Pimcore\Model\DataObject\Region;
use Pimcore\Model\DataObject\WeatherStation;
use Pimcore\Model\Document\Editable\Image;
use Pimcore\Model\Element\Data\MarkerHotspotItem;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class WeatherController extends AbstractController
{
public function overviewAction(Request $request, PaginatorInterface $paginator)
{
$region = $this->document->getProperty('region');
if ($request->get('city') != '') {
$communities = new Community\Listing();
$communities->addConditionParam('prognosis__id != "" and prognosis__id IS NOT NULL');
if ($region instanceof Region && !$region->getIsMainRegion()) {
$communities->addConditionParam('o_path LIKE "' . $region->getFullPath() . '%"');
}
$communities->addConditionParam('o_id = :id', ['id' => $request->get('city')]);
} else {
/** @var Image $map */
$map = $this->getDocumentEditable('image', 'map');
$communities = [];
foreach ($map->getMarker() ?: [] as $hotspot) {
/** @var MarkerHotspotItem $hotspotElement */
if ($hotspot['data'][0]) {
$hotspotElement = $hotspot['data'][0];
if ($hotspotElement->getType() == 'object') {
$element = AbstractObject::getById($hotspotElement->getValue());
if ($element instanceof Region || $element instanceof Community || $element instanceof WeatherStation) {
$communities[] = $element;
}
}
}
}
}
$paginator = $paginator->paginate($communities, $request->get('page', 1), 9);
if ($request->isXmlHttpRequest()) {
return $this->json([
'success' => true,
'html' => $this->renderView('Weather/accordion.html.twig', ['stations' => $paginator])
]);
}
return $this->renderTemplate('Weather/overview.html.twig', [
'stations' => $paginator
]);
}
/**
* @Route("/{_locale}/weather/typeahead", name="weather_typeahead")
* @param Request $request
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
public function typeAhead(Request $request) {
$region = Region::getById($request->get('region'));
$communities = new Community\Listing();
$communities->addConditionParam('prognosis__id != "" and prognosis__id IS NOT NULL');
if ($region instanceof Region && !$region->getIsMainRegion()) {
$communities->addConditionParam('o_path LIKE "' . $region->getFullPath() . '%"');
}
if ($request->get('query') != '') {
$communities->addConditionParam('name LIKE :query', ['query' => $request->get('query') . '%']);
}
$data = [];
$ignoreIds = [];
foreach($communities as $community) {
$ignoreIds[] = $community->getId();
$data[] = [
'value' => $community->getId(),
'label' => $community->getName()
];
}
$stations = new WeatherStation\Listing();
$communities = new Community\Listing();
$communities->addConditionParam('prognosis__id != "" and prognosis__id IS NOT NULL');
if ($region instanceof Region && !$region->getIsMainRegion()) {
$communities->addConditionParam('o_path LIKE "' . $region->getFullPath() . '%"');
}
if (!empty($ignoreIds)) {
$communities->addConditionParam('o_id NOT IN(' . implode(',', $ignoreIds) . ')');
}
if ($request->get('query') != '') {
$communities->addConditionParam('name LIKE :query', ['query' => '%' . $request->get('query') . '%']);
}
foreach($communities as $community) {
$data[] = [
'value' => $community->getId(),
'label' => $community->getName()
];
}
return $this->json(['success' => true, 'data' => ['options' => $data]]);
}
}