r42814 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r42813‎ | r42814 | r42815 >
Date:15:23, 30 October 2008
Author:yaron
Status:old
Tags:
Comment:
Added handling for 'icon' and 'legend label' parameters (the latter to create a
legend for a compound query); fixed handling of multiple fields per point
Modified paths:
  • /trunk/extensions/SemanticGoogleMaps/SGM_QueryPrinter.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticGoogleMaps/SGM_QueryPrinter.php
@@ -4,6 +4,8 @@
55 * Print query results in a Google Map. Based on Google Maps API code
66 * written by Robert Buzink and query results printing code written by
77 * Markus Kr�tzsch.
 8+ *
 9+ * @author Yaron Koren
810 */
911
1012 class SGMResultPrinter extends SMWResultPrinter {
@@ -20,18 +22,18 @@
2123 $result = "";
2224
2325 $locations = array();
 26+ $legend_labels = array();
2427 // print all result rows
2528 while ( $row = $res->getNext() ) {
26 - $lat = $lon = $title = $text = "";
27 - foreach ($row as $field) {
 29+ $lat = $lon = $title = $text = $icon = "";
 30+ foreach ($row as $i => $field) {
2831 $pr = $field->getPrintRequest();
29 - $first = true;
3032 while ( ($object = $field->getNextObject()) !== false ) {
3133 if ($object->getTypeID() == '_geo') { // use shorter "LongText" for wikipage
3234 // don't add geographical coordinates to the display
3335 } elseif ($object->getTypeID() == '_wpg') { // use shorter "LongText" for wikipage
3436 $text .= $pr->getHTMLText($skin) . " " . $object->getLongText($outputmode, $skin) . "<br />";
35 - if ($first) {
 37+ if ($i == 0) {
3638 $title = $object->getShortWikiText(false);
3739 }
3840 } else {
@@ -41,15 +43,38 @@
4244 list($lat,$lon) = explode(',', $object->getXSDValue());
4345
4446 }
45 - $first = false;
4647 }
47 - if ($lat != '' && $lon != '') {
48 - $locations[] = array($lat, $lon, $title, $text);
 48+ }
 49+ if ($lat != '' && $lon != '') {
 50+ // look for display_options field, which can
 51+ // be set by Semantic Compound Queries
 52+ if (is_array($row[0]->display_options)) {
 53+ if (array_key_exists('icon', $row[0]->display_options)) {
 54+ $icon = $row[0]->display_options['icon'];
 55+ // this is somewhat of a hack - if a
 56+ // legend label has been set, we're
 57+ // getting it for every point,
 58+ // instead of just once per icon
 59+ if (array_key_exists('legend label', $row[0]->display_options)) {
 60+ $legend_label = $row[0]->display_options['legend label'];
 61+ if (! array_key_exists($icon, $legend_labels)) {
 62+ $legend_labels[$icon] = $legend_label;
 63+ }
 64+ }
 65+ }
 66+ // icon can be set even for regular, non-compound
 67+ // queries -if it is, though, we have to translate
 68+ // the name into a URL here
 69+ } elseif (array_key_exists('icon', $this->m_params)) {
 70+ $icon_title = Title::newFromText($this->m_params['icon']);
 71+ $icon_image_page = new ImagePage($icon_title);
 72+ $icon = $icon_image_page->getDisplayedFile()->getURL();
4973 }
 74+
 75+ $locations[] = array($lat, $lon, $title, $text, $icon);
5076 }
5177 }
5278
53 -
5479 $coordinates = '1,1';
5580 $class = 'pmap';
5681 if (array_key_exists('width', $this->m_params)) {
@@ -90,8 +115,14 @@
91116 $map_text = <<<END
92117 <script src="http://maps.google.com/maps?file=api&v=2&key=$wgGoogleMapsKey" type="$wgJsMimeType"></script>
93118 <script type="text/javascript">
94 -function createMarker(point, title, label) {
95 - var marker = new GMarker(point, {title:title});
 119+function createMarker(point, title, label, icon) {
 120+ if (icon!='') {
 121+ var iconObj = new GIcon(G_DEFAULT_ICON);
 122+ iconObj.image = icon;
 123+ var marker = new GMarker(point, {title:title, icon:iconObj});
 124+ } else {
 125+ var marker = new GMarker(point, {title:title});
 126+ }
96127 GEvent.addListener(marker, 'click',
97128 function() {
98129 marker.openInfoWindowHtml(label, {maxWidth:350});
@@ -168,11 +199,11 @@
169200 }
170201 // add a marker to the map for each location
171202 foreach ($locations as $i => $location) {
172 - list($lat, $lon, $title, $label) = $location;
 203+ list($lat, $lon, $title, $label, $icon) = $location;
173204 $title = str_replace("'", "\'", $title);
174205 $label = str_replace("'", "\'", $label);
175206 $map_text .=<<<END
176 - map.addOverlay(createMarker(new GLatLng($lat, $lon), '$title', '$label'));
 207+ map.addOverlay(createMarker(new GLatLng($lat, $lon), '$title', '$label', '$icon'));
177208 END;
178209 }
179210
@@ -185,6 +216,20 @@
186217 // to avoid wiki parsing adding random '<p>' tags, we have
187218 // to replace all newlines with spaces
188219 $map_text = preg_replace('/\s+/m', ' ', $map_text);
 220+
 221+ // add the legend, if any labels have been defined
 222+ if (count($legend_labels) > 0) {
 223+ $map_text .= "\n<div style=\"margin: 10px; padding: 5px;\">\n";
 224+ foreach ($legend_labels as $icon => $legend_label) {
 225+/*
 226+ $icon_title = Title::newFromText($icon);
 227+ $icon_image = new ImagePage($icon_title);
 228+ $icon_url = $icon_image->getDisplayedFile()->getURL();
 229+*/
 230+ $map_text .= "<img src=\"$icon\" />&nbsp;$legend_label&nbsp;&nbsp;&nbsp; ";
 231+ }
 232+ $map_text .= "</div>\n";
 233+ }
189234 $result .= $map_text;
190235
191236 // print further results footer

Status & tagging log