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

LatLongField::geocode()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 11
rs 9.4286
cc 3
eloc 8
nc 3
nop 1
1
<?php
2
3
class LatLongField extends FieldGroup {
4
5
	protected $latField;
6
7
	protected $longField;
8
9
	protected $zoomField;
10
11
	protected $buttonText;
12
13
	private $guidePoints = null;
14
15
	private static $ctr = 0;
16
17
	/**
18
	 * @param string[] $buttonText
19
	 */
20
	public function __construct($children = array(), $buttonText = null) {
21
		self::$ctr++;
22
23
		if ((sizeof($children) < 2) || (sizeof($children) > 3) ||
24
			 (!$children[0] instanceof FormField) ||
25
			 (!$children[1] instanceof FormField)
26
		) {
27
			user_error('LatLongField argument 1 must be an array containing at least two FormField '.
28
				'objects for Lat/Long values, respectively.', E_USER_ERROR);
29
		}
30
		parent::__construct($children);
31
32
		$this->buttonText = $buttonText ? $buttonText : _t('LatLongField.LOOKUP', 'Search');
33
		$this->latField = $children[0]->getName();
34
		$this->longField = $children[1]->getName();
35
36
		if (sizeof($children) == 3) {
37
			$this->zoomField = $children[2]->getName();
38
		}
39
		$name = "";
40
		foreach ($children as $field) {
41
			$name .= $field->getName();
42
		}
43
44
		// hide the lat long and zoom fields from the interface
45
		foreach ($this->FieldList() as $fieldToHide) {
46
			$fieldToHide->addExtraClass('hide');
47
		}
48
49
		$this->name = $name;
50
	}
51
52
53
	public function FieldHolder($properties = array()) {
54
		Requirements::javascript(THIRDPARTY_DIR.'/jquery/jquery.js');
55
		Requirements::javascript(THIRDPARTY_DIR.'/jquery-livequery/jquery.livequery.js');
56
		Requirements::javascript(THIRDPARTY_DIR.'/jquery-metadata/jquery.metadata.js');
57
		Requirements::javascript(MAPPABLE_MODULE_PATH.'/javascript/mapField.js');
58
59
		$attributes = array(
60
			'class' => 'editableMap',
61
			'id' => 'GoogleMap',
62
			'data-LatFieldName' => $this->latField,
63
			'data-LonFieldName' => $this->longField,
64
			'data-ZoomFieldName' => $this->zoomField,
65
			'data-UseMapBounds' => false
66
	   );
67
68
		Requirements::css('mappable/css/mapField.css');
69
70
		// check for and if required add guide points
71
		if (!empty($this->guidePoints)) {
72
			$latlongps = array();
73
74
			foreach ($this->guidePoints as $guidepoint) {
75
				array_push($latlongps, $guidepoint);
76
			}
77
78
			$guidePointsJSON = json_encode($latlongps);
79
			// convert the mappable guidepoints to lat lon
80
81
			$attributes['data-GuidePoints'] = $guidePointsJSON;
82
83
			// we only wish to change the bounds to those of all the points iff
84
			// the item currently has no location
85
			$attributes['data-useMapBounds'] = true;
86
		}
87
		$content = '<div class="editableMapWrapper">'.$this->create_tag(
88
			"div",
89
			$attributes
90
	   ).'</div>';
91
92
		$this->FieldList()->push(new LiteralField('locationEditor', $content));
93
94
		$content2 = <<<HTML
95
<div id="mapSearch">
96
<input name="location_search" id="location_search" size=80/>
97
<button class="action" id="searchLocationButton">Search Location Name</button>
98
	<div id="mapSearchResults">
99
</div>
100
</div>
101
HTML;
102
103
		$this->FieldList()->push(new LiteralField('mapSearch', $content2));
104
105
		return parent::FieldHolder();
106
	}
107
108
	/*
109
	Set guidance points for the map being edited.  For example in a photographic set show the map
110
	position of some other images so that subsequent photo edits do not start with a map centred
111
	at the origin
112
113
	@var newGuidePoints array of points expressed as associative arrays containing keys latitude
114
						and longitude mapping to geographical locations
115
	*/
116
	public function setGuidePoints($newGuidePoints) {
117
		$this->guidePoints = $newGuidePoints;
118
	}
119
120
}
121