Completed
Push — 3.1 ( 9bc055...44afa5 )
by Gordon
19:43
created

MapExtension::BasicMap()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1
Metric Value
dl 0
loc 18
ccs 13
cts 13
cp 1
rs 9.4286
cc 1
eloc 10
nc 1
nop 0
crap 1
1
<?php
2
3
class MapExtension extends DataExtension implements Mappable
4
{
5
    /*
6
     * Template suffix for rendering MapInfoWindow aka  map bubble
7
     */
8
    private static $map_info_window_suffix = '_MapInfoWindow';
0 ignored issues
show
Unused Code introduced by
The property $map_info_window_suffix is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
9
10
    private static $db = array(
0 ignored issues
show
Unused Code introduced by
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
11
        'Lat' => 'Decimal(18,15)',
12
        'Lon' => 'Decimal(18,15)',
13
        'ZoomLevel' => 'Int',
14
        'MapPinEdited' => 'Boolean',
15
    );
16
17
    public static $has_one = array(
18
        'MapPinIcon' => 'Image',
19
    );
20
21
    public static $defaults = array(
22
        'Lat' => 0,
23
        'Lon' => 0,
24
        'Zoom' => 4,
25
        'MapPinEdited' => false,
26
    );
27
28
    /*
29
    Map editing field
30
     */
31
    private $mapField = null;
32
33
    /*
34
    Add a Location tab containing the map
35
    */
36 3
    public function updateCMSFields(FieldList $fields)
37
    {
38
        // These fields need removed, as they may have already been created by the form scaffolding
39 2
        $fields->removeByName('Lat');
40 2
        $fields->removeByName('Lon');
41 2
        $fields->removeByName('ZoomLevel');
42 2
        $fields->removeByName('MapPinIcon');
43 2
        $fields->removeByName('MapPinEdited');
44
45 2
        $fields->addFieldToTab('Root.Location',
46 2
            $this->getMapField()
47 3
        );
48
49 2
        $fields->addFieldToTab('Root.Location', $uf = new UploadField('MapPinIcon',
50 2
            _t('Mappable.MAP_PIN', 'Map Pin Icon.  Leave this blank for default pin to show')));
51 2
        $uf->setFolderName('mapicons');
52 2
    }
53
54 16
    public function getMappableLatitude()
55
    {
56 16
        return $this->owner->Lat;
57
    }
58
59 16
    public function getMappableLongitude()
60
    {
61 16
        return $this->owner->Lon;
62
    }
63
64
    /**
65
     * Renders the map info window for the DataObject.
66
     *
67
     * Be sure to define a template for that, named by the decorated class suffixed with _MapInfoWindow
68
     * e.g. MyPage_MapInfoWindow
69
     *
70
     * You can change the suffix globally by editing the MapExtension.map_info_window_suffix config val
71
     *
72
     * @return string
73
     */
74 9
    public function getMappableMapContent()
75
    {
76 9
        $defaultTemplate = 'MapInfoWindow';
77
        $classTemplate =
78 9
            SSViewer::get_templates_by_class(
79 9
                $this->owner->ClassName,
80 9
                Config::inst()->get('MapExtension', 'map_info_window_suffix')
81 9
            );
82
83 9
        $template = count($classTemplate) ? $classTemplate : $defaultTemplate;
84
85 9
        return MapUtil::sanitize($this->owner->renderWith($template));
86
    }
87
88
    /*
89
    If the marker pin is not at position 0,0 mark the pin as edited. This provides the option of
90
    filtering out (0,0) point which is often irrelevant for plots
91
    */
92 27
    public function onBeforeWrite()
93
    {
94 27
        $latzero = ($this->owner->Lat == 0);
95 27
        $lonzero = ($this->owner->Lon == 0);
96 27
        $latlonzero = $latzero && $lonzero;
97
98
        // if both latitude and longitude still default, do not set the map location as edited
99 27
        if (!$latlonzero) {
100 9
            $this->owner->MapPinEdited = true;
101 9
        }
102 27
    }
103
104
    /*
105
    If a user has uploaded a map pin icon display that, otherwise
106
    */
107 15
    public function getMappableMapPin()
108
    {
109 15
        $result = false;
110 15
        if ($this->owner->MapPinIconID != 0) {
111 2
            $mapPin = $this->owner->MapPinIcon();
112 2
            $result = $mapPin->getAbsoluteURL();
113 2
        } else {
114
            // check for a cached map pin already having been provided for the layer
115 14
            if ($this->owner->CachedMapPinURL) {
116 1
                $result = $this->owner->CachedMapPinURL;
117 1
            }
118
        }
119
120 15
        return $result;
121
    }
122
123
    /*
124
        Check for non zero coordinates, on the assumption that (0,0) will never be the desired coordinates
125
    */
126 10
    public function HasGeo()
0 ignored issues
show
Coding Style introduced by
Method name "MapExtension::HasGeo" is not in camel caps format
Loading history...
127
    {
128 10
        $isOrigin = ($this->owner->Lat == 0) && ($this->owner->Lon == 0);
129 10
        $result = !$isOrigin;
130 10
        if ($this->owner->hasExtension('MapLayerExtension')) {
131 10
            if ($this->owner->MapLayers()->count() > 0) {
132 1
                $result = true;
133 1
            }
134 10
        }
135 10
        $this->owner->extend('updateHasGeo', $result);
136 10
        return $result;
137
    }
138
139
    /*
140
    Render a map at the provided lat,lon, zoom from the editing functions,
141
    */
142 2
    public function BasicMap()
0 ignored issues
show
Coding Style introduced by
Method name "MapExtension::BasicMap" is not in camel caps format
Loading history...
143
    {
144 2
        $map = $this->owner->getRenderableMap()->
145 2
            setZoom($this->owner->ZoomLevel)->
146 2
            setAdditionalCSSClasses('fullWidthMap')->
147 2
            setShowInlineMapDivStyle(true);
148
149 2
        $autozoom = false;
150
151
152 2
153 2
        $this->owner->extend('updateBasicMap', $map, $autozoom);
154 1
155
        $map->setEnableAutomaticCenterZoom($autozoom);
156 1
        $map->setShowInlineMapDivStyle(true);
157 2
158 2
        return $map;
159 2
    }
160
161 2
    /**
162
     * Access the map editing field for the purpose of adding guide points.
163 2
     *
164 2
     * @return [LatLongField] instance of location editing field
0 ignored issues
show
Documentation introduced by
The doc-type [LatLongField] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
165
     */
166 2
    public function getMapField()
167
    {
168
        if (!isset($this->mapField)) {
169
            $this->mapField = new LatLongField(array(
170
                new TextField('Lat', 'Latitude'),
171
                new TextField('Lon', 'Longitude'),
172
                new TextField('ZoomLevel', 'Zoom'),
173
                )
174 3
            );
175
        }
176 3
177 3
        return $this->mapField;
178 3
    }
179 3
180 3
    /**
181
     * Template helper, used to decide whether or not to use compressed assets.
182 3
     */
183 3
    public function UseCompressedAssets()
0 ignored issues
show
Coding Style introduced by
Method name "MapExtension::UseCompressedAssets" is not in camel caps format
Loading history...
184
    {
185 3
        return Config::inst()->get('Mappable', 'use_compressed_assets');
186
    }
187
}
188