Completed
Branch AUTOMATED_TESTING (09d9f2)
by Gordon
13:24
created

LatLongField::hasData()   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
	private static $allowed_actions = array (
2 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $allowed_actions 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...
6
		'geocode'
7
	);
8
9
10
	protected $latField;
11
12
	protected $longField;
13
14
	protected $zoomField;
15
16
	protected $buttonText;
17
18
	private $guidePoints = null;
19
20
	private static $ctr = 0;
21
22
	public function __construct($children = array(), $buttonText = null) {
23
		self::$ctr++;
24
25
		if ((sizeof($children) < 2) || (sizeof($children) > 3) ||
26
			 (!$children[0] instanceof FormField) ||
27
			 (!$children[1] instanceof FormField)
28
		) {
29
			user_error('LatLongField argument 1 must be an array containing at least two FormField '.
30
				'objects for Lat/Long values, respectively.', E_USER_ERROR);
31
		}
32
		parent::__construct($children);
33
34
		$this->buttonText = $buttonText ? $buttonText : _t('LatLongField.LOOKUP', 'Search');
35
		$this->latField = $children[0]->getName();
36
		$this->longField = $children[1]->getName();
37
38
		if (sizeof($children) == 3) {
39
			$this->zoomField = $children[2]->getName();
40
		}
41
		$name = "";
42
		foreach ($children as $field) {
43
			$name .= $field->getName();
44
		}
45
46
		// hide the lat long and zoom fields from the interface
47
		foreach ($this->FieldList() as $fieldToHide) {
48
			$fieldToHide->addExtraClass('hide');
49
		}
50
51
		$this->name = $name;
52
	}
53
54
55
	public function FieldHolder($properties = array()) {
56
		Requirements::javascript(THIRDPARTY_DIR.'/jquery/jquery.js');
57
		Requirements::javascript(THIRDPARTY_DIR.'/jquery-livequery/jquery.livequery.js');
58
		Requirements::javascript(THIRDPARTY_DIR.'/jquery-metadata/jquery.metadata.js');
59
		Requirements::javascript(MAPPABLE_MODULE_PATH.'/javascript/mapField.js');
60
61
		$attributes = array(
62
            'class' => 'editableMap',
63
            'id' => 'GoogleMap',
64
            'data-LatFieldName' => $this->latField,
65
			'data-LonFieldName' => $this->longField,
66
			'data-ZoomFieldName' => $this->zoomField,
67
			'data-UseMapBounds' => false
68
       );
69
70
        Requirements::css('mappable/css/mapField.css');
71
        $guidePointsJSON = '';
0 ignored issues
show
Unused Code introduced by
$guidePointsJSON is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
72
        if (!empty($this->guidePoints)) {
73
        	$latlongps = array();
74
75
			foreach ($this->guidePoints as $guidepoint) {
76
				array_push($latlongps, $guidepoint);
77
			}
78
79
        	$guidePointsJSON = json_encode($latlongps);
80
        	// convert the mappable guidepoints to lat lon
81
82
        	$attributes['data-GuidePoints'] = $guidePointsJSON;
83
84
        	// we only wish to change the bounds to those of all the points iff
85
        	// the item currently has no location
86
        	$attributes['data-useMapBounds'] = true;
87
        }
88
        $content = '<div class="editableMapWrapper">' . $this->createTag(
0 ignored issues
show
Deprecated Code introduced by
The method FormField::createTag() has been deprecated with message: 4.0 Use FormField::create_tag()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
89
            "div",
90
            $attributes
91
       ) . '</div>';
92
93
        $this->FieldList()->push(new LiteralField('locationEditor', $content));
94
95
		$content2 = <<<HTML
96
<div id="mapSearch">
97
<input name="location_search" id="location_search" size=80/>
98
<button class="action" id="searchLocationButton">Search Location Name</button>
99
	<div id="mapSearchResults">
100
</div>
101
</div>
102
HTML;
103
104
		$this->FieldList()->push(new LiteralField('mapSearch', $content2));
105
106
		return parent::FieldHolder();
107
	}
108
109
110
	/*
111
	Perform place name search as a means of navigation when editing locations
112
	*/
113
	public function geocode(SS_HTTPRequest $r) {
114
		if ($address = $r->requestVar('address')) {
115
			if ($json = @file_get_contents(
116
				"http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=".
117
				urlencode($address))) {
118
				$response = Convert::json2array($json);
119
				$location = $response['results'][0]->geometry->location;
120
				return new SS_HTTPResponse($location->lat.",".$location->lng);
121
			}
122
		}
123
	}
124
125
126
	/*
127
	Set guidance points for the map being edited.  For example in a photographic set show the map
128
	position of some other images so that subsequent photo edits do not start with a map centred
129
	at the origin
130
131
	@var newGuidePoints array of points expressed as associative arrays containing keys latitude
132
						and longitude mapping to geographical locations
133
	*/
134
	public function setGuidePoints($newGuidePoints) {
135
		$this->guidePoints = $newGuidePoints;
136
	}
137
138
}
139