Completed
Push — AUTOMATED_TESTING ( aecfa5...b401db )
by Gordon
12:37
created

MapExtension   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 220
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 9
Metric Value
wmc 21
lcom 2
cbo 9
dl 0
loc 220
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A updateCMSFields() 0 16 1
A getMappableLatitude() 0 3 1
A getMappableLongitude() 0 3 1
A getMappableMapContent() 0 11 2
A onBeforeWrite() 0 10 3
A getMappableMapPin() 0 14 3
B HasGeo() 0 22 4
A BasicMap() 0 50 3
A getMapField() 0 11 2
A UseCompressedAssets() 0 3 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
	static $has_one = array(
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $has_one.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
18
		'MapPinIcon' => 'Image'
19
	);
20
21
	static $defaults = array (
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $defaults.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
22
		'Lat' =>0,
23
		'Lon' => 0,
24
		'Zoom' => 4,
25
		'MapPinEdited' => false
26
	);
27
28
29
	/*
30
	Add a Location tab containing the map
31
	*/
32
	public function updateCMSFields(FieldList $fields) {
33
		// These fields need removed, as they may have already been created by the form scaffolding
34
		$fields->removeByName('Lat');
35
		$fields->removeByName('Lon');
36
		$fields->removeByName('ZoomLevel');
37
		$fields->removeByName('MapPinIcon');
38
		$fields->removeByName('MapPinEdited');
39
40
		$fields->addFieldToTab("Root.Location",
41
			$this->getMapField()
42
		);
43
44
		$fields->addFieldToTab('Root.Location', $uf = new UploadField('MapPinIcon',
45
			_t('Mappable.MAP_PIN', 'Map Pin Icon.  Leave this blank for default pin to show')));
46
		$uf->setFolderName('mapicons');
47
	}
48
49
50
	public function getMappableLatitude() {
51
		return $this->owner->Lat;
52
	}
53
54
	public function getMappableLongitude() {
55
		return $this->owner->Lon;
56
	}
57
58
59
	/**
60
	 * Renders the map info window for the DataObject.
61
	 *
62
	 * Be sure to define a template for that, named by the decorated class suffixed with _MapInfoWindow
63
	 * e.g. MyPage_MapInfoWindow
64
	 *
65
	 * You can change the suffix globally by editing the MapExtension.map_info_window_suffix config val
66
	 *
67
	 * @return string
68
	 */
69
	public function getMappableMapContent() {
70
		$defaultTemplate = 'MapInfoWindow';
71
		$classTemplate =
72
			SSViewer::get_templates_by_class(
73
				$this->owner->ClassName,
74
				Config::inst()->get('MapExtension', 'map_info_window_suffix')
75
			);
76
77
		$template = count($classTemplate) ? $classTemplate : $defaultTemplate;
78
		return MapUtil::sanitize($this->owner->renderWith($template));
79
	}
80
81
82
	/*
83
	If the marker pin is not at position 0,0 mark the pin as edited. This provides the option of
84
	filtering out (0,0) point which is often irrelevant for plots
85
	*/
86
	public function onBeforeWrite() {
87
		$latzero = ($this->owner->Lat == 0);
88
		$lonzero = ($this->owner->Lon == 0);
89
		$latlonzero = $latzero && $lonzero;
90
91
		// if both latitude and longitude still default, do not set the map location as edited
92
		if (!$latlonzero) {
93
		  $this->owner->MapPinEdited = true;
94
		}
95
	}
96
97
98
	/*
99
	If a user has uploaded a map pin icon display that, otherwise
100
	*/
101
	public function getMappableMapPin() {
102
		$result = false;
103
		if ($this->owner->MapPinIconID != 0) {
104
			$mapPin = $this->owner->MapPinIcon();
105
			$result = $mapPin->getAbsoluteURL();
106
			$this->CachedMapPinURL = $result;
0 ignored issues
show
Bug introduced by
The property CachedMapPinURL does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
107
		} else {
108
		  // check for a cached map pin already having been provided for the layer
109
			if ($this->owner->CachedMapPinURL) {
110
				$result = $this->owner->CachedMapPinURL;
111
		  	}
112
		}
113
		return $result;
114
	}
