dmBridge PHP API
UNLVSpatialTemplateHelper.php
00001 <?php
00002 #
00003 # UNLVSpatial: a dmBridge module for spatial content
00004 #
00005 # Copyright © 2011 Board of Regents of the Nevada System of Higher
00006 # Education, on behalf of the University of Nevada, Las Vegas
00007 #
00008 
00016 class UNLVSpatialTemplateHelper extends DMCustomTemplateHelper
00017 implements DMTemplateHelper {
00018 
00024    public function helperWillRender() {
00025       $this->addBodyScriptTag("http://maps.google.com/maps/api/js?sensor=false");
00026       $this->addBodyScriptTag(
00027             dirname($_SERVER['PHP_SELF']) . "/includes/js/jquery-1.6.min.js");
00028       $this->addBodyScriptTag(
00029             dirname($_SERVER['PHP_SELF']) . "/includes/js/jquery.scrollTo-1.4.2-min.js");
00030    }
00031 
00046    public function getHtmlGoogleMapsObject($map_type = "TERRAIN",
00047          $point_zoom_level = 11,
00048          $rect_stroke_color = "#0000ff",
00049          $rect_stroke_weight = 1,
00050          $rect_fill_color = "#0000ff",
00051          $rect_fill_opacity = 0.3) {
00052       if ($this->getSpatialObject()->getSpatialModelClassName()
00053             == "UNLVSpatialRectObject") {
00054          return $this->getHtmlGoogleMapsObjectRect($map_type,
00055             $rect_stroke_color, $rect_stroke_weight,
00056             $rect_fill_color, $rect_fill_opacity);
00057       } else {
00058          return $this->getHtmlGoogleMapsObjectPinpoint($map_type,
00059             $point_zoom_level);
00060       }
00061    }
00062 
00063    private function getHtmlGoogleMapsObjectPinpoint($map_type = "TERRAIN",
00064          $point_zoom_level = 11) {
00065       $obj = $this->getSpatialObject();
00066       try {
00067          $title = $obj->getMetadata("title");
00068          $title = str_replace('"', '\"', $title);
00069 
00070          return '<script type="text/javascript">
00071             function initialize() {
00072                var latLong = new google.maps.LatLng('
00073                   . $obj->getSpatialCenterLat() . ',' . $obj->getSpatialCenterLong() . ');
00074                var mapOptions = {
00075                   scrollwheel: false,
00076                   zoom: ' . $point_zoom_level . ',
00077                   center: latLong,
00078                   mapTypeId: google.maps.MapTypeId.' . $map_type . '
00079                }
00080                var map = new google.maps.Map(
00081                   document.getElementById(
00082                      "UNLVSpatialMapCanvasObjectPinpoint"),
00083                   mapOptions);
00084                var marker = new google.maps.Marker({
00085                   position:latLong,
00086                   map:map,
00087                   title:"'. $title . '"
00088                });
00089             }
00090             window.onload = function() {
00091                initialize();
00092             }
00093             </script>
00094             <div id="UNLVSpatialMapCanvasObjectPinpoint"
00095                class="UNLVSpatialMapCanvas"></div>';
00096       } catch (DMNoSuchMethodException $e) {
00097          return "<!-- Map not inserted, as the current object does not have
00098             spatial information associated with it. -->";
00099       }
00100    }
00101 
00102    private function getHtmlGoogleMapsObjectRect($map_type = "TERRAIN",
00103          $stroke_color = "#0000ff", $stroke_weight = 1,
00104          $fill_color = "#0000ff", $fill_opacity = 0.3) {
00105       $obj = $this->getSpatialObject();
00106       try {
00107          return '<script type="text/javascript">
00108             function initialize() {
00109                var latLongNE = new google.maps.LatLng('
00110                   . $obj->getSpatialNorthLat() . ',' . $obj->getSpatialEastLong() . ');
00111                var latLongSW = new google.maps.LatLng('
00112                   . $obj->getSpatialSouthLat() . ',' . $obj->getSpatialWestLong() . ');
00113                var latLongBounds = new google.maps.LatLngBounds(
00114                   latLongSW, latLongNE);
00115                var mapOptions = {
00116                   scrollwheel: false,
00117                   zoom:0,
00118                   center: new google.maps.LatLng('
00119                      . $obj->getSpatialCenterLat() . ',' . $obj->getSpatialCenterLong() . '),
00120                   mapTypeId: google.maps.MapTypeId.' . $map_type . '
00121                }
00122                var map = new google.maps.Map(
00123                   document.getElementById(
00124                      "UNLVSpatialMapCanvasObjectPinpoint"),
00125                   mapOptions);
00126                var rect = new google.maps.Rectangle({
00127                   bounds:latLongBounds,
00128                   map:map,
00129                   fillColor:"' . $fill_color . '",
00130                   fillOpacity:' . $fill_opacity . ',
00131                   strokeColor:"' . $stroke_color . '",
00132                   strokeWeight:' . $stroke_weight . '
00133                });
00134                map.fitBounds(latLongBounds);
00135             }
00136             window.onload = function() {
00137                initialize();
00138             }
00139             </script>
00140             <div id="UNLVSpatialMapCanvasObjectPinpoint"
00141                class="UNLVSpatialMapCanvas"></div>';
00142       } catch (DMNoSuchMethodException $e) {
00143          return "<!-- Map not inserted, as the current object does not have
00144             spatial information associated with it. -->";
00145       }
00146    }
00147 
00162    public function getHtmlGoogleMapsObjectResults($map_type = "TERRAIN",
00163          $display_rects_as_points = true,
00164          $rect_stroke_color = "#0000ff",
00165          $rect_stroke_weight = 1,
00166          $rect_fill_color = "#0000ff",
00167          $rect_fill_opacity = 0.05) {
00168       $objects = $this->getSpatialObjects();
00169       $obj_count = count($objects);
00170       if (!$obj_count) {
00171          return "<!-- Map not inserted, as no current objects have
00172             spatial information associated with them. -->";
00173       }
00174 
00175       $rects_and_markers = "";
00176       $lats = $longs = array();
00177       $nlats = $slats = $wlongs = $elongs = array();
00178       for ($i = 0; $i < $obj_count; $i++) {
00179          if ($objects[$i]->getSpatialModelClassName() == "UNLVSpatialRectObject"
00180                && !$display_rects_as_points) { // append a rect
00181 
00182             $nlats[$i] = $objects[$i]->getSpatialNorthLat();
00183             $elongs[$i] = $objects[$i]->getSpatialEastLong();
00184             $slats[$i] = $objects[$i]->getSpatialSouthLat();
00185             $wlongs[$i] = $objects[$i]->getSpatialWestLong();
00186             $rects_and_markers .= 'var latLongNE = new google.maps.LatLng('
00187                      . $nlats[$i] . ',' . $elongs[$i] . ');
00188                   var latLongSW = new google.maps.LatLng('
00189                      . $slats[$i] . ',' . $wlongs[$i] . ');
00190                   var latLongBounds = new google.maps.LatLngBounds(
00191                      latLongSW, latLongNE);
00192                   var rect = new google.maps.Rectangle({
00193                   bounds:latLongBounds,
00194                   map:map,
00195                   fillColor:"' . $rect_fill_color . '",
00196                   fillOpacity:' . $rect_fill_opacity . ',
00197                   strokeColor:"' . $rect_stroke_color . '",
00198                   strokeWeight:' . $rect_stroke_weight . '
00199                });
00200                map.fitBounds(latLongBounds);
00201                google.maps.event.addListener(rect, "click", function() {
00202                   var selector = "table.dmResults tr:eq(' . $i . ')";
00203                   $.scrollTo(selector, 500, "swing");
00204                });';
00205          } else { // append a marker
00206             $lats[$i] = $objects[$i]->getSpatialCenterLat();
00207             $longs[$i] = $objects[$i]->getSpatialCenterLong();
00208 
00209             $title = str_replace('"', '\"', $objects[$i]->getMetadata("title"));
00210             $rects_and_markers .= 'var markerLL = new google.maps.LatLng('
00211                      . $lats[$i] . ',' . $longs[$i] . ');
00212                   var marker = new google.maps.Marker({
00213                   position:markerLL,
00214                   map:map,
00215                   title:"' . $title . '"
00216                });
00217                google.maps.event.addListener(marker, "click", function() {
00218                   var selector = "table.dmResults tr:eq(' . $i . ')";
00219                   $.scrollTo(selector, 500, "swing");
00220                });
00221                bounds.extend(markerLL);';
00222          }
00223       }
00224 
00225       // calculate map center (average of highest & lowest lat/long)
00226       $nlats = array_merge($nlats, $lats);
00227       $slats = array_merge($slats, $lats);
00228       $wlongs = array_merge($wlongs, $longs);
00229       $elongs = array_merge($elongs, $longs);
00230       $map_center_lat = (max($nlats) + min($slats)) / 2;
00231       $map_center_long = ((max($elongs) + min($wlongs)) / 2);
00232 
00233       return '<script type="text/javascript">
00234          function initialize() {
00235             var center = new google.maps.LatLng('
00236                . $map_center_lat . ',' . $map_center_long . ');
00237             var bounds = new google.maps.LatLngBounds();
00238             var mapOptions = {
00239                scrollwheel: false,
00240                zoom: 8,
00241                center: center,
00242                mapTypeId: google.maps.MapTypeId.' . $map_type . '
00243             }
00244             var map = new google.maps.Map(
00245                document.getElementById(
00246                   "UNLVSpatialMapCanvasObjectResultPinpoints"),
00247                mapOptions);' . $rects_and_markers . '
00248             map.fitBounds(bounds);
00249          }
00250          window.onload = function() {
00251             initialize();
00252          }
00253          </script>
00254          <div id="UNLVSpatialMapCanvasObjectResultPinpoints"
00255             class="UNLVSpatialMapCanvas"></div>';
00256    }
00257 
00265    public function getHtmlGoogleMapsSpatialSearch($center_lat, $center_long,
00266          $map_type = "TERRAIN", $zoom_level = 8) {
00267       $form_action = DMInternalURI::getURIWithParams("objects/spatial_results");
00268       $tmp = explode("/", DMHTTPRequest::getCurrent()->getURI()->getParams());
00269       if (count($tmp) > 1 && $tmp[0] == "objects") {
00270          try {
00271             $col = DMCollectionFactory::getCollection("/" . trim($tmp[1], "/"));
00272             $form_action = DMInternalURI::getURIWithParams(
00273                   "objects" . $col->getAlias() . "/spatial_results");
00274          } catch (DMUnavailableModelException $e) {
00275             // invalid collection, skip it
00276          }
00277       }
00278       
00279       return '<script type="text/javascript">
00280          function initialize() {
00281             var center = new google.maps.LatLng('
00282                . $center_lat . ',' . $center_long . ');
00283             var mapOptions = {
00284                scrollwheel: false,
00285                zoom: ' . $zoom_level . ',
00286                center: center,
00287                mapTypeId: google.maps.MapTypeId.' . $map_type . '
00288             }
00289             var map = new google.maps.Map(
00290                document.getElementById(
00291                   "UNLVSpatialMapCanvasSpatialSearch"),
00292                mapOptions);
00293             document.getElementById("UNLVSpatialSubmit").onclick = function() {
00294                ne = map.getBounds().getNorthEast();
00295                sw = map.getBounds().getSouthWest();
00296                document.getElementById("lat_n").value = ne.lat();
00297                document.getElementById("lat_s").value = sw.lat();
00298                document.getElementById("long_w").value = sw.lng();
00299                document.getElementById("long_e").value = ne.lng();
00300             }
00301          }
00302          window.onload = function() {
00303             initialize();
00304          }
00305          </script>
00306          <form name="UNLVSpatialSearch"
00307                action="' . $form_action . '">
00308             <div id="UNLVSpatialMapCanvasSpatialSearch"
00309                class="UNLVSpatialMapCanvas"></div>
00310             <fieldset id="UNLVSpatialSearch">
00311                <h5>Search for objects:</h5>
00312                <ul id="UNLVSpatialSearchType">
00313                   <li><label><input type="radio" name="mode" value="within"
00314                      checked="checked">Entirely within viewport</label></li>
00315                   <li><label><input type="radio" name="mode" value="intersect"
00316                      >At least partially within viewport</label></li>
00317                   <li><label><input type="radio" name="mode" value="outside"
00318                      >Outside viewport</label></li>
00319                </ul>
00320                <h5>In collections:</h5>
00321                <div class="dmSearchCollectionList">
00322                   <ul>' . $this->getCollectionListElements() . '</ul>
00323                   <p>
00324                      <input id="lat_n" type="hidden" name="lat_n">
00325                      <input id="lat_s" type="hidden" name="lat_s">
00326                      <input id="long_w" type="hidden" name="long_w">
00327                      <input id="long_e" type="hidden" name="long_e">
00328                      <div class="dmCheckAll">
00329                         ' . $this->getCheckAllLinks("UNLVSpatialSearch") . '
00330                      </div>
00331                   </p>
00332                </div>
00333                <input id="UNLVSpatialSubmit" class="dmSubmit" type="submit" value="Search">
00334             </fieldset>
00335          </form>';
00336    }
00337 
00344    public function getHtmlIndexerSummary() {
00345       $indexer = $this->getView()->getVar("indexer");
00346       if (!$indexer) {
00347          return "";
00348       }
00349       $dxml = new DMDOMDocument("1.0", "utf-8");
00350       $dxml->loadXML("<div/>");
00351 
00352       // indexed objects
00353       $fieldset = $dxml->createElement("fieldset");
00354       $legend = $dxml->createElement("legend", "Indexed objects");
00355       $fieldset->appendChild($legend);
00356       $dxml->documentElement->appendChild($fieldset);
00357       $table = $dxml->createElement("table");
00358       $fieldset->appendChild($table);
00359       $table->setAttribute("cellpadding", 0);
00360       $table->setAttribute("cellspacing", 2);
00361       $table->setAttribute("border", 0);
00362       $table->setAttribute("class", "columnar");
00363       $tr = $dxml->createElement("tr");
00364       $table->appendChild($tr);
00365       $th = $dxml->createElement("th", "Pointer");
00366       $tr->appendChild($th);
00367       $th = $dxml->createElement("th", "Title");
00368       $tr->appendChild($th);
00369       $th = $dxml->createElement("th", "N Lat");
00370       $tr->appendChild($th);
00371       $th = $dxml->createElement("th", "S Lat");
00372       $tr->appendChild($th);
00373       $th = $dxml->createElement("th", "W Long");
00374       $tr->appendChild($th);
00375       $th = $dxml->createElement("th", "E Long");
00376       $tr->appendChild($th);
00377 
00378       $i = 0;
00379       foreach ($indexer->getIndexedObjects() as $obj) {
00380          $tr = $dxml->createElement("tr");
00381          if ($i & 1) {
00382             $tr->setAttribute("class", "odd");
00383          }
00384          $table->appendChild($tr);
00385          $td = $dxml->createElement("td", $obj->getPtr());
00386          $tr->appendChild($td);
00387 
00388          $a = $dxml->createElement("a",
00389                DMString::xmlentities($obj->getMetadata("title")));
00390          $a->setAttribute("href", $obj->getURI());
00391          $td = $dxml->createElement("td");
00392          $td->appendChild($a);
00393          $tr->appendChild($td);
00394 
00395          $td = $dxml->createElement("td", $obj->getSpatialNorthLat());
00396          $tr->appendChild($td);
00397          $td = $dxml->createElement("td", $obj->getSpatialSouthLat());
00398          $tr->appendChild($td);
00399          $td = $dxml->createElement("td", $obj->getSpatialWestLong());
00400          $tr->appendChild($td);
00401          $td = $dxml->createElement("td", $obj->getSpatialEastLong());
00402          $tr->appendChild($td);
00403          $i++;
00404       }
00405 
00406       // skipped objects
00407       $fieldset = $dxml->createElement("fieldset");
00408       $legend = $dxml->createElement("legend", "Skipped objects");
00409       $fieldset->appendChild($legend);
00410       $dxml->documentElement->appendChild($fieldset);
00411       $table = $dxml->createElement("table");
00412       $fieldset->appendChild($table);
00413       $table->setAttribute("cellpadding", 0);
00414       $table->setAttribute("cellspacing", 2);
00415       $table->setAttribute("border", 0);
00416       $table->setAttribute("class", "columnar");
00417       $tr = $dxml->createElement("tr");
00418       $table->appendChild($tr);
00419       $th = $dxml->createElement("th", "Pointer");
00420       $tr->appendChild($th);
00421       $th = $dxml->createElement("th", "Title");
00422       $tr->appendChild($th);
00423 
00424       $i = 0;
00425       foreach ($indexer->getSkippedObjects() as $obj) {
00426          $tr = $dxml->createElement("tr");
00427          if ($i & 1) {
00428             $tr->setAttribute("class", "odd");
00429          }
00430          $table->appendChild($tr);
00431          $td = $dxml->createElement("td", $obj->getPtr());
00432          $tr->appendChild($td);
00433          $a = $dxml->createElement("a",
00434                DMString::xmlentities($obj->getMetadata("title")));
00435          $a->setAttribute("href", $obj->getURI());
00436          $td = $dxml->createElement("td");
00437          $td->appendChild($a);
00438          $tr->appendChild($td);
00439          $i++;
00440       }
00441 
00442       return $dxml->saveHTML($dxml->documentElement);
00443    }
00444 
00445    private function getCheckAllLinks($form_name) {
00446       return '<div class="dmCheckAll">
00447             <a onclick="checkAll(' . $form_name . ')">Check All</a> |
00448             <a onclick="uncheckAll(' . $form_name . ')">Uncheck All</a>
00449          </div>
00450          <script type="text/javascript">
00451          function checkAll(formName) {
00452             for (i = 0; i < formName.length; i++) {
00453                if (formName[i].type == "checkbox") {
00454                   formName[i].checked = true;
00455                }
00456             }
00457          }
00458          function uncheckAll(formName) {
00459             for (i = 0; i < formName.length; i++) {
00460                if (formName[i].type == "checkbox") {
00461                   formName[i].checked = false;
00462                }
00463             }
00464          }
00465          </script>';
00466    }
00467 
00468    private function getCollectionListElements() {
00469       $tpl_set = $this->getView()->getTemplate()->getTemplateSet();
00470       if (count($tpl_set->getAuthorizedCollections()) == 1) {
00471          return null;
00472       }
00473       $collection_list = "";
00474       foreach (DMCollection::getAuthorized() as $c) {
00475          if (count($tpl_set->getAuthorizedCollections()) > 1
00476                && !in_array($c, $tpl_set->getAuthorizedCollections())) {
00477             continue;
00478          }
00479 
00480          $input = DMHTTPRequest::getCurrent()->getSession()->getSearchInput();
00481          $checked = false;
00482          if ($input instanceof DMInput) {
00483             if (in_array($c->getAlias(), $input->getCollections())) {
00484                $checked = true;
00485             }
00486          } else {
00487             if ($this->getView()->getCollection()
00488                   && $this->getView()->getCollection()->getAlias()
00489                   != "/dmdefault") {
00490                $checked = ($c->equals($this->getView()->getCollection()));
00491             } else {
00492                $checked = true;
00493             }
00494          }
00495          $checked = $checked ? 'checked="checked"' : null;
00496          $collection_list .= sprintf(
00497             '<li>
00498                <label>
00499                   <input type="checkbox" name="CISOROOT" value="%s" %s>
00500                   %s
00501                </label>
00502             </li>',
00503             DMString::websafe($c->getAlias()),
00504             $checked,
00505             DMString::websafe($c->getName())
00506          );
00507          $checked = '';
00508       }
00509       return $collection_list;
00510    }
00511 
00516    public function getSpatialObject() {
00517       $obj = $this->getView()->getObject();
00518       UNLVSpatialObjectFactory::spatializeObject($obj);
00519       try {
00520          $obj->getSpatialModelClassName();
00521          // that worked, so it must be a spatialized object
00522          return $obj;
00523       } catch (DMNoSuchMethodException $e) {
00524          // not spatialized, so skip it
00525       }
00526       return null;
00527    }
00528 
00533    public function getSpatialObjects() {
00534       $objects = $this->getView()->getQuery()->getSearchResults();
00535       $sp_objects = array();
00536       foreach ($objects as $obj) {
00537          UNLVSpatialObjectFactory::spatializeObject($obj);
00538 
00539          try {
00540             $obj->getSpatialModelClassName();
00541             // that worked, so it must be a spatialized object
00542             $sp_objects[] = $obj;
00543          } catch (DMNoSuchMethodException $e) {
00544             // not spatialized, so skip it
00545          }
00546       }
00547       return $sp_objects;
00548    }
00549 
00550 }
 All Data Structures Functions Variables