smindel /
silverstripe-gis
| 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 = [ |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 20 | 'lon' => 174.78, |
||
| 21 | 'lat' => -41.29 |
||
| 22 | ]; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * The initial layer to show when editing. The only other permitted value is 'satellite', anything else will |
||
| 26 | * default to 'streets' in order that a map appears in the editing widget |
||
| 27 | * |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | private static $initial_layer = 'streets'; |
||
|
0 ignored issues
–
show
|
|||
| 31 | |||
| 32 | /** |
||
| 33 | * Zoom level of the map widget if the MapField is empty |
||
| 34 | * |
||
| 35 | * @var int |
||
| 36 | */ |
||
| 37 | private static $default_zoom = 13; |
||
|
0 ignored issues
–
show
|
|||
| 38 | |||
| 39 | /** |
||
| 40 | * Whether the user can create complex gemoetries like e.g. MultiPoints |
||
| 41 | * |
||
| 42 | * @var boolean |
||
| 43 | */ |
||
| 44 | protected $multiEnabled = false; |
||
| 45 | |||
| 46 | protected $hideFormField = false; |
||
| 47 | |||
| 48 | protected $controls = [ |
||
| 49 | 'polyline' => true, |
||
| 50 | 'polygon' => true, |
||
| 51 | 'marker' => true, |
||
| 52 | 'circle' => false, |
||
| 53 | 'rectangle' => false, |
||
| 54 | 'circlemarker' => false |
||
| 55 | ]; |
||
| 56 | |||
| 57 | 1 | public function Field($properties = array()) |
|
| 58 | { |
||
| 59 | 1 | $type = $this->hideFormField ? 'hidden' : 'readonly'; |
|
| 60 | 1 | $this->setAttribute($type, $type); |
|
| 61 | 1 | $srid = GIS::config()->default_srid; |
|
| 62 | 1 | $proj = GIS::config()->projections[$srid]; |
|
| 63 | 1 | Requirements::javascript('smindel/silverstripe-gis: client/dist/js/leaflet.js'); |
|
| 64 | 1 | Requirements::javascript('smindel/silverstripe-gis: client/dist/js/leaflet-search.js'); |
|
| 65 | 1 | Requirements::javascript('smindel/silverstripe-gis: client/dist/js/leaflet.draw.1.0.4.js'); |
|
| 66 | 1 | Requirements::javascript('smindel/silverstripe-gis: client/dist/js/proj4.js'); |
|
| 67 | 1 | Requirements::javascript('smindel/silverstripe-gis: client/dist/js/wicket.js'); |
|
| 68 | 1 | Requirements::javascript('smindel/silverstripe-gis: client/dist/js/MapField.js'); |
|
| 69 | 1 | Requirements::customScript(sprintf('proj4.defs("EPSG:%s", "%s");', $srid, $proj), 'EPSG:' . $srid); |
|
| 70 | 1 | Requirements::css('smindel/silverstripe-gis: client/dist/css/leaflet.css'); |
|
| 71 | 1 | Requirements::css('smindel/silverstripe-gis: client/dist/css/leaflet-search.css'); |
|
| 72 | 1 | Requirements::css('smindel/silverstripe-gis: client/dist/css/leaflet.draw.1.0.4.css'); |
|
| 73 | 1 | Requirements::css('smindel/silverstripe-gis: client/dist/css/MapField.css'); |
|
| 74 | 1 | return parent::Field($properties); |
|
| 75 | } |
||
| 76 | |||
| 77 | 1 | public function setValue($value, $data = null) |
|
| 78 | { |
||
| 79 | 1 | $this->value = $value instanceof DBGeography |
|
| 80 | ? $value->getValue() |
||
| 81 | 1 | : $value; |
|
| 82 | |||
| 83 | 1 | return $this; |
|
| 84 | } |
||
| 85 | |||
| 86 | 1 | public function setControl($control, $enable = true) |
|
| 87 | { |
||
| 88 | 1 | if (array_key_exists($control, $this->controls)) { |
|
| 89 | 1 | $this->controls[$control] = $enable; |
|
| 90 | } |
||
| 91 | 1 | return $this; |
|
| 92 | } |
||
| 93 | |||
| 94 | 1 | public function getControls() |
|
| 95 | { |
||
| 96 | 1 | return json_encode($this->controls); |
|
| 97 | } |
||
| 98 | |||
| 99 | 1 | public static function getDefaultSRID() |
|
| 100 | { |
||
| 101 | 1 | return GIS::config()->default_srid; |
|
| 102 | } |
||
| 103 | |||
| 104 | 1 | public function getDefaultLocation() |
|
| 105 | { |
||
| 106 | 1 | return json_encode(Config::inst()->get(MapField::class, 'default_location')); |
|
| 107 | } |
||
| 108 | |||
| 109 | 1 | public static function getDefaultZoom() |
|
| 110 | { |
||
| 111 | 1 | return Config::inst()->get(MapField::class, 'default_zoom'); |
|
| 112 | } |
||
| 113 | |||
| 114 | 1 | public function enableMulti($enable = true) |
|
| 115 | { |
||
| 116 | 1 | $this->multiEnabled = $enable; |
|
| 117 | 1 | return $this; |
|
| 118 | } |
||
| 119 | |||
| 120 | public function hideFormField($hide = true) |
||
| 121 | { |
||
| 122 | $this->hideFormField = $hide; |
||
| 123 | return $this; |
||
| 124 | } |
||
| 125 | |||
| 126 | 1 | public function getMultiEnabled() |
|
| 127 | { |
||
| 128 | 1 | return $this->multiEnabled; |
|
| 129 | } |
||
| 130 | |||
| 131 | 1 | public function getInitialLayer() |
|
| 132 | { |
||
| 133 | 1 | return Config::inst()->get(MapField::class, 'initial_layer'); |
|
| 134 | } |
||
| 135 | } |
||
| 136 |