dmBridge PHP API
DMAtomRepresentationTransformer.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 DMAtomRepresentationTransformer extends DMFeedRepresentationTransformer {
00014 
00015    private function assembleHead(DMObject $obj = null) {
00016       $date_updated = new DMDateTime();
00017       // not ideal but should happen rarely
00018       $date_updated = $date_updated->asRFC3339();
00019       if ($obj) {
00020          $date_updated = $obj->getDateUpdated()->asRFC3339();
00021       }
00022 
00023       $this->dxml->loadXML('<feed/>');
00024       $root = $this->dxml->documentElement;
00025       $root->setAttribute('xmlns', 'http://www.w3.org/2005/Atom');
00026       $atomlink = $this->dxml->createElement("link");
00027       $atomlink->setAttribute("rel", "self");
00028       $atomlink->setAttribute("href", DMHTTPRequest::getCurrent()->getURI());
00029       $root->appendChild($atomlink);
00030       // id
00031       $root->appendChild($this->dxml->createElement("id",
00032             DMHTTPRequest::getCurrent()->getURI()->getAbsoluteHostURI()));
00033       // title
00034       $root->appendChild(
00035          $this->dxml->createElement(
00036             'title', DMString::xmlentities($this->getTitle())));
00037       // subtitle
00038       $root->appendChild(
00039          $this->dxml->createElement(
00040             'subtitle', DMString::xmlentities($this->getSubtitle())));
00041       // link
00042       $link = $root->appendChild($this->dxml->createElement('link'));
00043       $link->setAttribute('href', DMHTTPRequest::getCurrent()->getURI());
00044       // updated
00045       $root->appendChild($this->dxml->createElement('updated', $date_updated));
00046       // author
00047       $au = $root->appendChild($this->dxml->createElement('author'));
00048       // author/name
00049       $au->appendChild($this->dxml->createElement('name',
00050          DMString::xmlentities(
00051             DMConfigXML::getInstance()->getInstitutionName())));
00052       // author/email
00053       $au->appendChild($this->dxml->createElement('email',
00054          DMString::xmlentities(
00055             DMConfigXML::getInstance()->getFeedManagingEditorEmail())));
00056       // rights
00057       $root->appendChild($this->dxml->createElement('rights',
00058          DMString::xmlentities(
00059             DMConfigXML::getInstance()->getFeedCopyright())));
00060       // generator
00061       $root->appendChild($this->dxml->createElement('generator',
00062             'dmBridge ' . DMConfigXML::getInstance()->getVersion()));
00063    }
00064 
00065 
00071    public function transformObject(DMObject $obj) {
00072       $this->assembleHead($obj);
00073       $root = $this->dxml->documentElement;
00074       // entry
00075       $onode = $root->appendChild($this->dxml->createElement('entry'));
00076       // entry/title
00077       $onode->appendChild($this->dxml->createElement(
00078          'title', DMString::xmlentities($obj->getMetadata('title')->getValue())));
00079       // entry/author
00080       if ($obj->getMetadata('creato')) { // may be null
00081          $name = $onode->appendChild($this->dxml->createElement("author"));
00082          $name->appendChild($this->dxml->createElement(
00083             "name", DMString::xmlentities($obj->getMetadata('creato')->getValue())));
00084       }
00085       // entry/link
00086       $link = $onode->appendChild($this->dxml->createElement('link'));
00087       $link->setAttribute('href',
00088             $obj->getURI(DMBridgeComponent::TemplateEngine));
00089       // entry/id
00090       $onode->appendChild(
00091          $this->dxml->createElement('id', $obj->getReferenceURL()));
00092       // entry/published
00093       $onode->appendChild($this->dxml->createElement('published',
00094          $obj->getDateCreated()->asRFC3339()));
00095       // entry/updated
00096       $onode->appendChild($this->dxml->createElement('updated',
00097          $obj->getDateUpdated()->asRFC3339()));
00098       // entry/summary
00099       $desc = ($obj->getMetadata('descri') instanceof DMDCElement) ?
00100          DMString::xmlentities($obj->getMetadata('descri')->getValue()) : null;
00101       $desc = sprintf('<img src="%s" /><p>%s</p>',
00102          $obj->getThumbURL(), $desc);
00103       $cdata = $this->dxml->createCDATASection($desc);
00104       $node = $onode->appendChild(
00105          $this->dxml->createElement('summary'));
00106       $node->setAttribute('type', 'html');
00107       $node->appendChild($cdata);
00108 
00109       return $this->output();
00110    }
00111 
00117    public function transformObjectComments(DMObject $obj) {
00118       $this->assembleHead($obj);
00119       $root = $this->dxml->documentElement;
00120       $cq = new DMCommentQuery(DMDataStoreFactory::getDataStore());
00121       $cq->setNumResultsPerPage(100);
00122       $cq->setApproved(1);
00123       $cq->addObject($obj);
00124       $comments = $cq->getSearchResults();
00125 
00126       foreach ($comments as $comment) {
00127          // entry
00128          $onode = $root->appendChild($this->dxml->createElement('entry'));
00129          // entry/title
00130          $onode->appendChild($this->dxml->createElement(
00131                "title", $comment->getName() . " "
00132                . $comment->getTimestamp()->format("Y-m-d")));
00133          // entry/author
00134          if ($obj->getMetadata('creato')) { // may be null
00135             $name = $onode->appendChild($this->dxml->createElement("author"));
00136             $name->appendChild($this->dxml->createElement(
00137                "name", DMString::xmlentities($comment->getName())));
00138          }
00139          // entry/link
00140          $link = $onode->appendChild($this->dxml->createElement('link'));
00141          $link->setAttribute('href',
00142                $obj->getURI(DMBridgeComponent::TemplateEngine));
00143          // entry/id
00144          $onode->appendChild(
00145             $this->dxml->createElement('id', $obj->getReferenceURL()));
00146          // entry/published
00147          $onode->appendChild($this->dxml->createElement('published',
00148             $comment->getTimestamp()->asRFC3339()));
00149          // entry/updated
00150          $onode->appendChild($this->dxml->createElement('updated',
00151             $comment->getTimestamp()->asRFC3339()));
00152          // entry/content
00153          $onode->appendChild($this->dxml->createElement('content',
00154             $comment->getValue()));
00155          $root->appendChild($onode);
00156       }
00157 
00158       if (count($comments)) {
00159          // set "updated" element to the date of the latest comment
00160          $root->getElementsByTagName("updated")->item(0)->nodeValue
00161                = $comments[0]->getTimestamp()->asRFC3339();
00162       }
00163 
00164       return $this->output();
00165    }
00166 
00172    public function transformResults(DMObjectQuery $query) {
00173       $objects = $query->getSearchResults();
00174       $this->assembleHead(count($objects) ? $objects[0] : null);
00175 
00176       $root = $this->dxml->documentElement;
00177       foreach ($objects as $o) {
00178          $onode = $root->appendChild($this->dxml->createElement('entry'));
00179          $onode->appendChild($this->dxml->createElement(
00180             'title', DMString::xmlentities($o->getMetadata('title')->getValue())));
00181          $link = $onode->appendChild($this->dxml->createElement('link'));
00182          $link->setAttribute('href',
00183                $o->getURI(DMBridgeComponent::TemplateEngine, "html"));
00184          $onode->appendChild(
00185             $this->dxml->createElement('id', $o->getReferenceURL()));
00186          $onode->appendChild($this->dxml->createElement('updated',
00187             $o->getDateUpdated()->asRFC3339()));
00188          $desc = ($o->getMetadata('descri') instanceof DMDCElement) ?
00189             DMString::xmlentities($o->getMetadata('descri')->getValue())
00190                   : null;
00191          $desc = sprintf('<img src="%s" /><p>%s</p>',
00192             $o->getThumbURL(), $desc);
00193          $cdata = $this->dxml->createCDATASection($desc);
00194          $node = $onode->appendChild(
00195             $this->dxml->createElement('summary'));
00196          $node->setAttribute('type', 'html');
00197          $node->appendChild($cdata);
00198       }
00199       return $this->output();
00200    }
00201 
00202 }
00203 
 All Data Structures Functions Variables