gmap.module 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511
  1. <?php
  2. /**
  3. * @file
  4. * GMap -- Routines to use the Google Maps API in Drupal.
  5. */
  6. use Drupal\gmap\GmapDefaults;
  7. /**
  8. * Define the Google Maps API version being used.
  9. *
  10. * Current minimum version: 2.113
  11. *
  12. * Minimum version last changed on: June 9 2008
  13. *
  14. * Reason: G_SATELLITE_3D_MAP support in gmap_addons.
  15. * See http://code.google.com/apis/earth/.
  16. *
  17. * See http://groups.google.com/group/Google-Maps-API/web/api-version-changes
  18. * for details on using other version numbers.
  19. * @todo move this to GmapDefaults class as const
  20. */
  21. define('GMAP_API_VERSION', '3');
  22. /**
  23. * Get the defaults for a gmap.
  24. * @deprecated use GmapDefaults::getInstance()->getDefaults()
  25. */
  26. function gmap_defaults() {
  27. include_once drupal_get_path('module', 'gmap') . '/lib/Drupal/gmap/GmapDefaults.php';
  28. return GmapDefaults::getInstance()->getDefaults();
  29. }
  30. /**
  31. * Implements hook_theme().
  32. *
  33. * @todo possible move to GmapDraw
  34. */
  35. function gmap_theme() {
  36. return array(
  37. 'gmap_views_ui_gmapextended' => array('render element' => 'form'),
  38. 'views_view_gmap' => array('render element' => 'element'),
  39. 'gmap_views_marker_label' => array('render element' => 'element'),
  40. 'gmap_marker_popup' => array('variables' => array('label' => '')),
  41. // 'gmap_overlay_edit' => array('render element' => 'element'),
  42. // 'gmap_macrotext' => array('render element' => 'element'),
  43. 'gmap_dimension' => array('render element' => 'element'),
  44. // 'gmap_address' => array('render element' => 'element'),
  45. // 'gmap_align' => array('render element' => 'element'),
  46. 'gmap' => array('render element' => 'element'),
  47. );
  48. }
  49. /**
  50. * Invokes hook_gmap() in every module.
  51. *
  52. * @todo move this to GmapBehaviours class
  53. * We can't use module_invoke_all() because we pass $map by reference.
  54. *
  55. * @see https://drupal.org/node/2146871
  56. */
  57. function gmap_module_invoke($op, &$map) {
  58. $return = array();
  59. foreach (module_implements('gmap') as $module) {
  60. $function = $module . '_gmap';
  61. $result = $function($op, $map);
  62. if (isset($result) && is_array($result)) {
  63. $return = array_merge_recursive($return, $result);
  64. }
  65. elseif (isset($result)) {
  66. $return[] = $result;
  67. }
  68. }
  69. return $return;
  70. }
  71. /**
  72. * Implements hook_gmap().
  73. *
  74. * @todo possible move to GmapDraw or GmapBehaviours class
  75. */
  76. function gmap_gmap($op, &$map) {
  77. switch ($op) {
  78. case 'macro':
  79. $return = array(
  80. 'points' => array(
  81. 'multiple' => TRUE,
  82. ),
  83. 'markers' => array(
  84. 'multiple' => TRUE,
  85. ),
  86. 'feed' => array(
  87. 'multiple' => TRUE,
  88. ),
  89. 'style' => array(
  90. 'multiple' => TRUE,
  91. ),
  92. );
  93. return $return;
  94. case 'pre_theme_map':
  95. break;
  96. case 'macro_multiple':
  97. $return = array(
  98. 'points',
  99. 'markers',
  100. 'feed',
  101. 'circle',
  102. 'rpolygon',
  103. 'polygon',
  104. 'line',
  105. 'style',
  106. );
  107. return $return;
  108. case 'libraries':
  109. return array('geometry');
  110. case 'behaviors':
  111. $return = array(
  112. 'locpick' => array(
  113. 'title' => t('Location chooser'),
  114. 'default' => FALSE,
  115. 'help' => t('Used to activate location choosing using a gmap.'),
  116. 'internal' => TRUE,
  117. ),
  118. 'nodrag' => array(
  119. 'title' => t('Disable dragging'),
  120. 'default' => FALSE,
  121. 'help' => t('Remove the ability for the user to drag the map. If dragging is disabled, keyboard shortcuts are implicitly disabled.'),
  122. ),
  123. 'nokeyboard' => array(
  124. 'title' => t('Disable keyboard'),
  125. 'default' => TRUE,
  126. 'help' => t('Disable the keyboard shortcuts.'),
  127. ),
  128. 'nomousezoom' => array(
  129. 'title' => t('Disable mousezoom'),
  130. 'default' => FALSE,
  131. 'help' => t('Disable using the scroll wheel to zoom the map.'),
  132. ),
  133. 'nocontzoom' => array(
  134. 'title' => t('Disable Continuous Zoom'),
  135. 'default' => FALSE,
  136. 'help' => t('Disable dynamically resizing images while waiting for tiles to load when zooming.'),
  137. ),
  138. 'autozoom' => array(
  139. 'title' => t('Use AutoZoom'),
  140. 'default' => FALSE,
  141. 'help' => t('Automatically zoom the map to fit all markers when markers are added.'),
  142. ),
  143. 'dynmarkers' => array(
  144. 'title' => t('Unconditionally enable marker interface'),
  145. 'default' => FALSE,
  146. 'help' => t('Load the marker loader system even if no markers to load are detected. Useful if you are injecting markers from somewhere else.'),
  147. ),
  148. 'overview' => array(
  149. 'title' => t('Enable Overview Map'),
  150. 'default' => FALSE,
  151. 'help' => t('Enable the "overview map" in the bottom right corner.'),
  152. 'previewable' => TRUE,
  153. ),
  154. /* 'notype' => array(
  155. 'title' => t('Disable map type control'),
  156. 'default' => FALSE,
  157. 'help' => t('Removes the map type control from the upper
  158. right corner. Recommended for very narrow maps.'),
  159. 'previewable' => TRUE,
  160. ), */
  161. 'collapsehack' => array(
  162. 'title' => t('Work around bugs when maps appear in collapsible fieldsets'),
  163. 'default' => FALSE,
  164. 'help' => t('Enabling this will work around some issues that can occur when maps appear inside collapsible fieldsets.'),
  165. ),
  166. // Note to myself, who keeps forgetting what
  167. // a scale control actually IS.:
  168. // |------------ 1mi ------------|
  169. 'scale' => array(
  170. 'title' => t('Add scale control to map.'),
  171. 'default' => FALSE,
  172. 'help' => t('Adds a scale control to the map in the default position.'),
  173. 'previewable' => TRUE,
  174. ),
  175. 'extramarkerevents' => array(
  176. 'title' => t('Enable extra marker events.'),
  177. 'default' => FALSE,
  178. 'help' => t('Used for advanced javascript work, this will enable the <em>mouseovermarker</em>, <em>mouseoutmarker</em>, and <em>dblclickmarker</em> events.'),
  179. 'internal' => TRUE,
  180. ),
  181. 'clickableshapes' => array(
  182. 'title' => t('Enable clickable shapes.'),
  183. 'default' => FALSE,
  184. 'help' => t('Used for advanced javascript work, this will enable the <em>clickshape</em> event.'),
  185. 'internal' => TRUE,
  186. ),
  187. // 'googlebar' => array(
  188. // 'title' => t('Enable Google Bar'),
  189. // 'default' => FALSE,
  190. // 'help' => t('Enable the "Google Bar" at bottom of the map.'),
  191. // 'previewable' => TRUE,
  192. // ),
  193. 'highlight' => array(
  194. 'title' => t('Highlight marker on rollover'),
  195. 'default' => FALSE,
  196. 'help' => t('Highlight marker by creating circle on mouse rollover event.'),
  197. 'previewable' => TRUE,
  198. ),
  199. );
  200. return $return;
  201. case 'baselayers':
  202. $map['Google']['Map'] = array(
  203. 'title' => t('Map: Standard street map.'),
  204. 'default' => TRUE,
  205. 'help' => t('The standard default street map. Internal name: G_NORMAL_MAP'),
  206. );
  207. $map['Google']['Satellite'] = array(
  208. 'title' => t('Satellite: Standard satellite map.'),
  209. 'default' => TRUE,
  210. 'help' => t('Satellite view without street overlay. Internal name: G_SATELLITE_MAP'),
  211. );
  212. $map['Google']['Hybrid'] = array(
  213. 'title' => t('Hybrid: Hybrid satellite map.'),
  214. 'default' => TRUE,
  215. 'help' => t('Satellite view with street overlay. Internal name: G_HYBRID_MAP'),
  216. );
  217. $map['Google']['Physical'] = array(
  218. 'title' => t('Terrain: Physical feature map.'),
  219. 'default' => FALSE,
  220. 'help' => t('Map with physical data (terrain, vegetation.) Internal name: G_PHYSICAL_MAP'),
  221. );
  222. break;
  223. }
  224. }
  225. /**
  226. * Adds the basic js files needed for a GMap.
  227. *
  228. * Is called by hook_element_info().
  229. * To add js-files for a specific Views display,
  230. * please use _gmap_pre_render_map().
  231. *
  232. * @deprecated use GmapDefaults::getInstance()->getBaseJs()
  233. */
  234. function _gmap_base_js() {
  235. include_once drupal_get_path('module', 'gmap') . '/lib/Drupal/gmap/GmapDefaults.php';
  236. return GmapDefaults::getInstance()->getBaseJs();
  237. }
  238. /**
  239. * Short description must be here.
  240. *
  241. * @todo remove this in next major release upgrade (7.x-3.x or 8.x-1.x)
  242. *
  243. * @deprecated will be removed in next major release upgrade
  244. *
  245. * @see http://drupal.org/node/1931138#comment-7137394
  246. *
  247. * @see https://drupal.org/node/2159689
  248. */
  249. function _gmap_doheader() {
  250. }
  251. /**
  252. * Convert a macro string into a GMap array.
  253. *
  254. * @todo move this to GmapMacroToolbox
  255. *
  256. * @param string $instring
  257. * Macro to process.
  258. *
  259. * @param int $ver
  260. * Version to treat macro as.
  261. * Set to 1 when processing very old macros, otherwise leave as is.
  262. *
  263. * @return array
  264. * A GMap array.
  265. */
  266. function gmap_parse_macro($instring, $ver = 2) {
  267. require_once drupal_get_path('module', 'gmap') . '/gmap_parse_macro.inc';
  268. return _gmap_parse_macro($instring, $ver);
  269. }
  270. /**
  271. * Theme a marker popup.
  272. *
  273. * @todo move this to GmapView class.
  274. * This will get called for markers embedded in macros.
  275. *
  276. * @ingroup themeable
  277. */
  278. function theme_gmap_marker_popup($vars) {
  279. return $vars['label'];
  280. }
  281. /**
  282. * Location chooser utility function.
  283. *
  284. * @todo move this to GmapLocation or GmapContrib class.
  285. *
  286. * Creates a map that can be interactively used to fill a form with a
  287. * location (latitude, longitude and zoom level).
  288. *
  289. * Note: This is a utility function designed for location.module, there is no
  290. * guarantee it will not be removed eventually.
  291. *
  292. * @param mixed $map
  293. * Either a macro to use as the base map for setting a location,
  294. * or an already set map associative array.
  295. *
  296. * @param array $form
  297. * A formset associative array. Cannot be more than one deep.
  298. *
  299. * @param array $fields
  300. * An associative array for the field names.
  301. * 'latitude', 'longitude'=>name of respective array, 'address' is optional.
  302. *
  303. * @return mixed
  304. * A string with the google map code to be inserted onto the page.
  305. */
  306. function gmap_set_location($map, &$form, $fields) {
  307. static $ctr = 0;
  308. $ctr++;
  309. if (!is_array($map)) {
  310. $map = array_merge(gmap_defaults(), gmap_parse_macro($map));
  311. }
  312. $id = 'loc' . $ctr;
  313. $map['id'] = $id;
  314. // This is a locpick map.
  315. $map['behavior']['locpick'] = TRUE;
  316. $element = array(
  317. '#type' => 'gmap',
  318. '#map' => $map['id'],
  319. '#gmap_settings' => $map,
  320. );
  321. $form[$fields['latitude']]['#map'] = $id;
  322. gmap_widget_setup($form[$fields['latitude']], 'locpick_latitude');
  323. $form[$fields['longitude']]['#map'] = $id;
  324. gmap_widget_setup($form[$fields['longitude']], 'locpick_longitude');
  325. if (isset($fields['address'])) {
  326. $form[$fields['address']]['#map'] = $id;
  327. gmap_widget_setup($form[$fields['address']], 'locpick_address');
  328. }
  329. return drupal_render($element);
  330. }
  331. /**
  332. * Handle filter preparation.
  333. *
  334. * @todo move this to GmapFilterToolbox
  335. */
  336. function _gmap_prepare($intext) {
  337. $out = FALSE;
  338. $matches = array();
  339. preg_match_all('/\[gmap([^\[\]]+ )* \] /x', $intext, $matches);
  340. $i = 0;
  341. while (isset($matches[1][$i])) {
  342. $out[0][$i] = $matches[0][$i];
  343. if ($matches[1][$i][0] == '1') {
  344. $ver = 1;
  345. $matches[1][$i] = substr($matches[0][$i], 1);
  346. }
  347. else {
  348. $ver = 2;
  349. }
  350. $map = array(
  351. '#type' => 'gmap',
  352. '#gmap_settings' => gmap_parse_macro($matches[1][$i], $ver),
  353. );
  354. $out[1][$i] = drupal_render($map);
  355. $i++;
  356. }
  357. // Endwhile process macro.
  358. return $out;
  359. }
  360. /**
  361. * Make sure a string is a valid css dimension.
  362. *
  363. * @todo move this to GmapToolbox
  364. */
  365. function gmap_todim($instring) {
  366. if (!is_string($instring)) {
  367. return FALSE;
  368. }
  369. $s = strtolower(trim($instring));
  370. $matches = array();
  371. if (preg_match('/^([\d.]+)\s*(em|ex|px|in|cm|mm|pt|pc|%)$/', $s, $matches)) {
  372. return $matches[1] . $matches[2];
  373. }
  374. else {
  375. return FALSE;
  376. }
  377. }
  378. /**
  379. * Ensure a textfield is a valid css dimension string.
  380. *
  381. * @todo move this to GmapForms
  382. */
  383. function gmap_dimension_validate(&$elem, &$form_state) {
  384. $value = gmap_todim($elem['#value']);
  385. if ($value) {
  386. // Normalize the css dimension string.
  387. form_set_value($elem, $value, $form_state);
  388. }
  389. else {
  390. form_error($elem, t('The specified value is not a valid CSS dimension.'));
  391. }
  392. }
  393. /**
  394. * Implements hook_filter_info().
  395. *
  396. * @todo move this to GmapFilterToolbox
  397. */
  398. function gmap_filter_info() {
  399. $filters['gmap_macro'] = array(
  400. 'title' => t('GMap Macro expander'),
  401. 'description' => t('GMap macros will be displayed as interactive maps.'),
  402. 'process callback' => '_gmap_filter_process',
  403. 'tips callback' => '_gmap_filter_tips',
  404. // @todo FIX ME ALREADY!!!
  405. 'cache' => FALSE,
  406. );
  407. return $filters;
  408. }
  409. /**
  410. * Filter process callback for gmap_macro.
  411. *
  412. * @todo move this to GmapFilterToolbox
  413. */
  414. function _gmap_filter_process($text, $filter, $format) {
  415. // Returns an array of $tables[0] = table macro $table[1]= table html.
  416. $gmaps = _gmap_prepare($text);
  417. // There are table macros in this node.
  418. if ($gmaps) {
  419. return str_replace($gmaps[0], $gmaps[1], $text);
  420. }
  421. else {
  422. return $text;
  423. }
  424. }
  425. /**
  426. * Implement tips callback for gmap_macro.
  427. *
  428. * @todo move this to GmapFilterToolbox
  429. */
  430. function _gmap_filter_tips($filter, $format, $long = FALSE) {
  431. // Only display macro if user can create one.
  432. if (user_access('create gmap macro')) {
  433. return t('Insert Google Map macro.') . l(t('Create a macro'), 'map/macro', array('attributes' => array('target' => '_blank')));
  434. }
  435. else {
  436. return t('Insert Google Map macro.');
  437. }
  438. }
  439. /**
  440. * Implements hook_menu().
  441. *
  442. * @todo move this to GmapMenu class ?
  443. */
  444. function gmap_menu() {
  445. $items['admin/config/services/gmap'] = array(
  446. 'title' => 'GMap',
  447. 'description' => 'Configure GMap settings',
  448. 'page callback' => 'drupal_get_form',
  449. 'page arguments' => array('gmap_admin_settings'),
  450. 'file' => 'gmap_settings_ui.inc',
  451. 'access arguments' => array('administer site configuration'),
  452. 'type' => MENU_NORMAL_ITEM,
  453. );
  454. return $items;
  455. }
  456. /**
  457. * Regenerate the markerdata file.
  458. *
  459. * @todo move this to GmapCacheToolbox
  460. */
  461. function gmap_regenerate_markers() {
  462. $contents = '';
  463. $contents .= "// GMap marker image data.\n";
  464. $contents .= "Drupal.gmap = Drupal.gmap || {};\n";
  465. $contents .= "Drupal.gmap.icondata = " . drupal_json_encode(gmap_get_icondata(TRUE)) . ";\n";
  466. $dir = 'public://js/';
  467. $filepath = $dir . 'gmap_markers.js';
  468. // Make sure js/ exists in the files folder.
  469. if (file_prepare_directory($dir, FILE_CREATE_DIRECTORY)) {
  470. $file = file_save_data($contents, 'public://js/gmap_markers.js', FILE_EXISTS_REPLACE);
  471. if ($file === FALSE) {
  472. drupal_set_message(t('GMap is unable to save the marker bundle. Please check gmap_markers.js permissions!'), 'error');
  473. }
  474. else {
  475. variable_set('gmap_marker_file', $file->fid);
  476. }
  477. }
  478. else {
  479. drupal_set_message(t('GMap is unable to save the marker bundle, so the markers will not work. Please check file system permissions of the %filepath!', array('%filepath' => $filepath)), 'error');
  480. watchdog('gmap', 'GMap write error when saving the marker bundle to %filepath.', array('%filepath' => $filepath), WATCHDOG_ERROR);
  481. }
  482. // Also regenerate the cached marker titles array.
  483. gmap_get_marker_titles(TRUE);
  484. }
  485. /**
  486. * Implements hook_flush_caches().
  487. *
  488. * @todo move this to GmapCacheToolbox
  489. */
  490. function gmap_flush_caches() {
  491. variable_del('gmap_marker_file');
  492. }
  493. /**
  494. * Implements hook_element_info().
  495. *
  496. * @todo move this to GmapElement class
  497. */
  498. function gmap_element_info() {
  499. $path = drupal_get_path('module', 'gmap');
  500. return array(
  501. 'gmap' => array(
  502. // This isn't a *form* input!!
  503. '#input' => FALSE,
  504. '#gmap_settings' => array_merge(gmap_defaults(), array(
  505. 'points' => array(),
  506. 'pointsOverlays' => array(),
  507. 'lines' => array(),
  508. )),
  509. '#attached' => array(
  510. 'css' => array(
  511. $path . '/gmap.css',
  512. ),
  513. 'js' => _gmap_base_js(),
  514. ),
  515. '#pre_render' => array('_gmap_pre_render_map'),
  516. '#theme' => 'gmap',
  517. ),
  518. 'gmap_macrotext' => array(
  519. '#input' => TRUE,
  520. '#gmap_newtype' => 'textarea',
  521. // '#theme' => 'gmap_macrotext',
  522. '#process' => array('process_gmap_control'),
  523. '#attached' => array(
  524. 'js' => array(
  525. "$path/js/macro.js" => array('weight' => 2),
  526. "$path/js/macrobuilder.js" => array('weight' => 2),
  527. ),
  528. ),
  529. '#theme' => 'textarea',
  530. ),
  531. 'gmap_overlay_edit' => array(
  532. '#input' => TRUE,
  533. '#process' => array('process_gmap_overlay_edit'),
  534. '#attached' => array(
  535. 'js' => array(
  536. "$path/js/gmap_shapes.js" => array('weight' => 2),
  537. "$path/js/overlay_edit.js" => array('weight' => 2),
  538. "$path/js/polylineEdit/src/polylineEdit_packed.js" => array('weight' => 3),
  539. "$path/js/polygonEdit/src/polygonEdit_packed.js" => array('weight' => 3),
  540. ),
  541. ),
  542. // '#theme' => 'select',
  543. ),
  544. 'gmap_style' => array(
  545. '#input' => TRUE,
  546. '#tree' => TRUE,
  547. '#gmap_style_type' => 'poly',
  548. '#process' => array('process_gmap_style'),
  549. ),
  550. 'gmap_address' => array(
  551. '#input' => TRUE,
  552. '#process' => array('process_gmap_address'),
  553. '#attached' => array(
  554. 'js' => array(
  555. "$path/js/address.js" => array('weight' => 2),
  556. ),
  557. ),
  558. '#autocomplete_path' => '',
  559. '#theme' => 'textfield',
  560. ),
  561. 'gmap_latitude' => array(
  562. '#input' => TRUE,
  563. '#gmap_newtype' => 'textfield',
  564. '#process' => array('process_gmap_control'),
  565. ),
  566. 'gmap_longitude' => array(
  567. '#input' => TRUE,
  568. '#gmap_newtype' => 'textfield',
  569. '#process' => array('process_gmap_control'),
  570. ),
  571. 'gmap_latlon' => array(
  572. '#input' => TRUE,
  573. '#gmap_newtype' => 'textfield',
  574. '#process' => array('process_gmap_control'),
  575. ),
  576. 'gmap_markerchooser' => array('#input' => TRUE, '#process' => array('process_gmap_markerchooser')),
  577. 'gmap_dimension' => array(
  578. '#input' => TRUE,
  579. '#gmap_newtype' => 'textfield',
  580. '#process' => array('process_gmap_control'),
  581. '#element_validate' => array('gmap_dimension_validate'),
  582. ),
  583. );
  584. }
  585. /**
  586. * Implements hook_views_pre_render().
  587. *
  588. * Pre render function to load ajax-js file, which is
  589. * only possible if ajax is enabled in the display.
  590. * @todo move this to GmapViewsToolbox
  591. */
  592. function gmap_views_pre_render(&$view) {
  593. static $gmap_processed = FALSE;
  594. // Add js only for gmap style views with ajax and not already processed.
  595. if (($view->plugin_name != 'gmap' && $view->plugin_name != 'gmapextended')
  596. || (!$view->use_ajax) || $gmap_processed
  597. ) {
  598. return;
  599. }
  600. // Mark the view as already processed.
  601. $gmap_processed = TRUE;
  602. // Add js, now we are certain ajax is enabled.
  603. drupal_add_js(drupal_get_path('module', 'gmap') . '/js/gmap_views_ajax.js', array('group' => JS_DEFAULT));
  604. }
  605. /**
  606. * Pre render function to make sure all required JS is available.
  607. *
  608. * Depending on the display's behavior.
  609. * @todo move this to GmapElement class
  610. */
  611. function _gmap_pre_render_map($element) {
  612. $path = drupal_get_path('module', 'gmap');
  613. if (!isset($element['#gmap_settings'])) {
  614. $element['#gmap_settings'] = $element['#settings'];
  615. }
  616. $map = $element['#gmap_settings'];
  617. if (isset($map['behavior']['locpick']) && $map['behavior']['locpick']) {
  618. $element['#attached']['js']["$path/js/locpick.js"] = array('weight' => 2);
  619. }
  620. if (!empty($map['markers']) || !empty($map['lines'])) {
  621. $element['#attached']['js']["$path/js/markerloader_static.js"] = array('weight' => 5);
  622. }
  623. if (!empty($map['shapes'])) {
  624. $element['#attached']['js']["$path/js/shapeloader_static.js"] = array('weight' => 5);
  625. $element['#attached']['js']["$path/js/gmap_shapes.js"] = array('weight' => 5);
  626. }
  627. if (isset($map['feed']) && is_array($map['feed'])) {
  628. $element['#attached']['js']["$path/js/markerloader_georss.js"] = array('weight' => 5);
  629. }
  630. // Add the markermanager.
  631. if ((isset($map['behavior']['dynmarkers']) && $map['behavior']['dynmarkers']) || !empty($map['markers'])) {
  632. static $header_set = FALSE;
  633. if (!$header_set) {
  634. $header_set = TRUE;
  635. if (!variable_get('gmap_marker_file', FALSE)) {
  636. gmap_regenerate_markers();
  637. }
  638. }
  639. }
  640. $mm = variable_get('gmap_mm_type', 'gmap');
  641. $mms = variable_get('gmap_markermanager', array());
  642. // If you really really want to override the marker manager, implement
  643. // this, take $mm by ref, and have fun. --Bdragon
  644. if (function_exists('_gmap_markermanager_override')) {
  645. _gmap_markermanager_override($mm);
  646. }
  647. if (isset($mms[$mm]['filename'])) {
  648. $element['#attached']['js'][$path . '/thirdparty/' . $mms[$mm]['filename']] = array('weight' => 5);
  649. $element['#attached']['js'][] = array('data' => array('gmap_markermanager' => $mms[$mm]), 'type' => 'setting');
  650. }
  651. $element['#attached']['js'][$path . '/js/' . $mm . '_marker.js'] = array('weight' => 5);
  652. return $element;
  653. }
  654. /**
  655. * Generic gmap control #process function.
  656. *
  657. * @todo move this to GmapElement class
  658. */
  659. function process_gmap_control($element, &$form_state, $complete_form) {
  660. $control = substr($element['#type'], 5);
  661. $element['#type'] = $element['#gmap_newtype'];
  662. unset($element['#gmap_newtype']);
  663. gmap_widget_setup($element, $control);
  664. // Attempt to run any #process handlers of our transmogrified type.
  665. // However, if we aren't the only #process handler, we assume someone else
  666. // is taking care of setting up any default handlers.
  667. $chain = FALSE;
  668. if (count($element['#process']) == 1) {
  669. // Unset #process so we can pull in the default.
  670. unset($element['#process']);
  671. $chain = TRUE;
  672. }
  673. // Inherit #input from the new type.
  674. unset($element['#input']);
  675. // Merge in the defaults for the target element type.
  676. $element += element_info($element['#type']);
  677. if (isset($element['#input']) && $element['#input']) {
  678. if (isset($element['#process']) && $chain) {
  679. // Chain process.
  680. foreach ($element['#process'] as $process) {
  681. if (function_exists($process)) {
  682. $form = $process($element, $form_state, $complete_form);
  683. }
  684. }
  685. }
  686. }
  687. return $element;
  688. }
  689. /**
  690. * Style fieldset #process function.
  691. *
  692. * @todo move this to GmapElement class
  693. */
  694. function process_gmap_style($element) {
  695. $isline = ($element['#gmap_style_type'] == 'line');
  696. // Stroke color.
  697. $element[0] = array(
  698. '#type' => 'textfield',
  699. '#size' => 6,
  700. '#maxlength' => 6,
  701. '#field_prefix' => '#',
  702. '#title' => t('Stroke color'),
  703. '#value' => $element['#value'][0],
  704. '#attributes' => array('class' => array('gmap_style')),
  705. );
  706. // Stroke weight.
  707. $element[1] = array(
  708. '#type' => 'textfield',
  709. '#title' => t('Stroke weight'),
  710. '#description' => t('Thickness of line, in pixels.'),
  711. '#size' => 3,
  712. '#maxlength' => 3,
  713. '#field_suffix' => t('px'),
  714. '#value' => $element['#value'][1],
  715. '#attributes' => array('class' => array('gmap_style')),
  716. );
  717. // Stroke opacity.
  718. $element[2] = array(
  719. '#type' => 'textfield',
  720. '#title' => t('Stroke opacity'),
  721. '#size' => 3,
  722. '#maxlength' => 3,
  723. '#field_suffix' => '%',
  724. '#value' => $element['#value'][2],
  725. '#attributes' => array('class' => array('gmap_style')),
  726. );
  727. // Fill color.
  728. $element[3] = array(
  729. '#type' => 'textfield',
  730. '#title' => t('Fill color'),
  731. '#description' => t('Hex color value for fill color. Example: #<em>00AA33</em>'),
  732. '#size' => 6,
  733. '#maxlength' => 6,
  734. '#field_prefix' => '#',
  735. '#value' => $isline ? '' : $element['#value'][3],
  736. '#disabled' => $isline,
  737. '#attributes' => array('class' => array('gmap_style')),
  738. );
  739. $element[4] = array(
  740. '#type' => 'textfield',
  741. '#title' => t('Fill opacity'),
  742. '#description' => t('Opacity of fill, from 0 to 100%.'),
  743. '#size' => 3,
  744. '#maxlength' => 3,
  745. '#field_suffix' => '%',
  746. '#value' => $isline ? '' : $element['#value'][4],
  747. '#disabled' => $isline,
  748. '#attributes' => array('class' => array('gmap_style')),
  749. );
  750. unset($element['#input']);
  751. $element_info = element_info('fieldset');
  752. $element = array_merge($element, $element_info);
  753. return $element;
  754. }
  755. /**
  756. * Overlay editor #process function.
  757. *
  758. * @todo move this to GmapElement class
  759. */
  760. function process_gmap_overlay_edit($element) {
  761. // Conver the root element into a fieldset.
  762. $element['#type'] = 'fieldset';
  763. if (!isset($element['#title'])) {
  764. $element['#title'] = t('Overlay editor');
  765. }
  766. $element['#tree'] = TRUE;
  767. $element['mapclicktype'] = array(
  768. '#type' => 'select',
  769. '#title' => t('Click map'),
  770. '#map' => $element['#map'],
  771. '#options' => array(
  772. 'Points' => t('Points'),
  773. 'Lines' => t('Lines'),
  774. 'Circles' => t('Circles'),
  775. 'GPolygon' => t('Filled Polygons'),
  776. ),
  777. );
  778. gmap_widget_setup($element['mapclicktype'], 'overlayedit_mapclicktype');
  779. $element['markerclicktype'] = array(
  780. '#type' => 'select',
  781. '#title' => t('Click marker / segment'),
  782. '#map' => $element['#map'],
  783. '#options' => array(
  784. 'Remove' => t('Remove'),
  785. // 'updatestyle' => t('Update Styles'),
  786. // 'removestyle' => t('Remove Styles'),
  787. 'Edit Info' => t('Edit Info'),
  788. ),
  789. );
  790. gmap_widget_setup($element['markerclicktype'], 'overlayedit_markerclicktype');
  791. $element['marker'] = array(
  792. '#type' => 'select',
  793. '#map' => $element['#map'],
  794. '#options' => gmap_get_marker_titles(),
  795. '#title' => t('Marker'),
  796. // '#theme' => 'gmap_overlay_edit',
  797. );
  798. gmap_widget_setup($element['marker'], 'overlayedit');
  799. $element['linestyle'] = array(
  800. '#type' => 'gmap_style',
  801. '#title' => t('Line style'),
  802. '#gmap_style_type' => 'line',
  803. // @@@.
  804. '#default_value' => array('0000ff', 5, 45, '', ''),
  805. );
  806. gmap_widget_setup($element['linestyle'], 'overlayedit_linestyle', $element['#map']);
  807. $element['linestyle']['linestyle_apply'] = array(
  808. '#tree' => FALSE,
  809. '#type' => 'checkbox',
  810. '#title' => t('Use for new and changed lines'),
  811. '#default_value' => FALSE,
  812. );
  813. gmap_widget_setup($element['linestyle']['linestyle_apply'], 'overlayedit_linestyle_apply', $element['#map']);
  814. $element['polystyle'] = array(
  815. '#type' => 'gmap_style',
  816. '#title' => t('Polygon style'),
  817. '#gmap_style_type' => 'poly',
  818. // @@@.
  819. '#default_value' => array('000000', 3, 25, 'ff0000', 45),
  820. );
  821. gmap_widget_setup($element['polystyle'], 'overlayedit_polystyle', $element['#map']);
  822. $element['polystyle']['polystyle_apply'] = array(
  823. '#tree' => FALSE,
  824. '#type' => 'checkbox',
  825. '#title' => t('Use for new and changed polygons'),
  826. '#default_value' => FALSE,
  827. );
  828. gmap_widget_setup($element['polystyle']['polystyle_apply'], 'overlayedit_polystyle_apply', $element['#map']);
  829. $element += element_info('fieldset');
  830. return $element;
  831. }
  832. /**
  833. * Address widget #process function.
  834. *
  835. * @todo move this to GmapElement class
  836. */
  837. function process_gmap_address($element) {
  838. $element['#type'] = 'textfield';
  839. gmap_widget_setup($element, 'address');
  840. $element += element_info('textfield');
  841. return $element;
  842. }
  843. /**
  844. * Marker chooser #process function.
  845. *
  846. * @todo move this to GmapElement class
  847. */
  848. function process_gmap_markerchooser($element) {
  849. $element['#type'] = 'select';
  850. $options = gmap_get_marker_titles();
  851. if (!isset($element['#required']) || !$element['#required']) {
  852. $options = array('' => t('Default')) + $options;
  853. }
  854. $element['#options'] = $options;
  855. $element += element_info('select');
  856. return $element;
  857. }
  858. /**
  859. * Perform some normalization on the map object to prevent errors.
  860. *
  861. * @todo move this to GmapToolbox
  862. */
  863. function gmap_map_cleanup(&$map) {
  864. // Google is picky about this one.
  865. $map['zoom'] = (int) $map['zoom'];
  866. // Normalize latitude / longitude.
  867. if ($map['latlong']) {
  868. $map['latlon'] = $map['latlong'];
  869. unset($map['latlong']);
  870. }
  871. // Normalize extent.
  872. if (isset($map['extent']) && is_string($map['extent'])) {
  873. $tmp = explode(',', $map['extent']);
  874. if (count($tmp) == 4) {
  875. // String form of extent has x,y, but native array
  876. // form is lat,lon, so need to flip them a bit.
  877. $map['extent'] = array(array($tmp[1], $tmp[0]), array($tmp[3], $tmp[2]));
  878. }
  879. else {
  880. // Extent was unparseable, don't pass it.
  881. unset($map['extent']);
  882. }
  883. }
  884. if (isset($map['latlon']) && (!isset($map['latitude']) || !isset($map['longitude']))) {
  885. list($map['latitude'], $map['longitude']) = explode(',', $map['latlon']);
  886. }
  887. unset($map['latlon']);
  888. foreach ($map['baselayers'] as $k => $v) {
  889. if (!$v) {
  890. unset($map['baselayers'][$k]);
  891. }
  892. }
  893. }
  894. /**
  895. * Gmap element theme hook.
  896. *
  897. * @todo looks like this is a part of feature GmapDraw class
  898. */
  899. function theme_gmap($variables) {
  900. $element = $variables['element'];
  901. // Track the mapids we've used already.
  902. static $mapids = array();
  903. $mapid = FALSE;
  904. if (isset($element['#map']) && $element['#map']) {
  905. // The default mapid is #map.
  906. $mapid = $element['#map'];
  907. }
  908. if (isset($element['#gmap_settings']['id'])) {
  909. // Settings overrides it.
  910. $mapid = $element['#gmap_settings']['id'];
  911. }
  912. if (!$mapid) {
  913. // Hmm, no mapid. Generate one.
  914. $mapid = gmap_get_auto_mapid();
  915. }
  916. // Push the mapid back into #map.
  917. $element['#map'] = $mapid;
  918. gmap_widget_setup($element, 'gmap', $mapid);
  919. if (!isset($element['#gmap_settings'])) {
  920. $element['#gmap_settings'] = array();
  921. }
  922. // Push the mapid back into #gmap_settings.
  923. $element['#gmap_settings']['id'] = $mapid;
  924. $mapdefaults = gmap_defaults();
  925. $map = array_merge($mapdefaults, $element['#gmap_settings']);
  926. // Styles is a subarray.
  927. if (isset($element['#gmap_settings']['styles'])) {
  928. $map['styles'] = array_merge($mapdefaults['styles'], $element['#gmap_settings']['styles']);
  929. }
  930. gmap_map_cleanup($map);
  931. // Add a class around map bubble contents.
  932. // @@@ Bdragon sez: Becw, this doesn't belong here.
  933. // Theming needs to get fixed instead..
  934. if (isset($map['markers'])) {
  935. foreach ($map['markers'] as $i => $marker) {
  936. if (isset($marker['text'])) {
  937. $map['markers'][$i]['text'] = '<div class="gmap-popup">' . $marker['text'] . '</div>';
  938. }
  939. }
  940. }
  941. switch (strtolower($map['align'])) {
  942. case 'left':
  943. $element['#attributes']['class'][] = 'gmap-left';
  944. break;
  945. case 'right':
  946. $element['#attributes']['class'][] = 'gmap-right';
  947. break;
  948. case 'center':
  949. case 'centre':
  950. $element['#attributes']['class'][] = 'gmap-center';
  951. }
  952. $style = array();
  953. $style[] = 'width: ' . $map['width'];
  954. $style[] = 'height: ' . $map['height'];
  955. $element['#attributes']['class'] = array_merge($element['#attributes']['class'], array(
  956. 'gmap',
  957. 'gmap-map',
  958. 'gmap-' . $mapid . '-gmap',
  959. ));
  960. // Some markup parsers (IE) don't handle empty inners well.
  961. // Use the space to let users know javascript is required.
  962. // @@@ Bevan sez: Google static maps could be useful here.
  963. // @@@ Bdragon sez: Yeah, would be nice, but hard to guarantee functionality.
  964. // Not everyone uses the static markerloader.
  965. $o = '<div style="' . implode('; ', $style) . ';" id="' . $element['#id'] . '"' . drupal_attributes($element['#attributes']) . '><noscript>' . t('Javascript is required to view this map.') . '</noscript></div>';
  966. gmap_module_invoke('pre_theme_map', $map);
  967. if (isset($mapids[$element['#map']])) {
  968. drupal_set_message(t('Duplicate map detected! GMap does not support multiplexing maps onto one MapID! GMap MapID: %mapid', array('%mapid' => $element['#map'])), 'error');
  969. // Return the div anyway. All but one map for a given id will be a graymap,
  970. // because obj.map gets stomped when trying to multiplex maps!
  971. return $o;
  972. }
  973. $mapids[$element['#map']] = TRUE;
  974. // Put map data in a setting.
  975. drupal_add_js(array('gmap' => array($element['#map'] => $map)), 'setting');
  976. return $o;
  977. }
  978. /**
  979. * Set up widget.
  980. *
  981. * This function will change a form element's ID so it is found
  982. * by the GMap handlers system.
  983. * @todo move this to GmapDraw class.
  984. *
  985. * @param array $element
  986. * The form element to modify.
  987. *
  988. * @param string $type
  989. * The gmap widget type to map to.
  990. *
  991. * @param null $map
  992. * The map id. If not defined, $element['#map'] will be used.
  993. */
  994. function gmap_widget_setup(&$element, $type, $map = NULL) {
  995. if (!$map) {
  996. if (isset($element['#map'])) {
  997. $map = $element['#map'];
  998. }
  999. else {
  1000. // Hmm, missing #map. Try to figure it out.
  1001. if (isset($element['#gmap_settings']['id'])) {
  1002. $map = $element['#gmap_settings']['id'];
  1003. }
  1004. }
  1005. }
  1006. if (!isset($element['#attributes']['class'])) {
  1007. $element['#attributes']['class'] = array();
  1008. }
  1009. $element['#attributes']['class'] = array_merge($element['#attributes']['class'], array(
  1010. 'gmap-control',
  1011. 'gmap-' . $type,
  1012. ));
  1013. $element['#id'] = gmap_get_id($map, $type);
  1014. $element['#map'] = $map;
  1015. }
  1016. /**
  1017. * Get a CSS id for a map and type.
  1018. *
  1019. * @todo move this to GmapToolbox
  1020. *
  1021. * Since CSS ids have to be unique, GMap related IDs are assigned by
  1022. * this function.
  1023. */
  1024. function gmap_get_id($map, $type) {
  1025. static $serial = array();
  1026. if (!isset($serial[$map])) {
  1027. $serial[$map] = array();
  1028. }
  1029. if (!isset($serial[$map][$type])) {
  1030. $serial[$map][$type] = -1;
  1031. }
  1032. $serial[$map][$type]++;
  1033. return 'gmap-' . $map . '-' . $type . $serial[$map][$type];
  1034. }
  1035. /**
  1036. * Generate a dynamic map identifier.
  1037. *
  1038. * @todo move this to GmapToolbox
  1039. */
  1040. function gmap_get_auto_mapid() {
  1041. static $auto = 0;
  1042. $auto++;
  1043. return 'auto' . $auto . 'map';
  1044. }
  1045. /**
  1046. * Get the list of marker titles.
  1047. *
  1048. * @todo move this to GmapMarkerInfo class
  1049. */
  1050. function gmap_get_marker_titles($reset = FALSE) {
  1051. static $titles;
  1052. if (!$reset) {
  1053. if (is_array($titles)) {
  1054. return $titles;
  1055. }
  1056. $cached = cache_get('gmap_marker_titles', 'cache');
  1057. if (!empty($cached)) {
  1058. $titles = $cached->data;
  1059. if (is_array($titles)) {
  1060. return $titles;
  1061. }
  1062. }
  1063. }
  1064. module_load_include('inc', 'gmap', 'gmap_markerinfo');
  1065. $titles = _gmap_get_marker_titles();
  1066. cache_set('gmap_marker_titles', $titles, 'cache');
  1067. return $titles;
  1068. }
  1069. /**
  1070. * Get the JSON icon data for all the default markers.
  1071. *
  1072. * @todo move this to GmapMarkerInfo class
  1073. */
  1074. function gmap_get_icondata($reset = FALSE) {
  1075. static $icons;
  1076. if (is_array($icons) && !$reset) {
  1077. return $icons;
  1078. }
  1079. $icons = cache_get('gmap_icondata');
  1080. if ($icons) {
  1081. $icons = $icons->data;
  1082. }
  1083. if ($reset || !$icons) {
  1084. module_load_include('inc', 'gmap', 'gmap_markerinfo');
  1085. $icons = _gmap_get_icondata();
  1086. }
  1087. cache_set('gmap_icondata', $icons, 'cache');
  1088. return $icons;
  1089. }
  1090. /**
  1091. * Utility function to allow high-precision decimals to work with the SQL layer.
  1092. *
  1093. * Use concatenation. (Apparently unquoted %s is bad.)
  1094. * @todo move this to GmapPolylineToolbox or GmapToolbox
  1095. */
  1096. function gmap_decimal($num) {
  1097. // Paraphrased from postgresql documentation:
  1098. //
  1099. // Numbers in SQL can be in one of these forms:
  1100. // digits,
  1101. // digits.[digits][e[+-]digits],
  1102. // [digits].digits[e[+-]digits],
  1103. // digitse[+-]digits,
  1104. // where "digits" is one or more decimal digits.
  1105. // Trim extra whitespace.
  1106. $num = trim($num);
  1107. // Check if we're in an acceptable form.
  1108. if (preg_match('/^[+\-]?((\d+)|(\d+\.\d*)|(\d*\.\d+))(e[+\-]?\d+)?$/', $num) === 1) {
  1109. // Good, we can pass that right along.
  1110. return $num;
  1111. }
  1112. // Otherwise, cast to float, possibly losing precision.
  1113. return (float) $num;
  1114. }
  1115. /**
  1116. * Utility function to use the google maps geocoder server side.
  1117. *
  1118. * @todo move this to GmapRequest class
  1119. *
  1120. * This is an easy, quick way to geocode a single address.
  1121. * Note: This is a REMOTE CALL TO GOOGLE.
  1122. * Do NOT use this where performance matters,
  1123. * as it could possibly take several seconds for this function to return.
  1124. * @see http://www.google.com/apis/maps/documentation/reference.html#GGeoStatusCode
  1125. * @see http://drupal.org/node/1940474
  1126. */
  1127. function gmap_geocode($address, $tld = 'com') {
  1128. $data = drupal_http_request(gmap_views_protocol() . '://maps.googleapis.' . $tld . '/maps/api/geocode/json?address=' . urlencode($address));
  1129. if ($data->code == 200) {
  1130. $data_decoded = json_decode($data->data);
  1131. if ($data_decoded->status == 'ZERO_RESULTS' || $data_decoded->status == 'OVER_QUERY_LIMIT') {
  1132. return array(
  1133. 'status' => $data_decoded->status,
  1134. 'accuracy' => NULL,
  1135. 'latitude' => NULL,
  1136. 'longitude' => NULL,
  1137. );
  1138. }
  1139. else {
  1140. return array(
  1141. 'status' => $data_decoded->status,
  1142. 'accuracy' => $data_decoded->results[0]->geometry->location_type,
  1143. 'latitude' => $data_decoded->results[0]->geometry->location->lat,
  1144. 'longitude' => $data_decoded->results[0]->geometry->location->lng,
  1145. );
  1146. }
  1147. }
  1148. // Non 200 is G_GEO_SERVER_ERROR (500).
  1149. return array(
  1150. 'status' => 500,
  1151. );
  1152. }
  1153. /**
  1154. * Simple way to draw a map from inside a theme.
  1155. *
  1156. * @todo move this to GmapDraw class
  1157. *
  1158. * @param array $latitude
  1159. * Latitude of marker.
  1160. *
  1161. * @param array $longitude
  1162. * Longitude of marker.
  1163. *
  1164. * @param string $markername
  1165. * Marker to use.'' will fall back to google's default marker.
  1166. *
  1167. * @param string $info
  1168. * What to show in the bubble when the marker is clicked.
  1169. * Leave blank if you don't want a bubble.
  1170. *
  1171. * @param string $zoom
  1172. * 'default' will use the default zoom from the settings page.
  1173. * 3 is usually a good value to use.
  1174. *
  1175. * @param string $width
  1176. * 'default' will use the default width from the settings page.
  1177. *
  1178. * @param string $height
  1179. * 'default' will use the default height from the settings page.
  1180. *
  1181. * @param bool $autoshow
  1182. * If set to TRUE, automatically show the marker bubble.
  1183. *
  1184. * @param array $map
  1185. * Override parts of the map array.
  1186. * If you need to do much with this, you should probabaly be putting together
  1187. * the map array manually.
  1188. *
  1189. * @return string
  1190. * Return rendered element.
  1191. */
  1192. function gmap_simple_map($latitude, $longitude, $markername = '', $info = '', $zoom = 'auto', $width = 'default', $height = 'default', $autoshow = FALSE, $map = array()) {
  1193. $settings = array(
  1194. 'id' => gmap_get_auto_mapid(),
  1195. // Center the map on the marker.
  1196. 'latitude' => $latitude,
  1197. 'longitude' => $longitude,
  1198. );
  1199. if ($zoom != 'default') {
  1200. $settings['zoom'] = $zoom;
  1201. }
  1202. if ($width != 'default') {
  1203. $settings['width'] = $width;
  1204. }
  1205. if ($height != 'default') {
  1206. $settings['height'] = $height;
  1207. }
  1208. $settings['markers'] = array(
  1209. array(
  1210. 'latitude' => $latitude,
  1211. 'longitude' => $longitude,
  1212. 'markername' => $markername,
  1213. 'offset' => 0,
  1214. ),
  1215. );
  1216. if (!empty($info)) {
  1217. $settings['markers'][0]['text'] = $info;
  1218. }
  1219. if ($autoshow) {
  1220. $settings['markers'][0]['autoclick'] = TRUE;
  1221. }
  1222. if (!empty($map)) {
  1223. $settings = array_merge($settings, $map);
  1224. }
  1225. $element = array(
  1226. '#type' => 'gmap',
  1227. '#gmap_settings' => $settings,
  1228. );
  1229. return drupal_render($element);
  1230. }
  1231. /**
  1232. * Implements hook_keys_service().
  1233. *
  1234. * (from the keys api)
  1235. * @todo move this to GmapContribToolbox class
  1236. */
  1237. function gmap_keys_service() {
  1238. // @todo Remove after everyone has upgraded from keys api.
  1239. if (module_exists('keys_api')) {
  1240. return array(
  1241. 'gmap' => array(
  1242. 'name' => t('Gmap'),
  1243. 'description' => t('Google Maps API Key'),
  1244. ),
  1245. );
  1246. }
  1247. elseif (module_exists('keys')) {
  1248. // @greenSkin:
  1249. // What is your reasoning behind predefining this?
  1250. // I'll avoid overriding you for now, but this seems rather arbitrary.
  1251. // Reference: http://drupal.org/cvs?commit=310498
  1252. // Probe keys to determine if it is defining our key for us.
  1253. $test = array();
  1254. if (function_exists('keys_keys_service')) {
  1255. $test = keys_keys_service();
  1256. }
  1257. if (!isset($test['google_maps'])) {
  1258. // Be forward compatible with future versions of keys api
  1259. // that no longer define it.
  1260. return array(
  1261. 'google_maps' => array(
  1262. 'name' => t('Google Maps'),
  1263. 'description' => t('Google Maps API Key'),
  1264. ),
  1265. );
  1266. }
  1267. }
  1268. }
  1269. /**
  1270. * Retrieve the Google Maps key that is in use for the site.
  1271. *
  1272. * @todo move this to GmapDefaults class
  1273. */
  1274. function gmap_get_key() {
  1275. $key = variable_get('gmap_api_key', FALSE);
  1276. if (module_exists('keys_api')) {
  1277. $key = keys_api_get_key('gmap', $_SERVER['HTTP_HOST']);
  1278. }
  1279. elseif (module_exists('keys')) {
  1280. $key = keys_get_key('google_maps');
  1281. }
  1282. return $key;
  1283. }
  1284. /**
  1285. * Implements hook_views_plugins().
  1286. *
  1287. * @todo move this to GmapViews class
  1288. */
  1289. function gmap_views_plugins() {
  1290. return array(
  1291. 'module' => 'gmap',
  1292. 'style' => array(
  1293. 'gmap' => array(
  1294. 'title' => t('GMap'),
  1295. 'help' => t('Displays rows as a map.'),
  1296. 'handler' => 'gmap_plugin_style_gmap',
  1297. 'theme' => 'gmap_view_gmap',
  1298. 'uses row plugin' => TRUE,
  1299. 'uses grouping' => TRUE,
  1300. 'uses options' => TRUE,
  1301. 'type' => 'normal',
  1302. ),
  1303. 'gmapextended' => array(
  1304. 'title' => t('Extended GMap'),
  1305. 'help' => t('Displays a map of markers.'),
  1306. 'handler' => 'gmap_plugin_style_gmapextended',
  1307. 'theme' => 'gmap_views_view_gmapextended',
  1308. 'uses row plugin' => TRUE,
  1309. 'uses fields' => TRUE,
  1310. 'uses options' => TRUE,
  1311. // 'uses grouping' => TRUE,
  1312. 'type' => 'normal',
  1313. ),
  1314. ),
  1315. );
  1316. }
  1317. /**
  1318. * Implements hook_views_ajax_data_alter().
  1319. *
  1320. * @todo move this to GmapViews class
  1321. */
  1322. function gmap_views_ajax_data_alter(&$commands, $view) {
  1323. // Add js callback only with gmap style plugins and with ajax.
  1324. $plugin_styles = array($view->plugin_name);
  1325. foreach ($view->display as $display) {
  1326. if (isset($display->display_options['style_plugin'])) {
  1327. $plugin_styles[] = $display->display_options['style_plugin'];
  1328. }
  1329. }
  1330. if (!count(array_intersect(array('gmap', 'gmapextended'), $plugin_styles))) {
  1331. return;
  1332. }
  1333. // Find the JQuery selector for the view's wrapper in the DOM.
  1334. $target = '';
  1335. foreach ($commands as $command) {
  1336. if ($command['command'] == 'insert') {
  1337. $target = $command['selector'];
  1338. }
  1339. }
  1340. $command = array('command' => 'gmapAjaxViewsFix', 'target' => $target);
  1341. // Save settings.
  1342. $js = drupal_add_js(NULL, array('scope' => 'header'));
  1343. $command['settings'] = $js['settings']['data'];
  1344. $commands[] = $command;
  1345. }
  1346. /**
  1347. * Views map theme.
  1348. */
  1349. function theme_gmap_views_ui_gmapextended($variables) {
  1350. $form = $variables['form'];
  1351. $output = drupal_render($form['description_markup']);
  1352. $header = array(
  1353. t('Field'),
  1354. t('Purpose'),
  1355. t('Separator'),
  1356. /* array(
  1357. 'data' => t('Sortable'),
  1358. 'align' => 'center',
  1359. ),
  1360. array(
  1361. 'data' => t('Default sort'),
  1362. 'align' => 'center',
  1363. ),*/
  1364. );
  1365. $rows = array();
  1366. foreach (element_children($form['field_purposes']) as $id) {
  1367. $row = array();
  1368. $row[] = drupal_render($form['info'][$id]['name']);
  1369. $row[] = drupal_render($form['field_purposes'][$id]);
  1370. $row[] = drupal_render($form['info'][$id]['separator']);
  1371. $rows[] = $row;
  1372. }
  1373. // Add the special 'None' row.
  1374. // $rows[] = array(t('None'), '', '', '',
  1375. // array('align' => 'center', 'data' => drupal_render($form['default'][-1])));
  1376. $output .= theme('table', array('header' => $header, 'rows' => $rows));
  1377. $output .= drupal_render_children($form);
  1378. return $output;
  1379. }
  1380. /**
  1381. * Preprocess function for theme_gmap_view_gmap().
  1382. *
  1383. * @todo possible move this to GmapView class
  1384. */
  1385. function template_preprocess_gmap_view_gmap(&$vars) {
  1386. $vars['map_object'] = $vars['rows'];
  1387. // Rows is actually our map object.
  1388. unset($vars['rows']);
  1389. $vars['map_element'] = array(
  1390. '#type' => 'gmap',
  1391. '#gmap_settings' => $vars['map_object'],
  1392. '#view' => $vars['view'],
  1393. );
  1394. // Theme the map.
  1395. $vars['map'] = drupal_render($vars['map_element']);
  1396. }
  1397. /**
  1398. * Determine the site protocol (http or https).
  1399. *
  1400. * @todo move this to GmapRequest class
  1401. */
  1402. function gmap_views_protocol() {
  1403. global $base_url;
  1404. return parse_url($base_url, PHP_URL_SCHEME) == 'https' ? 'https' : 'http';
  1405. }