-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFieldtypeMapDrawFeatures.module
More file actions
306 lines (262 loc) · 7.95 KB
/
FieldtypeMapDrawFeatures.module
File metadata and controls
306 lines (262 loc) · 7.95 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
<?php namespace ProcessWire;
/**
* ProcessWire Draw Map Features Fieldtype
*
* #pw-summary Stores a GeoJSON FeatureCollection drawn on a MapLibre map.
* #pw-body Based on FieldtypeMapMarker by Ryan Cramer and FieldtypeLeafletMapMarker by Mats Neander.
*
* @copyright 2023 NB Communication Ltd
* @license Mozilla Public License v2.0 http://mozilla.org/MPL/2.0/
*
* @property string $style
*
*/
class FieldtypeMapDrawFeatures extends Fieldtype implements ConfigurableModule {
/**
* getModuleInfo is a module required by all modules to tell ProcessWire about them
*
* @return array
*
*/
public static function getModuleInfo() {
return [
'title' => 'Draw Map Features',
'version' => 3,
'summary' => 'Stores a GeoJSON FeatureCollection drawn on a MapLibre map.',
'author' => 'nbcommunication',
'href' => 'https://github.com/nbcommunication/FieldtypeMapDrawFeatures',
'installs' => 'InputfieldMapDrawFeatures',
'icon' => 'object-group',
'requires' => 'ProcessWire>=3.0.210,PHP>=8.1.0',
];
}
/**
* Include our MapDrawFeatures class, which serves as the value for fields of type FieldtypeMapDrawFeatures
*
*/
public function __construct() {
require_once dirname(__FILE__) . '/MapDrawFeatures.php';
}
/**
* Return the Inputfield required by this Fieldtype
*
* @param Page $page
* @param Field $field
* @return InputfieldMapMarker
*
*/
public function getInputfield(Page $page, Field $field) {
$inputfield = $this->wire()->modules->get('InputfieldMapDrawFeatures');
$inputfield->set('style', $this->get('style'));
return $inputfield;
}
/**
* Return all compatible Fieldtypes
*
* @param Field $field
* @return null
*
*/
public function ___getCompatibleFieldtypes(Field $field) {
// there are no other fieldtypes compatible with this one
return null;
}
/**
* Sanitize value for runtime
*
* @param Page $page
* @param Field $field
* @param MapDrawFeatures $value
* @return MapDrawFeatures
*
*/
public function sanitizeValue(Page $page, Field $field, $value) {
// if it's not a MapDrawFeatures, then try to convert it
if(!$value instanceof MapDrawFeatures) {
$value = $this->getBlankValue($page, $field);
}
// If changed, tell the $page that this field changed
if(
$value->isChanged('south') ||
$value->isChanged('west') ||
$value->isChanged('north') ||
$value->isChanged('east') ||
$value->isChanged('features') ||
$value->isChanged('zoom')
) {
$page->trackChange($field->name);
}
return $value;
}
/**
* Get a blank value used by this fieldtype
*
* @param Page $page
* @param Field $field
* @return MapDrawFeatures
*
*/
public function getBlankValue(Page $page, Field $field) {
return new MapDrawFeatures();
}
/**
* Given a raw value (value as stored in DB), return the value as it would appear in a Page object
*
* @param Page $page
* @param Field $field
* @param string|float|array $value
* @return MapDrawFeatures
*
*/
public function ___wakeupValue(Page $page, Field $field, $value) {
// Get a blank MapDraw Features instance
$item = $this->getBlankValue($page, $field);
// populate the marker
$item->features = $value['data'];
$item->south = (float) $value['south'];
$item->west = (float) $value['west'];
$item->north = (float) $value['north'];
$item->east = (float) $value['east'];
$item->zoom = (float) $value['zoom'];
$item->setTrackChanges(true);
return $item;
}
/**
* Given an 'awake' value, as set by wakeupValue, convert the value back to a basic type for storage in DB.
*
* @param Page $page
* @param Field $field
* @param MapDrawFeatures $value
* @return array
* @throws WireException
*
*/
public function ___sleepValue(Page $page, Field $field, $value) {
$item = $value;
if(!$item instanceof MapDrawFeatures) {
throw new WireException('Expecting an instance of MapDrawFeatures');
}
return [
'data' => $item->features,
'south' => $item->south,
'west' => $item->west,
'north' => $item->north,
'east' => $item->east,
'zoom' => $item->zoom,
];
}
/**
* Return the database schema in specified format
*
* @param Field $field
* @return array
*
*/
public function getDatabaseSchema(Field $field) {
$schema = parent::getDatabaseSchema($field);
$schema['data'] = "MEDIUMTEXT NOT NULL DEFAULT ''"; // Features
$schema['south'] = "FLOAT(10,6) NOT NULL DEFAULT 0"; // south bounds (latitude)
$schema['west'] = "FLOAT(10,6) NOT NULL DEFAULT 0"; // west bounds (longitude)
$schema['north'] = "FLOAT(10,6) NOT NULL DEFAULT 0"; // north bounds (latitude)
$schema['east'] = "FLOAT(10,6) NOT NULL DEFAULT 0"; // east bounds (longitude)
$schema['zoom'] = "FLOAT(10,6) NOT NULL DEFAULT 0"; // zoom level
$schema['keys']['south'] = "KEY south (south)";
$schema['keys']['west'] = "KEY west (west)";
$schema['keys']['north'] = "KEY north (north)";
$schema['keys']['east'] = "KEY east (east)";
$schema['keys']['bounds'] = "KEY bounds (west, south, east, north)"; // keep an index of bounds
$schema['keys']['data'] = 'FULLTEXT KEY `data` (`data`)';
$schema['keys']['zoom'] = "KEY zoom (zoom)";
return $schema;
}
/**
* Match values for PageFinder
*
* @param DatabaseQuerySelect $query
* @param string $table
* @param string $subfield
* @param string $operator
* @param string $value
* @return DatabaseQuerySelect
*
*/
public function getMatchQuery($query, $table, $subfield, $operator, $value) {
if(!$subfield || $subfield === 'features') {
$subfield = 'data';
}
if(in_array($subfield, ['data', 'Point', 'LineString', 'Polygon'])) {
if(preg_match('/^(-?\d+(\.\d+)?),\s*(-?\d+(\.\d+)?)$/', trim($value, '"'), $matches)) {
// todo regex to handle e-notation
// This is a lnglat query
$lng = round((float) $matches[1], 6);
$lat = round((float) $matches[3], 6);
$table = $this->wire()->database->escapeTable($table);
$a = [];
foreach([
'west<:lng',
'south<:lat',
'east>:lng',
'north>:lat',
] as $q) {
$a[] = "{$table}.{$q}";
}
$query->where('(' . implode(' AND ', $a) . ')', [
':lng' => $lng,
':lat' => $lat,
]);
if($subfield !== 'data') {
$this->addHookAfter('Pages::found()', function(HookEvent $event) use ($table, $subfield, $lng, $lat) {
$pages = $event->arguments(0);
$details = $event->arguments(1);
$selectors = $pages->getSelectors();
$fieldName = str_replace('field_', '', $table);
if($selectors && strpos($selectors, "{$fieldName}.{$subfield}") !== false) {
// Filter features
foreach($pages as $page) {
$item = $page->get($fieldName);
if($item) {
if(!count($item->getFeatures("{$subfield}|Multi{$subfield}", [$lng, $lat]))) {
$pages->remove($page);
}
}
}
$event->replace = true;
$event->return = $pages;
$event->removeHook(null);
}
});
}
}
return $query;
}
if($subfield !== 'data' || $this->wire()->database->isOperator($operator)) {
// if dealing with something other than features, or operator is native to SQL,
// then let Fieldtype::getMatchQuery handle it instead
return parent::getMatchQuery($query, $table, $subfield, $operator, $value);
}
// If we get here, then we're performing either %= (LIKE and variations) or *= (FULLTEXT and variations)
$ft = new DatabaseQuerySelectFulltext($query);
$ft->match($table, $subfield, $operator, $value);
return $query;
}
/**
* Module configuration
*
* @param array $data
* @return InputfieldWrapper
*
*/
public static function getModuleConfigInputfields(array $data) {
$inputfields = new InputfieldWrapper();
$inputfields->add([
'type' => 'text',
'name' => 'style',
'label' => __('Style URL'),
'description' => __('The MapLibre JSON style for the map.'),
'notes' => __('This can either be a full URL or relative to the site root.'),
'required' => true,
'value' => $data['style'] ?? '',
]);
return $inputfields;
}
}