dmBridge PHP API
DMAPITagController.php
00001 <?php
00002 #
00003 # dmBridge: a data access framework for CONTENTdm(R)
00004 #
00005 # Copyright © 2009, 2010, 2011 Board of Regents of the Nevada System of Higher
00006 # Education, on behalf of the University of Nevada, Las Vegas
00007 #
00008 
00013 class DMAPITagController extends DMAbstractController {
00014 
00018    public function index() {
00019       $uri = DMHTTPRequest::getCurrent()->getURI();
00020       $random = ($uri->getQueryValue("sort") == "random");
00021       $page = abs((int) substr($uri->getQueryValue("page"), 0, 7));
00022       $rpp = abs((int) substr($uri->getQueryValue("rpp"), 0, 4));
00023 
00024       $page = ($page < 1) ? 1 : $page;
00025       $rpp = ($rpp > 100) ? 100 : $rpp;
00026       $rpp = ($rpp == 1) ? 50 : ($rpp < 10) ? 10 : $rpp;
00027       $term = substr($uri->getQueryValue("term"), 0, 100);
00028       $collections = explode(",", $uri->getQueryValue("collections"));
00029       $objects = explode(",", $uri->getQueryValue("objects"));
00030 
00031       $ds = DMDataStoreFactory::getDataStore();
00032       $tq = new DMTagQuery($ds);
00033       $tq->setPage($page);
00034       $tq->setNumResultsPerPage($rpp);
00035       $tq->setSortByFrequency(!$random);
00036       if ($term) {
00037          $predicate = new DMQueryPredicate();
00038          $predicate->setString($term);
00039          $tq->setPredicates(array($predicate));
00040       }
00041       foreach ($collections as $alias) {
00042          if ($alias) {
00043             $col = DMCollectionFactory::getCollection(
00044                   DMCollection::getSanitizedAlias($alias));
00045             $tq->addCollection($col);
00046          }
00047       }
00048       foreach ($objects as $aliasptr) {
00049          $split = explode("/", $aliasptr);
00050          if (count($split) == 3 && $split[1] && $split[2]) {
00051             $col = DMCollectionFactory::getCollection(
00052                   DMCollection::getSanitizedAlias($split[1]));
00053             $obj = DMObjectFactory::getObject($col, (int) $split[2]);
00054             $tq->addObject($obj);
00055          }
00056       }
00057 
00058       $tags = $tq->getSearchResultsAsCounts();
00059       $total = $tq->getNumResults();
00060 
00061       $response = new DMHTTPResponse();
00062       $rep = DMHTTPResponseFactory::getRepresentation();
00063       $trans = DMHTTPResponseFactory::getTransformer();
00064       $rep->setBody($trans->transformTagCounts($tags, $page, $rpp, $total));
00065       $response->setRepresentation($rep);
00066       $response->send();
00067       die;
00068    }
00069 
00073    public function view($id) {
00074       $response = new DMHTTPResponse();
00075       $rep = DMHTTPResponseFactory::getRepresentation();
00076       $trans = DMHTTPResponseFactory::getTransformer();
00077       $response->setRepresentation($rep);
00078 
00079       try {
00080          $tag = DMDataStoreFactory::getDataStore()->loadTag($id);
00081          if (!$tag->isApproved()) {
00082             throw new DMUnavailableModelException("Invalid tag.");
00083          }
00084 
00085          $rep->setBody($trans->transformTag($tag));
00086       } catch (DMUnavailableModelException $e) {
00087          $response->setStatusCode(404);
00088          $rep->setBody($trans->transformException($e));
00089       }
00090 
00091       $response->send();
00092       die;
00093    }
00094 
00095 }
 All Data Structures Functions Variables