From 3f3177fe36bf6d7b8a0697a3b97bdb90f3e467ff Mon Sep 17 00:00:00 2001 From: Michael Heit Date: Wed, 20 Dec 2017 09:23:41 +0100 Subject: [PATCH 1/4] add google places helper Add a helper for google places and add libraries options to google Map helper --- src/View/Helper/GoogleMapHelper.php | 6 ++ src/View/Helper/GooglePlacesHelper.php | 117 +++++++++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 src/View/Helper/GooglePlacesHelper.php diff --git a/src/View/Helper/GoogleMapHelper.php b/src/View/Helper/GoogleMapHelper.php index 41455e43..c652ef7e 100644 --- a/src/View/Helper/GoogleMapHelper.php +++ b/src/View/Helper/GoogleMapHelper.php @@ -237,6 +237,7 @@ class GoogleMapHelper extends Helper { 'localImages' => false, 'https' => null, // auto detect 'key' => null, + 'libraries' => null, ]; /** @@ -332,6 +333,7 @@ public function initialize(array $config) { * - key * - api * - language (iso2: en, de, ja, ...) + * - libraries * * You can adds more after the URL like "&key=value&..." via * - query string array: additional query strings (e.g. callback for deferred execution - not supported yet by this helper) @@ -342,6 +344,10 @@ public function initialize(array $config) { public function apiUrl(array $query = []) { $url = $this->_protocol() . static::API; + if ($this->_runtimeConfig['libraries']) { + $libraries = is_array( $this->_runtimeConfig['libraries'] ) ? implode( ',', $this->_runtimeConfig['libraries'] ) : $this->_runtimeConfig['libraries']; + $query['libraries'] = $libraries; + } if ($this->_runtimeConfig['map']['api']) { $query['v'] = $this->_runtimeConfig['map']['api']; } diff --git a/src/View/Helper/GooglePlacesHelper.php b/src/View/Helper/GooglePlacesHelper.php new file mode 100644 index 00000000..7a619287 --- /dev/null +++ b/src/View/Helper/GooglePlacesHelper.php @@ -0,0 +1,117 @@ +input + * + * @param string $fieldName name of input field + * @param array $fieldOptions associative array of settings are passed. Should be the same as uses on Form->control + * @param array $googleOptions associative array of settings for places.Autocomplete + * + * @return string divContainer + */ + public function input( $fieldName, array $fieldOptions = [], $googleOptions = [] ) { + return $this->control( $fieldName, $fieldOptions, $googleOptions ); + } + + /** + * This the initialization point of the script + * Returns the div container you can echo on the website + * + * @param string $fieldName name of input field + * @param array $fieldOptions associative array of settings are passed. Should be the same as uses on Form->control + * @param array $googleOptions associative array of settings for places.Autocomplete + * + * @return string divContainer + */ + public function control( $fieldName, $fieldOptions = [], $googleOptions = [] ) { + + $id = isset( $fieldOptions['id'] ) && $fieldOptions[ 'id' ] != '' ? $fieldOptions[ 'id' ] : $fieldName; + + $html = $this->Form->control( $fieldName, $fieldOptions ); + $html .= $this->Form->hidden( "{$fieldName}_lat", [ 'id' => "{$id}_lat" ] ); + $html .= $this->Form->hidden( "{$fieldName}_lon", [ 'id' => "{$id}_lon" ] ); + + $this->_script( $id, $googleOptions ); + + return $html; + } + + /** + * Inserts the required javascript code + * + * @param $id string the id of the input field + * @param array $options associative array of settings for places.Autocomplete + */ + private function _script( $id, $options = [] ) { + // autoinclude js? + if ($this->_runtimeConfig['autoScript'] && !$this->_apiIncluded) { + $res = $this->Html->script($this->apiUrl( ), ['block' => $this->_runtimeConfig['block']]); + $this->_apiIncluded = true; + + if (!$this->_runtimeConfig['block']) { + $result .= $res . PHP_EOL; + } + // usually already included + //http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js + } + // still not very common: http://code.google.com/intl/de-DE/apis/maps/documentation/javascript/basics.html + if (false && !empty($this->_runtimeConfig['autoScript']) && !$this->_gearsIncluded) { + $res = $this->Html->script($this->gearsUrl(), ['block' => $this->_runtimeConfig['block']]); + if (!$this->_runtimeConfig['block']) { + $result .= $res . PHP_EOL; + } + } + + $js = " + function initialize() { + var options = " . json_encode( $options ) . "; + var input = document.getElementById('" . $id . "'); + var hidden_lat = document.getElementById('" . $id . "_lat'); + var hidden_lon = document.getElementById('" . $id . "_lon'); + var autocomplete = new google.maps.places.Autocomplete(input, options); + + google.maps.event.addDomListener(input, 'keydown', function(event) { + if (event.keyCode === 13 && $('.pac-container:visible').length ) { + event.preventDefault(); + } + }); + autocomplete.addListener('place_changed', function() { + var place = autocomplete.getPlace(); + hidden_lat.value = place.geometry.location.lat() + hidden_lon.value = place.geometry.location.lng() + }); + + } + initialize(); + "; + + $script = 'jQuery(document).ready(function() {' . $js . '});'; + + $this->Html->scriptBlock($script, ['block' => true]); + } +} \ No newline at end of file From 73b80acfeaebf5ed34f078c73f2e63a9204c2426 Mon Sep 17 00:00:00 2001 From: Michael Heit Date: Wed, 20 Dec 2017 11:21:36 +0100 Subject: [PATCH 2/4] fixes --- src/View/Helper/GooglePlacesHelper.php | 35 +++++++++++++------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/src/View/Helper/GooglePlacesHelper.php b/src/View/Helper/GooglePlacesHelper.php index 7a619287..10bf3179 100644 --- a/src/View/Helper/GooglePlacesHelper.php +++ b/src/View/Helper/GooglePlacesHelper.php @@ -1,4 +1,5 @@ control( $fieldName, $fieldOptions, $googleOptions ); } @@ -48,15 +50,15 @@ public function input( $fieldName, array $fieldOptions = [], $googleOptions = [] * * @return string divContainer */ - public function control( $fieldName, $fieldOptions = [], $googleOptions = [] ) { + public function control( $fieldName, array $fieldOptions = [], array $googleOptions = [] ) { - $id = isset( $fieldOptions['id'] ) && $fieldOptions[ 'id' ] != '' ? $fieldOptions[ 'id' ] : $fieldName; + $id = isset( $fieldOptions['id'] ) && $fieldOptions['id'] != '' ? $fieldOptions['id'] : $fieldName; $html = $this->Form->control( $fieldName, $fieldOptions ); $html .= $this->Form->hidden( "{$fieldName}_lat", [ 'id' => "{$id}_lat" ] ); $html .= $this->Form->hidden( "{$fieldName}_lon", [ 'id' => "{$id}_lon" ] ); - $this->_script( $id, $googleOptions ); + $html = $this->_script( $id, $googleOptions ) . $html; return $html; } @@ -66,26 +68,23 @@ public function control( $fieldName, $fieldOptions = [], $googleOptions = [] ) { * * @param $id string the id of the input field * @param array $options associative array of settings for places.Autocomplete + * + * @return string the scriptBlock for api */ - private function _script( $id, $options = [] ) { + protected function _script( $id, $options = [] ) { + $api = ''; // autoinclude js? - if ($this->_runtimeConfig['autoScript'] && !$this->_apiIncluded) { - $res = $this->Html->script($this->apiUrl( ), ['block' => $this->_runtimeConfig['block']]); + if ( $this->_runtimeConfig['autoScript'] && ! $this->_apiIncluded ) { + $res = $this->Html->script( $this->apiUrl(), [ 'block' => $this->_runtimeConfig['block'] ] ); $this->_apiIncluded = true; - if (!$this->_runtimeConfig['block']) { - $result .= $res . PHP_EOL; + if ( ! $this->_runtimeConfig['block'] ) { + $api = $res . PHP_EOL; } // usually already included //http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js } - // still not very common: http://code.google.com/intl/de-DE/apis/maps/documentation/javascript/basics.html - if (false && !empty($this->_runtimeConfig['autoScript']) && !$this->_gearsIncluded) { - $res = $this->Html->script($this->gearsUrl(), ['block' => $this->_runtimeConfig['block']]); - if (!$this->_runtimeConfig['block']) { - $result .= $res . PHP_EOL; - } - } + $js = " function initialize() { @@ -112,6 +111,8 @@ function initialize() { $script = 'jQuery(document).ready(function() {' . $js . '});'; - $this->Html->scriptBlock($script, ['block' => true]); + $this->Html->scriptBlock( $script, [ 'block' => true ] ); + + return $api; } } \ No newline at end of file From 6936d3f090806802d71a744e1dc4c201b718bd86 Mon Sep 17 00:00:00 2001 From: Michael Heit Date: Wed, 20 Dec 2017 12:23:52 +0100 Subject: [PATCH 3/4] codeStyle --- src/View/Helper/GooglePlacesHelper.php | 158 +++++++++++++------------ 1 file changed, 80 insertions(+), 78 deletions(-) diff --git a/src/View/Helper/GooglePlacesHelper.php b/src/View/Helper/GooglePlacesHelper.php index 10bf3179..e5470915 100644 --- a/src/View/Helper/GooglePlacesHelper.php +++ b/src/View/Helper/GooglePlacesHelper.php @@ -2,8 +2,6 @@ namespace Geo\View\Helper; -use Geo\View\Helper\GoogleMapHelper; - /** * This is a CakePHP helper that helps users to integrate Google Places * into their application by only writing PHP code. This helper depends on jQuery. @@ -17,78 +15,82 @@ * @property \Cake\View\Helper\HtmlHelper $Html * @property \Cake\View\Helper\FormHelper $Form */ -class GooglePlacesHelper extends GoogleMapHelper { - - /** - * Needed helpers - * - * @var array - */ - public $helpers = [ 'Form', 'Html' ]; - - - /** - * Wrapper function for control like Form->input - * - * @param string $fieldName name of input field - * @param array $fieldOptions associative array of settings are passed. Should be the same as uses on Form->control - * @param array $googleOptions associative array of settings for places.Autocomplete - * - * @return string divContainer - */ - public function input( $fieldName, array $fieldOptions = [], array $googleOptions = [] ) { - return $this->control( $fieldName, $fieldOptions, $googleOptions ); - } - - /** - * This the initialization point of the script - * Returns the div container you can echo on the website - * - * @param string $fieldName name of input field - * @param array $fieldOptions associative array of settings are passed. Should be the same as uses on Form->control - * @param array $googleOptions associative array of settings for places.Autocomplete - * - * @return string divContainer - */ - public function control( $fieldName, array $fieldOptions = [], array $googleOptions = [] ) { - - $id = isset( $fieldOptions['id'] ) && $fieldOptions['id'] != '' ? $fieldOptions['id'] : $fieldName; - - $html = $this->Form->control( $fieldName, $fieldOptions ); - $html .= $this->Form->hidden( "{$fieldName}_lat", [ 'id' => "{$id}_lat" ] ); - $html .= $this->Form->hidden( "{$fieldName}_lon", [ 'id' => "{$id}_lon" ] ); - - $html = $this->_script( $id, $googleOptions ) . $html; - - return $html; - } - - /** - * Inserts the required javascript code - * - * @param $id string the id of the input field - * @param array $options associative array of settings for places.Autocomplete - * - * @return string the scriptBlock for api - */ - protected function _script( $id, $options = [] ) { - $api = ''; - // autoinclude js? - if ( $this->_runtimeConfig['autoScript'] && ! $this->_apiIncluded ) { - $res = $this->Html->script( $this->apiUrl(), [ 'block' => $this->_runtimeConfig['block'] ] ); - $this->_apiIncluded = true; - - if ( ! $this->_runtimeConfig['block'] ) { - $api = $res . PHP_EOL; - } - // usually already included - //http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js - } - - - $js = " +class GooglePlacesHelper extends GoogleMapHelper +{ + + /** + * Needed helpers + * + * @var array + */ + public $helpers = ['Form', 'Html']; + + + /** + * Wrapper function for control like Form->input + * + * @param string $fieldName name of input field + * @param array $fieldOptions associative array of settings are passed. Should be the same as uses on Form->control + * @param array $googleOptions associative array of settings for places.Autocomplete + * + * @return string divContainer + */ + public function input($fieldName, array $fieldOptions = [], array $googleOptions = []) + { + return $this->control($fieldName, $fieldOptions, $googleOptions); + } + + /** + * This the initialization point of the script + * Returns the div container you can echo on the website + * + * @param string $fieldName name of input field + * @param array $fieldOptions associative array of settings are passed. Should be the same as uses on Form->control + * @param array $googleOptions associative array of settings for places.Autocomplete + * + * @return string divContainer + */ + public function control($fieldName, array $fieldOptions = [], array $googleOptions = []) + { + + $id = isset($fieldOptions['id']) && $fieldOptions['id'] != '' ? $fieldOptions['id'] : $fieldName; + + $html = $this->Form->control($fieldName, $fieldOptions); + $html .= $this->Form->hidden("{$fieldName}_lat", ['id' => "{$id}_lat"]); + $html .= $this->Form->hidden("{$fieldName}_lon", ['id' => "{$id}_lon"]); + + $html = $this->_script($id, $googleOptions) . $html; + + return $html; + } + + /** + * Inserts the required javascript code + * + * @param $id string the id of the input field + * @param array $options associative array of settings for places.Autocomplete + * + * @return string the scriptBlock for api + */ + protected function _script($id, $options = []) + { + $api = ''; + // autoinclude js? + if ($this->_runtimeConfig['autoScript'] && !$this->_apiIncluded) { + $res = $this->Html->script($this->apiUrl(), ['block' => $this->_runtimeConfig['block']]); + $this->_apiIncluded = true; + + if (!$this->_runtimeConfig['block']) { + $api = $res . PHP_EOL; + } + // usually already included + //http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js + } + + + $js = " function initialize() { - var options = " . json_encode( $options ) . "; + var options = " . json_encode($options) . "; var input = document.getElementById('" . $id . "'); var hidden_lat = document.getElementById('" . $id . "_lat'); var hidden_lon = document.getElementById('" . $id . "_lon'); @@ -109,10 +111,10 @@ function initialize() { initialize(); "; - $script = 'jQuery(document).ready(function() {' . $js . '});'; + $script = 'jQuery(document).ready(function() {' . $js . '});'; - $this->Html->scriptBlock( $script, [ 'block' => true ] ); + $this->Html->scriptBlock($script, ['block' => true]); - return $api; - } -} \ No newline at end of file + return $api; + } +} From 615634fd62111bf5b449cbbf99c57b07dec211e6 Mon Sep 17 00:00:00 2001 From: Michael Heit Date: Wed, 20 Dec 2017 12:34:29 +0100 Subject: [PATCH 4/4] codeStyle now with the correct standard --- src/View/Helper/GooglePlacesHelper.php | 150 ++++++++++++------------- 1 file changed, 72 insertions(+), 78 deletions(-) diff --git a/src/View/Helper/GooglePlacesHelper.php b/src/View/Helper/GooglePlacesHelper.php index e5470915..182e6306 100644 --- a/src/View/Helper/GooglePlacesHelper.php +++ b/src/View/Helper/GooglePlacesHelper.php @@ -15,80 +15,73 @@ * @property \Cake\View\Helper\HtmlHelper $Html * @property \Cake\View\Helper\FormHelper $Form */ -class GooglePlacesHelper extends GoogleMapHelper -{ - - /** - * Needed helpers - * - * @var array - */ - public $helpers = ['Form', 'Html']; - - - /** - * Wrapper function for control like Form->input - * - * @param string $fieldName name of input field - * @param array $fieldOptions associative array of settings are passed. Should be the same as uses on Form->control - * @param array $googleOptions associative array of settings for places.Autocomplete - * - * @return string divContainer - */ - public function input($fieldName, array $fieldOptions = [], array $googleOptions = []) - { - return $this->control($fieldName, $fieldOptions, $googleOptions); - } - - /** - * This the initialization point of the script - * Returns the div container you can echo on the website - * - * @param string $fieldName name of input field - * @param array $fieldOptions associative array of settings are passed. Should be the same as uses on Form->control - * @param array $googleOptions associative array of settings for places.Autocomplete - * - * @return string divContainer - */ - public function control($fieldName, array $fieldOptions = [], array $googleOptions = []) - { - - $id = isset($fieldOptions['id']) && $fieldOptions['id'] != '' ? $fieldOptions['id'] : $fieldName; - - $html = $this->Form->control($fieldName, $fieldOptions); - $html .= $this->Form->hidden("{$fieldName}_lat", ['id' => "{$id}_lat"]); - $html .= $this->Form->hidden("{$fieldName}_lon", ['id' => "{$id}_lon"]); - - $html = $this->_script($id, $googleOptions) . $html; - - return $html; - } - - /** - * Inserts the required javascript code - * - * @param $id string the id of the input field - * @param array $options associative array of settings for places.Autocomplete - * - * @return string the scriptBlock for api - */ - protected function _script($id, $options = []) - { - $api = ''; - // autoinclude js? - if ($this->_runtimeConfig['autoScript'] && !$this->_apiIncluded) { - $res = $this->Html->script($this->apiUrl(), ['block' => $this->_runtimeConfig['block']]); - $this->_apiIncluded = true; - - if (!$this->_runtimeConfig['block']) { - $api = $res . PHP_EOL; - } - // usually already included - //http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js - } - - - $js = " +class GooglePlacesHelper extends GoogleMapHelper { + + /** + * Needed helpers + * + * @var array + */ + public $helpers = ['Form', 'Html']; + + /** + * Wrapper function for control like Form->input + * + * @param string $fieldName name of input field + * @param array $fieldOptions associative array of settings are passed. Should be the same as uses on Form->control + * @param array $googleOptions associative array of settings for places.Autocomplete + * + * @return string divContainer + */ + public function input($fieldName, array $fieldOptions = [], array $googleOptions = []) { + return $this->control($fieldName, $fieldOptions, $googleOptions); + } + + /** + * This the initialization point of the script + * Returns the div container you can echo on the website + * + * @param string $fieldName name of input field + * @param array $fieldOptions associative array of settings are passed. Should be the same as uses on Form->control + * @param array $googleOptions associative array of settings for places.Autocomplete + * + * @return string divContainer + */ + public function control($fieldName, array $fieldOptions = [], array $googleOptions = []) { + $id = isset($fieldOptions['id']) && $fieldOptions['id'] != '' ? $fieldOptions['id'] : $fieldName; + + $html = $this->Form->control($fieldName, $fieldOptions); + $html .= $this->Form->hidden("{$fieldName}_lat", ['id' => "{$id}_lat"]); + $html .= $this->Form->hidden("{$fieldName}_lon", ['id' => "{$id}_lon"]); + + $html = $this->_script($id, $googleOptions) . $html; + + return $html; + } + + /** + * Inserts the required javascript code + * + * @param string $id the id of the input field + * @param array $options associative array of settings for places.Autocomplete + * + * @return string the scriptBlock for api + */ + protected function _script($id, $options = []) { + $api = ''; + // autoinclude js? + if ($this->_runtimeConfig['autoScript'] && !$this->_apiIncluded) { + $res = $this->Html->script($this->apiUrl(), ['block' => $this->_runtimeConfig['block']]); + $this->_apiIncluded = true; + + if (!$this->_runtimeConfig['block']) { + $api = $res . PHP_EOL; + } + // usually already included + //http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js + } + + $js = " function initialize() { var options = " . json_encode($options) . "; var input = document.getElementById('" . $id . "'); @@ -111,10 +104,11 @@ function initialize() { initialize(); "; - $script = 'jQuery(document).ready(function() {' . $js . '});'; + $script = 'jQuery(document).ready(function() {' . $js . '});'; + + $this->Html->scriptBlock($script, ['block' => true]); - $this->Html->scriptBlock($script, ['block' => true]); + return $api; + } - return $api; - } }