115
116
117
	/*
118
		Check for non zero coordinates, on the assumption that (0,0) will never be the desired coordinates
119
	*/
120
	public function HasGeo() {
121
		$isOrigin = ($this->owner->Lat == 0) && ($this->owner->Lon == 0);
122
		$result = !$isOrigin;
123
		if ($this->owner->hasExtension('MapLayerExtension')) {
124
			if ($this->owner->MapLayers()->count() > 0) {
125
				$result = true;
126
			}
127
		}
128
129
130
		$this->owner->extend('updateHasGeo', $result);
131
		/**
132
		 * FIXME - move this to PointsOfInterest module
133
		if ($this->owner->hasExtension('PointsOfInterestLayerExtension')) {
134
			if ($this->owner->PointsOfInterestLayers()->count() > 0) {
135
				$result = true;
136
			}
137
		}
138
		 */
139
140
		return $result;
141
	}
142
143
144
	/*
145
	Render a map at the provided lat,lon, zoom from the editing functions,
146
	*/
147
	public function BasicMap() {
148
		$map = $this->owner->getRenderableMap()->
149
			setZoom($this->owner->ZoomLevel)->
150
			setAdditionalCSSClasses('fullWidthMap')->
151
			setShowInlineMapDivStyle(true);
152
153
		$autozoom = false;
154
155
		// add any KML map layers
156
		if (Object::has_extension($this->owner->ClassName, 'MapLayerExtension')) {
157
		  foreach($this->owner->MapLayers() as $layer) {
158
			$map->addKML($layer->KmlFile()->getAbsoluteURL());
159
			// we have a layer, so turn on autozoom
160
			$autozoom = true;
161
		  }
162
			$map->setEnableAutomaticCenterZoom(true);
163
		}
164
165
		$this->owner->extend('updateBasicMap', $map);
166
167
		/**
168
		FIXME - move to POI module
169
		if (Object::has_extension($this->owner->ClassName, 'PointsOfInterestLayerExtension')) {
170
			foreach($this->owner->PointsOfInterestLayers() as $layer) {
171
				$layericon = $layer->DefaultIcon();
172
				if ($layericon->ID === 0) {
173
					$layericon = null;
174
				}
175
				foreach ($layer->PointsOfInterest() as $poi) {
176
					if ($poi->MapPinEdited) {
177
						if ($poi->MapPinIconID == 0 && $layericon) {
178
							$poi->CachedMapPinURL = $layericon->getAbsoluteURL();
179
						}
180
						$map->addMarkerAsObject($poi);
181
182
						// we have a point of interest, so turn on auto zoom
183
						$autozoom = true;
184
					}
185
				}
186
			}
187
			$map->setClusterer(true);
188
		}
189
190
		**/
191
192
		$map->setEnableAutomaticCenterZoom($autozoom);
193
		$map->setShowInlineMapDivStyle(true);
194
195
		return $map;
196
	}
197
198
199
	/**
200
	 * Access the map editing field for the purpose of adding guide points
201
	 * @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...
202
	 */
203
	public function getMapField() {
204
		if (!isset($this->mapField)) {
205
			$this->mapField = new LatLongField(array(
0 ignored issues
show
Bug introduced by
The property mapField does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
206
				new TextField('Lat', 'Latitude'),
207
				new TextField('Lon', 'Longitude'),
208
				new TextField('ZoomLevel', 'Zoom')
209
				)
210
			);
211
		}
212
		return $this->mapField;
213
	}
214
215
216
	/**
217
	 * Template helper, used to decide whether or not to use compressed assets
218
	 */
219
	public function UseCompressedAssets() {
220
		return Config::inst()->get('Mappable', 'use_compressed_assets');
221
	}
222
}
223