src/Controller/WeatherController.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Knp\Component\Pager\PaginatorInterface;
  4. use Pimcore\Model\DataObject\AbstractObject;
  5. use Pimcore\Model\DataObject\Community;
  6. use Pimcore\Model\DataObject\Region;
  7. use Pimcore\Model\DataObject\WeatherStation;
  8. use Pimcore\Model\Document\Editable\Image;
  9. use Pimcore\Model\Element\Data\MarkerHotspotItem;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. class WeatherController extends AbstractController
  13. {
  14.     public function overviewAction(Request $requestPaginatorInterface $paginator)
  15.     {
  16.         $region $this->document->getProperty('region');
  17.         if ($request->get('city') != '') {
  18.             $communities = new Community\Listing();
  19.             $communities->addConditionParam('prognosis__id != "" and prognosis__id IS NOT NULL');
  20.             if ($region instanceof Region && !$region->getIsMainRegion()) {
  21.                 $communities->addConditionParam('o_path LIKE "' $region->getFullPath() . '%"');
  22.             }
  23.             $communities->addConditionParam('o_id = :id', ['id' => $request->get('city')]);
  24.         } else {
  25.             /** @var Image $map */
  26.             $map $this->getDocumentEditable('image''map');
  27.             $communities = [];
  28.             foreach ($map->getMarker() ?: [] as $hotspot) {
  29.                 /** @var MarkerHotspotItem $hotspotElement */
  30.                 if ($hotspot['data'][0]) {
  31.                     $hotspotElement $hotspot['data'][0];
  32.                     if ($hotspotElement->getType() == 'object') {
  33.                         $element AbstractObject::getById($hotspotElement->getValue());
  34.                         if ($element instanceof Region || $element instanceof Community || $element instanceof WeatherStation) {
  35.                             $communities[] = $element;
  36.                         }
  37.                     }
  38.                 }
  39.             }
  40.         }
  41.         $paginator $paginator->paginate($communities$request->get('page'1), 9);
  42.         if ($request->isXmlHttpRequest()) {
  43.             return $this->json([
  44.                 'success' => true,
  45.                 'html' => $this->renderView('Weather/accordion.html.twig', ['stations' => $paginator])
  46.             ]);
  47.         }
  48.         return $this->renderTemplate('Weather/overview.html.twig', [
  49.             'stations' => $paginator
  50.         ]);
  51.     }
  52.     /**
  53.      * @Route("/{_locale}/weather/typeahead", name="weather_typeahead")
  54.      * @param Request $request
  55.      * @return \Symfony\Component\HttpFoundation\JsonResponse
  56.      */
  57.     public function typeAhead(Request $request) {
  58.         $region Region::getById($request->get('region'));
  59.         $communities = new Community\Listing();
  60.         $communities->addConditionParam('prognosis__id != "" and prognosis__id IS NOT NULL');
  61.         if ($region instanceof Region && !$region->getIsMainRegion()) {
  62.             $communities->addConditionParam('o_path LIKE "' $region->getFullPath() . '%"');
  63.         }
  64.         if ($request->get('query') != '') {
  65.             $communities->addConditionParam('name LIKE :query', ['query' => $request->get('query') . '%']);
  66.         }
  67.         $data = [];
  68.         $ignoreIds = [];
  69.         foreach($communities as $community) {
  70.             $ignoreIds[] = $community->getId();
  71.             $data[] = [
  72.                 'value' => $community->getId(),
  73.                 'label' => $community->getName()
  74.             ];
  75.         }
  76.         $stations = new WeatherStation\Listing();
  77.         $communities = new Community\Listing();
  78.         $communities->addConditionParam('prognosis__id != "" and prognosis__id IS NOT NULL');
  79.         if ($region instanceof Region && !$region->getIsMainRegion()) {
  80.             $communities->addConditionParam('o_path LIKE "' $region->getFullPath() . '%"');
  81.         }
  82.         if (!empty($ignoreIds)) {
  83.             $communities->addConditionParam('o_id NOT IN(' implode(','$ignoreIds) . ')');
  84.         }
  85.         if ($request->get('query') != '') {
  86.             $communities->addConditionParam('name LIKE :query', ['query' => '%' $request->get('query') . '%']);
  87.         }
  88.         foreach($communities as $community) {
  89.             $data[] = [
  90.                 'value' => $community->getId(),
  91.                 'label' => $community->getName()
  92.             ];
  93.         }
  94.         return $this->json(['success' => true'data' => ['options' => $data]]);
  95.     }
  96. }