Passed
Pull Request — master (#25)
by Gordon
06:28
created

MapField::getInitialLayer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
The private property $default_location is not used, and could be removed.
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
introduced by
The private property $initial_layer is not used, and could be removed.
Loading history...
31
32
    /**
33
     * Whether the user can create complex gemoetries like e.g. MultiPoints
34
     *
35
     * @var boolean
36
     */
37
    protected $multiEnabled = false;
38
39
    protected $hideFormField = false;
40
41
    protected $controls = [
42
        'polyline' => true,
43
        'polygon' => true,
44
        'marker' => true,
45
        'circle' => false,
46
        'rectangle' => false,
47
        'circlemarker' => false
48
    ];
49
50 1
    public function Field($properties = array())
51
    {
52 1
        $type = $this->hideFormField ? 'hidden' : 'readonly';
53 1
        $this->setAttribute($type, $type);
54 1
        $srid = GIS::config()->default_srid;
55 1
        $proj = GIS::config()->projections[$srid];
56 1
        Requirements::javascript('smindel/silverstripe-gis: client/dist/js/leaflet.js');
57 1
        Requirements::javascript('smindel/silverstripe-gis: client/dist/js/leaflet-search.js');
58 1
        Requirements::javascript('smindel/silverstripe-gis: client/dist/js/leaflet.draw.1.0.4.js');
59 1
        Requirements::javascript('smindel/silverstripe-gis: client/dist/js/proj4.js');
60 1
        Requirements::javascript('smindel/silverstripe-gis: client/dist/js/wicket.js');
61 1
        Requirements::javascript('smindel/silverstripe-gis: client/dist/js/MapField.js');
62 1
        Requirements::customScript(sprintf('proj4.defs("EPSG:%s", "%s");', $srid, $proj), 'EPSG:' . $srid);
63 1
        Requirements::css('smindel/silverstripe-gis: client/dist/css/leaflet.css');
64 1
        Requirements::css('smindel/silverstripe-gis: client/dist/css/leaflet-search.css');
65 1
        Requirements::css('smindel/silverstripe-gis: client/dist/css/leaflet.draw.1.0.4.css');
66 1
        Requirements::css('smindel/silverstripe-gis: client/dist/css/MapField.css');
67 1
        return parent::Field($properties);
68
    }
69
70 1
    public function setValue($value, $data = null)
71
    {
72 1
        $this->value = $value instanceof DBGeography
73
            ? $value->getValue()
74 1
            : $value;
75
76 1
        return $this;
77
    }
78
79 1
    public function setControl($control, $enable = true)
80
    {
81 1
        if (array_key_exists($control, $this->controls)) {
82 1
            $this->controls[$control] = $enable;
83
        }
84 1
        return $this;
85
    }
86
87 1
    public function getControls()
88
    {
89 1
        return json_encode($this->controls);
90
    }
91
92 1
    public static function getDefaultSRID()
93
    {
94 1
        return GIS::config()->default_srid;
95
    }
96
97 1
    public function getDefaultLocation()
98
    {
99 1
        return json_encode(Config::inst()->get(MapField::class, 'default_location'));
100
    }
101
102 1
    public function enableMulti($enable = true)
103
    {
104 1
        $this->multiEnabled = $enable;
105 1
        return $this;
106
    }
107
108
    public function hideFormField($hide = true)
109
    {
110
        $this->hideFormField = $hide;
111
        return $this;
112
    }
113
114 1
    public function getMultiEnabled()
115
    {
116 1
        return $this->multiEnabled;
117
    }
118
119 1
    public function getInitialLayer()
120
    {
121 1
        return Config::inst()->get(MapField::class, 'initial_layer');
122
    }
123
}
124