1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Smindel\GIS\Forms; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Forms\FormField; |
6
|
|
|
use SilverStripe\View\Requirements; |
7
|
|
|
use SilverStripe\Core\Config\Config; |
8
|
|
|
use Smindel\GIS\GIS; |
9
|
|
|
use Smindel\GIS\ORM\FieldType\DBGeography; |
10
|
|
|
|
11
|
|
|
class MapField extends FormField |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Center for the map widget if the MapField is empty, |
15
|
|
|
* in Long, Lat (EPSG:4326) |
16
|
|
|
* |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
private static $default_location = [ |
|
|
|
|
20
|
|
|
'lon' => 174.78, |
21
|
|
|
'lat' => -41.29 |
22
|
|
|
]; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Zoom level of the map widget if the MapField is empty |
26
|
|
|
* |
27
|
|
|
* @var int |
28
|
|
|
*/ |
29
|
|
|
private static $default_zoom = 13; |
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Whether the user can create complex gemoetries like e.g. MultiPoints |
33
|
|
|
* |
34
|
|
|
* @var boolean |
35
|
|
|
*/ |
36
|
|
|
protected $multiEnabled = false; |
37
|
|
|
|
38
|
|
|
protected $hideFormField = false; |
39
|
|
|
|
40
|
|
|
protected $controls = [ |
41
|
|
|
'polyline' => true, |
42
|
|
|
'polygon' => true, |
43
|
|
|
'marker' => true, |
44
|
|
|
'circle' => false, |
45
|
|
|
'rectangle' => false, |
46
|
|
|
'circlemarker' => false |
47
|
|
|
]; |
48
|
|
|
|
49
|
1 |
|
public function Field($properties = array()) |
50
|
|
|
{ |
51
|
1 |
|
$type = $this->hideFormField ? 'hidden' : 'readonly'; |
52
|
1 |
|
$this->setAttribute($type, $type); |
53
|
1 |
|
$srid = GIS::config()->default_srid; |
54
|
1 |
|
$proj = GIS::config()->projections[$srid]; |
55
|
1 |
|
Requirements::javascript('smindel/silverstripe-gis: client/dist/js/leaflet.js'); |
56
|
1 |
|
Requirements::javascript('smindel/silverstripe-gis: client/dist/js/leaflet-search.js'); |
57
|
1 |
|
Requirements::javascript('smindel/silverstripe-gis: client/dist/js/leaflet.draw.1.0.4.js'); |
58
|
1 |
|
Requirements::javascript('smindel/silverstripe-gis: client/dist/js/proj4.js'); |
59
|
1 |
|
Requirements::javascript('smindel/silverstripe-gis: client/dist/js/wicket.js'); |
60
|
1 |
|
Requirements::javascript('smindel/silverstripe-gis: client/dist/js/MapField.js'); |
61
|
1 |
|
Requirements::customScript(sprintf('proj4.defs("EPSG:%s", "%s");', $srid, $proj), 'EPSG:' . $srid); |
62
|
1 |
|
Requirements::css('smindel/silverstripe-gis: client/dist/css/leaflet.css'); |
63
|
1 |
|
Requirements::css('smindel/silverstripe-gis: client/dist/css/leaflet-search.css'); |
64
|
1 |
|
Requirements::css('smindel/silverstripe-gis: client/dist/css/leaflet.draw.1.0.4.css'); |
65
|
1 |
|
Requirements::css('smindel/silverstripe-gis: client/dist/css/MapField.css'); |
66
|
1 |
|
return parent::Field($properties); |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
public function setValue($value, $data = null) |
70
|
|
|
{ |
71
|
1 |
|
$this->value = $value instanceof DBGeography |
72
|
|
|
? $value->getValue() |
73
|
1 |
|
: $value; |
74
|
|
|
|
75
|
1 |
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
1 |
|
public function setControl($control, $enable = true) |
79
|
|
|
{ |
80
|
1 |
|
if (array_key_exists($control, $this->controls)) { |
81
|
1 |
|
$this->controls[$control] = $enable; |
82
|
|
|
} |
83
|
1 |
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
public function getControls() |
87
|
|
|
{ |
88
|
1 |
|
return json_encode($this->controls); |
89
|
|
|
} |
90
|
|
|
|
91
|
1 |
|
public static function getDefaultSRID() |
92
|
|
|
{ |
93
|
1 |
|
return GIS::config()->default_srid; |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
public function getDefaultLocation() |
97
|
|
|
{ |
98
|
1 |
|
return json_encode(Config::inst()->get(MapField::class, 'default_location')); |
99
|
|
|
} |
100
|
|
|
|
101
|
1 |
|
public static function getDefaultZoom() |
102
|
|
|
{ |
103
|
1 |
|
return Config::inst()->get(MapField::class, 'default_zoom'); |
104
|
|
|
} |
105
|
|
|
|
106
|
1 |
|
public function enableMulti($enable = true) |
107
|
|
|
{ |
108
|
1 |
|
$this->multiEnabled = $enable; |
109
|
1 |
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function hideFormField($hide = true) |
113
|
|
|
{ |
114
|
|
|
$this->hideFormField = $hide; |
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
1 |
|
public function getMultiEnabled() |
119
|
|
|
{ |
120
|
1 |
|
return $this->multiEnabled; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|