src/Controller/ApiController.php line 737

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Repository\AppsRepository;
  4. use App\Repository\CategoriesRepository;
  5. use http\Encoding\Stream\Debrotli;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\ErrorHandler\Debug;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Doctrine\Persistence\ManagerRegistry;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\JsonResponse;
  13. #[Route('/api')]
  14. class ApiController extends AbstractController
  15. {
  16.     private function checkToken($token,ManagerRegistry $doctrine){
  17.         // schauen, ob Token in der Datenbank
  18.         $entityManager $doctrine->getManager();
  19.         $qb $entityManager->createQueryBuilder();
  20.         $qb->select('a')
  21.             ->from('App\Entity\Tokens''a')
  22.             ->where('a.token = ?1')
  23.             ->setParameter(1$token);
  24.         $query $qb->getQuery();
  25.         $result $query->getResult();
  26.         if ($result){
  27.             return $result[0]->getApp()->getId();
  28.         } else {
  29.             return false;
  30.         }
  31.     }
  32.     private function checkCors($referer,ManagerRegistry $doctrine){
  33.         // schauen, ob Token in der Datenbank
  34.         $entityManager $doctrine->getManager();
  35.         $qb $entityManager->createQueryBuilder();
  36.         $qb->select('a')
  37.             ->from('App\Entity\AllowedWebsites''a')
  38.             ->where('a.website = ?1')
  39.             ->setParameter(1$referer);
  40.         $query $qb->getQuery();
  41.         $result $query->getResult();
  42.         if ($result){
  43.             return $result[0]->getWebsite();
  44.         } else {
  45.             return false;
  46.         }
  47.     }
  48.     private function getCats(ManagerRegistry $doctrine){
  49.         // schauen, ob Token in der Datenbank
  50.         $entityManager $doctrine->getManager();
  51.         $qb $entityManager->createQueryBuilder();
  52.         $qb->select('a')
  53.            ->from('App\Entity\Categories''a');
  54.         $query $qb->getQuery();
  55.         $result $query->getResult();
  56.         $allcats = [];
  57.         foreach($result as $r){
  58.             $allcats[intval($r->getId())] = $r->getTitle();
  59.         }
  60.         if ($result){
  61.             return $allcats;
  62.         } else {
  63.             return false;
  64.         }
  65.     }
  66.     private function getPlzCentroid($plz,ManagerRegistry $doctrine){
  67.         // schauen, ob Token in der Datenbank
  68.         $entityManager $doctrine->getManager();
  69.         $qb $entityManager->createQueryBuilder();
  70.         $qb->select('p')
  71.             ->from('App\Entity\Plz''p')
  72.             ->where('p.plz = ?1')
  73.             ->setParameter(1$plz);
  74.         $query $qb->getQuery();
  75.         $result $query->getResult();
  76.         if ($result){
  77.             return $result[0]->getCentroid();
  78.         } else {
  79.             return false;
  80.         }
  81.     }
  82.     private function checkFormat($format){
  83.         if(!in_array($format,array('json','geojson'))){
  84.             return false;
  85.         }
  86.         return true;
  87.     }
  88.     #[Route('/'name'app_api')]
  89.     public function index(): Response
  90.     {
  91.         return $this->redirectToRoute('app_login');
  92.     }
  93.     #[Route('/{token}/getappsettings'name'app_settings')]
  94.     public function app_settings($token,ManagerRegistry $doctrine,AppsRepository $appsRepositoryCategoriesRepository $categoriesRepository): Response
  95.     {
  96.         $response = new JsonResponse();
  97.         if($this->checkToken($token,$doctrine)>0){
  98.             $appId $this->checkToken($token,$doctrine);
  99.         } else {
  100.             $response->setData(array( 'error' => 'token error - please contact Webmaster' ));
  101.             return $response;
  102.         }
  103.         $app $appsRepository->findOneById($appId);
  104.         $user $app->getUser();
  105.         $labels $user->getLabelConfig();
  106.         $private $user->getFormPrivateConfig();
  107.         $required $user->getFormRequiredConfig();
  108.         $popup $user->getFormPopupConfig();
  109.         $fields $user->getFormConfig();
  110.         $mode $app->isOrderSwitch()?array_reverse($app->getListMode()):$app->getListMode();
  111.         $colors = [
  112.             'clusterColor' => $app->getClusterColor(),
  113.             'clusterTextColor' => $app->getClusterTextColor(),
  114.         ];
  115.         $publicFields = [];
  116.         foreach($fields as $field){
  117.             if($private[$field] == 0){
  118.                 $publicFields[] = [
  119.                     'name' => $field,
  120.                     'label' => $labels[$field],
  121.                     'required' => $required[$field],
  122.                     'popup' => $popup[$field]
  123.                 ];
  124.             }
  125.         }
  126.         $categories $app->getCategories();
  127.         $cats = [];
  128.         foreach($categories as $cat){
  129.             $cats[] = [
  130.                 'id' => $cat->getId(),
  131.                 'val' => $cat->getTitle(),
  132.                 'markerColor' => $cat->getMarkerColor()
  133.             ];
  134.         }
  135.         $settings = array(
  136.             'legendTitle' => $app->getLegendTitle(),
  137.             'initialCenterLat' => $app->getCenterLat(),
  138.             'initialCenterLon' => $app->getCenterLon(),
  139.             'initialZoom' => $app->getZoom(),
  140.             'minZoom' => $app->getMinzoom(),
  141.             'maxZoom' => $app->getMaxzoom(),
  142.             'mapWidth' => $app->getMapwidth(),
  143.             'mapHeight' => $app->getMapheight(),
  144.             'distanceSteps' => explode(',',$app->getDistanceSteps()),
  145.             'showCatFilter' => $app->isShowCatFilter(),
  146.             'showZipSearch' => $app->isShownZipSearch(),
  147.             'categories' => $cats,
  148.             'colors' => $colors,
  149.             'form' => $publicFields,
  150.             'listMode' => $mode
  151.         );
  152.         $response->setData($settings);
  153.         return $response;
  154.     }
  155.     #[Route('/{token}/distance/{lon}/{lat}/{radius}/{cats}/{format}',defaults:['radius'=>1000000,'cats'=>'all','format'=>'geojson'], name'app_distance')]
  156.     public function bydistance($token,$lon,$lat,ManagerRegistry $doctrine,Request $request,$radius=1000000,$cats="all",$format='geojson'): Response
  157.     {
  158.         //Fehler abfangen
  159.         if(!preg_match('#\d+,*#',$cats)){
  160.             $cats 'all';
  161.         }
  162.         if($request->query->has('format')){
  163.             $format =   $request->query->get('format');
  164.         }
  165.         $allcats $this->getCats($doctrine);
  166.         $response = new JsonResponse();
  167.         if($this->checkToken($token,$doctrine)>0){
  168.             $appId $this->checkToken($token,$doctrine);
  169.         } else {
  170.             $response->setData(array( 'error' => 'token error - please contact Webmaster' ));
  171.             return $response;
  172.         }
  173.         if(!$this->checkFormat($format)){
  174.             $response->setData(array( 'error' => 'format error: supported formats are json and geojson (Feature Collection) ' ));
  175.             return $response;
  176.         }
  177.         $andWhere'';
  178.         $orderBy='distance';
  179.         $orderDir='asc';
  180.         // EntityManager holen
  181.         $entityManager $doctrine->getManager();
  182.         $conn $entityManager->getConnection();
  183.         $catsWhere 'AND (';
  184.         if($cats != 'all'){
  185.             $all explode(',',$cats);
  186.             $i 0;
  187.             foreach ($all as $c){
  188.                 if ($i >= 1){
  189.                     $catsWhere .= ' OR ';
  190.                 }
  191.                 $catsWhere .= "FIND_IN_SET('" $c ."', (select GROUP_CONCAT(categories_id SEPARATOR ',') from places_categories where places_id = id))";
  192.                 $i++;
  193.             }
  194.         }
  195.         $catsWhere .= ') ';
  196.         $sql "SELECT id,title,st_astext(coord) as latlon,ST_Distance(ST_GeomFromText(\"POINT($lat $lon)\", 4326), coord ) as distance,
  197.                 ST_Distance(ST_GeomFromText(\"POINT($lat $lon)\", 4326), coord )/1000 as distancekm, $lon as requestedLon,$lat as requestedLat, ST_Longitude(coord) as lon, ST_Latitude(coord) as lat,
  198.                 address, zip, city, email1, email2,phone1,phone2,facebook, linkedin,twitter,person1,person2,www, 
  199.                 (select GROUP_CONCAT(categories_id SEPARATOR ',') from places_categories where places_id = id) as cats  
  200.                 from places left join places_apps on places.id = places_apps.places_id 
  201.                 where places_apps.apps_id=:appid ";
  202.                 if ($cats != 'all'){
  203.                     $sql .=  $catsWhere;
  204.                 }
  205.                 $sql .= "having distance < :radius $andWhere order by $orderBy $orderDir";
  206.         $stmt $conn->prepare($sql);
  207.         //$stmt->bindValue(':lon', $lon, \DOCTRINE\DBAL\TYPES);
  208.         //$stmt->bindValue(':lat', $lat, \PDO::PARAM_DECIMAL);
  209.         $stmt->bindValue(':appid'$appId"integer");
  210.         $stmt->bindValue(':radius'$radius"integer");
  211.         //dd($stmt);
  212.         $items $stmt->execute()->fetchAll();
  213.         if ($format === 'json'){
  214.             $response->setData($items );
  215.         } elseif($format === 'geojson'){
  216.             $fc = array(
  217.                 "type"=> "FeatureCollection",
  218.                 "features" => array()
  219.             );
  220.             foreach($items as $item){
  221.                 $feature=array(
  222.                     "type" => "Feature",
  223.                     "geometry"=> array(
  224.                         "type"=>"Point",
  225.                         "coordinates"=>[$item['lon'],$item['lat']]
  226.                     ),
  227.                     "properties"=>array()
  228.                 );
  229.                 foreach($item as $k=>$v){
  230.                     $feature["properties"][$k] = $v;
  231.                 }
  232.                 $cats explode(',',$feature['properties']['cats']);
  233.                 $cay =[];
  234.                 //dump($cats);
  235.                 if (count($cats) > 0){
  236.                     foreach($cats as $k => $v){
  237.                         if ($v 0){
  238.                             $cay[] = array (
  239.                                 'id' => $v,
  240.                                 'name' => $allcats[$v]
  241.                             );
  242.                         }
  243.                     }
  244.                 }
  245.                 $feature['properties']['catsArray'] = $cay;
  246.                 $fc['features'][] = $feature;
  247.             }
  248.             //dd('hallo');
  249.             $response->setData($fc);
  250.         } else {
  251.             $response->setData(array( 'error' => 'program error' ));
  252.         }
  253.         return $response;
  254.     }
  255.     #[Route('/{token}/plzdistance/{plz}/{radius}/{cats}/{format}',defaults:['radius'=>1000000,'cats'=>'all','format'=>'geojson'], name'app_plz_distance')]
  256.     public function plzbydistance($token,$plz,ManagerRegistry $doctrine,Request $request,$radius=1000000,$cats="all",$format='geojson')
  257.     {
  258.         // EntityManager holen
  259.         $entityManager $doctrine->getManager();
  260.         $conn $entityManager->getConnection();
  261.         $sql "SELECT ST_Longitude(centroid) as lon, ST_Latitude(centroid) as lat 
  262.                 from plz 
  263.                 where plz=:plz ";
  264.         $stmt $conn->prepare($sql);
  265.         $stmt->bindValue(':plz'$plz"string");
  266.         $items $stmt->execute()->fetchAll();
  267.         $lon $items[0]['lon'];
  268.         $lat $items[0]['lat'];
  269.         return $this->bydistance($token,$lon,$lat,$doctrine,$request,$radius,$cats,$format);
  270.     }
  271.     #[Route('/{token}/cats'name'app_cats')]
  272.     public function getcats4app($token,ManagerRegistry $doctrine,Request $request,AppsRepository $appsRepository): Response
  273.     {
  274.         if($this->checkToken($token,$doctrine)>0){
  275.             $appId $this->checkToken($token,$doctrine);
  276.         }
  277.         $app $appsRepository->findOneById($appId);
  278.         $categories $app->getCategories();
  279.         $return = [];
  280.         foreach($categories as $cat){
  281.             $return[] = [
  282.                 'id' => $cat->getId(),
  283.                 'val' => $cat->getTitle()
  284.             ];
  285.         }
  286.         $response = new JsonResponse();
  287.         $response->setData($return);
  288.         return $response;
  289.     }
  290.     #[Route('/{token}/contains/{area}/{resolution}',defaults:['resolution'=>'low'], name'app_contains')]
  291.     public function areacontains($token,$area,$resolution,ManagerRegistry $doctrine,Request $request): Response
  292.     {
  293.         $response = new JsonResponse();
  294.         if($this->checkToken($token,$doctrine)>0){
  295.             $appId $this->checkToken($token,$doctrine);
  296.         } else {
  297.             $response->setData(array( 'error' => 'token error - please contact Webmaster' ));
  298.             return $response;
  299.         }
  300.         $format 'json'// Default
  301.         $resolution $resolution=='low'?'low':'middle';
  302.         $dbcol $resolution=='low'?'geom200':'geom20';
  303.         if($request->query->has('format')){
  304.             $format =   $request->query->get('format');
  305.         }
  306.         $response = new JsonResponse();
  307.         // EntityManager holen
  308.         $entityManager $doctrine->getManager();
  309.         $conn $entityManager->getConnection();
  310.         $sql "SELECT * FROM places WHERE ST_WITHIN(coord,(select $dbcol from area where id=:id))";
  311.         $sql "SELECT id,title,st_astext(coord) as latlon, ST_Longitude(coord) as lon, ST_Latitude(coord) as lat,
  312.                 address, zip, city, email1, email2,phone1,phone2,facebook, linkedin,twitter,person1,person2,www, 
  313.                 (select GROUP_CONCAT(categories_id SEPARATOR ',') from places_categories where places_id = id) as cats  
  314.                 from places left join places_apps on places.id = places_apps.places_id 
  315.                 where places_apps.apps_id=:appid and ST_WITHIN(coord,(select $dbcol from area where id=:id))";
  316.         $stmt $conn->prepare($sql);
  317.         $stmt->bindValue(':id'$area"integer");
  318.         $stmt->bindValue(':appid'$appId"integer");
  319.         $items $stmt->execute()->fetchAll();
  320.         if ($format === 'json'){
  321.             $response->setData($items);
  322.         } elseif($format === 'geojson'){
  323.             $fc = array(
  324.                 "type"=> "FeatureCollection",
  325.                 "features" => array()
  326.             );
  327.             foreach($items as $item){
  328.                 $feature=array(
  329.                     "type" => "Feature",
  330.                     "geometry"=> array(
  331.                         "type"=>"Point",
  332.                         "coordinates"=>[$item['lon'],$item['lat']]
  333.                     ),
  334.                     "properties"=>array()
  335.                 );
  336.                 foreach($item as $k=>$v){
  337.                     $feature["properties"][$k] = $v;
  338.                 }
  339.                 $cats explode(',',$feature['properties']['cats']);
  340.                 $feature['properties']['catsArray'] = $cats;
  341.                 $fc['features'][] = $feature;
  342.             }
  343.             $response->setData($fc);
  344.         } else {
  345.             $response->setData(array( 'error' => 'program error' ));
  346.         }
  347.         return $response;
  348.         
  349.     }
  350.     #[Route('/{token}/plzcontains/{plz}'name'app_plzcontains')]
  351.     public function plzcontains($token,$plz,ManagerRegistry $doctrine,Request $request): Response
  352.     {
  353.         $response = new JsonResponse();
  354.         if($this->checkToken($token,$doctrine)>0){
  355.             $appId $this->checkToken($token,$doctrine);
  356.         } else {
  357.             $response->setData(array( 'error' => 'token error - please contact Webmaster' ));
  358.             return $response;
  359.         }
  360.         $format 'json'// Default
  361.         if($request->query->has('format')){
  362.             $format =   $request->query->get('format');
  363.         }
  364.         $response = new JsonResponse();
  365.         // EntityManager holen
  366.         $entityManager $doctrine->getManager();
  367.         $conn $entityManager->getConnection();
  368.         $sql "SELECT * FROM places WHERE ST_WITHIN(coord,(select geom from plz where plz=:plz))";
  369.         $sql "SELECT id,title,st_astext(coord) as latlon, ST_Longitude(coord) as lon, ST_Latitude(coord) as lat,
  370.                 address, zip, city, email1, email2,phone1,phone2,facebook, linkedin,twitter,person1,person2,www, 
  371.                 (select GROUP_CONCAT(categories_id SEPARATOR ',') from places_categories where places_id = id) as cats  
  372.                 from places left join places_apps on places.id = places_apps.places_id 
  373.                 where places_apps.apps_id=:appid and ST_WITHIN(coord,(select geom from plz where plz=:plz))";
  374.         $stmt $conn->prepare($sql);
  375.         $stmt->bindValue(':plz'$plz"string");
  376.         $stmt->bindValue(':appid'$appId"integer");
  377.         $items $stmt->execute()->fetchAll();
  378.         if ($format === 'json'){
  379.             $response->setData($items);
  380.         } elseif($format === 'geojson'){
  381.             $fc = array(
  382.                 "type"=> "FeatureCollection",
  383.                 "features" => array()
  384.             );
  385.             foreach($items as $item){
  386.                 $feature=array(
  387.                     "type" => "Feature",
  388.                     "geometry"=> array(
  389.                         "type"=>"Point",
  390.                         "coordinates"=>[$item['lon'],$item['lat']]
  391.                     ),
  392.                     "properties"=>array()
  393.                 );
  394.                 foreach($item as $k=>$v){
  395.                     $feature["properties"][$k] = $v;
  396.                 }
  397.                 $cats explode(',',$feature['properties']['cats']);
  398.                 $feature['properties']['catsArray'] = $cats;
  399.                 $fc['features'][] = $feature;
  400.             }
  401.             $response->setData($fc);
  402.         } else {
  403.             $response->setData(array( 'error' => 'program error' ));
  404.         }
  405.         return $response;
  406.     }
  407.     #[Route('/{token}/plzcentroid/{plz}/{radius}/{format}',defaults:['radius'=>50000], name'app_plzcentroid')]
  408.     public function plzcentroid($token,$plz,ManagerRegistry $doctrine,Request $request,$radius=50000,$format='geojson'): Response
  409.     {
  410.         $response = new JsonResponse();
  411.         if($this->checkToken($token,$doctrine)>0){
  412.             $appId $this->checkToken($token,$doctrine);
  413.         } else {
  414.             $response->setData(array( 'error' => 'token error - please contact Webmaster' ));
  415.             return $response;
  416.         }
  417.         if($request->query->has('format')){
  418.             $format =   $request->query->get('format');
  419.         }
  420.         $response = new JsonResponse();
  421.         // EntityManager holen
  422.         $entityManager $doctrine->getManager();
  423.         $conn $entityManager->getConnection();
  424.         $plzCentroid $this->getPlzCentroid(trim($plz),$doctrine);
  425.         $x $plzCentroid->getX();
  426.         $y $plzCentroid->getY();
  427.         $srid $plzCentroid->getSrid()==null?4326:$plzCentroid->getSrid();
  428.         $plzCentroid 'ST_GeomFromText("POINT(' .$x .' ' .$y.')", ' $srid ;
  429.         $sql "SELECT id,title,st_astext(coord) as latlon,ST_Distance($plzCentroid), coord ) as distance,
  430.                 ST_Distance($plzCentroid), coord )/1000 as distancekm, $x as requestedLon, $y as requestedLat, ST_Longitude(coord) as lon, ST_Latitude(coord) as lat,
  431.                 address, zip, city, email1, email2,phone1,phone2,facebook, linkedin,twitter,person1,person2,www,
  432.                 (select GROUP_CONCAT(categories_id SEPARATOR ',') from places_categories where places_id = id) as cats
  433.                 from places left join places_apps on places.id = places_apps.places_id
  434.                 where places_apps.apps_id=:appid
  435.                 having distance < :radius order by distance asc";
  436.         $stmt $conn->prepare($sql);
  437.         $stmt->bindValue(':appid'$appId"integer");
  438.         $stmt->bindValue(':radius'$radius"integer");
  439.         $items $stmt->execute()->fetchAll();
  440.         if ($format === 'json'){
  441.             $response->setData($items);
  442.         } elseif($format === 'geojson'){
  443.             $fc = array(
  444.                 "type"=> "FeatureCollection",
  445.                 "features" => array()
  446.             );
  447.             foreach($items as $item){
  448.                 $feature=array(
  449.                     "type" => "Feature",
  450.                     "geometry"=> array(
  451.                         "type"=>"Point",
  452.                         "coordinates"=>[$item['lon'],$item['lat']]
  453.                     ),
  454.                     "properties"=>array()
  455.                 );
  456.                 foreach($item as $k=>$v){
  457.                     $feature["properties"][$k] = $v;
  458.                 }
  459.                 $cats explode(',',$feature['properties']['cats']);
  460.                 $feature['properties']['catsArray'] = $cats;
  461.                 $fc['features'][] = $feature;
  462.             }
  463.             $response->setData($fc);
  464.         } else {
  465.             $response->setData(array( 'error' => 'program error' ));
  466.         }
  467.         return $response;
  468.     }
  469.     #[Route('/{token}/plz4city/{city}',defaults:[], name'app_plzcity')]
  470.     public function plzcity($token,$city,ManagerRegistry $doctrine,Request $request): Response
  471.     {
  472.         $response = new JsonResponse();
  473.         if($this->checkToken($token,$doctrine)>0){
  474.             $appId $this->checkToken($token,$doctrine);
  475.         } else {
  476.             $response->setData(array( 'error' => 'token error - please contact Webmaster' ));
  477.             return $response;
  478.         }
  479.         $format 'json'// Default
  480.         $response = new JsonResponse();
  481.         // EntityManager holen
  482.         $entityManager $doctrine->getManager();
  483.         $conn $entityManager->getConnection();
  484.         $search explode(' ',$city);
  485.         if(count($search)==1){
  486.             $regexp $search[0].'.*';
  487.         }
  488.         if(count($search)==2){
  489.             $regexp $search[0] .'.+'.$search[1].'.*';
  490.         } elseif(count($search)==3){
  491.             $regexp $search[0] .'.+'.$search[1].'.+'.$search[2].'.*';
  492.         } elseif(count($search)==4){
  493.             $regexp $search[0] .'.+'.$search[1].'.+'.$search[2].'.+'.$search[3].'.*';
  494.         } else {
  495.             $response->setData(array( 'error' => 'too much tokens for city' ));
  496.         }
  497.         $sql 'select plz,name from plz_complete where REGEXP_LIKE(name,"^' $regexp .'") order by name,plz asc';
  498.         //dd($sql);
  499.         $stmt $conn->prepare($sql);
  500.         //$stmt->bindValue(':city', $city, "string");
  501.         $items $stmt->execute()->fetchAll();
  502.         if ($format === 'json'){
  503.             $response->setData($items);
  504.         } else {
  505.             $response->setData(array( 'error' => 'program error' ));
  506.         }
  507.         return $response;
  508.     }
  509.     #[Route('/{token}/pointinfo/{lon}/{lat}'name'app_pointinfo')]
  510.     public function pointinfo($token,$lon,$lat,ManagerRegistry $doctrine,Request $request): Response
  511.     {
  512.         $response = new JsonResponse();
  513.         if($this->checkToken($token,$doctrine)>0){
  514.             $appId $this->checkToken($token,$doctrine);
  515.         } else {
  516.             $response->setData(array( 'error' => 'token error - please contact Webmaster' ));
  517.             return $response;
  518.         }
  519.         // EntityManager holen
  520.         $entityManager $doctrine->getManager();
  521.         $conn $entityManager->getConnection();
  522.         $sql "SELECT maptype_id,title from area where ST_WITHIN(ST_GeomFromText(\"POINT($lat $lon)\", 4326),geom20)
  523.                 UNION select 4,plz from plz where ST_WITHIN(ST_GeomFromText(\"POINT($lat $lon)\", 4326),geom) and LENGTH(plz) = 5";
  524.         $sql "select distinct p.plz,p.title,c.landkreis,c.bundesland,c.bundesland_kurz from plz p left join  plz_complete c on p.plz=c.plz where p.type=5 and ST_WITHIN(ST_GeomFromText(\"POINT($lat $lon)\", 4326),p.geom)";
  525.         $stmt $conn->prepare($sql);
  526.         //$stmt->bindValue(':plz', $plz, "string");
  527.         //$stmt->bindValue(':appid', $appId, "integer");
  528.         $items $stmt->execute()->fetchAll();
  529.         /*foreach($items  as $item){
  530.             $ay['plz'] =
  531.             /*if($item['maptype_id']===1){
  532.                 $ay['land']=$item['title'];
  533.             }
  534.             if($item['maptype_id']===2){
  535.                 $ay['landkreis']=$item['title'];
  536.             }
  537.             if($item['maptype_id']===3){
  538.                 $ay['gemeinde']=$item['title'];
  539.             }
  540.             if($item['maptype_id']===4){
  541.                 $ay['plz']=$item['title'];
  542.             }
  543.         }*/
  544.         $response->setData($items[0]);
  545.         return $response;
  546.     }
  547.     #[Route('/{token}/city/{fragment}/{limit}',defaults:['limit'=>50000], name'app_city')]
  548.     public function city($token,$fragment,$limit,ManagerRegistry $doctrine,Request $request): Response
  549.     {
  550.         $referer = (string) $request->headers->get('referer'); // get the referer, it can be empty!
  551.         //$refererStr = u($referer);
  552.         $response = new JsonResponse();
  553.         if($this->checkToken($token,$doctrine)>0){
  554.             $appId $this->checkToken($token,$doctrine);
  555.         } else {
  556.             $response->setData(array( 'error' => 'token error - please contact Webmaster' ));
  557.             return $response;
  558.         }
  559.         // EntityManager holen
  560.         $entityManager $doctrine->getManager();
  561.         $conn $entityManager->getConnection();
  562.         $select = array();
  563.         // Erst die suchen die genau passen
  564.         $sql "SELECT name,plz,landkreis FROM plz_complete where name = :cityexact group by plz order by name,plz COLLATE utf8mb4_german2_ci limit :limit ";
  565.         $stmt $conn->prepare($sql);
  566.         $stmt->bindValue(':cityexact'urldecode(trim($fragment)), "string");
  567.         $stmt->bindValue(':limit'$limit"integer");
  568.         $items $stmt->executeQuery()->fetchAll();
  569.         $check =[];
  570.         $final = array();
  571.         //if(count($items)>1  and strlen(urldecode(trim($fragment))) > 3){
  572.         if(count($items)>1){
  573.             foreach ($items as $item){
  574.                 $check[$item['landkreis']][] = $item['plz'];
  575.             }
  576.             // jetzt entscheiden welche Stadt genommen wird (die, die am meisten PLZ hat)
  577.             foreach ($check as $k => $v) {
  578.                 $ct[$k] = count($check[$k]);
  579.             }
  580.             arsort ($ct);
  581.             foreach ($ct as $k=>$v){
  582.                 $first $check[$k][0];
  583.                 $last =  $check[$k][$v 1];
  584.                 if ($first == $last){
  585.                     $plzToShow "(PLZ $first)";
  586.                 } else {
  587.                     $plzToShow "(PLZ $first-$last)";
  588.                 }
  589.                 $replace=ucfirst($fragment);
  590.                 $name preg_replace('/(' $fragment .')/i',"<mark>$replace</mark>",$fragment);
  591.                 $final[] = array('name' => $name ' (' $k .')''plz' => $plzToShow);
  592.             }
  593.         }
  594.         // Jetzt die, die mit der Eingabe anfangen
  595.         $sql "SELECT name,plz,landkreis FROM plz_complete where name like :city and name != :name group by landkreis,plz order by name,plz COLLATE utf8mb4_german2_ci limit :limit ";
  596.         $stmt $conn->prepare($sql);
  597.         $stmt->bindValue(':city',urldecode(trim($fragment)) ."%""string");
  598.         $stmt->bindValue(':limit'$limit"integer");
  599.         $stmt->bindValue(':name',urldecode(trim($fragment)),"string");
  600.         $items $stmt->execute()->fetchAll();
  601.         $check =[];
  602.         foreach ($items as $item){
  603.             $check[$item['landkreis']][] = array('plz'=>$item['plz'],'ort'=>$item['name']);
  604.         }
  605.         $ct=[];
  606.         foreach ($check as $k => $v) {
  607.            $ct[$k] = array('anzahl'=>count($check[$k]),'ort'=>$v[0]['ort']);
  608.         }
  609.         arsort ($ct);
  610.         foreach ($ct as $k=>$v){
  611.             $first $check[$k][0]['plz'];
  612.             $last =  $check[$k][$v['anzahl'] - 1]['plz'];
  613.             $ort $check[$k][0]['ort'];
  614.             if ($first == $last){
  615.                 $plzToShow "(PLZ $first)";
  616.             } else {
  617.                 $plzToShow "(PLZ $first-$last)";
  618.             }
  619.             $name preg_replace('/(' $fragment .')/i',"<mark>$1</mark>",$ort);
  620.             $final[] = array('name' => $name'plz' => $plzToShow);
  621.         }
  622.         /*$i=0;
  623.         foreach($items as $item){
  624.             //$select[$item['name']]['maxPLZ'] = null;
  625.             if(array_key_exists($item['name'],$select)){
  626.                 $select[$item['name']]['maxPLZ']=$item['plz'];
  627.                 $i++;
  628.                 $select[$item['name']]['plzCnt']=$i;
  629.             } else {
  630.                 $i=1;
  631.                 $select[$item['name']]['name']=$item['name'];
  632.                 $select[$item['name']]['minPLZ']=$item['plz'];
  633.                 $select[$item['name']]['plzCnt']=$i;
  634.             }
  635.         }*/
  636.         // Jetzt der Rest
  637.         $sql "SELECT name,plz,landkreis FROM plz_complete where name like :city and name not like :city2 and name != :name group by landkreis,plz order by name,plz COLLATE utf8mb4_german2_ci limit :limit ";
  638.         $stmt $conn->prepare($sql);
  639.         $stmt->bindValue(':city''%' urldecode(trim($fragment)) ."%""string");
  640.         $stmt->bindValue(':city2'urldecode(trim($fragment)) ."%""string");
  641.         $stmt->bindValue(':limit'$limit"integer");
  642.         $stmt->bindValue(':name',urldecode(trim($fragment)),"string");
  643.         $items $stmt->execute()->fetchAll();
  644.         $check =[];
  645.         foreach ($items as $item){
  646.             $check[$item['landkreis']][] = array('plz'=>$item['plz'],'ort'=>$item['name']);
  647.         }
  648.         $ct=[];
  649.         foreach ($check as $k => $v) {
  650.             $ct[$k] = array('anzahl'=>count($check[$k]),'ort'=>$v[0]['ort']);
  651.         }
  652.         arsort ($ct);
  653.         foreach ($ct as $k=>$v){
  654.             $first $check[$k][0]['plz'];
  655.             $last =  $check[$k][$v['anzahl'] - 1]['plz'];
  656.             $ort $check[$k][0]['ort'];
  657.             if ($first == $last){
  658.                 $plzToShow "(PLZ $first)";
  659.             } else {
  660.                 $plzToShow "(PLZ $first-$last)";
  661.             }
  662.             $name preg_replace('/(' $fragment .')/i',"<mark>$1</mark>",$ort);
  663.             $final[] = array('name' => $name'plz' => $plzToShow);
  664.         }
  665.         $response->setData($final);
  666.         return $response;
  667.     }
  668.     #[Route('/{token}/cityplz/{city}/{minplz}/{maxplz}',defaults:['limit'=>50000,'minplz'=>0,'maxplz'=>100000], name'app_cityplz')]
  669.     public function cityplz($token,$city,$minplz,$maxplz,$limit,ManagerRegistry $doctrine,Request $request): Response
  670.     {
  671.         $response = new JsonResponse();
  672.         if($this->checkToken($token,$doctrine)>0){
  673.             $appId $this->checkToken($token,$doctrine);
  674.         } else {
  675.             $response->setData(array( 'error' => 'token error - please contact Webmaster' ));
  676.             return $response;
  677.         }
  678.         $entityManager $doctrine->getManager();
  679.         $conn $entityManager->getConnection();
  680.         if (preg_match('/\D+ \(.+\)/',urldecode(trim($city)))){
  681.             // Beispiel $city = "Frankfurt (Oder)"
  682.             $pattern str_replace([' (',')'],['%','%'],urldecode(trim($city)));
  683.             $sql "SELECT ST_AsGeoJSON(geom) geom,plz.plz FROM plz_complete complete left join plz on complete.plz = plz.plz where complete.name = :city and (cast(plz.plz as UNSIGNED)>=:plzmin and cast(plz.plz as UNSIGNED)<=:plzmax) COLLATE utf8mb4_german2_ci;";
  684.             $stmt $conn->prepare($sql);
  685.             $stmt->bindValue(':city'$pattern"string");
  686.             $stmt->bindValue(':plzmin'$minplz"integer");
  687.             $stmt->bindValue(':plzmax'$maxplz"integer");
  688.             /*$sql = "SELECT ST_AsGeoJSON(geom) geom,plz FROM plz where title like :city and (cast(plz as UNSIGNED)>=:plzmin and cast(plz as UNSIGNED)<=:plzmax)";
  689.             $stmt = $conn->prepare($sql);
  690.             $stmt->bindValue(':city', $pattern, "string");
  691.             $stmt->bindValue(':plzmin', $minplz, "integer");
  692.             $stmt->bindValue(':plzmax', $maxplz, "integer");*/
  693.         } else {
  694.             //$sql = "SELECT ST_AsGeoJSON(geom) geom,plz FROM plz where (title = :city or title like :city2 or title like :city3) and (cast(plz as UNSIGNED)>=:plzmin and cast(plz as UNSIGNED)<=:plzmax) ";
  695.             $sql "SELECT ST_AsGeoJSON(geom) geom,plz.plz FROM plz_complete complete left join plz on complete.plz = plz.plz where (complete.name = :city or complete.name like :city2 or complete.name like :city3) and (cast(plz.plz as UNSIGNED)>=:plzmin and cast(plz.plz as UNSIGNED)<=:plzmax) COLLATE utf8mb4_german2_ci;";
  696.             $stmt $conn->prepare($sql);
  697.             $stmt->bindValue(':city'urldecode(trim($city)), "string");
  698.             $stmt->bindValue(':city2'urldecode(trim($city)) .' %'"string");
  699.             $stmt->bindValue(':city3'urldecode(trim($city)) .'-%'"string");
  700.             $stmt->bindValue(':plzmin'$minplz"integer");
  701.             $stmt->bindValue(':plzmax'$maxplz"integer");
  702.         }
  703.         $items $stmt->execute()->fetchAll();
  704.         $fc = array(
  705.             "type"=> "FeatureCollection",
  706.             "features" => array()
  707.         );
  708.         foreach($items as $item){
  709.             $geomArray json_decode($item['geom'],true);
  710.             $feature=array(
  711.                 "type" => "Feature",
  712.                 "geometry"=> array(
  713.                     "type"=>$geomArray['type'],
  714.                     "coordinates"=>$geomArray['coordinates']
  715.                 ),
  716.                 "properties"=>array('plz'=>$item['plz'])
  717.             );
  718.             $fc['features'][] = $feature;
  719.         }
  720.         $response->setData($fc);
  721.       return $response;
  722.     }
  723.     #[Route('/{token}/autoplz/{fragment}',defaults:[], name'app_autoplz')]
  724.     public function autoplz($token,$fragment,ManagerRegistry $doctrine,Request $request): Response
  725.     {
  726.         $response = new JsonResponse();
  727.         if($this->checkToken($token,$doctrine)>0){
  728.             $appId $this->checkToken($token,$doctrine);
  729.         } else {
  730.             $response->setData(array( 'error' => 'token error - please contact Webmaster' ));
  731.             return $response;
  732.         }
  733.         $entityManager $doctrine->getManager();
  734.         $conn $entityManager->getConnection();
  735.         $sql "SELECT concat(plz,' ',title) plz FROM plz where plz like :plz and title IS NOT NULL order by plz";
  736.         $stmt $conn->prepare($sql);
  737.         $stmt->bindValue(':plz'urldecode(trim($fragment)) . '%'"string");
  738.         $items $stmt->execute()->fetchAll();
  739.         $response->setData($items);
  740.         return $response;
  741.     }
  742.     #[Route('/city4plz/{plz}',defaults:[], name'app_city4plz')]
  743.     public function city4plz($plz,ManagerRegistry $doctrine,Request $request): Response
  744.     {
  745.         $response = new JsonResponse();
  746.         if($this->getUser()){
  747.             $entityManager $doctrine->getManager();
  748.             $conn $entityManager->getConnection();
  749.             $sql "SELECT title FROM plz where plz = :plz and title IS NOT NULL order by plz";
  750.             $stmt $conn->prepare($sql);
  751.             $stmt->bindValue(':plz'urldecode(trim($plz)), "string");
  752.             $items $stmt->execute()->fetchAll();
  753.             $response->setData($items);
  754.         } else {
  755.             $response->setData('unauthorized');
  756.         }
  757.         return $response;
  758.     }
  759. //
  760. }