dmBridge PHP API
DMJSONRepresentationTransformerV1.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 DMJSONRepresentationTransformerV1 extends DMJSONRepresentationTransformer
00014 implements DMHTTPAPIRepresentationTransformer {
00015 
00019    protected $version = 1;
00020 
00025    public function nullResponse() {
00026       return $this->output();
00027    }
00028 
00033    public function transformCollection(DMCollection $col) {
00034       $fields = array();
00035       foreach ($col->getFields() as $f) {
00036          if ($f->isHidden()) {
00037             continue;
00038          }
00039          $field = array();
00040          $field['name'] = $f->getName();
00041          $field['isControlled'] = (int) $f->isControlled();
00042          $field['isSearchable'] = (int) $f->isSearchable();
00043          $field['dc'] = $f->getDCNick();
00044          $field['type'] = $f->getType();
00045          $fields[$f->getNick()] = $field;
00046       }
00047 
00048       $this->addKey('dmCollection',
00049          array(
00050             'alias' => $col->getAlias(),
00051             'name' => $col->getName(),
00052             'num_objects' => $col->getNumObjects(),
00053             'overview_href' => $col->getOverviewURI()
00054                ? (string) $col->getOverviewURI() : null,
00055             'description' => $col->getDescription(),
00056             'image_512_href' => $col->getImage512URI()
00057                ? (string) $col->getImage512URI() : null,
00058             'fields' => $fields
00059          )
00060       );
00061       return $this->output();
00062    }
00063 
00068    public function transformCollections(array $collections) {
00069       $cols = array();
00070       foreach ($collections as $c) {
00071          if ($c instanceof DMCollection) {
00072             $cols[$c->getAlias()] = array(
00073                'href' => (string) $c->getURI(DMBridgeComponent::HTTPAPI),
00074                'name' => $c->getName(),
00075                'num_objects' => $c->getNumObjects()
00076             );
00077          }
00078       }
00079       $this->addKey('dmCollections', $cols);
00080       return $this->output();
00081    }
00082 
00087    public function transformComment(DMComment $comment) {
00088       $data = array(
00089          'alias' => $comment->getObject()->getCollection()->getAlias(),
00090          'ptr' => $comment->getObject()->getPtr(),
00091          'href' => (string) $comment->getObject()->getURI(
00092                DMBridgeComponent::HTTPAPI),
00093          'dmComment' => array(
00094             'id' => $comment->getID(),
00095             'timestamp' => $comment->getTimestamp()->asISO8601(),
00096             'poster' => array(
00097                'name' => $comment->getName(),
00098                'email' => $comment->getEmail()
00099             ),
00100             'body' => $comment->getValue()
00101          )
00102       );
00103       $this->addKey('dmObject', $data);
00104       return $this->output();
00105    }
00106 
00115    public function transformComments(array $comments, $page, $rpp, $total) {
00116       $data = array();
00117       foreach ($comments as $c) {
00118          if (!$c instanceof DMComment) {
00119             continue;
00120          }
00121 
00122          $data[] = array(
00123             'id' => $c->getID(),
00124             'href' => (string) $c->getURI(DMBridgeComponent::HTTPAPI)
00125          );
00126       }
00127 
00128       $this->addKey('page', $page);
00129       $this->addKey('resultsPerPage', $rpp);
00130       $this->addKey('numPages', ceil($total / $rpp));
00131       $this->addKey('total', $total);
00132       $this->addKey('comments', $data);
00133       return $this->output();
00134    }
00135 
00141    public function transformException(Exception $ex) {
00142       $data = array();
00143       $data['message'] = $ex->getMessage();
00144       if (DMConfigIni::getInstance()->getString("dmbridge.debug_mode")) {
00145          $data['type'] = get_class($ex);
00146          $data['stack_trace'] = $ex->getTraceAsString();
00147       }
00148       $data['documentation'] = (string) DMInternalURI::getURIWithParams(
00149             "api/1/doc", array(), "html");
00150 
00151       $this->addKey('dmException', $data);
00152       return $this->output();
00153    }
00154 
00159    public function transformObject(DMObject $obj) {
00160       $ds = DMDataStoreFactory::getDataStore();
00161       $children = array();
00162       foreach ($obj->getChildren() as $c) {
00163          $children[] = array(
00164             'pointer' => $c->getPtr(),
00165             'title' => $c->getTitle(),
00166             'thumbnail' => array(
00167                'href' => (string) $c->getThumbnailURL()
00168             ),
00169             'href' => (string) $c->getURI(DMBridgeComponent::HTTPAPI)
00170          );
00171       }
00172       $metadata = array();
00173       foreach ($obj->getMetadata() as $f) {
00174          if (strlen($f->getValue()) < 1 || $f->isHidden()) {
00175             continue;
00176          }
00177          $metadata[] = array(
00178             'nick' => $f->getNick(),
00179             'name' => $f->getName(),
00180             'value' => $f->getValue(),
00181             'dc_map' => $f->getDCNick(),
00182             'searchable' => $f->isSearchable() ? 1 : 0,
00183             'controlled' => $f->isControlled() ? 1 : 0
00184          );
00185       }
00186 
00187       $cq = new DMCommentQuery($ds);
00188       $cq->addObject($obj);
00189       $cq->setApproved(1);
00190       $comments = array();
00191       foreach ($cq->getSearchResults() as $c) {
00192          $comments[] = array(
00193             'id' => $c->getID(),
00194             'href' => (string) $c->getURI(DMBridgeComponent::HTTPAPI)
00195          );
00196       }
00197 
00198       $tq = new DMTagQuery($ds);
00199       $tq->addObject($obj);
00200       $tq->setApproved(1);
00201       $tags = array();
00202       foreach ($tq->getSearchResultsAsObjects() as $t) {
00203          $tags[] = array(
00204             'id' => $t->getID(),
00205             'href' => (string) $t->getURI(DMBridgeComponent::HTTPAPI),
00206             'text' => $t->getValue()
00207          );
00208       }
00209 
00210       $data = array();
00211       $data['alias'] = $obj->getCollection()->getAlias();
00212       $data['ptr'] = $obj->getPtr();
00213       $data['created'] = $obj->getDateCreated()->asISO8601();
00214       $data['last_updated'] = $obj->getDateUpdated()->asISO8601();
00215       $data['is_compound'] = (int) $obj->isCompound();
00216       $data['web_template'] = array(
00217          'href' => (string) $obj->getReferenceURL()
00218       );
00219       $data['file'] = array(
00220          'href' => (string) $obj->getFileURL(),
00221          'filename' => $obj->getFilename(),
00222          'mime_type' => $obj->getMediaType()->__toString(),
00223          'height' => $obj->getHeight(),
00224          'width' => $obj->getWidth()
00225       );
00226       $data['image_generator'] = rtrim(DMHTTPRequest::getCurrent()->getURI(), "/")
00227          . DMConfigIni::getInstance()->getString("contentdm.getimage_exe.path");
00228       $data['image'] = array(
00229          'small' => (string) $obj->getImageURL(300, 300),
00230          'medium' => (string) $obj->getImageURL(600, 600),
00231          'large' => (string) $obj->getImageURL(900, 900)
00232       );
00233       $data['thumbnail'] = array(
00234          'href' => (string) $obj->getThumbnailURL()
00235       );
00236       $data['rating'] = array(
00237          'num_ratings' => $ds->getNumRatingsForObject($obj),
00238          'average' => $obj->getRating(100)
00239       );
00240       $data['parent'] = ($obj->getParent())
00241          ? (string) $obj->getParent()->getURI(DMBridgeComponent::HTTPAPI)
00242          : null;
00243       $data['children'] = $children;
00244       $data['metadata'] = $metadata;
00245       $data['tags'] = $tags;
00246       $data['comments'] = $comments;
00247 
00248       $this->addKey('dmObject', $data);
00249       return $this->output();
00250    }
00251 
00258    public function transformObjectComments(DMObject $obj, $page, $rpp) {
00259       $comments = DMDataStoreFactory::getDataStore()
00260          ->getApprovedCommentsForObject($obj, $page, $rpp);
00261       $cdata = array();
00262       foreach ($comments as $c) {
00263          if (!$c instanceof DMComment) {
00264             continue;
00265          }
00266          $comment = array(
00267             'id' => $c->getID(),
00268             'href' => (string) $c->getURI(DMBridgeComponent::HTTPAPI),
00269             'timestamp' => $c->getTimestamp()->asISO8601(),
00270             'poster' => array(
00271                'name' => $c->getName(),
00272                'email' => $c->getEmail()
00273             ),
00274             'body' => $c->getValue()
00275          );
00276          $cdata[] = $comment;
00277       }
00278 
00279       $data = array(
00280          'alias' => $obj->getCollection()->getAlias(),
00281          'pointer' => $obj->getPtr(),
00282          'href' => (string) $obj->getURI(DMBridgeComponent::HTTPAPI),
00283          'comments' => $cdata
00284       );
00285 
00286       $this->addKey('dmObject', $data);
00287       return $this->output();
00288    }
00289 
00294    public function transformObjectRating(DMObject $obj) {
00295       $ds = DMDataStoreFactory::getDataStore();
00296       $data = array(
00297          'alias' => $obj->getCollection()->getAlias(),
00298          'pointer' => $obj->getPtr(),
00299          'href' => (string) $obj->getURI(DMBridgeComponent::HTTPAPI),
00300          'rating' => array(
00301             'num_ratings' => $ds->getNumRatingsForObject($obj),
00302             'average' => $obj->getRating(100)
00303          )
00304       );
00305 
00306       $this->addKey('dmObject', $data);
00307       return $this->output();
00308    }
00309 
00314    public function transformObjectTags(DMObject $obj) {
00315       $ds = DMDataStoreFactory::getDataStore();
00316       $tq = new DMTagQuery($ds);
00317       $tq->addObject($obj);
00318       $tq->setApproved(1);
00319       $tags = array();
00320       foreach ($tq->getSearchResultsAsObjects() as $t) {
00321          $tags[] = array(
00322             'href' => (string) $t->getURI(DMBridgeComponent::HTTPAPI),
00323             'text' => $t->getValue()
00324          );
00325       }
00326 
00327       $data = array(
00328          'alias' => $obj->getCollection()->getAlias(),
00329          'pointer' => $obj->getPtr(),
00330          'href' => (string) $obj->getURI(DMBridgeComponent::HTTPAPI),
00331          'tags' => $tags
00332       );
00333 
00334       $this->addKey('dmObject', $data);
00335       return $this->output();
00336    }
00337 
00343    public function transformRatings(DMObjectQuery $q) {
00344       // results
00345       $this->addKey('resultsPerPage', $q->getNumResultsPerPage());
00346       $this->addKey('total', $q->getNumResults());
00347 
00348       $data = array();
00349       foreach ($q->getSearchResults() as $obj) {
00350          $data[] = array(
00351             'alias' => $obj->getCollection()->getAlias(),
00352             'pointer' => $obj->getPtr(),
00353             'href' => (string) $obj->getURI(DMBridgeComponent::HTTPAPI),
00354             'rating' => $obj->getRating()
00355          );
00356       }
00357       $this->addKey('results', $data);
00358 
00359       return $this->output();
00360    }
00361 
00366    public function transformResults(DMObjectQuery $q) {
00367       // suggestion
00368       if ($q->getSuggestion()) {
00369          $this->addKey('suggestion', $q->getSuggestion());
00370       }
00371 
00372       // results
00373       $this->addKey('page', $q->getPage());
00374       $this->addKey('resultsPerPage', $q->getNumResultsPerPage());
00375       $this->addKey('numPages', $q->getNumPages());
00376       $this->addKey('total', $q->getNumResults());
00377 
00378       $data = array();
00379       foreach ($q->getSearchResults() as $obj) {
00380          $data[] = array(
00381             'alias' => $obj->getCollection()->getAlias(),
00382             'pointer' => $obj->getPtr(),
00383             'href' => (string) $obj->getURI(DMBridgeComponent::HTTPAPI)
00384          );
00385       }
00386       $this->addKey('results', $data);
00387 
00388       // facets
00389       $data = array();
00390       foreach ($q->getFacetTerms() as $f) {
00391          $data[] = array(
00392             'field' => $f->getField()->getNick(),
00393             'name' => $f->getField()->getName(),
00394             'count' => $f->getCount(),
00395             'href' => (string) $f->getURI()
00396          );
00397       }
00398       $this->addKey('facets', $data);
00399 
00400       return $this->output();
00401    }
00402 
00407    public function transformStatus() {
00408       // "are_available" should not be hard-coded
00409       $data = array(
00410          'institution' => array(
00411             'name' => DMConfigXML::getInstance()->getInstitutionName()
00412          ),
00413          'comments' => array(
00414             'are_available' => (int) DMConfigXML::getInstance()->isCommentingEnabled(),
00415             'are_moderated' => (int) DMConfigXML::getInstance()->isCommentModerationEnabled()
00416          ),
00417          'tags' => array(
00418             'are_available' => (int) DMConfigXML::getInstance()->isTaggingEnabled(),
00419             'are_moderated' => (int) DMConfigXML::getInstance()->isTagModerationEnabled()
00420          ),
00421          'ratings' => array(
00422             'are_available' => (int) DMConfigXML::getInstance()->isRatingEnabled()
00423          ),
00424          'version' => DMConfigXML::getInstance()->getVersion(),
00425          'version_sequence' => DMConfigXML::getInstance()->getVersionSequence()
00426       );
00427       $this->addKey('dmStatus', $data);
00428       return $this->output();
00429    }
00430 
00436    public function transformTag(DMTag $tag) {
00437       $data = array(
00438          'alias' => $tag->getObject()->getCollection()->getAlias(),
00439          'ptr' => $tag->getObject()->getPtr(),
00440          'href' => (string) $tag->getObject()->getURI(DMBridgeComponent::HTTPAPI),
00441          'dmTag' => array(
00442             'id' => $tag->getID(),
00443             'timestamp' => $tag->getTimestamp()->asISO8601(),
00444             'value' => $tag->getValue()
00445          )
00446       );
00447       $this->addKey('dmObject', $data);
00448       return $this->output();
00449    }
00450 
00459    public function transformTagCounts(array $tags, $page, $rpp, $total) {
00460       $data = array();
00461       foreach ($tags as $value => $count) {
00462          $data[$value] = $count;
00463       }
00464       $this->addKey('page', $page);
00465       $this->addKey('resultsPerPage', $rpp);
00466       $this->addKey('numPages', ceil($total / $rpp));
00467       $this->addKey('total', $total);
00468       $this->addKey('tags', $data);
00469       return $this->output();
00470    }
00471 
00476    public function transformVocabularyFrequencies(array $freqs) {
00477       $data = array();
00478       foreach ($freqs as $term => $count) {
00479          $data[$term] = $count;
00480       }
00481       $this->addKey('vocabulary', $data);
00482       return $this->output();
00483    }
00484 
00489    public function transformVocabularySuggestions(array $words) {
00490       $this->addKey('vocabulary', $words);
00491       return $this->output();
00492    }
00493 
00494    protected function addKey($key, $value) {
00495       if (is_null($key)) {
00496          $this->json_array[] = $value;
00497       } else {
00498          $this->json_array['dmBridgeResponse'][$key] = $value;
00499       }
00500    }
00501 
00502    protected function output() {
00503       $this->addKey('copyright', DMConfigXML::getInstance()->getFeedCopyright());
00504       $this->addKey('querySeconds', $this->getQSeconds());
00505       $this->addKey('version', $this->getVersion());
00506 
00507       // json_encode has problems with non-UTF-8 data and we cannot assume
00508       // the output is valid UTF-8 as of CONTENTdm version 5.3.
00509       // http://code.google.com/p/dmbridge/issues/detail?id=42
00510       $output = json_encode($this->json_array);
00511       if (!DMString::isUTF8($output)) {
00512          $output = DMString::clean($output);
00513       }
00514       return $output;
00515    }
00516 
00517 }
 All Data Structures Functions Variables