PageRenderTime 24ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/concrete/blocks/google_map/form_setup_html.php

http://github.com/concrete5/concrete5
PHP | 296 lines | 266 code | 21 blank | 9 comment | 32 complexity | 16b9dbb120074cb4cca6603db61ebc91 MD5 | raw file
Possible License(s): MIT, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. <?php
  2. defined('C5_EXECUTE') or die('Access Denied.');
  3. /* @var Concrete\Core\Form\Service\Form $form */
  4. /* @var string $title */
  5. /* @var string $location */
  6. /* @var string $latitude */
  7. /* @var string $longitude */
  8. /* @var int $zoom */
  9. /* @var string $width */
  10. /* @var string $height */
  11. /* @var int $scrollwheel */
  12. if (!isset($title)) {
  13. $title = '';
  14. }
  15. if (!isset($location)) {
  16. $location = '';
  17. }
  18. if (!isset($location)) {
  19. $location = '';
  20. }
  21. if (!isset($latitude)) {
  22. $latitude = '';
  23. }
  24. if (!isset($longitude)) {
  25. $longitude = '';
  26. }
  27. if (empty($zoom)) {
  28. $zoom = 14;
  29. }
  30. if (!isset($width)) {
  31. $width = '100%';
  32. }
  33. if (!isset($height)) {
  34. $height = '400px';
  35. }
  36. $scrollwheel = !empty($scrollwheel);
  37. ?>
  38. <div class="ccm-google-map-block-container row">
  39. <div class="col-xs-12">
  40. <div class="form-group">
  41. <?= $form->label('apiKey', t('API Key') . ' <i class="fa fa-question-circle launch-tooltip" title="' . t('The API Key must be enabled for Google Maps and Google Places.') . "\n" . t('API keys can be obtained in the Google Developers Console.') . '"></i>') ?>
  42. <div class="input-group">
  43. <?= $form->text('apiKey', Config::get('app.api_keys.google.maps')) ?>
  44. <span class="input-group-btn">
  45. <a id="ccm-google-map-check-key" class="btn btn-default" href="#">
  46. <?= t('Check') ?>
  47. &nbsp;
  48. <i id="ccm-google-map-check-key-spinner" class="fa fa-play"></i>
  49. </a>
  50. </span>
  51. </div>
  52. <div id="block_note" class="alert alert-info" role="alert"><?= t('Checking API Key...') ?></div>
  53. </div>
  54. <div class="form-group">
  55. </div>
  56. <div class="form-group">
  57. <?= $form->label('title', t('Map Title (optional)')) ?>
  58. <?= $form->text('title', $title) ?>
  59. </div>
  60. <div id="ccm-google-map-block-location" class="form-group">
  61. <?= $form->label('location', t('Location') . ' <i class="fa fa-question-circle launch-tooltip" title="' . t('Start typing a location (e.g. Apple Store or 235 W 3rd, New York) then click on the correct entry on the list.') . '"></i>') ?>
  62. <?= $form->text('location', $location) ?>
  63. <?= $form->hidden('latitude', $latitude) ?>
  64. <?= $form->hidden('longitude', $longitude) ?>
  65. <div id="map-canvas"></div>
  66. </div>
  67. </div>
  68. <div class="col-xs-4">
  69. <div class="form-group">
  70. <?= $form->label('zoom', t('Zoom')) ?>
  71. <?php
  72. $zoomLevels = range(0, 21);
  73. $zoomArray = array_combine($zoomLevels, $zoomLevels);
  74. ?>
  75. <?= $form->select('zoom', $zoomArray, $zoom) ?>
  76. </div>
  77. </div>
  78. <div class="col-xs-4">
  79. <div class="form-group">
  80. <?= $form->label('width', t('Map Width')) ?>
  81. <div class="input-group">
  82. <span class="input-group-addon"><i class="fa fa-arrows-h"></i></span>
  83. <?= $form->text('width', $width) ?>
  84. </div>
  85. </div>
  86. </div>
  87. <div class="col-xs-4">
  88. <div class="form-group">
  89. <?= $form->label('height', t('Map Height')) ?>
  90. <div class="input-group">
  91. <span class="input-group-addon"><i class="fa fa-arrows-v"></i></span>
  92. <?= $form->text('height', $height) ?>
  93. </div>
  94. </div>
  95. </div>
  96. <div class="col-xs-12">
  97. <div class="form-group">
  98. <div class="checkbox">
  99. <label>
  100. <?= $form->checkbox('scrollwheel', 1, $scrollwheel) ?>
  101. <?= t('Enable Scroll Wheel') ?>
  102. </label>
  103. </div>
  104. </div>
  105. </div>
  106. </div>
  107. <script>
  108. $(document).ready(function() {
  109. 'use strict';
  110. var $key = $('#apiKey'),
  111. $location = $('#ccm-google-map-block-location > input[id="location"]'),
  112. $note = $("#block_note");
  113. var setupApiKey = (function() {
  114. var $checkSpinner = $('#ccm-google-map-check-key-spinner'),
  115. checking = false,
  116. $script = null,
  117. originalGMAuthFailure = window.gm_authFailure,
  118. lastKey = null,
  119. lastKeyError = null,
  120. autocomplete = null;
  121. function setAutocompletion(places) {
  122. if (autocomplete) {
  123. $location.removeAttr('placeholder autocomplete disabled style').removeClass('gm-err-autocomplete notfound');
  124. google.maps.event.removeListener(autocomplete.listener);
  125. google.maps.event.clearInstanceListeners(autocomplete.autocomplete);
  126. clearInterval(autocomplete.pacTimer);
  127. $('.pac-container').remove();
  128. $location.off('change');
  129. autocomplete = null;
  130. }
  131. if (!places) {
  132. return;
  133. }
  134. autocomplete = {
  135. autocomplete: new google.maps.places.Autocomplete($location[0])
  136. }
  137. autocomplete.listener = google.maps.event.addListener(autocomplete.autocomplete, 'place_changed', function () {
  138. if (autocomplete === null) {
  139. return;
  140. }
  141. var place = autocomplete.autocomplete.getPlace();
  142. if (!place.geometry) {
  143. $location.addClass('notfound');
  144. $note.text(<?= json_encode(t('The place you entered could not be found.')) ?>).removeClass('alert-info alert-success').removeClass('alert-danger').css('visibility', '');
  145. } else {
  146. $('#ccm-google-map-block-location > input[id=latitude]').val(place.geometry.location.lat());
  147. $('#ccm-google-map-block-location > input[id=longitude]').val(place.geometry.location.lng());
  148. $location.removeClass('notfound');
  149. $note.css('visibility', 'hidden');
  150. }
  151. });
  152. $location.on('change', function() {
  153. $location.addClass('notfound');
  154. });
  155. autocomplete.pacTimer = setInterval(function () {
  156. $('.pac-container').css('z-index', '2000');
  157. if ($('#ccm-google-map-block-location > input[id="location"]').length === 0) {
  158. setAutocompletion(null);
  159. }
  160. }, 250);
  161. }
  162. return function(onSuccess, onError, forceRecheck) {
  163. if (checking) {
  164. onError(<?= json_encode(t('Please wait, operation in progress.')) ?>);
  165. return;
  166. }
  167. if (!onSuccess) {
  168. onSuccess = function() {};
  169. }
  170. if (!onError) {
  171. onError = function() {};
  172. }
  173. var key = $.trim($key.val());
  174. if (key === lastKey && !forceRecheck) {
  175. if (lastKeyError === null) {
  176. onSuccess();
  177. } else {
  178. onError(lastKeyError);
  179. }
  180. return;
  181. }
  182. function completed(places) {
  183. setAutocompletion(places);
  184. if (lastKeyError === null) {
  185. onSuccess();
  186. } else {
  187. onError(lastKeyError);
  188. }
  189. }
  190. setAutocompletion();
  191. checking = true;
  192. if ($script !== null) {
  193. $script.remove();
  194. $script = null;
  195. }
  196. var scriptLoadedFunctionName;
  197. for (var i = 0; ; i++) {
  198. scriptLoadedFunctionName = '_ccm_gmapblock_loaded_' + i;
  199. if (typeof window[scriptLoadedFunctionName] === 'undefined') {
  200. break;
  201. }
  202. }
  203. function scriptLoaded(error) {
  204. delete window[scriptLoadedFunctionName];
  205. function placesLoaded(error, places) {
  206. if (originalGMAuthFailure) {
  207. window.gm_authFailure = originalGMAuthFailure;
  208. } else {
  209. delete window.gm_authFailure;
  210. }
  211. $checkSpinner.removeClass('fa-refresh fa-spin').addClass('fa-play');
  212. lastKey = key;
  213. lastKeyError = error;
  214. setTimeout(function() {
  215. checking = false;
  216. completed(places)
  217. }, 10);
  218. }
  219. if (error !== null) {
  220. placesLoaded(error);
  221. return;
  222. }
  223. var places = new google.maps.places.PlacesService(document.createElement('div'));
  224. window.gm_authFailure = function() {
  225. placesLoaded(<?= json_encode(t('The API Key is NOT valid.')) ?>);
  226. };
  227. places.getDetails(
  228. {
  229. placeId: 'ChIJJ3SpfQsLlVQRkYXR9ua5Nhw'
  230. },
  231. function(place, status) {
  232. if (status === 'REQUEST_DENIED') {
  233. placesLoaded(<?= json_encode(t('The API Key is NOT valid for Google Places.')) ?>);
  234. } else {
  235. placesLoaded(null, places);
  236. }
  237. }
  238. );
  239. }
  240. window[scriptLoadedFunctionName] = function() {
  241. scriptLoaded(null);
  242. };
  243. window.gm_authFailure = function() {
  244. scriptLoaded(<?= json_encode(t('The API Key is NOT valid.')) ?>);
  245. };
  246. $checkSpinner.removeClass('fa-play').addClass('fa-refresh fa-spin');
  247. $(document.body).append($script = $('<' + 'script src="https://maps.googleapis.com/maps/api/js?key=' + encodeURIComponent(key) + '&libraries=places&callback=' + encodeURIComponent(scriptLoadedFunctionName) + '"></' + 'script>'));
  248. };
  249. })();
  250. $key.on('change keydown keypress', function() {
  251. $note.html('&nbsp;').css('visibility', 'hidden');
  252. });
  253. $('#ccm-google-map-check-key')
  254. .on('click', function(e) {
  255. e.preventDefault();
  256. $note.text(<?= json_encode(t('Checking API Key...')) ?>).removeClass('alert-success alert-danger').addClass('alert-info').css('visibility', '');
  257. setupApiKey(
  258. function() {
  259. $note.text(<?= json_encode(t('The API Key is valid.')) ?>).removeClass('alert-info alert-danger').addClass('alert-success');
  260. },
  261. function(err) {
  262. $note.text(err).removeClass('alert-success alert-info').addClass('alert-danger');
  263. },
  264. true
  265. );
  266. })
  267. .trigger('click')
  268. ;
  269. $location.on('keydown', function(e) {
  270. if (e.keyCode === 13) {
  271. e.preventDefault();
  272. }
  273. });
  274. }());
  275. </script>