Browse code

Implement ajax controller for geo data retrieval

Benjamin Roth authored on02/02/2023 23:52:47
Showing1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,96 @@
1
+<?php
2
+
3
+declare(strict_types=1);
4
+
5
+/*
6
+ * This file is part of alox bundle for Contao.
7
+ *
8
+ * (c) Benjamin Roth
9
+ *
10
+ * @license commercial
11
+ */
12
+
13
+namespace vossmedien\AloxBundle\Controller\Frontend;
14
+
15
+use Contao\CoreBundle\Controller\AbstractController;
16
+use Contao\CoreBundle\Framework\ContaoFramework;
17
+use Doctrine\DBAL\Connection;
18
+use Psr\Container\ContainerInterface;
19
+use Symfony\Component\HttpFoundation\JsonResponse;
20
+use Symfony\Component\HttpFoundation\Request;
21
+use Symfony\Component\HttpFoundation\RequestStack;
22
+use Symfony\Component\Routing\Annotation\Route;
23
+use Symfony\Component\HttpFoundation\Response;
24
+use Symfony\Contracts\Translation\TranslatorInterface;
25
+
26
+class GeodbAjaxController extends AbstractController
27
+{
28
+
29
+    /**
30
+     * @var TranslatorInterface
31
+     */
32
+    protected $translator;
33
+    /**
34
+     * @var Request
35
+     */
36
+    protected $request;
37
+    /**
38
+     * @var ContaoFramework
39
+     */
40
+    protected $framework;
41
+    /**
42
+     * @var Connection
43
+     */
44
+    protected $db;
45
+
46
+
47
+
48
+    public function __construct(ContainerInterface $container, TranslatorInterface $translator, RequestStack $request, ContaoFramework $framework, Connection $connection)
49
+    {
50
+        $this->container = $container;
51
+        $this->initializeContaoFramework();
52
+        $this->translator = $translator;
53
+        $this->request = $request->getCurrentRequest();
54
+        $this->framework = $framework;
55
+        $this->db = $connection;
56
+    }
57
+
58
+
59
+    /**
60
+     * @Route("/_geodb/getLatLon", name="geodb_latlon", defaults={"_scope" = "frontend", "_token_check" = false})
61
+     *
62
+     * @return Response
63
+     */
64
+    public function getLatLon()
65
+    {
66
+        global $objPage;
67
+
68
+        if (isset($_REQUEST['q']))
69
+        {
70
+            $result = [
71
+                'items' => []
72
+            ];
73
+
74
+            $query = $this->db->executeQuery("SELECT lat, lon, postal, name FROM tl_ab_geodb WHERE (name LIKE ? OR postal LIKE ?) AND type IN ('Dorf','Ortsteil','Ortschaft','Ort','Kreisstadt','Gemeinde','Stadt','Stadtteil') AND invalid != '1' AND postal != ''",[
75
+                '%' . $_REQUEST['q'] . '%',
76
+                '%' . $_REQUEST['q'] . '%',
77
+            ]);
78
+
79
+            if ($query->rowCount())
80
+            {
81
+                foreach ($query->fetchAllAssociative() as $item)
82
+                {
83
+                    $result['items'][] = [
84
+                        'latLon' => $item['lat'].','.$item['lon'],
85
+                        'name' => $item['postal'].' '.$item['name']
86
+                    ];
87
+                }
88
+            }
89
+
90
+            return new JsonResponse($result);
91
+        }
92
+
93
+
94
+        return new Response('Required parameter missing',412);
95
+    }
96
+}