afd1a9e30fc7d805438f66e1fae70293e2764945
[philipp/winterrodeln/mediawiki_extensions/wrmap.git] / wrmap.body.php
1 <?php
2
3 function wrMapParserFirstCallInit() {
4         global $wgParser;
5         global $wgGoogleMapsKey;
6         $wrGoogleMaps = new WrGoogleMaps($wgGoogleMapsKey);
7         $wgParser->setHook('wrgmap', array($wrGoogleMaps, 'render'));
8         return true;
9 }
10
11
12
13 // Java script
14 // -----------
15
16 // Global JavaScript functions
17 define(WRGMAPJSFUNCTIONS, <<<JAVASCRIPT
18 <script type="text/javascript">
19 //<![CDATA[
20 var wrSleddingIcon = new GIcon(G_DEFAULT_ICON);
21 wrSleddingIcon.image = "/vorlagen/gmap_rodelbahn_c.png";
22 wrSleddingIcon.shadow = "/vorlagen/gmap_rodelbahn_c_s.png";
23 wrSleddingIcon.iconSize = new GSize(17, 17);
24 wrSleddingIcon.shadowSize = new GSize(23, 23);
25 wrSleddingIcon.iconAnchor = new GPoint(9, 9);
26 wrSleddingIcon.infoWindowAnchor = new GPoint(9, 9);
27
28
29 function wrCreateMarker(latitude, longitude, name, icon) {
30         var point = new GLatLng(latitude, longitude);
31         var marker = new GMarker(point, icon);
32         var articlePath = "$wgArticlePath";
33         var p = articlePath.replace("\$1", name.replace(' ', '_'));
34         GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml("<strong><a href='" + p + "'>" + name+ "</a></strong>");});
35         return marker;
36 }
37 //]]>
38 </script>
39 JAVASCRIPT
40 );
41
42
43 /** This class was inpired by the GoogleMaps class of the GoogleMaps extension. */
44 class WrGoogleMaps {
45         /// the Google API key (obtained from
46         /// http://www.google.com/apis/maps/signup.html)
47         private $apiKey = null;
48         
49         /// How many <wrgmap> tags are on the current page?
50         private $mapsCount = 0;
51         
52         /// Constructor
53         function WrGoogleMaps($apiKey) {
54                 $this->apiKey = $apiKey;
55         }
56         
57         /// Renders the <wrgmap> tag
58         /// @param $content string - the content of the <wrgmap> tag
59         /// @param $args array - the array of attribute name/value pairs for the tag
60         /// @param $parser Parser - the MW Parser object for the current page
61         ///
62         /// @return string - the html for rendering the map
63         function render($content, $args, &$parser) {
64                 ++$this->mapsCount;
65                 
66                 // Decode data
67                 if (isset($args['lat'])) $latitude = floatval($args['lat']); else $latitude = 47.267648;
68                 if (isset($args['lon'])) $longitude = floatval($args['lon']); else $longitude = 11.404655;
69                 if (isset($args['zoom'])) $zoom = intval($args['zoom']); else $zoom = 10; // Google Zoom Level
70                 $staticSizeX = 600;
71                 $staticSizeY = 450;
72
73                 // Query database
74                 $dbr = wfGetDB(DB_SLAVE);
75                 $res = $dbr->select('wrsleddingcache', array('page_title', 'position_latitude', 'position_longitude'), array('show_in_overview', 'not under_construction'));
76                 $sleddingRoutes = array();
77                 while ($sleddingRoute = $dbr->fetchRow($res)) $sleddingRoutes[] = $sleddingRoute;
78                 $dbr->freeResult($res);
79                 
80                 // Load Google Maps Script and define functions
81                 $output = '';
82                 if ($this->mapsCount == 1) {
83                         $output .= '<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=' . htmlspecialchars($this->apiKey) . '" type="text/javascript"></script>' . "\n";
84                         $output .= WRGMAPJSFUNCTIONS;
85                 }
86                         
87                 // Create static map image link
88                 $staticLink = "http://maps.google.com/staticmap?center=$latitude,$longitude&zoom=$zoom&size=${staticSizeX}x$staticSizeY&key=$this->apiKey";
89                 $staticMarkers = array();
90                 foreach ($sleddingRoutes as $s) {
91                         $lat = $s['position_latitude'];
92                         $lon = $s['position_longitude'];
93                         $staticMarkers[] = sprintf('%.3f,%.3f,bluer', $lat, $lon);
94                 }
95                 if (count($staticMarkers) > 0) $staticLink .= '&markers=' . implode('|', $staticMarkers);
96
97                 // Create <div/> element where the map is placed in
98                 $mapName = 'wrgmap' . $this->mapsCount;
99                 $output .= '<div id="' . $mapName . '" style="width: 100%; height: 450px; border-style:none; display:none;"></div>';
100                 
101                 // Return output
102                 $output .= '<script type="text/javascript">' . "\n//<![CDATA[\n";
103                 $output .= 'if (GBrowserIsCompatible()) {' . "\n";
104                 $output .= "\tdocument.getElementById(\"$mapName\").style.display = \"block\";\n";
105                 $output .= $this->addMap($mapName, $latitude, $longitude, $zoom);
106                 foreach ($sleddingRoutes as $s) {
107                         $lat = $s['position_latitude'];
108                         $lon = $s['position_longitude'];
109                         $pageTitle = $s['page_title'];
110                         if (!$lat || !$lon) continue;
111                         $output .= $this->addJsMarker($lat, $lon, $pageTitle);
112                 }
113                 $output .= "} else {\n"; // browser not compatible -> create static map
114                 $output .= "\tdocument.write(\"<img alt=\\\"Landkarte mit Rodelbahnen\\\" src=\\\"" . htmlspecialchars($staticLink) . "\\\" width=\\\"$staticSizeX\\\" height=\\\"$staticSizeY\\\" />\");\n";
115                 $output .= "}\n//]]>\n</script>\n";
116                 $output .= '<noscript><img alt="Landkarte mit Rodelbahnen" src="' . htmlspecialchars($staticLink) . "\" width=\"$staticSizeX\" height=\"$staticSizeY\" /></noscript>\n";
117
118                 return wrCommonReplaceByMarker($output, 'wrmap');
119         }
120         
121         // returns a string that creates a map object called 'map'
122         private function addMap($mapName, $latitude, $longitude, $zoom) {
123                 return "\tvar map = new GMap2(document.getElementById('$mapName'), {'mapTypes': [G_NORMAL_MAP, G_HYBRID_MAP, G_PHYSICAL_MAP, G_SATELLITE_MAP]});\n" .
124                 "\tmap.addControl(new GLargeMapControl());\n" .
125                 "\tmap.addControl(new GMapTypeControl());\n" .
126                 "\tmap.addControl(new GScaleControl());\n" .
127                 "\tmap.setCenter(new GLatLng($latitude, $longitude), $zoom);\n" .
128                 "\tmap.setMapType(G_PHYSICAL_MAP);\n" .
129                 "\tmap.enableScrollWheelZoom();\n";
130         }
131
132         // returns a string with a add marker javascript call
133         private function addJsMarker($latitude, $longitude, $pageTitle) {
134                 return "\tmap.addOverlay(wrCreateMarker($latitude, $longitude, \"" . htmlspecialchars(addslashes($pageTitle)) . "\", wrSleddingIcon));\n";
135         }
136         
137 }
138
139
140 ?>