@@ -210,6 +210,9 @@ |
||
| 210 | 210 | return $fieldNames[$name]; |
| 211 | 211 | } |
| 212 | 212 | |
| 213 | + /** |
|
| 214 | + * @param string $name |
|
| 215 | + */ |
|
| 213 | 216 | protected function recordFieldData($name) |
| 214 | 217 | { |
| 215 | 218 | $fieldName = $this->childFieldName($name); |
@@ -12,285 +12,285 @@ |
||
| 12 | 12 | class GoogleMapField extends FormField |
| 13 | 13 | { |
| 14 | 14 | |
| 15 | - protected $data; |
|
| 16 | - |
|
| 17 | - /** |
|
| 18 | - * @var FormField |
|
| 19 | - */ |
|
| 20 | - protected $latField; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * @var FormField |
|
| 24 | - */ |
|
| 25 | - protected $lngField; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * @var FormField |
|
| 29 | - */ |
|
| 30 | - protected $zoomField; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * The merged version of the default and user specified options |
|
| 34 | - * @var array |
|
| 35 | - */ |
|
| 36 | - protected $options = array(); |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * @param DataObject $data The controlling dataobject |
|
| 40 | - * @param string $title The title of the field |
|
| 41 | - * @param array $options Various settings for the field |
|
| 42 | - */ |
|
| 43 | - public function __construct(DataObject $data, $title, $options = array()) |
|
| 44 | - { |
|
| 45 | - $this->data = $data; |
|
| 46 | - |
|
| 47 | - // Set up fieldnames |
|
| 48 | - $this->setupOptions($options); |
|
| 49 | - |
|
| 50 | - $this->setupChildren(); |
|
| 51 | - |
|
| 52 | - parent::__construct($this->getName(), $title); |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - // Auto generate a name |
|
| 56 | - public function getName() |
|
| 57 | - { |
|
| 58 | - $fieldNames = $this->getOption('field_names'); |
|
| 59 | - return sprintf( |
|
| 60 | - '%s_%s_%s', |
|
| 61 | - $this->data->class, |
|
| 62 | - $fieldNames['Latitude'], |
|
| 63 | - $fieldNames['Longitude'] |
|
| 64 | - ); |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * Merge options preserving the first level of array keys |
|
| 69 | - * @param array $options |
|
| 70 | - */ |
|
| 71 | - public function setupOptions(array $options) |
|
| 72 | - { |
|
| 73 | - $this->options = static::config()->default_options; |
|
| 74 | - foreach ($this->options as $name => &$value) { |
|
| 75 | - if (isset($options[$name])) { |
|
| 76 | - if (is_array($value)) { |
|
| 77 | - $value = array_merge($value, $options[$name]); |
|
| 78 | - } else { |
|
| 79 | - $value = $options[$name]; |
|
| 80 | - } |
|
| 81 | - } |
|
| 82 | - } |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * Set up child hidden fields, and optionally the search box. |
|
| 87 | - * @return FieldList the children |
|
| 88 | - */ |
|
| 89 | - public function setupChildren() |
|
| 90 | - { |
|
| 91 | - $name = $this->getName(); |
|
| 92 | - |
|
| 93 | - // Create the latitude/longitude hidden fields |
|
| 94 | - $this->latField = HiddenField::create( |
|
| 95 | - $name.'[Latitude]', |
|
| 96 | - 'Lat', |
|
| 97 | - $this->recordFieldData('Latitude') |
|
| 98 | - )->addExtraClass('googlemapfield-latfield'); |
|
| 99 | - |
|
| 100 | - $this->lngField = HiddenField::create( |
|
| 101 | - $name.'[Longitude]', |
|
| 102 | - 'Lng', |
|
| 103 | - $this->recordFieldData('Longitude') |
|
| 104 | - )->addExtraClass('googlemapfield-lngfield'); |
|
| 105 | - |
|
| 106 | - $this->zoomField = HiddenField::create( |
|
| 107 | - $name.'[Zoom]', |
|
| 108 | - 'Zoom', |
|
| 109 | - $this->recordFieldData('Zoom') |
|
| 110 | - )->addExtraClass('googlemapfield-zoomfield'); |
|
| 111 | - |
|
| 112 | - $this->children = new FieldList( |
|
| 113 | - $this->latField, |
|
| 114 | - $this->lngField, |
|
| 115 | - $this->zoomField |
|
| 116 | - ); |
|
| 117 | - |
|
| 118 | - if ($this->options['show_search_box']) { |
|
| 119 | - $this->children->push( |
|
| 120 | - TextField::create('Search') |
|
| 121 | - ->addExtraClass('googlemapfield-searchfield') |
|
| 122 | - ->setAttribute('placeholder', 'Search for a location') |
|
| 123 | - ); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - return $this->children; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * @param array $properties |
|
| 131 | - * @see https://developers.google.com/maps/documentation/javascript/reference |
|
| 132 | - * {@inheritdoc} |
|
| 133 | - */ |
|
| 134 | - public function Field($properties = array()) |
|
| 135 | - { |
|
| 136 | - $jsOptions = array( |
|
| 137 | - 'coords' => array( |
|
| 138 | - $this->recordFieldData('Latitude'), |
|
| 139 | - $this->recordFieldData('Longitude') |
|
| 140 | - ), |
|
| 141 | - 'map' => array( |
|
| 142 | - 'zoom' => $this->recordFieldData('Zoom') ?: $this->getOption('map.zoom'), |
|
| 143 | - 'mapTypeId' => 'ROADMAP', |
|
| 144 | - ), |
|
| 145 | - ); |
|
| 146 | - |
|
| 147 | - $jsOptions = array_replace_recursive($this->options, $jsOptions); |
|
| 148 | - $this->setAttribute('data-settings', Convert::array2json($jsOptions)); |
|
| 149 | - $this->requireDependencies(); |
|
| 150 | - return parent::Field($properties); |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * Set up and include any frontend requirements |
|
| 155 | - * @return void |
|
| 156 | - */ |
|
| 157 | - protected function requireDependencies() |
|
| 158 | - { |
|
| 159 | - $gmapsParams = array( |
|
| 160 | - 'callback' => 'googlemapfieldInit', |
|
| 161 | - ); |
|
| 162 | - if ($key = $this->getOption('api_key')) { |
|
| 163 | - $gmapsParams['key'] = $key; |
|
| 164 | - } |
|
| 165 | - Requirements::css(GOOGLEMAPFIELD_BASE .'/css/GoogleMapField.css'); |
|
| 166 | - Requirements::javascript(GOOGLEMAPFIELD_BASE .'/javascript/GoogleMapField.js'); |
|
| 167 | - Requirements::javascript('//maps.googleapis.com/maps/api/js?' . http_build_query($gmapsParams)); |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * {@inheritdoc} |
|
| 172 | - */ |
|
| 173 | - public function setValue($record) |
|
| 174 | - { |
|
| 175 | - $this->latField->setValue( |
|
| 176 | - $record['Latitude'] |
|
| 177 | - ); |
|
| 178 | - $this->lngField->setValue( |
|
| 179 | - $record['Longitude'] |
|
| 180 | - ); |
|
| 181 | - $this->zoomField->setValue( |
|
| 182 | - $record['Zoom'] |
|
| 183 | - ); |
|
| 184 | - return $this; |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - /** |
|
| 188 | - * Take the latitude/longitude fields and save them to the DataObject. |
|
| 189 | - * {@inheritdoc} |
|
| 190 | - */ |
|
| 191 | - public function saveInto(DataObjectInterface $record) |
|
| 192 | - { |
|
| 193 | - $record->setCastedField($this->childFieldName('Latitude'), $this->latField->dataValue()); |
|
| 194 | - $record->setCastedField($this->childFieldName('Longitude'), $this->lngField->dataValue()); |
|
| 195 | - $record->setCastedField($this->childFieldName('Zoom'), $this->zoomField->dataValue()); |
|
| 196 | - return $this; |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - /** |
|
| 200 | - * @return FieldList The Latitude/Longitude fields |
|
| 201 | - */ |
|
| 202 | - public function getChildFields() |
|
| 203 | - { |
|
| 204 | - return $this->children; |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - protected function childFieldName($name) |
|
| 208 | - { |
|
| 209 | - $fieldNames = $this->getOption('field_names'); |
|
| 210 | - return $fieldNames[$name]; |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - protected function recordFieldData($name) |
|
| 214 | - { |
|
| 215 | - $fieldName = $this->childFieldName($name); |
|
| 216 | - return $this->data->$fieldName ?: $this->getDefaultValue($name); |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - public function getDefaultValue($name) |
|
| 220 | - { |
|
| 221 | - $fieldValues = $this->getOption('default_field_values'); |
|
| 222 | - return isset($fieldValues[$name]) ? $fieldValues[$name] : null; |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - /** |
|
| 226 | - * @return string The VALUE of the Latitude field |
|
| 227 | - */ |
|
| 228 | - public function getLatData() |
|
| 229 | - { |
|
| 230 | - $fieldNames = $this->getOption('field_names'); |
|
| 231 | - return $this->data->$fieldNames['Latitude']; |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - /** |
|
| 235 | - * @return string The VALUE of the Longitude field |
|
| 236 | - */ |
|
| 237 | - public function getLngData() |
|
| 238 | - { |
|
| 239 | - $fieldNames = $this->getOption('field_names'); |
|
| 240 | - return $this->data->$fieldNames['Longitude']; |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - /** |
|
| 244 | - * Get the merged option that was set on __construct |
|
| 245 | - * @param string $name The name of the option |
|
| 246 | - * @return mixed |
|
| 247 | - */ |
|
| 248 | - public function getOption($name) |
|
| 249 | - { |
|
| 250 | - // Quicker execution path for "."-free names |
|
| 251 | - if (strpos($name, '.') === false) { |
|
| 252 | - if (isset($this->options[$name])) { |
|
| 253 | - return $this->options[$name]; |
|
| 254 | - } |
|
| 255 | - } else { |
|
| 256 | - $names = explode('.', $name); |
|
| 257 | - |
|
| 258 | - $var = $this->options; |
|
| 259 | - |
|
| 260 | - foreach ($names as $n) { |
|
| 261 | - if (!isset($var[$n])) { |
|
| 262 | - return null; |
|
| 263 | - } |
|
| 264 | - $var = $var[$n]; |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - return $var; |
|
| 268 | - } |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - /** |
|
| 272 | - * Set an option for this field |
|
| 273 | - * @param string $name The name of the option to set |
|
| 274 | - * @param mixed $val The value of said option |
|
| 275 | - * @return $this |
|
| 276 | - */ |
|
| 277 | - public function setOption($name, $val) |
|
| 278 | - { |
|
| 279 | - // Quicker execution path for "."-free names |
|
| 280 | - if (strpos($name, '.') === false) { |
|
| 281 | - $this->options[$name] = $val; |
|
| 282 | - } else { |
|
| 283 | - $names = explode('.', $name); |
|
| 284 | - |
|
| 285 | - // We still want to do this even if we have strict path checking for legacy code |
|
| 286 | - $var = &$this->options; |
|
| 287 | - |
|
| 288 | - foreach ($names as $n) { |
|
| 289 | - $var = &$var[$n]; |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - $var = $val; |
|
| 293 | - } |
|
| 294 | - return $this; |
|
| 295 | - } |
|
| 15 | + protected $data; |
|
| 16 | + |
|
| 17 | + /** |
|
| 18 | + * @var FormField |
|
| 19 | + */ |
|
| 20 | + protected $latField; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * @var FormField |
|
| 24 | + */ |
|
| 25 | + protected $lngField; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * @var FormField |
|
| 29 | + */ |
|
| 30 | + protected $zoomField; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * The merged version of the default and user specified options |
|
| 34 | + * @var array |
|
| 35 | + */ |
|
| 36 | + protected $options = array(); |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * @param DataObject $data The controlling dataobject |
|
| 40 | + * @param string $title The title of the field |
|
| 41 | + * @param array $options Various settings for the field |
|
| 42 | + */ |
|
| 43 | + public function __construct(DataObject $data, $title, $options = array()) |
|
| 44 | + { |
|
| 45 | + $this->data = $data; |
|
| 46 | + |
|
| 47 | + // Set up fieldnames |
|
| 48 | + $this->setupOptions($options); |
|
| 49 | + |
|
| 50 | + $this->setupChildren(); |
|
| 51 | + |
|
| 52 | + parent::__construct($this->getName(), $title); |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + // Auto generate a name |
|
| 56 | + public function getName() |
|
| 57 | + { |
|
| 58 | + $fieldNames = $this->getOption('field_names'); |
|
| 59 | + return sprintf( |
|
| 60 | + '%s_%s_%s', |
|
| 61 | + $this->data->class, |
|
| 62 | + $fieldNames['Latitude'], |
|
| 63 | + $fieldNames['Longitude'] |
|
| 64 | + ); |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * Merge options preserving the first level of array keys |
|
| 69 | + * @param array $options |
|
| 70 | + */ |
|
| 71 | + public function setupOptions(array $options) |
|
| 72 | + { |
|
| 73 | + $this->options = static::config()->default_options; |
|
| 74 | + foreach ($this->options as $name => &$value) { |
|
| 75 | + if (isset($options[$name])) { |
|
| 76 | + if (is_array($value)) { |
|
| 77 | + $value = array_merge($value, $options[$name]); |
|
| 78 | + } else { |
|
| 79 | + $value = $options[$name]; |
|
| 80 | + } |
|
| 81 | + } |
|
| 82 | + } |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * Set up child hidden fields, and optionally the search box. |
|
| 87 | + * @return FieldList the children |
|
| 88 | + */ |
|
| 89 | + public function setupChildren() |
|
| 90 | + { |
|
| 91 | + $name = $this->getName(); |
|
| 92 | + |
|
| 93 | + // Create the latitude/longitude hidden fields |
|
| 94 | + $this->latField = HiddenField::create( |
|
| 95 | + $name.'[Latitude]', |
|
| 96 | + 'Lat', |
|
| 97 | + $this->recordFieldData('Latitude') |
|
| 98 | + )->addExtraClass('googlemapfield-latfield'); |
|
| 99 | + |
|
| 100 | + $this->lngField = HiddenField::create( |
|
| 101 | + $name.'[Longitude]', |
|
| 102 | + 'Lng', |
|
| 103 | + $this->recordFieldData('Longitude') |
|
| 104 | + )->addExtraClass('googlemapfield-lngfield'); |
|
| 105 | + |
|
| 106 | + $this->zoomField = HiddenField::create( |
|
| 107 | + $name.'[Zoom]', |
|
| 108 | + 'Zoom', |
|
| 109 | + $this->recordFieldData('Zoom') |
|
| 110 | + )->addExtraClass('googlemapfield-zoomfield'); |
|
| 111 | + |
|
| 112 | + $this->children = new FieldList( |
|
| 113 | + $this->latField, |
|
| 114 | + $this->lngField, |
|
| 115 | + $this->zoomField |
|
| 116 | + ); |
|
| 117 | + |
|
| 118 | + if ($this->options['show_search_box']) { |
|
| 119 | + $this->children->push( |
|
| 120 | + TextField::create('Search') |
|
| 121 | + ->addExtraClass('googlemapfield-searchfield') |
|
| 122 | + ->setAttribute('placeholder', 'Search for a location') |
|
| 123 | + ); |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + return $this->children; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * @param array $properties |
|
| 131 | + * @see https://developers.google.com/maps/documentation/javascript/reference |
|
| 132 | + * {@inheritdoc} |
|
| 133 | + */ |
|
| 134 | + public function Field($properties = array()) |
|
| 135 | + { |
|
| 136 | + $jsOptions = array( |
|
| 137 | + 'coords' => array( |
|
| 138 | + $this->recordFieldData('Latitude'), |
|
| 139 | + $this->recordFieldData('Longitude') |
|
| 140 | + ), |
|
| 141 | + 'map' => array( |
|
| 142 | + 'zoom' => $this->recordFieldData('Zoom') ?: $this->getOption('map.zoom'), |
|
| 143 | + 'mapTypeId' => 'ROADMAP', |
|
| 144 | + ), |
|
| 145 | + ); |
|
| 146 | + |
|
| 147 | + $jsOptions = array_replace_recursive($this->options, $jsOptions); |
|
| 148 | + $this->setAttribute('data-settings', Convert::array2json($jsOptions)); |
|
| 149 | + $this->requireDependencies(); |
|
| 150 | + return parent::Field($properties); |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * Set up and include any frontend requirements |
|
| 155 | + * @return void |
|
| 156 | + */ |
|
| 157 | + protected function requireDependencies() |
|
| 158 | + { |
|
| 159 | + $gmapsParams = array( |
|
| 160 | + 'callback' => 'googlemapfieldInit', |
|
| 161 | + ); |
|
| 162 | + if ($key = $this->getOption('api_key')) { |
|
| 163 | + $gmapsParams['key'] = $key; |
|
| 164 | + } |
|
| 165 | + Requirements::css(GOOGLEMAPFIELD_BASE .'/css/GoogleMapField.css'); |
|
| 166 | + Requirements::javascript(GOOGLEMAPFIELD_BASE .'/javascript/GoogleMapField.js'); |
|
| 167 | + Requirements::javascript('//maps.googleapis.com/maps/api/js?' . http_build_query($gmapsParams)); |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * {@inheritdoc} |
|
| 172 | + */ |
|
| 173 | + public function setValue($record) |
|
| 174 | + { |
|
| 175 | + $this->latField->setValue( |
|
| 176 | + $record['Latitude'] |
|
| 177 | + ); |
|
| 178 | + $this->lngField->setValue( |
|
| 179 | + $record['Longitude'] |
|
| 180 | + ); |
|
| 181 | + $this->zoomField->setValue( |
|
| 182 | + $record['Zoom'] |
|
| 183 | + ); |
|
| 184 | + return $this; |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + /** |
|
| 188 | + * Take the latitude/longitude fields and save them to the DataObject. |
|
| 189 | + * {@inheritdoc} |
|
| 190 | + */ |
|
| 191 | + public function saveInto(DataObjectInterface $record) |
|
| 192 | + { |
|
| 193 | + $record->setCastedField($this->childFieldName('Latitude'), $this->latField->dataValue()); |
|
| 194 | + $record->setCastedField($this->childFieldName('Longitude'), $this->lngField->dataValue()); |
|
| 195 | + $record->setCastedField($this->childFieldName('Zoom'), $this->zoomField->dataValue()); |
|
| 196 | + return $this; |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + /** |
|
| 200 | + * @return FieldList The Latitude/Longitude fields |
|
| 201 | + */ |
|
| 202 | + public function getChildFields() |
|
| 203 | + { |
|
| 204 | + return $this->children; |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + protected function childFieldName($name) |
|
| 208 | + { |
|
| 209 | + $fieldNames = $this->getOption('field_names'); |
|
| 210 | + return $fieldNames[$name]; |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + protected function recordFieldData($name) |
|
| 214 | + { |
|
| 215 | + $fieldName = $this->childFieldName($name); |
|
| 216 | + return $this->data->$fieldName ?: $this->getDefaultValue($name); |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + public function getDefaultValue($name) |
|
| 220 | + { |
|
| 221 | + $fieldValues = $this->getOption('default_field_values'); |
|
| 222 | + return isset($fieldValues[$name]) ? $fieldValues[$name] : null; |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + /** |
|
| 226 | + * @return string The VALUE of the Latitude field |
|
| 227 | + */ |
|
| 228 | + public function getLatData() |
|
| 229 | + { |
|
| 230 | + $fieldNames = $this->getOption('field_names'); |
|
| 231 | + return $this->data->$fieldNames['Latitude']; |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + /** |
|
| 235 | + * @return string The VALUE of the Longitude field |
|
| 236 | + */ |
|
| 237 | + public function getLngData() |
|
| 238 | + { |
|
| 239 | + $fieldNames = $this->getOption('field_names'); |
|
| 240 | + return $this->data->$fieldNames['Longitude']; |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + /** |
|
| 244 | + * Get the merged option that was set on __construct |
|
| 245 | + * @param string $name The name of the option |
|
| 246 | + * @return mixed |
|
| 247 | + */ |
|
| 248 | + public function getOption($name) |
|
| 249 | + { |
|
| 250 | + // Quicker execution path for "."-free names |
|
| 251 | + if (strpos($name, '.') === false) { |
|
| 252 | + if (isset($this->options[$name])) { |
|
| 253 | + return $this->options[$name]; |
|
| 254 | + } |
|
| 255 | + } else { |
|
| 256 | + $names = explode('.', $name); |
|
| 257 | + |
|
| 258 | + $var = $this->options; |
|
| 259 | + |
|
| 260 | + foreach ($names as $n) { |
|
| 261 | + if (!isset($var[$n])) { |
|
| 262 | + return null; |
|
| 263 | + } |
|
| 264 | + $var = $var[$n]; |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + return $var; |
|
| 268 | + } |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + /** |
|
| 272 | + * Set an option for this field |
|
| 273 | + * @param string $name The name of the option to set |
|
| 274 | + * @param mixed $val The value of said option |
|
| 275 | + * @return $this |
|
| 276 | + */ |
|
| 277 | + public function setOption($name, $val) |
|
| 278 | + { |
|
| 279 | + // Quicker execution path for "."-free names |
|
| 280 | + if (strpos($name, '.') === false) { |
|
| 281 | + $this->options[$name] = $val; |
|
| 282 | + } else { |
|
| 283 | + $names = explode('.', $name); |
|
| 284 | + |
|
| 285 | + // We still want to do this even if we have strict path checking for legacy code |
|
| 286 | + $var = &$this->options; |
|
| 287 | + |
|
| 288 | + foreach ($names as $n) { |
|
| 289 | + $var = &$var[$n]; |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + $var = $val; |
|
| 293 | + } |
|
| 294 | + return $this; |
|
| 295 | + } |
|
| 296 | 296 | } |
@@ -92,19 +92,19 @@ discard block |
||
| 92 | 92 | |
| 93 | 93 | // Create the latitude/longitude hidden fields |
| 94 | 94 | $this->latField = HiddenField::create( |
| 95 | - $name.'[Latitude]', |
|
| 95 | + $name . '[Latitude]', |
|
| 96 | 96 | 'Lat', |
| 97 | 97 | $this->recordFieldData('Latitude') |
| 98 | 98 | )->addExtraClass('googlemapfield-latfield'); |
| 99 | 99 | |
| 100 | 100 | $this->lngField = HiddenField::create( |
| 101 | - $name.'[Longitude]', |
|
| 101 | + $name . '[Longitude]', |
|
| 102 | 102 | 'Lng', |
| 103 | 103 | $this->recordFieldData('Longitude') |
| 104 | 104 | )->addExtraClass('googlemapfield-lngfield'); |
| 105 | 105 | |
| 106 | 106 | $this->zoomField = HiddenField::create( |
| 107 | - $name.'[Zoom]', |
|
| 107 | + $name . '[Zoom]', |
|
| 108 | 108 | 'Zoom', |
| 109 | 109 | $this->recordFieldData('Zoom') |
| 110 | 110 | )->addExtraClass('googlemapfield-zoomfield'); |
@@ -162,8 +162,8 @@ discard block |
||
| 162 | 162 | if ($key = $this->getOption('api_key')) { |
| 163 | 163 | $gmapsParams['key'] = $key; |
| 164 | 164 | } |
| 165 | - Requirements::css(GOOGLEMAPFIELD_BASE .'/css/GoogleMapField.css'); |
|
| 166 | - Requirements::javascript(GOOGLEMAPFIELD_BASE .'/javascript/GoogleMapField.js'); |
|
| 165 | + Requirements::css(GOOGLEMAPFIELD_BASE . '/css/GoogleMapField.css'); |
|
| 166 | + Requirements::javascript(GOOGLEMAPFIELD_BASE . '/javascript/GoogleMapField.js'); |
|
| 167 | 167 | Requirements::javascript('//maps.googleapis.com/maps/api/js?' . http_build_query($gmapsParams)); |
| 168 | 168 | } |
| 169 | 169 | |
@@ -9,8 +9,7 @@ discard block |
||
| 9 | 9 | * Maps Geocoding API. |
| 10 | 10 | * @author <@willmorgan> |
| 11 | 11 | */ |
| 12 | -class GoogleMapField extends FormField |
|
| 13 | -{ |
|
| 12 | +class GoogleMapField extends FormField { |
|
| 14 | 13 | |
| 15 | 14 | protected $data; |
| 16 | 15 | |
@@ -40,8 +39,7 @@ discard block |
||
| 40 | 39 | * @param string $title The title of the field |
| 41 | 40 | * @param array $options Various settings for the field |
| 42 | 41 | */ |
| 43 | - public function __construct(DataObject $data, $title, $options = array()) |
|
| 44 | - { |
|
| 42 | + public function __construct(DataObject $data, $title, $options = array()) { |
|
| 45 | 43 | $this->data = $data; |
| 46 | 44 | |
| 47 | 45 | // Set up fieldnames |
@@ -53,8 +51,7 @@ discard block |
||
| 53 | 51 | } |
| 54 | 52 | |
| 55 | 53 | // Auto generate a name |
| 56 | - public function getName() |
|
| 57 | - { |
|
| 54 | + public function getName() { |
|
| 58 | 55 | $fieldNames = $this->getOption('field_names'); |
| 59 | 56 | return sprintf( |
| 60 | 57 | '%s_%s_%s', |
@@ -68,8 +65,7 @@ discard block |
||
| 68 | 65 | * Merge options preserving the first level of array keys |
| 69 | 66 | * @param array $options |
| 70 | 67 | */ |
| 71 | - public function setupOptions(array $options) |
|
| 72 | - { |
|
| 68 | + public function setupOptions(array $options) { |
|
| 73 | 69 | $this->options = static::config()->default_options; |
| 74 | 70 | foreach ($this->options as $name => &$value) { |
| 75 | 71 | if (isset($options[$name])) { |
@@ -86,8 +82,7 @@ discard block |
||
| 86 | 82 | * Set up child hidden fields, and optionally the search box. |
| 87 | 83 | * @return FieldList the children |
| 88 | 84 | */ |
| 89 | - public function setupChildren() |
|
| 90 | - { |
|
| 85 | + public function setupChildren() { |
|
| 91 | 86 | $name = $this->getName(); |
| 92 | 87 | |
| 93 | 88 | // Create the latitude/longitude hidden fields |
@@ -131,8 +126,7 @@ discard block |
||
| 131 | 126 | * @see https://developers.google.com/maps/documentation/javascript/reference |
| 132 | 127 | * {@inheritdoc} |
| 133 | 128 | */ |
| 134 | - public function Field($properties = array()) |
|
| 135 | - { |
|
| 129 | + public function Field($properties = array()) { |
|
| 136 | 130 | $jsOptions = array( |
| 137 | 131 | 'coords' => array( |
| 138 | 132 | $this->recordFieldData('Latitude'), |
@@ -154,8 +148,7 @@ discard block |
||
| 154 | 148 | * Set up and include any frontend requirements |
| 155 | 149 | * @return void |
| 156 | 150 | */ |
| 157 | - protected function requireDependencies() |
|
| 158 | - { |
|
| 151 | + protected function requireDependencies() { |
|
| 159 | 152 | $gmapsParams = array( |
| 160 | 153 | 'callback' => 'googlemapfieldInit', |
| 161 | 154 | ); |
@@ -170,8 +163,7 @@ discard block |
||
| 170 | 163 | /** |
| 171 | 164 | * {@inheritdoc} |
| 172 | 165 | */ |
| 173 | - public function setValue($record) |
|
| 174 | - { |
|
| 166 | + public function setValue($record) { |
|
| 175 | 167 | $this->latField->setValue( |
| 176 | 168 | $record['Latitude'] |
| 177 | 169 | ); |
@@ -188,8 +180,7 @@ discard block |
||
| 188 | 180 | * Take the latitude/longitude fields and save them to the DataObject. |
| 189 | 181 | * {@inheritdoc} |
| 190 | 182 | */ |
| 191 | - public function saveInto(DataObjectInterface $record) |
|
| 192 | - { |
|
| 183 | + public function saveInto(DataObjectInterface $record) { |
|
| 193 | 184 | $record->setCastedField($this->childFieldName('Latitude'), $this->latField->dataValue()); |
| 194 | 185 | $record->setCastedField($this->childFieldName('Longitude'), $this->lngField->dataValue()); |
| 195 | 186 | $record->setCastedField($this->childFieldName('Zoom'), $this->zoomField->dataValue()); |
@@ -199,25 +190,21 @@ discard block |
||
| 199 | 190 | /** |
| 200 | 191 | * @return FieldList The Latitude/Longitude fields |
| 201 | 192 | */ |
| 202 | - public function getChildFields() |
|
| 203 | - { |
|
| 193 | + public function getChildFields() { |
|
| 204 | 194 | return $this->children; |
| 205 | 195 | } |
| 206 | 196 | |
| 207 | - protected function childFieldName($name) |
|
| 208 | - { |
|
| 197 | + protected function childFieldName($name) { |
|
| 209 | 198 | $fieldNames = $this->getOption('field_names'); |
| 210 | 199 | return $fieldNames[$name]; |
| 211 | 200 | } |
| 212 | 201 | |
| 213 | - protected function recordFieldData($name) |
|
| 214 | - { |
|
| 202 | + protected function recordFieldData($name) { |
|
| 215 | 203 | $fieldName = $this->childFieldName($name); |
| 216 | 204 | return $this->data->$fieldName ?: $this->getDefaultValue($name); |
| 217 | 205 | } |
| 218 | 206 | |
| 219 | - public function getDefaultValue($name) |
|
| 220 | - { |
|
| 207 | + public function getDefaultValue($name) { |
|
| 221 | 208 | $fieldValues = $this->getOption('default_field_values'); |
| 222 | 209 | return isset($fieldValues[$name]) ? $fieldValues[$name] : null; |
| 223 | 210 | } |
@@ -225,8 +212,7 @@ discard block |
||
| 225 | 212 | /** |
| 226 | 213 | * @return string The VALUE of the Latitude field |
| 227 | 214 | */ |
| 228 | - public function getLatData() |
|
| 229 | - { |
|
| 215 | + public function getLatData() { |
|
| 230 | 216 | $fieldNames = $this->getOption('field_names'); |
| 231 | 217 | return $this->data->$fieldNames['Latitude']; |
| 232 | 218 | } |
@@ -234,8 +220,7 @@ discard block |
||
| 234 | 220 | /** |
| 235 | 221 | * @return string The VALUE of the Longitude field |
| 236 | 222 | */ |
| 237 | - public function getLngData() |
|
| 238 | - { |
|
| 223 | + public function getLngData() { |
|
| 239 | 224 | $fieldNames = $this->getOption('field_names'); |
| 240 | 225 | return $this->data->$fieldNames['Longitude']; |
| 241 | 226 | } |
@@ -245,8 +230,7 @@ discard block |
||
| 245 | 230 | * @param string $name The name of the option |
| 246 | 231 | * @return mixed |
| 247 | 232 | */ |
| 248 | - public function getOption($name) |
|
| 249 | - { |
|
| 233 | + public function getOption($name) { |
|
| 250 | 234 | // Quicker execution path for "."-free names |
| 251 | 235 | if (strpos($name, '.') === false) { |
| 252 | 236 | if (isset($this->options[$name])) { |
@@ -274,8 +258,7 @@ discard block |
||
| 274 | 258 | * @param mixed $val The value of said option |
| 275 | 259 | * @return $this |
| 276 | 260 | */ |
| 277 | - public function setOption($name, $val) |
|
| 278 | - { |
|
| 261 | + public function setOption($name, $val) { |
|
| 279 | 262 | // Quicker execution path for "."-free names |
| 280 | 263 | if (strpos($name, '.') === false) { |
| 281 | 264 | $this->options[$name] = $val; |