dmBridge PHP API
DMAPIObjectController.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 DMAPIObjectController extends DMAbstractController {
00014 
00020    public function comments($aliasptr) {
00021       if (DMHTTPRequest::getCurrent()->getMethod() == DMHTTPMethod::POST) {
00022          $this->postComment($aliasptr);
00023       } else {
00024          $this->viewComments($aliasptr);
00025       }
00026    }
00027 
00028    private function postComment($alias_ptr) {
00029       $tmp = explode(",", $alias_ptr);
00030       $response = new DMHTTPResponse();
00031       $rep = DMHTTPResponseFactory::getRepresentation();
00032       $response->setRepresentation($rep);
00033 
00034       try {
00035          $alias = DMCollection::getSanitizedAlias($tmp[0]);
00036          $ptr = (int) $tmp[1];
00037          $col = DMCollectionFactory::getCollection($alias);
00038          $obj = DMObjectFactory::getObject($col, $ptr);
00039 
00040          $moderation = true;
00041          $msg = "";
00042          if (DMConfigXML::getInstance()->isCommentModerationEnabled()) {
00043             $msg = "Comment added, subject to moderation.";
00044          } else {
00045             $msg = "Comment added.";
00046             $moderation = false;
00047          }
00048 
00049          $obj = DMObjectFactory::getObject(
00050                DMCollectionFactory::getCollection($alias), $ptr);
00051 
00052          $req_rep = DMHTTPRequest::getCurrent()->getRepresentation();
00053          $c = new DMComment();
00054          $c->setName(substr($req_rep->getFormValue("name"), 0, 100));
00055          $c->setEmail($req_rep->getFormValue("email"));
00056 
00057          $c->setValue(substr($req_rep->getFormValue("text"), 0, 10000));
00058          $c->setTimestamp(new DMDateTime());
00059          $c->setApproved(!$moderation);
00060 
00061          $obj->addComment($c);
00062          if (DMConfigXML::getInstance()->isCommentNotificationEnabled()) {
00063             $mailer = new DMMailer();
00064             $mailer->sendNotificationOfNewComment($c);
00065          }
00066          if ($moderation) {
00067             $response->setStatusCode(202);
00068          } else {
00069             $response->setStatusCode(201);
00070             $response->addHeader("Location",
00071                   $c->getURI(DMBridgeComponent::HTTPAPI));
00072          }
00073 
00074          $response->send();
00075          die;
00076       } catch (DMUnavailableModelException $e) {
00077          bailOut($e);
00078       } catch (Exception $e) {
00079          bailOut($e);
00080       }
00081    }
00082 
00083    private function viewComments($aliasptr) {
00084       $tmp = explode(",", $aliasptr);
00085       $uri = DMHTTPRequest::getCurrent()->getURI();
00086 
00087       $page = abs((int) substr($uri->getQueryValue("page"), 0, 7));
00088       $page = ($page < 1) ? 1 : $page;
00089       $rpp = abs((int) substr($uri->getQueryValue("rpp"), 0, 4));
00090       $rpp = ($rpp > 100) ? 100 : $rpp;
00091       $rpp = ($rpp == 1) ? 50 : ($rpp < 10) ? 10 : $rpp;
00092 
00093       try {
00094          $col = DMCollectionFactory::getCollection(
00095                DMCollection::getSanitizedAlias($tmp[0]));
00096          $obj = DMObjectFactory::getObject($col, (int) $tmp[1]);
00097 
00098          $response = new DMHTTPResponse();
00099          $rep = DMHTTPResponseFactory::getRepresentation();
00100          $trans = DMHTTPResponseFactory::getTransformer();
00101          $rep->setBody($trans->transformObjectComments($obj, $page, $rpp));
00102          $response->setRepresentation($rep);
00103          $response->send();
00104          die;
00105       } catch (DMException $e) {
00106          bailOut($e);
00107       }
00108    }
00109 
00113    public function highestRated($alias) {
00114       $col = DMCollectionFactory::getCollection(
00115             DMCollection::getSanitizedAlias($alias));
00116       $uri = DMHTTPRequest::getCurrent()->getURI();
00117 
00118       $rpp = abs((int) substr($uri->getQueryValue("rpp"), 0, 4));
00119       $rpp = ($rpp > 100) ? 100 : $rpp;
00120       $rpp = ($rpp == 1) ? 50 : ($rpp < 10) ? 10 : $rpp;
00121 
00122 
00123       $q = new DMObjectQuery();
00124       $q->addCollection($col);
00125       $q->setNumResultsPerPage($rpp);
00126       $q->setRatingRange(0, 1);
00127       // "prime" the query
00128       $q->getSearchResults();
00129 
00130       $response = new DMHTTPResponse();
00131       $rep = DMHTTPResponseFactory::getRepresentation();
00132       $trans = DMHTTPResponseFactory::getTransformer();
00133       $rep->setBody($trans->transformRatings($q));
00134       $response->setRepresentation($rep);
00135       $response->send();
00136       die;
00137    }
00138 
00147    public function index($alias) {
00148       $alias = DMCollection::getSanitizedAlias($alias);
00149       if (strlen($alias) < 2) {
00150          $alias = "/dmdefault";
00151       }
00152       $alias = DMCollection::getSanitizedAlias($alias);
00153       $collection = DMCollectionFactory::getCollection($alias);
00154 
00155       // check for user input from a search
00156       $input = new DMInput(DMHTTPRequest::getCurrent()->getURI());
00157       $query = null;
00158       if ($input->isPresent()) {
00159          $query = $this->search($input, $collection);
00160       } else {
00161          $query = $this->browse($collection);
00162       }
00163       // execute search
00164       $query->getSearchResults();
00165 
00166       $response = new DMHTTPResponse();
00167       $rep = DMHTTPResponseFactory::getRepresentation();
00168       $trans = DMHTTPResponseFactory::getTransformer();
00169       $rep->setBody($trans->transformResults($query));
00170       $response->setRepresentation($rep);
00171       $response->send();
00172       die;
00173    }
00174 
00180    private function browse(DMCollection $c) {
00181       $uri = DMHTTPRequest::getCurrent()->getURI();
00182       $sort = $uri->getQueryValue("sort")
00183             ? substr($uri->getQueryValue("sort"), 0, 20) : null;
00184       $order = $uri->getQueryValue("order")
00185             ? substr($uri->getQueryValue("order"), 0, 10) : null;
00186       $page = $uri->getQueryValue("page")
00187             ? abs((int) $uri->getQueryValue("page")) : 1;
00188       $rpp = $uri->getQueryValue("rpp")
00189             ? abs((int) $uri->getQueryValue("rpp")) : 20;
00190       if ($uri->getQueryValue("view")
00191             && $uri->getQueryValue("view") == "feed") {
00192          $sort = array("dmmodified" => "desc");
00193          $page = 1;
00194       }
00195 
00196       // cap max results per page at 100
00197       $rpp = ($rpp > 100) ? 100 : $rpp;
00198       $rpp = ($rpp == 1) ? 50 : ($rpp < 10) ? 10 : $rpp;
00199 
00200       $q = new DMObjectQuery();
00201       $q->setCollections(array($c));
00202       $q->setSortFields(array($sort => $order));
00203       $q->setPage($page);
00204       $q->setNumResultsPerPage($rpp);
00205       return $q;
00206    }
00207 
00208    public function rating($alias_ptr) {
00209       $response = new DMHTTPResponse();
00210       $rep = DMHTTPResponseFactory::getRepresentation();
00211       $trans = DMHTTPResponseFactory::getTransformer();
00212       $response->setRepresentation($rep);
00213 
00214       $tmp = explode(",", $alias_ptr);
00215       try {
00216          $alias = DMCollection::getSanitizedAlias($tmp[0]);
00217          $ptr = (int) $tmp[1];
00218          $col = DMCollectionFactory::getCollection($alias);
00219          $obj = DMObjectFactory::getObject($col, $ptr);
00220 
00221          if (DMHTTPRequest::getCurrent()->getMethod() == DMHTTPMethod::POST) {
00222             $req_rep = DMHTTPRequest::getCurrent()->getRepresentation();
00223             $rating = new DMRating($req_rep->getFormValue("value"), 100);
00224             $obj->addRating($rating);
00225             $response->setStatusCode(204);
00226             $response->send();
00227             die;
00228          }
00229 
00230          $rep->setBody($trans->transformObjectRating($obj));
00231       } catch (UnavailableModelDMException $e) {
00232          $response->setStatusCode(404);
00233          $response->send();
00234          die;
00235       } catch (DMIllegalArgumentException $e) {
00236          $response->setStatusCode(400);
00237          $response->send();
00238          die;
00239       }
00240 
00241       $response->send();
00242       die;
00243    }
00244 
00250    private function search(DMInput $input, DMCollection $col) {
00251       $uri = DMHTTPRequest::getCurrent()->getURI();
00252       $input->validate();
00253 
00254       $sort = $uri->getQueryValue("sort")
00255             ? substr($uri->getQueryValue("sort"), 0, 10) : null;
00256       $order = $uri->getQueryValue("order")
00257             ? substr($uri->getQueryValue("order"), 0, 4) : null;
00258       $page = $uri->getQueryValue("page")
00259             ? abs((int) $uri->getQueryValue("page")) : 1;
00260       $rpp = $uri->getQueryValue("rpp")
00261             ? abs((int) $uri->getQueryValue("rpp")) : 20;
00262       $rpp = ($rpp > 100) ? 100 : $rpp;
00263       $rpp = ($rpp == 1) ? 50 : ($rpp < 10) ? 10 : $rpp;
00264 
00265       $min_rating = $uri->getQueryValue("min_rating")
00266             ? abs($uri->getQueryValue("min_rating")) : null;
00267       $max_rating = $uri->getQueryValue("max_rating")
00268             ? abs($uri->getQueryValue("max_rating")) : null;
00269 
00270       $query = new DMObjectQuery();
00271       $query->setPredicates($input->getTerms());
00272       $query->setCollections($input->getCollections());
00273       $query->setSortFields(array($sort => $order));
00274       $query->setNumResultsPerPage($rpp);
00275       $query->setPage($page);
00276       if ($min_rating || $max_rating) {
00277          if ($max_rating > 100) $max_rating = 100;
00278          if ($min_rating < 0) $min_rating = 0;
00279          $qf = new DMRatingFilter();
00280          $qf->setRange($min_rating, $max_rating);
00281          $query->addFilter($qf);
00282       }
00283 
00284       foreach ($col->getFacets() as $f) {
00285          $query->addFacetTerm($f);
00286       }
00287       return $query;
00288    }
00289 
00293    public function compoundSearch($aliasptr) {
00294       $response = new DMHTTPResponse();
00295       $rep = DMHTTPResponseFactory::getRepresentation();
00296       $trans = DMHTTPResponseFactory::getTransformer();
00297       $response->setRepresentation($rep);
00298       $uri = DMHTTPRequest::getCurrent()->getURI();
00299 
00300       $tmp = explode(",", $aliasptr);
00301       $col = DMCollectionFactory::getCollection(
00302             DMCollection::getSanitizedAlias($tmp[0]));
00303       $ptr = abs((int) substr($tmp[1], 0, 8));
00304       $term = substr($uri->getQueryValue("term"), 0, 150);
00305 
00306       if (!strlen($term)) {
00307          $response->setStatusCode(400);
00308          $response->send();
00309          die;
00310       }
00311 
00312       $page = $uri->getQueryValue("page")
00313             ? abs((int) $uri->getQueryValue("page")) : 1;
00314       $rpp = $uri->getQueryValue("rpp")
00315             ? abs((int) $uri->getQueryValue("rpp")) : 20;
00316       $rpp = ($rpp > 100) ? 100 : $rpp;
00317       $rpp = ($rpp == 1) ? 50 : ($rpp < 10) ? 10 : $rpp;
00318 
00319       // instantiate an object so we can find its parent to search within
00320       $obj = DMObjectFactory::getObject($col, $ptr);
00321       $obj = ($obj->isChild()) ? $obj->getParent() : $obj;
00322 
00323       $qt = new DMQueryPredicate();
00324       $qt->setString($term);
00325       $qt->setField(new DMDCElement("full"));
00326       $qt->setMode("exact");
00327 
00328       $query = new DMObjectQuery();
00329       $query->setPredicates(array($qt));
00330       $query->setCollections(array($col));
00331       $query->setSortFields(array("title" => "asc"));
00332       $query->setNumResultsPerPage($rpp);
00333       $query->setPage($page);
00334       $query->addObject($obj);
00335 
00336       $query->getSearchResults();
00337 
00338       $rep->setBody($trans->transformResults($query));
00339       $response->send();
00340       die;
00341    }
00342 
00343    public function searchByRating() {
00344       $response = new DMHTTPResponse();
00345       $rep = DMHTTPResponseFactory::getRepresentation();
00346       $trans = DMHTTPResponseFactory::getTransformer();
00347       $response->setRepresentation($rep);
00348       $uri = DMHTTPRequest::getCurrent()->getURI();
00349 
00350       $page = (int) substr($uri->getQueryValue("page"), 0, 7);
00351       $rpp = (int) $uri->getQueryValue("rpp");
00352       $rpp = ($rpp > 100) ? 100 : $rpp;
00353       $rpp = ($rpp == 1) ? 50 : ($rpp < 10) ? 10 : $rpp;
00354       $min_rating = $uri->getQueryValue("min_rating");
00355       $max_rating = $uri->getQueryValue("max_rating");
00356       $collections = explode(",", $uri->getQueryValue("collections"));
00357 
00358       $q = new DMObjectQuery();
00359       $q->setPage($page);
00360       $q->setNumResultsPerPage($rpp);
00361       $q->setRatingRange((float) $min_rating, (float) $max_rating);
00362       foreach ($collections as $alias) {
00363          if ($alias) {
00364             $col = DMCollectionFactory::getCollection(
00365                   DMCollection::getSanitizedAlias($alias));
00366             $q->addCollection($col);
00367          }
00368       }
00369 
00370       if ($min_rating == null || $max_rating == null
00371             || $min_rating >= $max_rating) {
00372          $response->setStatusCode(400);
00373          $rep->setBody("Invalid rating.");
00374          $response->send();
00375          die;
00376       }
00377 
00378       // "prime" the query
00379       $q->getSearchResults();
00380 
00381       $rep->setBody($trans->transformRatings($q));
00382       $response->send();
00383       die;
00384    }
00385 
00386    public function tags($aliasptr) {
00387       if (DMHTTPRequest::getCurrent()->getMethod() == DMHTTPMethod::POST) {
00388          $this->postTag($aliasptr);
00389       } else {
00390          $this->viewTags($aliasptr);
00391       }
00392    }
00393 
00394    private function postTag($alias_ptr) {
00395       $tmp = explode(",", $alias_ptr);
00396 
00397       $response = new DMHTTPResponse();
00398       $rep = DMHTTPResponseFactory::getRepresentation();
00399       $trans = DMHTTPResponseFactory::getTransformer();
00400       $response->setRepresentation($rep);
00401       $req = DMHTTPRequest::getCurrent();
00402 
00403       try {
00404          $alias = DMCollection::getSanitizedAlias($tmp[0]);
00405          $ptr = (int) $tmp[1];
00406          $col = DMCollectionFactory::getCollection($alias);
00407          $obj = DMObjectFactory::getObject($col, $ptr);
00408 
00409          $req_rep = $req->getRepresentation();
00410          $t = new DMTag();
00411          $t->setValue(substr($req_rep->getFormValue("value"), 0, 100));
00412          $t->setTimestamp(new DMDateTime());
00413 
00414          $moderation = true;
00415          $msg = "";
00416          if (DMConfigXML::getInstance()->isTagModerationEnabled()) {
00417             $msg = "Tag added, subject to moderation.";
00418          } else {
00419             $msg = "Tag added.";
00420             $moderation = false;
00421          }
00422          $t->setApproved(!$moderation);
00423          $obj->addTag($t);
00424 
00425          if ($moderation) {
00426             $response->setStatusCode(202);
00427          } else {
00428             $response->setStatusCode(201);
00429             $response->addHeader("Location",
00430                   $t->getURI(DMBridgeComponent::HTTPAPI));
00431          }
00432       } catch (DMException $e) {
00433          $response->setStatusCode($e->getHTTPResponseCode());
00434          $rep->setBody($trans->transformException($e));
00435       }
00436 
00437       $response->send();
00438       die;
00439    }
00440 
00441    private function viewTags($aliasptr) {
00442       $tmp = explode(",", $aliasptr);
00443 
00444       $col = DMCollectionFactory::getCollection(
00445             DMCollection::getSanitizedAlias($tmp[0]));
00446       $obj = DMObjectFactory::getObject($col, (int) $tmp[1]);
00447 
00448       $response = new DMHTTPResponse();
00449       $rep = DMHTTPResponseFactory::getRepresentation();
00450       $trans = DMHTTPResponseFactory::getTransformer();
00451       $rep->setBody($trans->transformObjectTags($obj));
00452       $response->setRepresentation($rep);
00453       $response->send();
00454       die;
00455    }
00456 
00460    public function view($aliasptr) {
00461       $response = new DMHTTPResponse();
00462       $rep = DMHTTPResponseFactory::getRepresentation();
00463       $trans = DMHTTPResponseFactory::getTransformer();
00464       $response->setRepresentation($rep);
00465 
00466       $tmp = explode(',', $aliasptr);
00467       $alias = DMCollection::getSanitizedAlias($tmp[0]);
00468       $col = DMCollectionFactory::getCollection($alias);
00469       $ptr = (int) $tmp[1];
00470 
00471       try {
00472          $obj = DMObjectFactory::getObject($col, $ptr);
00473          $rep->setBody($trans->transformObject($obj));
00474       } catch (DMUnavailableModelException $e) {
00475          $response->setStatusCode(404);
00476          $rep->setBody($trans->transformException($e));
00477       }
00478 
00479       $response->send();
00480       die;
00481    }
00482 
00483 }
 All Data Structures Functions Variables