-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFieldtypeGeocoder.module
More file actions
610 lines (505 loc) · 16.7 KB
/
FieldtypeGeocoder.module
File metadata and controls
610 lines (505 loc) · 16.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
<?php
/**
* COPYRIGHT NOTICE
* Copyright (c) 2025 Neue Rituale GbR
* @author NR <code@neuerituale.com>
*/
namespace ProcessWire;
use Geocoder\Collection;
use Geocoder\Dumper\GeoArray;
use Geocoder\Dumper\GeoJson;
use Geocoder\Formatter\StringFormatter;
use Geocoder\Http\Provider\AbstractHttpProvider;
use Geocoder\Location;
use Geocoder\Provider\OpenCage\OpenCage;
use Geocoder\Provider\Provider;
use Geocoder\Query\GeocodeQuery;
use Geocoder\Query\Query;
use Geocoder\Query\ReverseQuery;
use Geocoder\StatefulGeocoder;
use Http\Client\Curl\Client;
/**
* @method array search($text, array $options = array())
* @method void loadGeocoderPhp()
* @method Client getAdapter()
* @method AbstractHttpProvider|OpenCage getProvider($adapter)
* @method StatefulGeocoder getGeocoder(Provider $provider)
* @method Query filterQuery(Query $query)
* @method string getLanguage(string $fallback = 'native')
* @method string formatAddress(Location $location)
*/
class FieldtypeGeocoder extends Fieldtype implements Module, ConfigurableModule {
public static function getModuleInfo() {
return array(
'title' => 'Geocoder',
'version' => 106,
'summary' => 'Collect and store Geocode information from several providers',
'href' => 'https://github.com/neuerituale/FieldtypeGeocoder',
'icon' => 'globe',
'installs' => 'InputfieldGeocoder',
'searchable' => 'geocoder'
);
}
public function init(): void {
require_once(__DIR__ . '/Geocoder.php');
}
/**
* @param Field $field
* @return array
*/
public function getDatabaseSchema(Field $field): array {
$schema = parent::getDatabaseSchema($field);
$len = $this->wire('database')->getMaxIndexLength();
$schema['data'] = 'text NOT NULL';
$schema['keys']['data_exact'] = "KEY `data_exact` (`data`($len))";
$schema['keys']['data'] = 'FULLTEXT KEY `data` (`data`)';
$schema['formatted'] = 'TEXT NOT NULL';
$schema['geodata'] = 'JSON';
$schema['lat'] = "DECIMAL(10,6) DEFAULT NULL"; // latitude
$schema['lng'] = "DECIMAL(10,6) DEFAULT NULL"; // longitude
$schema['provider'] = "TINYTEXT"; // Provider (Google/Mapbox etc)
$schema['status'] = "INT(10) unsigned NOT NULL DEFAULT 1"; // geocode status
return $schema;
}
/**
* Return the Inputfield used to collect input for a field of this type
* @throws WirePermissionException
* @return Module|_Module|null
*/
public function getInputfield(Page $page, Field $field) {
return $this->modules->get('InputfieldGeocoder');
}
/**
* Return a blank ready-to-populate value
*
* @param Page $page
* @param Field $field
* @return Geocoder
*
*/
public function getBlankValue(Page $page, Field $field): Geocoder {
return new Geocoder();
}
/**
* Sanitize value for runtime
* @param Page $page
* @param Field $field
* @param int|object|WireArray|string $value
* @return Geocoder
*/
public function sanitizeValue(Page $page, Field $field, $value): Geocoder {
/** @var Geocoder $geocoder */
$geocoder = $value;
if(!$geocoder instanceof Geocoder) {
$geocoder = $this->getBlankValue($page, $field);
$geocoder->setTrackChanges(true);
$geocoder->query = $this->sanitizer->text($value);
}
return $geocoder;
}
/**
* @param Page $page
* @param Field $field
* @param array|int|string $value
* @return Geocoder
*/
public function ___wakeupValue(Page $page, Field $field, $value): Geocoder {
$geocoder = $this->getBlankValue($page, $field);
if("$value[lat]" === "0") $value['lat'] = '';
if("$value[lng]" === "0") $value['lng'] = '';
$geocoder->setArray([
'query' => $value['data'],
'formatted' => (string) $value['formatted'],
'geodata' => json_decode($value['geodata'],JSON_OBJECT_AS_ARRAY),
'lat' => $value['lat'],
'lng' => $value['lng'],
'provider' => (string) $value['provider'],
'status' => (int) $value['status']
]);
return $geocoder->setTrackChanges(true);
}
/**
* For storage in DB
* @param Page $page
* @param Field $field
* @param array|float|int|object|string $value
* @return array
* @throws WireException
*/
public function ___sleepValue(Page $page, Field $field, $value): array {
/** @var Geocoder $geocoder */
$geocoder = $value;
if(!$geocoder instanceof Geocoder) throw new WireException("Expecting an instance of Geocoder");
// TODO add stuff from inputProcess
// Skip geocoding:
//
// if status not New
// if geodate is not empty and
// formatted is not emty and
// lat is not empty and
// lng is not empty and
// If geodata changes or
// If the only change is the query fields
$changes = $geocoder->getChanges();
if(
$geocoder->status !== 1 &&
is_array($geocoder->geodata) &&
!!count($geocoder->geodata) &&
!empty($geocoder->formatted) &&
!empty($geocoder->lat) &&
!empty($geocoder->lng) &&
(
$geocoder->isChanged('geodata') ||
(
count($changes) === 1 &&
$changes[0] === 'query'
)
)
) {
$geocoder->addStatus(Geocoder::statusSkipGeocoding);
}
// skip geocoding
if(!$geocoder->hasStatus(Geocoder::statusSkipGeocoding)) {
if($geocoder->isChanged('query') && !empty($geocoder->query)) {
//echo "forward";
$this->forwardQuery($geocoder);
$this->message("Forward geocoding");
} else if(
($geocoder->isChanged('lat') && !empty($geocoder->lat)) ||
($geocoder->isChanged('lng') && !empty($geocoder->lng))
) {
//echo "reverse";
$this->reverseQuery($geocoder);
$this->message("Reverse geocoding");
} else {
//echo "no geocoding";
$this->message("No geocoding");
}
} else {
//echo "skip geocoding";
$this->message("Skip geocoding");
}
// remove skip geocoding
$geocoder->removeStatus(Geocoder::statusSkipGeocoding);
return [
'data' => $geocoder->query,
'formatted' => $geocoder->formatted,
'geodata' => json_encode($geocoder->geodata),
'lat' => is_numeric($geocoder->lat) ? (float) $geocoder->lat : null,
'lng' => is_numeric($geocoder->lng) ? (float) $geocoder->lng : null,
'provider' => (string) $geocoder->provider,
'status' => (int) $geocoder->status
];
}
/**
* @param $text
* @param array $options
* @return array
* @throws \Exception
*
* @see SearchableModule
*/
public function ___search($text, array $options = array()): array {
$result = array(
'title' => 'Geocoder',
'items' => array(),
'properties' => ['forward', 'reverse'],
'total' => 0
);
if(!empty($options['help'])) return $result;
if($options['type'] === 'geocoder') {
$geocoder = new Geocoder();
// reverse or forward search
if($options['property'] === 'reverse') $this->reverseQuery($geocoder, explode(',', $text, 2));
else $this->forwardQuery($geocoder, $text);
/** @var Collection $responds */
$collection = $geocoder->_collection;
if($collection instanceof Collection && !$collection->isEmpty()) {
$items = [];
$dumper = new GeoJson();
/** @var Location $location */
foreach($collection as $location) {
$coords = $location->getCoordinates();
$items[] = [
'title' => $location->getFormattedAddress(),
'name' => $dumper->dump($location),
'subtitle' => $coords->getLatitude() . ', ' . $coords->getLongitude(),
'url' => "http://m.osmtools.de/index.php?mlon={$coords->getLongitude()}&mlat={$coords->getLatitude()}&icon=5&zoom=13",
'icon' => 'location-arrow',
'group' => $location->getCountry(),
];
}
$result['items'] = $items;
$result['total'] = count($items);
}
}
return $result;
}
/**
* Method called when the field is database-queried from a $pages->find() selector
*
* @param DatabaseQuerySelect|PageFinderDatabaseQuerySelect $query
* @param string $table
* @param string $subfield
* @param string $operator
* @param string $value
* @return DatabaseQuerySelect|PageFinderDatabaseQuerySelect
*
* @throws WireException
*/
public function getMatchQuery($query, $table, $subfield, $operator, $value) {
$table = $this->database->escapeTable($table);
if($subfield === 'query') $subfield = 'data';
switch($subfield) {
// Fulltext fields
// e.g. $pages->find('geocoder*=Berl') same as $pages->find('geocoder.data*=Berl');
// e.g. $pages->find('geocoder.formatted*=Germany');
case 'data':
case 'formatted':
case 'provider':
$ft = new DatabaseQuerySelectFulltext($query);
$ft->match($table, $subfield, $operator, $value);
return $query;
// Default fields
// e.g $pages->find('geocoder.lat=10.394854839, geocoder.lng>45.345345')
case 'lat':
case 'lng':
return parent::getMatchQuery($query, $table, $subfield, $operator, $value);
// Status search (support bitwise)
// e.g $pages->find('geocoder.status=3')
// e.g $pages->find('geocoder.status&2|4')
case 'status':
// normal operator
if(!$this->database->isOperator($operator, WireDatabasePDO::operatorTypeBitwise))
return parent::getMatchQuery($query, $table, $subfield, $operator, $value);
/** @var Database $database */
$database = wire()->database;
$table = $database->escapeTable($table);
$subfield = $database->escapeCol($subfield);
$query->where("{$table}.{$subfield}{$operator}?", (int) $value);
return $query;
// Proximity Search
// use lat and lng for calculation
// e.g. $pages->find('geocoder.proximity=34.3453453|3.34879345, limit=3')
// do not user sort=xyz!
case 'proximity':
// Ignore second value
if($value === $query->selector->value[1]) return $query;
// Join
$query->join($query->field->getTable() . " AS $table ON $table.pages_id=pages.id");
// Ignore invalid selectors
if( !is_array($query->selector->value) || count($query->selector->value) !== 2 ) return $query;
// Bind lat and lng
$query->bindValues([
'lat' => (float) $query->selector->value[0] / 180 * M_PI,
'lng' => (float) $query->selector->value[1] / 180 * M_PI,
]);
// Select
$query->select('( 6368 * SQRT(2*( 1-cos(RADIANS(' . $table . '.lat)) * cos(:lat) * (sin(RADIANS(' . $table . '.lng)) * sin(:lng) + cos(RADIANS(' . $table . '.lng)) * cos(:lng)) - sin(RADIANS(' . $table . '.lat)) * sin(:lat)))) AS ' . $table . '_distance');
// Order by distance
$query->orderby($table . '_distance');
return $query;
// GEOJson Properties search
default :
// cast to int or float
if(is_numeric($value)) $value = $value*1;
// get full fieldname with dot
$field = substr($query->selector->field, strpos($query->selector->field, '.') + 1);
// All default comparison operators are allowed
if($this->database->isOperator($operator, WireDatabasePDO::operatorTypeComparison)) {
// is empty or null
// is not empty and not null
// mysql 5.7 or greater needed
if(!strlen($value)) {
if($operator === '=') {
$query->where("
JSON_UNQUOTE(JSON_EXTRACT(geodata, '$.$field')) IS NULL
OR JSON_UNQUOTE(JSON_EXTRACT(geodata, '$.$field')) = ''
OR JSON_UNQUOTE(JSON_EXTRACT(geodata, '$.$field')) = 'null'
");
} else {
$query->where("
JSON_UNQUOTE(JSON_EXTRACT(geodata, '$.$field')) IS NOT NULL
AND JSON_UNQUOTE(JSON_EXTRACT(geodata, '$.$field')) != ''
AND JSON_UNQUOTE(JSON_EXTRACT(geodata, '$.$field')) != 'null'
");
}
}
// is value
// is greater/lower than...
else {
$query->where("JSON_UNQUOTE(JSON_EXTRACT(geodata, '$.$field')) {$operator} ?", $value);
}
} else {
throw new \mysqli_sql_exception('Invalid operator for subfield: ' . $operator);
}
return $query;
}
}
/* Geocoder */
/**
* Set query and update geocoder
* @param Geocoder $geocoder
* @param string|null $query
* @return Geocoder
*/
public function forwardQuery(Geocoder $geocoder, string $query = null): Geocoder {
if(!is_null($query)) $geocoder->set('query', $query);
return $this->update($geocoder);
}
/**
* Set lat and lng and update geocoder
* @param Geocoder $geocoder
* @param float|array|null $lat
* @param float|null $lng
* @return Geocoder
*/
public function reverseQuery(Geocoder $geocoder, $lat = null, float $lng = null): Geocoder {
if(is_array($lat)) { $lng = $lat[1]; $lat = $lat[0]; }
if(!is_null($lat)) $geocoder->set('lat', $lat);
if(!is_null($lng)) $geocoder->set('lng', $lng);
return $this->update($geocoder, false);
}
/**
* @param Geocoder $geocoder
* @param bool $forward
* @return Geocoder
*/
public function update(Geocoder $geocoder, bool $forward = true): Geocoder {
// clear and stop invalid queries
if(
($forward && empty($geocoder->query)) ||
(!$forward && empty($geocoder->lat) && empty($geocoder->lng))
) return $geocoder->clear()->addStatus(Geocoder::statusError);
// load geocoder-php
$this->loadGeocoderPhp();
$a = $this->getAdapter();
$p = $this->getProvider($a);
$g = $this->getGeocoder($p);
// query
$collection = $forward
? $g->geocodeQuery( $this->filterQuery(GeocodeQuery::create($geocoder->query)) )
: $g->reverseQuery( $this->filterQuery(ReverseQuery::fromCoordinates($geocoder->lat, $geocoder->lng)) )
;
// clear geocoder
list($query, $lat, $lng) = [$geocoder->query, $geocoder->lat, $geocoder->lng];
if($collection->isEmpty()) {
if($forward) $geocoder->clear()->set('query', $query);
else $geocoder->clear()->set('lat', $lat)->set('lng', $lng);
$geocoder->addStatus(Geocoder::statusNotFound);
return $geocoder;
}
// remove error and notfound statuses
if($collection->count() === 1) $geocoder->setSingleResult();
else $geocoder->setMultipleResults();
// tmp save collection
$geocoder->_collection = $collection;
// Select first location
$location = $collection->first();
// Basics
$geocoder->setArray([
'query' => $query,
'formatted' => $this->formatAddress($location),
'geodata' => (new GeoArray())->dump($location),
'provider' => $location->getProvidedBy(),
]);
// Set Coordinates
if($forward) {
$coordinates = $location->getCoordinates();
$geocoder->set('lat', $coordinates->getLatitude())->set('lng', $coordinates->getLongitude());
} else {
$geocoder->set('lat', $lat)->set('lng', $lng);
}
// Skip Geocoder in sleep
$geocoder->addStatus(Geocoder::statusSkipGeocoding);
return $geocoder;
}
public function ___loadGeocoderPhp() {
// check global geocoder class and include autoload
if(!class_exists("\Geocoder\StatefulGeocoder")) {
if(!file_exists(__DIR__ . '/vendor/autoload.php')) {
wire()->error('Please install geocoder-php in your the module directory.');
throw new \Exception('Please install geocoder-php in your the module directory.');
}
require_once(/*NoCompile*/__DIR__ . '/vendor/autoload.php');
}
}
/**
* Get the http client (PSR-18)
* @return Client
*/
public function ___getAdapter(): Client {
$this->loadGeocoderPhp();
return new Client(null, null, [
CURLOPT_CONNECTTIMEOUT => 2,
CURLOPT_SSL_VERIFYPEER => false,
]);
}
/**
* @param $adapter
* @return AbstractHttpProvider|OpenCage
*/
public function ___getProvider($adapter) {
$this->loadGeocoderPhp();
return new OpenCage(
$adapter,
$this->modules->getConfig('FieldtypeGeocoder', 'apiKey') ?? ''
);
}
/**
* @param Provider $provider
* @return StatefulGeocoder
*/
public function ___getGeocoder(Provider $provider): StatefulGeocoder {
$this->loadGeocoderPhp();
return new StatefulGeocoder($provider, $this->getLanguage());
}
/**
* @param Query $query
* @return Query
*/
public function ___filterQuery(Query $query): Query {
return $query;
}
/**
* @param string $fallback
* @return string
*/
public function ___getLanguage(string $fallback = 'native'): string {
$locale = setlocale(LC_ALL, 0);
$languageCode = locale_get_primary_language($locale);
return (!empty($languageCode) && $languageCode !== 'c') ? $languageCode : $fallback;
}
/**
* @param Location $location
* @return string
*/
public function ___formatAddress(Location $location): string {
// Formatted Address
$addressFormat = $this->get('formatterMapping') ?? '';
return str_contains($addressFormat, '%')
? (new StringFormatter())->format($location, $addressFormat)
: $location->getFormattedAddress()
;
}
public function ___upgrade($fromVersion, $toVersion): void {
$database = wire()->database;
if($fromVersion < 106) {
// Update database schema
// drop existing index and add two new indexes (BTREE and FULLTEXT)
/** @var FieldsArray $geocoderFields */
$geocoderFields = wire()->fields->find('type=FieldtypeGeocoder');
$len = $database->getMaxIndexLength();
/** @var Field $field */
foreach($geocoderFields as $field) {
$table = $field->getTable();
try {$database->exec("ALTER TABLE `$table` DROP INDEX `data`;"); } catch(\Exception $e) { /*ignore*/ }
$database->exec("
ALTER TABLE `$table`
ADD KEY `data_exact` (`data`($len)),
ADD FULLTEXT KEY `data` (`data`);
");
}
}
}
}