Completed
Push — AUTOMATED_TESTING ( 33af86...424fb1 )
by Gordon
04:09
created

LatLongField::getGuidePoints()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

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