2b2cf59d4cf3b17f1c66a1559f8dfb759dcc7663
[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;   // latitude as float value
68                 if (isset($args['lon'])) $longitude = floatval($args['lon']); else $longitude = 11.404655; // longitude as float value
69                 if (isset($args['zoom'])) $zoom = intval($args['zoom']); else $zoom = 10; // Google Zoom Level
70                 $staticSizeX = 600;
71                 $staticSizeY = 450;
72                 $latitude_s = sprintf('%.6F', $latitude);
73                 $longitude_s = sprintf('%.6F', $longitude);
74
75                 // Query database
76                 $dbr = wfGetDB(DB_SLAVE);
77                 $res = $dbr->select('wrsleddingcache', array('page_title', 'position_latitude', 'position_longitude'), array('show_in_overview', 'not under_construction'));
78                 $sleddingRoutes = array();
79                 while ($sleddingRoute = $dbr->fetchRow($res)) $sleddingRoutes[] = $sleddingRoute;
80                 $dbr->freeResult($res);
81                 
82                 // Load Google Maps Script and define functions
83                 $output = '';
84                 if ($this->mapsCount == 1) {
85                         $output .= '<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=' . htmlspecialchars($this->apiKey) . '" type="text/javascript"></script>' . "\n";
86                         $output .= WRGMAPJSFUNCTIONS;
87                 }
88                         
89                 // Create static map image link
90                 $staticLink = "http://maps.google.com/staticmap?center=$latitude_s,$longitude_s&zoom=$zoom&size=${staticSizeX}x$staticSizeY&key=$this->apiKey";
91                 $staticMarkers = array();
92                 foreach ($sleddingRoutes as $s) {
93                         $lat = $s['position_latitude'];  // this is a string
94                         $lon = $s['position_longitude']; // this is a string
95                         $staticMarkers[] = sprintf('%s,%s,bluer', $lat, $lon);
96                 }
97                 if (count($staticMarkers) > 0) $staticLink .= '&markers=' . implode('|', $staticMarkers);
98
99                 // Create <div/> element where the map is placed in
100                 $mapName = 'wrgmap' . $this->mapsCount;
101                 $output .= '<div id="' . $mapName . '" style="width: 100%; height: 450px; border-style:none; display:none;"></div>';
102                 
103                 // Return output
104                 $output .= '<script type="text/javascript">' . "\n//<![CDATA[\n";
105                 $output .= 'if (GBrowserIsCompatible()) {' . "\n";
106                 $output .= "\tdocument.getElementById(\"$mapName\").style.display = \"block\";\n";
107                 $output .= $this->addMap($mapName, $latitude, $longitude, $zoom);
108                 foreach ($sleddingRoutes as $s) {
109                         $lat = $s['position_latitude'];
110                         $lon = $s['position_longitude'];
111                         $pageTitle = $s['page_title'];
112                         if (!$lat || !$lon) continue;
113                         $output .= $this->addJsMarker($lat, $lon, $pageTitle);
114                 }
115                 $output .= "} else {\n"; // browser not compatible -> create static map
116                 $output .= "\tdocument.write(\"<img alt=\\\"Landkarte mit Rodelbahnen\\\" src=\\\"" . htmlspecialchars($staticLink) . "\\\" width=\\\"$staticSizeX\\\" height=\\\"$staticSizeY\\\" />\");\n";
117                 $output .= "}\n//]]>\n</script>\n";
118                 $output .= '<noscript><img alt="Landkarte mit Rodelbahnen" src="' . htmlspecialchars($staticLink) . "\" width=\"$staticSizeX\" height=\"$staticSizeY\" /></noscript>\n";
119
120                 return wrCommonReplaceByMarker($output, 'wrmap');
121         }
122         
123         // returns a string that creates a map object called 'map'
124         // $latitude, $longitude and $zoom have to be float/integer values.
125         private function addMap($mapName, $latitude, $longitude, $zoom) {
126                 return "\tvar map = new GMap2(document.getElementById('$mapName'), {'mapTypes': [G_NORMAL_MAP, G_HYBRID_MAP, G_PHYSICAL_MAP, G_SATELLITE_MAP]});\n" .
127                 "\tmap.addControl(new GLargeMapControl());\n" .
128                 "\tmap.addControl(new GMapTypeControl());\n" .
129                 "\tmap.addControl(new GScaleControl());\n" .
130                 sprintf("\tmap.setCenter(new GLatLng(%.6F, %.6F), %d);\n", $latitude, $longitude, $zoom) .
131                 "\tmap.setMapType(G_PHYSICAL_MAP);\n";
132                 // "\tmap.enableScrollWheelZoom();\n";
133         }
134
135         // returns a string with a add marker javascript call
136         // $latitude and $longitude are expected as string
137         private function addJsMarker($latitude, $longitude, $pageTitle) {
138                 return "\tmap.addOverlay(wrCreateMarker($latitude, $longitude, \"" . htmlspecialchars(addslashes($pageTitle)) . "\", wrSleddingIcon));\n";
139         }
140         
141 }
142
143
144 ?>