/plugins/molajo/webservices/googlemaps/driver.php

https://github.com/ot2sen/Tamka · PHP · 144 lines · 82 code · 23 blank · 39 comment · 45 complexity · 154f2b624ed355e72da1ef290a6e3b01 MD5 · raw file

  1. <?php
  2. /**
  3. * @package Molajo
  4. * @subpackage Web Services
  5. * @copyright Copyright (C) 2011 Amy Stephen. All rights reserved.
  6. * @license GNU General Public License Version 2, or later http://www.gnu.org/licenses/gpl.html
  7. */
  8. defined('MOLAJO') or die();
  9. class MolajoWebServicesGoogleMaps {
  10. /**
  11. * MolajoWebServicesGoogleMaps::driver
  12. *
  13. * Interfaces with Google Static Maps API
  14. *
  15. * @param string The context for the content passed to the plugin.
  16. * @param object The content object.
  17. * @param object The content params
  18. * @param string The 'page' number
  19. * @param string Then name of the text field in the content object
  20. * @return string
  21. * @since 1.6
  22. *
  23. * Static Map API
  24. * http://code.google.com/apis/maps/documentation/staticmaps/
  25. *
  26. */
  27. function driver ($context, &$content, &$params, $page, $location)
  28. {
  29. /** extract maps **/
  30. preg_match_all( "#{googlemaps}(.*?){/googlemaps}#s", $content->$location, $matches );
  31. if (count($matches) == 0) { return; }
  32. /** process maps **/
  33. for ( $i=0; $i < count($matches[0]); $i++ ) {
  34. $key = $content->id.'-'.$i;
  35. $options = explode(";", $matches[1][$i]);
  36. /** extract parameter pairs **/
  37. for ($p=0; $p < count($options); $p++) {
  38. if ($p == 0 ) {
  39. $title == '';
  40. $latitude = 0;
  41. $longitude = 0;
  42. $zoom = 0;
  43. $width = 0;
  44. $height = 0;
  45. $maptype = '';
  46. }
  47. $x = explode("=", $options[$p]);
  48. if ($x[0] == 'title') {
  49. $title = filter_var($x[1], FILTER_SANITIZE_STRING);
  50. } else if ($x[0] == 'center') {
  51. $y = explode(',', $x[1]);
  52. if (is_numeric($y[0])) { $latitude = $y[0]; } else { $latitude = 0; }
  53. if (is_numeric($y[1])) { $longitude = $y[1]; } else { $longitude = 0; }
  54. } else if ($x[0] == 'zoom') {
  55. if (((int) $x[1] > 0) && ((int) $x[1] < 22)) { $zoom = $x[1]; } else { $zoom = 0; }
  56. } else if ($x[0] == 'size') {
  57. $y = explode('x', $x[1]);
  58. if (is_numeric($y[0])) { $width = $y[0]; } else { $width = 0; }
  59. if (is_numeric($y[1])) { $height = $y[1]; } else { $height = 0; }
  60. } else if ($x[0] == 'maptype') {
  61. if (($x[1] == 'roadmap') || ($x[1] == 'satellite') || ($x[1] == 'terrain') || ($x[1] == 'hybrid ')) {
  62. $maptype = $x[1];
  63. } else {
  64. $maptype = 'roadmap';
  65. }
  66. }
  67. }
  68. /** generate map request **/
  69. $mapScript = '
  70. function initialize() {
  71. var myLatlng = new google.maps.LatLng('.$latitude.','.$longitude.');
  72. var myOptions = {
  73. zoom: '.$zoom.',
  74. center: myLatlng,
  75. mapTypeId: google.maps.MapTypeId.'.$maptype.',
  76. };
  77. var map = new google.maps.Map(document.getElementById("map'.$key.'"),
  78. myOptions);
  79. }
  80. function loadScript() {
  81. var script = document.createElement("script");
  82. script.type = "text/javascript";
  83. script.src = "http://maps.google.com/maps/api/js?sensor=false&callback=initialize";
  84. document.body.appendChild(script);
  85. }
  86. window.onload = loadScript;
  87. ';
  88. /** load map request **/
  89. if ($i == 0) {
  90. // $document =& JFactory::getDocument();
  91. // $document->addScript('http://maps.google.com/maps/api/js?sensor=false');
  92. $molajoSystemPlugin =& JPluginHelper::getPlugin('system', 'molajo');
  93. $systemParams = new JParameter($molajoSystemPlugin->params);
  94. }
  95. // $document->addScriptDeclaration($mapScript);
  96. /** static map request **/
  97. // if ($systemParams->def('enable_static_google_map', 0) == 1) {
  98. if ($title == '') {
  99. $title = $content->$title;
  100. }
  101. $staticMap = 'http://maps.google.com/maps/api/staticmap?alt='.$title;
  102. $staticMap .= '&center='.$latitude.','.$longitude;
  103. $staticMap .= '&zoom='.$zoom;
  104. $staticMap .= '&size='.$width.'x'.$height;
  105. $staticMap .= '&maptype='.$maptype;
  106. // $layoutOutput = '<div id="map'.$key.'"><noscript><img src="'.$staticMap.'&sensor=false" width="'.$width.'" height="'.$height.'" /></noscript></div>';
  107. $layoutOutput = '<div id="map'.$key.'"><img src="'.$staticMap.'&sensor=false" width="'.$width.'" height="'.$height.'" /></div>';
  108. /** no static map request **/
  109. // } else {
  110. // $layoutOutput = '<div id="map'.$key.'" width="'.$width.'" height="'.$height.'" title="'.$title.'" /></div>';
  111. // }
  112. /** insert into output and replace request **/
  113. $content->$location = str_replace( $matches[0][$i], $layoutOutput, $content->$location);
  114. }
  115. }
  116. }