| 1 |  |  | <?php | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 3 |  |  | class LatLongField extends FieldGroup { | 
            
                                                                        
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 5 |  |  | 	private static $allowed_actions = array ( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 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 = ''; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 |  |  |  |