1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2013-2015 2amigOS! Consulting Group LLC |
4
|
|
|
* @link http://2amigos.us |
5
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License |
6
|
|
|
*/ |
7
|
|
|
namespace dosamigos\leaflet; |
8
|
|
|
|
9
|
|
|
use dosamigos\leaflet\controls\Control; |
10
|
|
|
use dosamigos\leaflet\layers\Layer; |
11
|
|
|
use dosamigos\leaflet\layers\LayerGroup; |
12
|
|
|
use dosamigos\leaflet\layers\TileLayer; |
13
|
|
|
use dosamigos\leaflet\layers\Polygon; |
14
|
|
|
use dosamigos\leaflet\types\LatLng; |
15
|
|
|
use dosamigos\leaflet\widgets\Map; |
16
|
|
|
use yii\base\Component; |
17
|
|
|
use yii\base\InvalidConfigException; |
18
|
|
|
use yii\base\InvalidParamException; |
19
|
|
|
use yii\helpers\ArrayHelper; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class LeafLet |
23
|
|
|
* @package dosamigos\leaflet |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @property LatLng $center |
28
|
|
|
* |
29
|
|
|
*/ |
30
|
|
|
class LeafLet extends Component |
31
|
|
|
{ |
32
|
|
|
// JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_NUMERIC_CHECK |
33
|
|
|
const JSON_OPTIONS = 352; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var integer a counter used to generate [[name]] for layers. |
37
|
|
|
* @internal |
38
|
|
|
*/ |
39
|
|
|
public static $counter = 0; |
40
|
|
|
/** |
41
|
|
|
* @var string the prefix to the automatically generated object names. |
42
|
|
|
* @see [[generateName()]] |
43
|
|
|
*/ |
44
|
|
|
public static $autoNamePrefix = 'l'; |
45
|
|
|
/** |
46
|
|
|
* @var string the name to give to the variable. The name of the map specified on the |
47
|
|
|
* [[TileLayer]] component overrides this one. |
48
|
|
|
*/ |
49
|
|
|
public $name = 'map'; |
50
|
|
|
/** |
51
|
|
|
* @var int the zoom level of the map |
52
|
|
|
*/ |
53
|
|
|
public $zoom = 13; |
54
|
|
|
/** |
55
|
|
|
* @var array the options for the underlying LeafLetJs JS component. |
56
|
|
|
* Please refer to the LeafLetJs api reference for possible |
57
|
|
|
* [options](http://leafletjs.com/reference.html). |
58
|
|
|
*/ |
59
|
|
|
public $clientOptions = []; |
60
|
|
|
/** |
61
|
|
|
* @var array the event handlers for the underlying LeafletJs JS plugin. |
62
|
|
|
* Please refer to the LeafLetJs js api object options for possible events. |
63
|
|
|
*/ |
64
|
|
|
public $clientEvents = []; |
65
|
|
|
/** |
66
|
|
|
* @var Layer[] holding ui layers (do not confuse with map layers, these are markers, popups, polygons, etc) |
67
|
|
|
*/ |
68
|
|
|
private $_layers = []; |
69
|
|
|
/** |
70
|
|
|
* @var LayerGroup[] holding layer groups |
71
|
|
|
*/ |
72
|
|
|
private $_layerGroups = []; |
73
|
|
|
/** |
74
|
|
|
* @var LatLng sets the center of the map |
75
|
|
|
*/ |
76
|
|
|
private $_center; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Returns the center of the map. |
80
|
|
|
* @return LatLng center of the map. |
81
|
|
|
*/ |
82
|
33 |
|
public function getCenter() |
83
|
|
|
{ |
84
|
33 |
|
return $this->_center; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Sets the center of the map. |
89
|
|
|
* |
90
|
|
|
* @param LatLng $value center of the map. |
91
|
|
|
*/ |
92
|
30 |
|
public function setCenter(LatLng $value) |
93
|
|
|
{ |
94
|
30 |
|
$this->_center = $value; |
95
|
30 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @var Control[] holding controls to be added to the map. |
99
|
|
|
*/ |
100
|
|
|
private $_controls = []; |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param \dosamigos\leaflet\controls\Control[] $controls |
104
|
|
|
* |
105
|
|
|
* @throws \yii\base\InvalidParamException |
106
|
|
|
*/ |
107
|
3 |
|
public function setControls(array $controls) |
108
|
|
|
{ |
109
|
3 |
|
foreach ($controls as $control) { |
110
|
3 |
|
if (!($control instanceof Control)) { |
111
|
3 |
|
throw new InvalidParamException("All controls must be of type Control."); |
112
|
|
|
} |
113
|
3 |
|
} |
114
|
3 |
|
$this->_controls = $controls; |
115
|
3 |
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return \dosamigos\leaflet\controls\Control[] |
119
|
|
|
*/ |
120
|
21 |
|
public function getControls() |
121
|
|
|
{ |
122
|
21 |
|
return $this->_controls; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param Control $control |
127
|
|
|
*/ |
128
|
3 |
|
public function addControl(Control $control) |
129
|
|
|
{ |
130
|
3 |
|
$this->_controls[] = $control; |
131
|
3 |
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @var \dosamigos\leaflet\layers\TileLayer |
135
|
|
|
*/ |
136
|
|
|
private $_tileLayer; |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param \dosamigos\leaflet\layers\TileLayer $tileLayer |
140
|
|
|
* |
141
|
|
|
* @return static the component itself |
142
|
|
|
*/ |
143
|
18 |
|
public function setTileLayer(TileLayer $tileLayer) |
144
|
|
|
{ |
145
|
18 |
|
if (!empty($tileLayer->map) && strcmp($tileLayer->map, $this->name) !== 0) { |
146
|
3 |
|
$this->name = $tileLayer->map; |
147
|
3 |
|
} |
148
|
18 |
|
if (empty($tileLayer->map)) { |
149
|
15 |
|
$tileLayer->map = $this->name; |
150
|
15 |
|
} |
151
|
18 |
|
$this->_tileLayer = $tileLayer; |
152
|
|
|
|
153
|
18 |
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @return \dosamigos\leaflet\layers\TileLayer |
158
|
|
|
*/ |
159
|
15 |
|
public function getTileLayer() |
160
|
|
|
{ |
161
|
15 |
|
return $this->_tileLayer; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @var array holds the js script lines to be registered. |
166
|
|
|
*/ |
167
|
|
|
private $_js = []; |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param string|array $js custom javascript code to be registered. |
171
|
|
|
* *Warning*: This method overrides any previous settings. |
172
|
|
|
* |
173
|
|
|
* @return static the component itself |
174
|
|
|
*/ |
175
|
3 |
|
public function setJs($js) |
176
|
|
|
{ |
177
|
3 |
|
$this->_js = is_array($js) ? $js : [$js]; |
178
|
3 |
|
return $this; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param string $js appends javascript code to be registered. |
183
|
|
|
* |
184
|
|
|
* @return static the component itself |
185
|
|
|
*/ |
186
|
3 |
|
public function appendJs($js) |
187
|
|
|
{ |
188
|
3 |
|
$this->_js[] = $js; |
189
|
3 |
|
return $this; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @return array the queued javascript code to be registered. |
194
|
|
|
* *Warning*: This method does not include map initialization. |
195
|
|
|
*/ |
196
|
18 |
|
public function getJs() |
197
|
|
|
{ |
198
|
18 |
|
$js = []; |
199
|
18 |
|
foreach ($this->getLayers() as $layer) { |
200
|
|
|
|
201
|
18 |
|
if ($layer instanceof Polygon) { |
202
|
3 |
|
$layerJs = $layer->encode(); |
203
|
3 |
|
$insertAtTheBottom = $layer->insertAtTheBottom ? 'true' : 'false'; |
204
|
3 |
|
$js[] = "$this->name.addLayer($layerJs, $insertAtTheBottom);"; |
205
|
3 |
|
continue; |
206
|
|
|
} |
207
|
15 |
|
$layer->map = $this->name; |
208
|
15 |
|
$js[] = $layer->encode(); |
209
|
18 |
|
} |
210
|
18 |
|
$groups = $this->getEncodedLayerGroups($this->getLayerGroups()); |
211
|
18 |
|
$controls = $this->getEncodedControls($this->getControls()); |
212
|
18 |
|
$plugins = $this->getEncodedPlugins($this->getPlugins()->getInstalledPlugins()); |
213
|
18 |
|
$js = ArrayHelper::merge($js, $groups); |
214
|
18 |
|
$js = ArrayHelper::merge($js, $controls); |
215
|
18 |
|
$js = ArrayHelper::merge($js, $plugins); |
216
|
18 |
|
$js = ArrayHelper::merge($js, $this->_js); |
217
|
18 |
|
return $js; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @var PluginManager |
222
|
|
|
*/ |
223
|
|
|
private $_plugins; |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @return PluginManager |
227
|
|
|
*/ |
228
|
21 |
|
public function getPlugins() |
229
|
|
|
{ |
230
|
21 |
|
return $this->_plugins; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Installs a plugin |
235
|
|
|
* |
236
|
|
|
* @param Plugin $plugin |
237
|
|
|
*/ |
238
|
18 |
|
public function installPlugin(Plugin $plugin) |
239
|
|
|
{ |
240
|
18 |
|
$plugin->map = $this->name; |
241
|
18 |
|
$this->getPlugins()->install($plugin); |
242
|
18 |
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Removes an installed plugin |
246
|
|
|
* |
247
|
|
|
* @param $plugin |
248
|
|
|
* |
249
|
|
|
* @return mixed|null |
250
|
|
|
*/ |
251
|
3 |
|
public function removePlugin($plugin) |
252
|
|
|
{ |
253
|
3 |
|
return $this->getPlugins()->remove($plugin); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Initializes the widget. |
258
|
|
|
*/ |
259
|
33 |
|
public function init() |
260
|
|
|
{ |
261
|
33 |
|
parent::init(); |
262
|
33 |
|
if (empty($this->center) || empty($this->zoom)) { |
263
|
3 |
|
throw new InvalidConfigException("'center' and/or 'zoom' attributes cannot be empty."); |
264
|
|
|
} |
265
|
30 |
|
$this->_plugins = new PluginManager(); |
266
|
30 |
|
$this->clientOptions['center'] = $this->center->toArray(true); |
267
|
30 |
|
$this->clientOptions['zoom'] = $this->zoom; |
268
|
30 |
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Helper method to render the widget. It is also possible to use the widget directly: |
272
|
|
|
* ``` |
273
|
|
|
* echo Map::widget(['leafLet' => $leafLetObject, ...]); |
274
|
|
|
* ``` |
275
|
|
|
* |
276
|
|
|
* @param array $config |
277
|
|
|
* |
278
|
|
|
* @return string |
279
|
|
|
*/ |
280
|
12 |
|
public function widget($config = []) |
281
|
|
|
{ |
282
|
12 |
|
ob_start(); |
283
|
12 |
|
ob_implicit_flush(false); |
284
|
12 |
|
$config['leafLet'] = $this; |
285
|
12 |
|
$widget = new Map($config); |
286
|
12 |
|
$out = $widget->run(); |
287
|
12 |
|
return ob_get_clean() . $out; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* @param Layer $layer the layer script to add to the js script code. It could be any object extending from [[Layer]] |
292
|
|
|
* component (markers, polylines, popup, etc) |
293
|
|
|
* |
294
|
|
|
* @return static the component itself |
295
|
|
|
*/ |
296
|
18 |
|
public function addLayer(Layer $layer) |
297
|
|
|
{ |
298
|
18 |
|
$this->_layers[] = $layer; |
299
|
18 |
|
return $this; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* @return Layer[] the stored layers |
304
|
|
|
*/ |
305
|
18 |
|
public function getLayers() |
306
|
|
|
{ |
307
|
18 |
|
return $this->_layers; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* @param LayerGroup $group sets a layer group |
312
|
|
|
* |
313
|
|
|
* @return static the component itself |
314
|
|
|
*/ |
315
|
6 |
|
public function addLayerGroup(LayerGroup $group) |
316
|
|
|
{ |
317
|
6 |
|
$this->_layerGroups[] = $group; |
318
|
6 |
|
return $this; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* @return layers\LayerGroup[] all stored layer groups |
323
|
|
|
*/ |
324
|
21 |
|
public function getLayerGroups() |
325
|
|
|
{ |
326
|
21 |
|
return $this->_layerGroups; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Clears all stored layer groups |
331
|
|
|
* @return static the component itself |
332
|
|
|
*/ |
333
|
3 |
|
public function clearLayerGroups() |
334
|
|
|
{ |
335
|
3 |
|
$this->_layerGroups = []; |
336
|
3 |
|
return $this; |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* @param Control[] $controls |
341
|
|
|
* |
342
|
|
|
* @return array |
343
|
|
|
*/ |
344
|
18 |
|
public function getEncodedControls($controls) |
345
|
|
|
{ |
346
|
18 |
|
return $this->getEncodedObjects($controls); |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* @param LayerGroup[] $groups |
351
|
|
|
* |
352
|
|
|
* @return array |
353
|
|
|
*/ |
354
|
18 |
|
public function getEncodedLayerGroups($groups) |
355
|
|
|
{ |
356
|
18 |
|
return $this->getEncodedObjects($groups); |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* @param Plugin[] $plugins |
361
|
|
|
* |
362
|
|
|
* @return array |
363
|
|
|
*/ |
364
|
18 |
|
public function getEncodedPlugins($plugins) |
365
|
|
|
{ |
366
|
18 |
|
return $this->getEncodedObjects($plugins); |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
/** |
370
|
|
|
* @return string |
371
|
|
|
*/ |
372
|
45 |
|
public static function generateName() |
373
|
|
|
{ |
374
|
45 |
|
return self::$autoNamePrefix . self::$counter++; |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* @param $objects |
379
|
|
|
* |
380
|
|
|
* @return array |
381
|
|
|
*/ |
382
|
18 |
|
protected function getEncodedObjects($objects) |
383
|
|
|
{ |
384
|
18 |
|
$js = []; |
385
|
18 |
|
foreach ((array)$objects as $object) { |
386
|
15 |
|
if (property_exists($object, 'map')) { |
387
|
15 |
|
$object->map = $this->name; |
388
|
15 |
|
} |
389
|
15 |
|
$js[] = method_exists($object, 'encode') ? $object->encode() : null; |
390
|
18 |
|
} |
391
|
18 |
|
return array_filter($js); |
392
|
|
|
} |
393
|
|
|
} |
394
|
|
|
|