/google_maps/api.php

https://github.com/mmakaay/Modules · PHP · 224 lines · 137 code · 23 blank · 64 comment · 20 complexity · ec3529f7aa1e41f1a45b4bbf8731b61f MD5 · raw file

  1. <?php
  2. /**
  3. * A utility function, which can be used to build the HTML code that is
  4. * needed for displaying a map.
  5. *
  6. * @param string $maptool_type
  7. * This parameter sets the type of map to show. This can be one of:
  8. * - location-editor : an editor that can be used to setup a marker
  9. * or a streetview.
  10. * - map-editor : an editor that can be used to setup a map view.
  11. * This does not provide marker and streetview
  12. * functionality.
  13. * - viewer : a viewer, that can be used to display a map state,
  14. * possibly containing a single marker or a streetview.
  15. * - plotter : a viewer that can be used to plot a lot of markers.
  16. *
  17. * @param array $state
  18. * State data to include in the maptool data. For types other than
  19. * "plotter", the data is an array that can contain the following fields:
  20. * - map_latitude
  21. * - map_longitude
  22. * - map_zoom
  23. * - map_type
  24. * - marker_latitude
  25. * - marker_longitude
  26. * - streetview_latitude
  27. * - streetview_longitude
  28. * - streetview_heading
  29. * - streetview_pitch
  30. * - streetview_zoom
  31. * - geoloc_country
  32. * - geoloc_city
  33. *
  34. * In case of type "plotter", the data is an array containing plot
  35. * points for the map. Each plot point is an array, containing the fields:
  36. * - latitude
  37. * - longitude
  38. * - info: optional HTML code to add in a info window for the related marker
  39. */
  40. function mod_google_maps_build_maptool($maptool_type, $state = NULL)
  41. {
  42. global $PHORUM;
  43. // We might have to load the language file ourselves,
  44. // in case this code is included from the admin interface.
  45. // We only need to include the english language file in that case.
  46. if (! isset($PHORUM['DATA']['LANG']['mod_google_maps'])) {
  47. include_once dirname(__FILE__) . "/lang/english.php";
  48. }
  49. // Check the maptool type parameter.
  50. if ($maptool_type !== 'location-editor' &&
  51. $maptool_type !== 'map-editor' &&
  52. $maptool_type !== 'viewer' &&
  53. $maptool_type !== 'plotter') {
  54. trigger_error('Illegal maptool type: ' . $maptool_type, E_USER_ERROR);
  55. }
  56. // Defaults.
  57. $data = array(
  58. 'reset_latitude' => 40,
  59. 'reset_longitude' => -20,
  60. 'reset_zoom' => 1,
  61. 'reset_type' => 'roadmap'
  62. );
  63. // Override defaults with settings from the module configuration
  64. // (i.e. the reset_* options that define the reset state of/ the map.)
  65. foreach ($PHORUM['mod_google_maps'] as $key => $val) {
  66. if (isset($data["reset_$key"])) {
  67. $data["reset_$key"] = $val;
  68. }
  69. }
  70. // Add map state data.
  71. if ($maptool_type === 'plotter') {
  72. $data['plot'] = $state;
  73. } else {
  74. if (!empty($state)) {
  75. foreach ($state as $key => $val) {
  76. $data[$key] = $val;
  77. }
  78. }
  79. }
  80. $PHORUM['maptool'] = $data;
  81. // Add the maptool type to the maptool data.
  82. $PHORUM['maptool']['type'] = $maptool_type;
  83. // Add the Google API language to use.
  84. $PHORUM['maptool']['api_language'] =
  85. $PHORUM["DATA"]["LANG"]["mod_google_maps"]["geocoding_lang"];
  86. // Generate the URL to use for the map that is loaded in the iframe.
  87. // All fields from $PHORUM['maptool'] are added as parameters to this URL.
  88. $parameters = array(
  89. PHORUM_ADDON_URL, "module=google_maps", "addon=mapframe"
  90. );
  91. foreach ($PHORUM['maptool'] as $key => $val) {
  92. if ($key === 'plot') continue;
  93. $parameters[] = urlencode($key) . "=" . urlencode($val);
  94. }
  95. $PHORUM['maptool']['url'] = call_user_func_array(
  96. 'phorum_get_url', $parameters);
  97. // Add language variables for easy access from within the maptool scripts.
  98. $PHORUM['maptool']['lang'] = $PHORUM["DATA"]["LANG"]["mod_google_maps"];
  99. // Grab the map code.
  100. ob_start();
  101. include dirname(__FILE__) . "/maptool/{$maptool_type}.php";
  102. $maptool = ob_get_contents();
  103. ob_end_clean();
  104. return $maptool;
  105. }
  106. /**
  107. * A utility function, which can be used to filter incoming map state data.
  108. *
  109. * @param array $data
  110. * @return array $filtered
  111. */
  112. function mod_google_maps_filter_state_data($data)
  113. {
  114. $filtered = array();
  115. foreach (array(
  116. 'map_longitude' => 'float',
  117. 'map_latitude' => 'float',
  118. 'map_zoom' => 'zoom',
  119. 'map_type' => 'type',
  120. 'marker_longitude' => 'float',
  121. 'marker_latitude' => 'float',
  122. 'streetview_longitude' => 'float',
  123. 'streetview_latitude' => 'float',
  124. 'streetview_heading' => 'float',
  125. 'streetview_pitch' => 'float',
  126. 'streetview_zoom' => 'zoom',
  127. 'geoloc_country' => 'string',
  128. 'geoloc_city' => 'string') as $field => $check)
  129. {
  130. $value = isset($_POST[$field]) && $_POST[$field] !== ''
  131. ? $_POST[$field] : NULL;
  132. if ($value !== NULL) {
  133. switch ($check) {
  134. case 'float':
  135. settype($value, 'float');
  136. break;
  137. case 'zoom':
  138. settype($value, 'float');
  139. if ($value <= 0) $value = 0;
  140. break;
  141. case 'type':
  142. if ($value !== 'roadmap' &&
  143. $value !== 'satellite' &&
  144. $value !== 'hybrid' &&
  145. $value !== 'terrain')
  146. {
  147. $value = 'roadmap';
  148. }
  149. break;
  150. case 'string':
  151. $value = trim($value);
  152. break;
  153. }
  154. }
  155. $filtered[$field] = $value;
  156. }
  157. return $filtered;
  158. }
  159. /**
  160. * Conversion of module v1 user settings data to v2 user settings data.
  161. *
  162. * @param array $data
  163. * @return array
  164. */
  165. function mod_google_maps_upgrade_userdata($d)
  166. {
  167. if (!empty($d['center'])) {
  168. if (preg_match(
  169. '/^\((-?\d+(?:\.\d+)?)\s*,\s*(-?\d+(?:\.\d+)?)\)$/',
  170. $d['center'], $m
  171. )) {
  172. $d['map_latitude'] = $m[1];
  173. $d['map_longitude'] = $m[1];
  174. unset($d['center']);
  175. }
  176. }
  177. if (!empty($d['marker'])) {
  178. if (preg_match(
  179. '/^\((-?\d+(?:\.\d+)?)\s*,\s*(-?\d+(?:\.\d+)?)\)$/',
  180. $d['marker'], $m
  181. )) {
  182. $d['marker_latitude'] = $m[1];
  183. $d['marker_longitude'] = $m[1];
  184. unset($d['marker']);
  185. }
  186. }
  187. if (!empty($d['type'])) {
  188. if ($d['type'] === 'normal') {
  189. $d['map_type'] = 'roadmap';
  190. } else {
  191. $d['map_type'] = $d['type'];
  192. }
  193. unset($d['type']);
  194. }
  195. if (!empty($d['zoom'])) {
  196. $d['map_zoom'] = $d['zoom'];
  197. unset($d['zoom']);
  198. }
  199. return $d;
  200. }
  201. ?>