dmBridge PHP API
UNLVSpatialController.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 UNLVSpatialController extends DMTEObjectController {
00017 
00018    public function apiSearch() {
00019       $query = $this->getQueryFromURI(DMHTTPRequest::getCurrent()->getURI());
00020 
00021       $response = new DMHTTPResponse();
00022       $rep = DMHTTPResponseFactory::getRepresentation();
00023       $response->setRepresentation($rep);
00024       try {
00025          $trans = DMHTTPResponseFactory::getTransformer();
00026          $rep->setBody($trans->transformSpatialResults($query));
00027       } catch (DMException $e) {
00028          $response->setStatusCode($e->getHTTPResponseCode());
00029          $rep->setBody($e->getMessage());
00030       }
00031       $response->send();
00032       die;
00033    }
00034 
00038    public function index($alias) {
00039       $alias = DMCollection::getSanitizedAlias($alias);
00040       if (strlen($alias) < 2) {
00041          $alias = "/dmdefault";
00042       }
00043 
00044       try {
00045          $ts = $this->getTemplateSet();
00046          if (!$ts) {
00047             throw new DMUnavailableModelException(
00048                   DMLocalizedString::getString("INVALID_TPL_SET"));
00049          }
00050 
00051          $specified_collection = DMCollectionFactory::getCollection($alias);
00052 
00053          // if there are any collection aliases in the search that we do not
00054          // have access to, bail out
00055          if (!$this->isAuthorizedToSearch()) {
00056             throw new DMSecurityException(
00057                DMLocalizedString::getString("ACCESS_DENIED_TO_COL"));
00058          }
00059 
00060          $query = $this->getQueryFromURI(
00061                DMHTTPRequest::getCurrent()->getURI());
00062          $query->getSearchResults();
00063 
00064          $path = "/templates/object/results.html.php";
00065          $path = $query->getNumResults()
00066                ? $path : "/templates/object/no_results.html.php";
00067 
00068          $tpl = $ts->getTemplateAtPathname($path);
00069          global $view;
00070          $view = new DMResultsView($tpl, $this->getSession(),
00071                $specified_collection, $query);
00072          $this->getSession()->setResultsView($view);
00073 
00074          $response = new DMHTTPResponse();
00075          $rep = DMHTTPResponseFactory::getRepresentation();
00076          $rep->setMediaType(new DMMediaType("text", "html"));
00077          $response->setRepresentation($rep);
00078          $response->send();
00079       } catch (DMException $e) {
00080          if (!$this->getTemplateSet()) {
00081             throw $e;
00082          }
00083          $tpl = $this->getTemplateSet()->getTemplateAtPathname(
00084                "/templates/error/view.html.php");
00085          global $view;
00086          $view = new DMSystemErrorView($tpl, $this->getSession(), $e);
00087          return;
00088       }
00089    }
00090 
00091    private function getQueryFromURI(DMInternalURI $uri) {
00092       $example_obj = new UNLVSpatialRectObject();
00093       $example_obj->setSpatialNorthLat((float) $uri->getQueryValue("lat_n"));
00094       $example_obj->setSpatialSouthLat((float) $uri->getQueryValue("lat_s"));
00095       $example_obj->setSpatialWestLong((float) $uri->getQueryValue("long_w"));
00096       $example_obj->setSpatialEastLong((float) $uri->getQueryValue("long_e"));
00097 
00098       $term = new UNLVSpatialQueryTerm();
00099       $term->setExampleObject($example_obj);
00100 
00101       switch ($uri->getQueryValue("mode")) {
00102       case "intersect":
00103          $term->setMatchingMode("intersect");
00104          break;
00105       case "outside":
00106          $term->setMatchingMode("outside");
00107          break;
00108       default:
00109          $term->setMatchingMode("within");
00110          break;
00111       }
00112 
00113       $query = new UNLVSpatialQuery();
00114       $query->addQueryTerm($term);
00115 
00116       $aliases = $uri->getQueryValue("CISOROOT");
00117       if (is_array($aliases)) {
00118          if (in_array("all", $aliases)) {
00119             $query->addCollection(DMCollectionFactory::getCollection("/dmdefault"));
00120          } else {
00121             foreach ($aliases as $alias) {
00122                $collection = DMCollectionFactory::getCollection(
00123                      DMCollection::getSanitizedAlias($alias));
00124                $query->addCollection($collection);
00125             }
00126          }
00127       }
00128 
00129       $page = (int) $uri->getQueryValue("page");
00130       $rpp = (int) $uri->getQueryValue("rpp");
00131       $page = ($page < 1) ? 1 : $page;
00132       $rpp = ($rpp > 500) ? 500 : $rpp;
00133       $rpp = ($rpp < 10) ? 50 : $rpp;
00134       $query->setPage($page);
00135       $query->setNumResultsPerPage($rpp);
00136       return $query;
00137    }
00138 
00139    public function view($aliasptr) {
00140       $tmp = explode(",", $aliasptr);
00141 
00142       $col = DMCollectionFactory::getCollection(
00143             DMCollection::getSanitizedAlias($tmp[0]));
00144       $obj = self::getDataStore()->getObject($col, (int) $tmp[1]);
00145       if (!$obj) {
00146          throw new DMUnavailableModelException(
00147                DMLocalizedString::getString("NONEXISTENT_OBJECT"));
00148       }
00149 
00150       $response = new DMHTTPResponse();
00151       $rep = DMHTTPResponseFactory::getRepresentation();
00152       $response->setRepresentation($rep);
00153       try {
00154          $trans = DMHTTPResponseFactory::getTransformer();
00155          $rep->setBody($trans->transformObject($obj));
00156       } catch (DMException $e) {
00157          $response->setStatusCode($e->getHTTPResponseCode());
00158          $rep->setBody($e->getMessage());
00159       }
00160       $response->send();
00161       die;
00162    }
00163 
00164 }
 All Data Structures Functions Variables