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, $e);
00087          return;
00088       }
00089    }
00090 
00091    private function getQueryFromURI(DMInternalURI $uri) {
00092       $example_obj = new UNLVSpatialRectObject();
00093       $example_obj->setSpatialNorthLat((float) $uri->getQueryValue("nlat"));
00094       $example_obj->setSpatialSouthLat((float) $uri->getQueryValue("slat"));
00095       $example_obj->setSpatialWestLong((float) $uri->getQueryValue("wlong"));
00096       $example_obj->setSpatialEastLong((float) $uri->getQueryValue("elong"));
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       if (is_array($uri->getQueryValue("aliases"))) {
00117          foreach ($uri->getQueryValue("aliases") as $alias) {
00118             $collection = DMCollectionFactory::getCollection(
00119                   DMCollection::getSanitizedAlias($alias));
00120             if ($collection) {
00121                $query->addCollection($collection);
00122             }
00123          }
00124       }
00125 
00126       $page = (int) $uri->getQueryValue("page");
00127       $rpp = (int) $uri->getQueryValue("rpp");
00128       $page = ($page < 1) ? 1 : $page;
00129       $rpp = ($rpp > 500) ? 500 : $rpp;
00130       $rpp = ($rpp < 10) ? 50 : $rpp;
00131       $query->setPage($page);
00132       $query->setNumResultsPerPage($rpp);
00133       return $query;
00134    }
00135 
00136    public function view($aliasptr) {
00137       $tmp = explode(",", $aliasptr);
00138 
00139       $col = DMCollectionFactory::getCollection(
00140             DMCollection::getSanitizedAlias($tmp[0]));
00141       $obj = self::getDataStore()->getObject($col, (int) $tmp[1]);
00142       if (!$obj) {
00143          throw new DMUnavailableModelException(
00144                DMLocalizedString::getString("NONEXISTENT_OBJECT"));
00145       }
00146 
00147       $response = new DMHTTPResponse();
00148       $rep = DMHTTPResponseFactory::getRepresentation();
00149       $response->setRepresentation($rep);
00150       try {
00151          $trans = DMHTTPResponseFactory::getTransformer();
00152          $rep->setBody($trans->transformObject($obj));
00153       } catch (DMException $e) {
00154          $response->setStatusCode($e->getHTTPResponseCode());
00155          $rep->setBody($e->getMessage());
00156       }
00157       $response->send();
00158       die;
00159    }
00160 
00161 }
 All Data Structures Functions Variables