Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 181
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
Location
0.00% covered (danger)
0.00%
0 / 181
0.00% covered (danger)
0.00%
0 / 8
1190
0.00% covered (danger)
0.00%
0 / 1
 fetchId
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
42
 fetchList
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 1
20
 fetchListByOffice
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 1
12
 fetchFromCsv
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 1
12
 fetchGeoJsonLocations
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 1
20
 fetchGeoJson
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
72
 readSearchResultList
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
6
 fetchLocationsForCompilation
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3/**
4 * @package ClientDldb
5 * @copyright BerlinOnline Stadtportal GmbH & Co. KG
6 **/
7
8namespace BO\Zmsdldb\MySQL;
9
10use BO\Zmsdldb\MySQL\Entity\Location as Entity;
11use BO\Zmsdldb\MySQL\Collection\Locations as Collection;
12use BO\Zmsdldb\Elastic\Location as Base;
13
14/**
15 * @SuppressWarnings(Coupling)
16 */
17class Location extends Base
18{
19    public function fetchId($location_id)
20    {
21        try {
22            if ($location_id) {
23                $sqlArgs = [$this->locale, (int)$location_id];
24                $sql = 'SELECT data_json FROM location WHERE locale = ? AND id = ?';
25
26                $stm = $this->access()->prepare($sql);
27                $stm->setFetchMode(\PDO::FETCH_CLASS | \PDO::FETCH_PROPS_LATE, '\\BO\\Zmsdldb\\MySQL\\Entity\\Location');
28                $stm->execute($sqlArgs);
29                if (!$stm || ($stm && $stm->rowCount() == 0)) {
30                    return false;
31                }
32                $service = $stm->fetch();
33                return $service;
34            }
35            return false;
36        } catch (\Exception $e) {
37            throw $e;
38        }
39    }
40
41    public function fetchList($service_csv = false, $mixLanguages = false)
42    {
43        # COALESCE(l2.data_json, l.data_json) AS data_json
44        # IF(l2.id, l2.data_json, l.data_json) AS data_json
45        try {
46            $sqlArgs = [$this->locale];
47            $where = [];
48            $join = [];
49            $groupBy = '';
50            if (false === $mixLanguages) {
51                $where[] = 'l.locale = ?';
52                $sql = 'SELECT data_json FROM location AS l';
53            } else {
54                $where[] = "l.locale='de'";
55                $sql = "SELECT 
56                COALESCE(l2.data_json, l.data_json) AS data_json
57                FROM location AS l
58                LEFT JOIN location AS l2 ON l2.id = l.id AND l2.locale = ?
59                ";
60            }
61
62            if (!empty($service_csv)) {
63                #$sqlArgs[] = $this->locale;
64                $ids = explode(',', $service_csv);
65                $questionMarks = array_fill(0, count($ids), '?');
66                $join[] = 'LEFT JOIN location_service AS ls ON ls.location_id = l.id';# AND ls.locale = ?';
67
68                $where[] = "ls.service_id IN (" . implode(', ', $questionMarks) . ")";
69                $groupBy = 'GROUP BY l.id';
70                array_push($sqlArgs, ...$ids);
71            }
72            $sql .= " " . implode(' ', $join);
73            $sql .= " WHERE " . implode(' AND ', $where);
74            $sql .= " " . $groupBy;
75
76            $locationList = new Collection();
77
78            $stm = $this->access()->prepare($sql);
79            $stm->execute($sqlArgs);
80            $stm->fetchAll(\PDO::FETCH_FUNC, function ($data_json) use ($locationList) {
81                $location = new \BO\Zmsdldb\MySQL\Entity\Location();
82                $location->offsetSet('data_json', $data_json);
83
84                $locationList[$location['id']] = $location;
85            });
86
87            return $locationList;
88        } catch (\Exception $e) {
89            throw $e;
90        }
91    }
92
93    public function fetchListByOffice($office, $mixLanguages = false)
94    {
95        try {
96            $sqlArgs = [$this->locale];
97            $where = [];
98            $join = [];
99            $groupBy = '';
100            if (false === $mixLanguages) {
101                $where[] = 'l.locale = ?';
102                $sql = 'SELECT data_json FROM location AS l';
103            } else {
104                $where[] = "l.locale='de'";
105                $sql = "SELECT 
106                COALESCE(l2.data_json, l.data_json) AS data_json
107                FROM location AS l
108                LEFT JOIN location AS l2 ON l2.id = l.id AND l2.locale = ?
109                ";
110            }
111            $where[] = "l.category_identifier = ?";
112            $sqlArgs[] = $office;
113
114            $sql .= " " . implode(' ', $join);
115            $sql .= " WHERE " . implode(' AND ', $where);
116
117            $sql .= " " . $groupBy;
118
119            $locationList = new Collection();
120            $stm = $this->access()->prepare($sql);
121            $stm->execute($sqlArgs);
122            $stm->fetchAll(\PDO::FETCH_FUNC, function ($data_json) use ($locationList) {
123                $location = new \BO\Zmsdldb\MySQL\Entity\Location();
124                $location->offsetSet('data_json', $data_json);
125
126                $locationList[$location['id']] = $location;
127            });
128
129            return $locationList;
130        } catch (\Exception $e) {
131            throw $e;
132        }
133    }
134
135    /**
136     *
137     * @return Collection\Services
138     */
139    public function fetchFromCsv($location_csv, $mixLanguages = false)
140    {
141        try {
142            $sqlArgs = [$this->locale];
143            $where = [];
144            if (false === $mixLanguages) {
145                $where[] = 'l.locale = ?';
146                $sql = 'SELECT data_json FROM location AS l';
147            } else {
148                $where[] = "l.locale='de'";
149                $sql = "SELECT 
150                COALESCE(l2.data_json, l.data_json) AS data_json
151                FROM location AS l
152                LEFT JOIN location AS l2 ON l2.id = l.id AND l2.locale = ?
153                ";
154            }
155
156            $ids = explode(',', $location_csv);
157            $questionMarks = array_fill(0, count($ids), '?');
158            $where[] = 'l.id IN (' . implode(', ', $questionMarks) . ')';
159            array_push($sqlArgs, ...$ids);
160
161            $sql .= " WHERE " . implode(' AND ', $where);
162
163            $locationList = new Collection();
164
165            $stm = $this->access()->prepare($sql);
166            $stm->execute($sqlArgs);
167            $stm->fetchAll(\PDO::FETCH_FUNC, function ($data_json) use ($locationList) {
168                $location = new \BO\Zmsdldb\MySQL\Entity\Location();
169                $location->offsetSet('data_json', $data_json);
170
171                $locationList[$location['id']] = $location;
172            });
173
174            return $locationList;
175        } catch (\Exception $e) {
176            throw $e;
177        }
178    }
179
180    protected function fetchGeoJsonLocations($category, $getAll)
181    {
182        try {
183            $sqlArgs = [$this->locale, $this->locale, $this->locale];
184            $sql = 'SELECT 
185                l.id, l.name, l.authority_name, l.category_json, 
186                c.contact_json, c.address_json, c.geo_json,
187                m.url AS meta__url
188            FROM 
189                location AS l
190            LEFT JOIN 
191                contact AS c ON c.object_id = l.id AND c.locale = ?
192            LEFT JOIN 
193                meta AS m ON m.object_id = l.id AND m.locale = ?
194            WHERE l.locale = ?';
195
196            if (!empty($category) && false === $getAll) {
197                $sqlArgs[] = $category;
198                $sql .= ' AND category_identifier = ?';
199            }
200            $sql .= ' ORDER BY l.category_identifier, l.name';
201
202            $locationList = new Collection();
203
204            $stm = $this->access()->prepare($sql);
205            $stm->execute($sqlArgs);
206            $stm->fetchAll(\PDO::FETCH_FUNC, function (
207                $id,
208                $name,
209                $authority_name,
210                $category_json,
211                $contact_json,
212                $address_json,
213                $geo_json,
214                $meta__url
215            ) use ($locationList) {
216                $location = new \BO\Zmsdldb\MySQL\Entity\Location();
217                $location->offsetSet('id', $id);
218                $location->offsetSet('name', $name);
219                $location->offsetSet('authority_name', $authority_name);
220                $location->offsetSet('category_json', $category_json);
221                $location->offsetSet('contact_json', $contact_json);
222                $location->offsetSet('address_json', $address_json);
223                $location->offsetSet('geo_json', $geo_json);
224                $location->offsetSet('meta__url', $meta__url);
225
226                $locationList[$location['id']] = $location;
227            });
228
229            return $locationList;
230        } catch (\Exception $e) {
231            throw $e;
232        }
233    }
234
235    /**
236     * @todo Refactoring required, functions in this class should return entities, not JSON data
237     */
238    public function fetchGeoJson($category = null, $getAll = false)
239    {
240        $locationList = $this->fetchGeoJsonLocations($category, $getAll);
241        $geoJson = [];
242        // TODO check refactoring: the following lines were ineffective cause the line $geoJson=[] happened afterwards
243        //if (!empty($category) && false === $getAll) {
244        //    $geoJson['category'] = $category;
245        //}
246        foreach ($locationList as $location) {
247            if (empty($location['category']['identifier'])) {
248                continue;
249            }
250            if (!isset($geoJson[$location['category']['identifier']])) {
251                $geoJson[$location['category']['identifier']] = [
252                    'name' => $location['category']['name'],
253                    'type' => 'cluster',
254                    'active' => (
255                        !empty($category)
256                        && $category == $location['category']['identifier'] ? true : (
257                            !empty($category) && $category != $location['category']['identifier'] ? false : true
258                        )
259                    ),
260                    'data' => ['type' => 'FeatureCollection', 'features' => []]
261                ];
262            }
263            $geoJson[$location['category']['identifier']]['data']['features'][] = $location->getGeoJson();
264        }
265        return $geoJson;
266    }
267
268    public function readSearchResultList($query, $service_csv = null)
269    {
270        try {
271            #$query = '+' . implode(' +', explode(' ', $query));
272            $sqlArgs = [$this->locale, $this->locale, $query];
273            $sql = "SELECT l.data_json 
274            FROM search AS se
275            LEFT JOIN location AS l ON l.id = se.object_id AND l.locale = ?
276            WHERE 
277                se.locale = ? AND MATCH (search_value) AGAINST (? IN BOOLEAN MODE)
278                AND (search_type IN ('name', 'keywords', 'address')) AND entity_type='location'
279             GROUP BY se.object_id
280            ";
281            /*
282            if (!empty($service_csv)) {
283                $ids = explode(',', $service_csv);
284                $qm = array_fill(0, count($ids), '?');
285                $sql .= ' AND se.object_id IN (' . implode(', ', $qm) . ')';
286                array_push($sqlArgs, ...$ids);
287            }*/
288            #print_r($sql);exit;
289
290            $locationList = new Collection();
291
292            $stm = $this->access()->prepare($sql);
293            $stm->execute($sqlArgs);
294            $stm->fetchAll(\PDO::FETCH_FUNC, function ($data_json) use ($locationList) {
295                $location = new \BO\Zmsdldb\MySQL\Entity\Location();
296                $location->offsetSet('data_json', $data_json);
297
298                $locationList[$location['id']] = $location;
299            });
300
301            return $locationList;
302        } catch (\Exception $e) {
303            throw $e;
304        }
305    }
306
307    public function fetchLocationsForCompilation($authoritys = [], $locations = [])
308    {
309        try {
310            $sqlArgs = [$this->locale];
311
312            $sql = "SELECT 
313                l.data_json
314            FROM location AS l
315            ";
316            $where = ['l.locale = ?'];
317
318            if (!empty($authoritys)) {
319                $where[] = 'l.authority_id IN (' . implode(',', $authoritys) . ')';
320            }
321
322            if (!empty($locations)) {
323                $where[] = 'l.id IN (' . implode(',', $locations) . ')';
324            }
325
326            $sql .= ' WHERE ' . implode(' AND ', $where);
327
328            $locationList = new Collection();
329
330            $stm = $this->access()->prepare($sql);
331            $stm->execute($sqlArgs);
332            $stm->fetchAll(\PDO::FETCH_FUNC, function ($data_json) use ($locationList) {
333                $location = new \BO\Zmsdldb\MySQL\Entity\Location();
334                $location->offsetSet('data_json', $data_json);
335
336                $locationList[$location['id']] = $location;
337            });
338
339            return $locationList;
340        } catch (\Exception $e) {
341            throw $e;
342        }
343    }
344}