dmBridge PHP API
DMObjectTemplateHelper.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 
00019 class DMObjectTemplateHelper extends DMGenericTemplateHelper
00020 implements DMTemplateHelper {
00021 
00026    public function getHtmlAddFavoriteButton() {
00027       $obj = $this->getView()->getObject();
00028       $collection = $obj->getCollection();
00029       $alias = $collection->getAlias();
00030       $alias = ($alias == "/dmdefault") ? "" : $alias;
00031 
00032       if (!$obj->isFavorite()) {
00033          return sprintf('<form method="post" action="%s">
00034                <p class="dmAddFavorite">
00035                   <input type="hidden" name="alias" value="%s">
00036                   <input type="hidden" name="ptr" value="%d">
00037                   <input type="submit" value="Add To Favorites">
00038                </p>
00039             </form>',
00040             DMInternalURI::getURIWithParams(
00041                   sprintf("objects%s/favorites", $alias)),
00042             DMString::websafe($obj->getCollection()->getAlias()),
00043             $obj->getPtr());
00044       }
00045 
00046       return sprintf('<span class="dmAddFavoriteText">This object is in
00047                <a href="%s">your favorites</a>.
00048             </span>',
00049             DMInternalURI::getURIWithParams(sprintf("objects%s/favorites", $alias)));
00050    }
00051 
00068    public function getHtmlChildLinksAsList($thumbs = false) {
00069       $obj = $this->getView()->getObject();
00070       $obj = ($obj->isChild()) ? $obj->getParent() : $obj;
00071 
00072       $links = "";
00073       foreach ($obj->getChildren() as $child) {
00074          $selected = ($child->getPtr() == $obj->getPtr())
00075             ? 'class="dmCurrentPage"' : null;
00076 
00077          $links .= sprintf('<li %s><div>', $selected);
00078 
00079          if ($thumbs) {
00080             $links .= sprintf(
00081                '<a href="%s">
00082                   <img class="dmThumbnail" src="%s" alt="Page thumbnail">
00083                </a>',
00084                DMString::websafe($child->getURI()),
00085                DMString::websafe($child->getThumbnailURL()));
00086          }
00087          $links .= sprintf('<a href="%s">%s</a></div></li>',
00088             DMString::websafe($child->getURI()),
00089             DMString::websafe($child->getTitle()));
00090       }
00091       return '<ol class="dmChildLinks">' . $links . '</ol>';
00092    }
00093 
00106    public function getHtmlChildLinksAsPulldown() {
00107       $obj = $this->getView()->getObject();
00108       $obj = ($obj->isChild()) ? $obj->getParent() : $obj;
00109 
00110       $dxml = new DMDOMDocument("1.0", "utf-8");
00111       $dxml->loadXML("<form/>");
00112       $dxml->documentElement->setAttribute("method", "get");
00113       $dxml->documentElement->setAttribute(
00114          'action', trim(dirname($_SERVER['PHP_SELF']), "/"));
00115 
00116       $sel = $dxml->createElement("select");
00117       $sel->setAttribute("onchange",
00118          sprintf("window.location = '/%s/%s/' + this.value",
00119             trim(dirname($_SERVER['PHP_SELF']), '/'),
00120             trim($obj->getCollection()->getAlias(), "/")));
00121 
00122       foreach ($obj->getChildren() as $c) {
00123          $opt = $dxml->createElement(
00124             "option", DMString::websafe($c->getMetadata("title")));
00125          $opt->setAttribute("value", $c->getPtr());
00126          if ($c->getPtr() == $obj->getPtr()) {
00127             $opt->setAttribute("selected", "selected");
00128          }
00129          $sel->appendChild($opt);
00130       }
00131 
00132       $dxml->documentElement->appendChild($sel);
00133       return $dxml->saveHTML($dxml->documentElement);
00134    }
00135 
00144    public function getHtmlCommentsSection() {
00145       $obj = $this->getView()->getObject();
00146 
00147       try {
00148          $are_comments = $obj->hasComments();
00149 
00150          $obj = ($obj->isChild()) ? $obj->getParent() : $obj;
00151 
00152          if ($are_comments) {
00153             $comments = "";
00154             foreach (DMDataStoreFactory::getDataStore()
00155                   ->getApprovedCommentsForObject($obj, 1, 1000) as $c) {
00156                $comments .= sprintf('<tr><th class="dmCommentAuthor">%s</th>',
00157                   DMString::websafe($c->getName()));
00158                $comments .= sprintf('<th class="dmCommentDate">%s</th></tr>',
00159                   $c->getTimestamp()->format('Y-m-d @ h:i A'));
00160                $comments .= sprintf('<tr><td colspan="2" class="dmCommentBody">%s</td></tr>',
00161                   nl2br(DMString::websafe($c->getValue())));
00162             }
00163             $comments = '<table class="dmComments" cellspacing="0" cellpadding="0"
00164                border="0">' . $comments . '</table>';
00165          } else if (!DMHTTPRequest::getCurrent()->getSession()->hasCommentedOnObject($obj)) {
00166             $comments = '<p>There are no comments yet.
00167                Be the first to comment below!</p>';
00168          } else {
00169             $comments = '<p>There are no approved comments yet.</p>';
00170          }
00171       } catch (DMException $e) {
00172          return '<p>Sorry, we seem to be having some issues with our commenting
00173             system at the moment. Come back soon!</p>';
00174       }
00175 
00176       if (DMHTTPRequest::getCurrent()->getSession()->hasCommentedOnObject($obj)) {
00177          return '<p>Your comment has been received and is pending review.</p>'
00178             . $comments;
00179       }
00180 
00181       $this->getView()->getCommentForm()->getFieldByName("alias")->setValue(
00182             $obj->getCollection()->getAlias());
00183       $this->getView()->getCommentForm()->getFieldByName("ptr")->setValue(
00184             $obj->getPtr());
00185       $this->getView()->getCommentForm()->getFieldByName("response")
00186             ->setValue(null);
00187       $a = DMAbstractForm::getCaptchaOperands();
00188 
00189       if (!DMConfigXML::getInstance()->isCommentingEnabled()) {
00190          return $comments;
00191       }
00192 
00193       return $comments . sprintf(
00194          '<form method="post" action="">
00195             <fieldset>
00196                <legend>Comment on this object</legend>
00197                <ol>
00198                   <li><label for="dmCommentName">Your Name:</label>%s</li>
00199                   <li><label for="dmCommentEmail">Your E-mail:</label>%s</li>
00200                   <li>
00201                      <label for="dmCommentText">Your Comment:</label>
00202                      %s
00203                      <div style="display:none">
00204                         <label for="dmFauxComment">Leave this field
00205                            blank:</label>
00206                         <textarea name="fauxcomment" id="dmFauxComment"
00207                            rows="0" cols="0"></textarea>
00208                      </div>
00209                   </li>
00210                   <li>
00211                      %s%s%s
00212                      <label id="dmCaptchaChallenge">What\'s %d + %d?</label>
00213                      %s
00214                   </li>
00215                   <li>
00216                      <input type="hidden" name="action" value="comment">
00217                      <input type="submit" value="Submit" class="dmSubmit">
00218                   </li>
00219                </ol>
00220             </fieldset>
00221          </form>',
00222          $this->getView()->getCommentForm()
00223             ->getFieldByName("name")->getHTMLTag(),
00224          $this->getView()->getCommentForm()
00225             ->getFieldByName("email")->getHTMLTag(),
00226          $this->getView()->getCommentForm()
00227             ->getFieldByName("text")->getHTMLTag(),
00228          $this->getView()->getCommentForm()
00229             ->getFieldByName("alias")->getHTMLTag(),
00230          $this->getView()->getCommentForm()
00231             ->getFieldByName("ptr")->getHTMLTag(),
00232          $this->getView()->getCommentForm()
00233             ->getFieldByName("answer")->getHTMLTag(),
00234          $a[0],
00235          $a[1],
00236          $this->getView()->getCommentForm()
00237             ->getFieldByName("response")->getHTMLTag()
00238       );
00239    }
00240 
00247    public function getHtmlCompoundObjectSearchForm(
00248          $button_text = "Search Object Text") {
00249       $obj = $this->getView()->getObject();
00250       if (!$obj instanceof DMObject) {
00251          return null;
00252       }
00253       if (!$obj->getFullText()) {
00254          return null;
00255       }
00256       if (!$obj->isChild() && !$obj->hasChildren()) {
00257          return null;
00258       }
00259 
00260       $action = DMInternalURI::getURIWithParams(
00261          sprintf("%s/%d/search",
00262             $obj->getCollection()->getAlias(),
00263             $obj->getPtr()));
00264 
00265       $term = DMHTTPRequest::getCurrent()->getURI()->getQueryValue("term");
00266       return sprintf(
00267          '<form method="get" action="%s">
00268             <p>
00269                <input type="text" name="term" value="%s">
00270                <input type="submit" value="%s">
00271             </p>
00272          </form>',
00273          $action,
00274          (strlen($term) > 0)
00275             ? DMString::websafe(substr($term, 0, 100)) : "",
00276          DMString::websafe($button_text));
00277    }
00278 
00284    public function getHtmlCompoundObjectPageNumbersInSearchResultsAsString(
00285          $separator = ", ") {
00286       $query = $this->getView()->getQuery();
00287       $current_obj = $this->getView()->getObject();
00288 
00289       if (!$query instanceof DMObjectQuery) {
00290          return false;
00291       }
00292       $pages = array();
00293       foreach ($query->getSearchResults() as $obj) {
00294          if (!$obj->isChild()) {
00295             continue; // shouldn't ever happen...
00296          }
00297          if ($obj->getPtr() == $current_obj->getPtr()) {
00298             $pages[] = $obj->getPage();
00299          } else {
00300             $pages[] = sprintf('<a href="%s&term=%s">%d</a>',
00301                DMString::websafe(
00302                      $obj->getURI(DMBridgeComponent::TemplateEngine)),
00303                $query->getPredicates(0),
00304                $obj->getPage());
00305          }
00306       }
00307       return implode($separator, array_unique($pages));
00308    }
00309 
00319    public function getHtmlCompoundPageFlipLinks(
00320          $separator = " | ",
00321          $prev_page_link = "< Previous Page",
00322          $next_page_link = "Next Page >") {
00323       $obj = $this->getView()->getObject();
00324       $obj = ($obj->isChild()) ? $obj->getParent() : $obj;
00325 
00326       $pages = array();
00327       $children = $obj->getChildren();
00328       $count = count($children);
00329       for ($i = 0; $i < $count; $i++) {
00330          if ($obj->getPtr() <> $children[$i]->getPtr()) {
00331             continue;
00332          }
00333 
00334          if (array_key_exists($i-1, $children)) {
00335             $pages[] = sprintf('<a href="%s">%s</a>',
00336                DMString::websafe(
00337                      $children[$i-1]->getURI(DMBridgeComponent::TemplateEngine)),
00338                DMString::websafe($prev_page_link));
00339          } else {
00340             $pages[] = DMString::websafe($prev_page_link);
00341          }
00342 
00343          if (array_key_exists($i+1, $children)) {
00344             $pages[] = sprintf('<a href="%s">%s</a>',
00345                DMString::websafe(
00346                      $children[$i+1]->getURI(DMBridgeComponent::TemplateEngine)),
00347                DMString::websafe($next_page_link));
00348          } else {
00349             $pages[] = DMString::websafe($next_page_link);
00350          }
00351 
00352          break;
00353       }
00354       return implode($separator, $pages);
00355    }
00356 
00366    public function getHtmlDownloadImageLink(
00367          $resolutions = array(800, 1200, 1600), $separator = " | ") {
00368       asort($resolutions);
00369       $links = array();
00370       foreach ($resolutions as $r) {
00371          $links[] = sprintf('<a class="dmDownloadLink" href="%s">%d</a>',
00372                DMString::websafe($this->getView()->getObject()->getImageURL($r, $r)),
00373                $r);
00374       }
00375       return implode($separator, $links);
00376    }
00377 
00386    public function getHtmlHighlightedFullText() {
00387       $obj = $this->getView()->getObject();
00388       $allow = array(" ", "-");
00389       $term = DMHTTPRequest::getCurrent()->getURI()->getQueryValue("term");
00390       $needle = DMString::paranoid(
00391          strtolower(substr($term, 0, 100)),
00392          $allow);
00393       $haystack = DMString::websafe($obj->getFullText());
00394       return DMString::highlight($needle, $haystack);
00395    }
00396 
00416    public function getHtmlMetadataAsDL(
00417          $ctrld_term_separator = null,
00418          $link_ctrld_terms_to_view = null,
00419          $add_link_to_feed = true,
00420          $omit_title = false,
00421          DMObject $object = null,
00422          $hyperlink_urls = true) {
00423 
00424       $dxml = new DMDOMDocument("1.0", "utf-8");
00425       $dxml->loadXML("<dl/>");
00426       $dxml->documentElement->setAttribute("class", "dmMetadataList");
00427 
00428       if (!$object instanceof DMObject) {
00429          $object = $this->getView()->getObject();
00430       }
00431 
00432       foreach ($object->getMetadata() as $field) {
00433          if (!$field instanceof DMDCElement) {
00434             continue;
00435          }
00436          if ($field->isHidden()) {
00437             continue; // ignore hidden fields
00438          }
00439          if ($field->getNick() == "full") {
00440             continue; // ignore fulltext field
00441          }
00442          if (strlen($field->getValue()) < 1) {
00443             continue; // ignore empty fields
00444          }
00445          if ($omit_title && $field->getNick() == "title") {
00446             continue;
00447          }
00448 
00449          $ul = null;
00450          if ($field->isControlled() && $field->isSearchable()) {
00451             $ul = $dxml->createElement("ul");
00452             $terms = explode("; ", $field->getValue());
00453 
00454             $q = array();
00455             foreach ($terms as $term) {
00456                $li = $dxml->createElement("li");
00457                if ($link_ctrld_terms_to_view !== false) {
00458                   $q = array(
00459                      'CISOBOX1' => rtrim(trim($term), ";"),
00460                      'CISOFIELD1' => 'CISOSEARCHALL',
00461                      'CISOOP1' => 'exact',
00462                      'CISOROOT' => DMString::websafe(
00463                         $object->getCollection()->getAlias()),
00464                      'view' => $link_ctrld_terms_to_view
00465                   );
00466                   $a = $dxml->createElement("a",
00467                      DMString::websafe(ltrim(rtrim($term, ";"))));
00468                   $alias = $object->getCollection()->getAlias();
00469                   $alias = ($alias == "/dmdefault") ? "" : $alias;
00470                   $a->setAttribute("href",
00471                      DMInternalURI::getURIWithParams(
00472                            "objects" . $object->getCollection()->getAlias(),
00473                            $q));
00474                   $li->appendChild($a);
00475                } else {
00476                   $li->nodeValue = DMString::websafe(ltrim(rtrim($term, ";")));
00477                }
00478 
00479                if ($add_link_to_feed) {
00480                   $a = $dxml->createElement("a", "(feed)");
00481                   $a->setAttribute("title",
00482                      "Subscribe to a search for this term");
00483                   $a->setAttribute("class", "dmFeedLink");
00484                   $a->setAttribute("href",
00485                      DMInternalURI::getURIWithParams(
00486                            "objects" . $object->getCollection()->getAlias(),
00487                            $q, "atom"));
00488                   $li->appendChild($a);
00489                }
00490                $ul->appendChild($li);
00491             } // foreach
00492          } // if
00493 
00494          $dxml->documentElement->appendChild(
00495             $dxml->createElement("dt",
00496                   DMString::websafe($field->getName())));
00497 
00498          $dd = $dxml->createElement("dd");
00499          if ($ul instanceof DOMElement) {
00500             $dd->appendChild($ul);
00501          } else {
00502             if ($hyperlink_urls) {
00503                $dd->appendChild(DMString::hyperlink(
00504                      DMString::websafe($field->getValue()), $dxml));
00505             } else {
00506                $dd->nodeValue = DMString::websafe($field->getValue());
00507             }
00508          }
00509          $dxml->documentElement->appendChild($dd);
00510       } // foreach
00511       return $dxml->saveHTML($dxml->documentElement);
00512    }
00513 
00520    public function getHtmlMetadataAsMetaTags() {
00521       $data = array();
00522       foreach ($this->getView()->getObject()->getMetadata() as $field) {
00523          if (!$field instanceof DMDCElement) {
00524             continue;
00525          }
00526          if ($field->isHidden()) {
00527             continue; // ignore hidden fields
00528          }
00529          if ($field->getNick() == "full") {
00530             continue; // ignore fulltext field
00531          }
00532          if (strlen($field->getValue()) < 1) {
00533             continue; // ignore empty fields
00534          }
00535          $data[] = sprintf(
00536             '<meta name="DC.%s" content="%s">',
00537             DMString::websafe($field->getDCName()),
00538             DMString::websafe($field->getValue())
00539          );
00540       }
00541       return implode("\n", $data);
00542    }
00543 
00552    public function getHtmlPreviousNextLinks(
00553          $separator = " | ", $prev_text = "< Previous",
00554          $next_text = "Next >") {
00555       $view = $this->getView();
00556       $prev_view = $view->getViewForPreviousObject();
00557       $next_view = $view->getViewForNextObject();
00558       $prev_url = $prev_view ? $prev_view->getURI() : null;
00559       $next_url = $next_view ? $next_view->getURI() : null;
00560       $results_url = $view->getResultsView()
00561             ? $view->getResultsView()->getURI() : null;
00562       // display appropriate "Back To..." text
00563       $results_text = "Back To Results";
00564       if ($results_url) {
00565          $params = $results_url->getParamComponents();
00566          if (count($params)) {
00567             if ($params[count($params) - 1] == "favorites") {
00568                $results_text = "Back To Favorites";
00569             }
00570          }
00571       }
00572 
00573       $links = array();
00574       if ($prev_url != $results_url && $prev_url) {
00575          $links[] = sprintf('<a href="%s">%s</a>',
00576             DMString::websafe($prev_url), DMString::websafe($prev_text));
00577       }
00578       $links[] = sprintf('<a href="%s">%s</a>',
00579          DMString::websafe($results_url), DMString::websafe($results_text));
00580       if ($next_url != $results_url && $next_url) {
00581          $links[] = sprintf('<a href="%s">%s</a>',
00582             DMString::websafe($next_url), DMString::websafe($next_text));
00583       }
00584       return implode($separator, $links);
00585    }
00586 
00596    public function getHtmlRatingsSection($min, $max, $multiple_of,
00597          $decimal_places = 2) {
00598       if (!DMConfigXML::getInstance()->isRatingEnabled()) {
00599          return "<!-- Rating section not inserted, as rating is
00600             disabled. -->";
00601       }
00602 
00603       $ds = DMDataStoreFactory::getDataStore();
00604       $obj = $this->getView()->getObject();
00605       $obj = ($obj->isChild()) ? $obj->getParent() : $obj;
00606 
00607       $ratings = "";
00608       $num_ratings = $ds->getNumRatingsForObject($obj);
00609       if ($num_ratings) {
00610          $ratings = sprintf('<h4>Rating: %s / %d (%d ratings)</h4>',
00611             round($obj->getRating($max), $decimal_places),
00612             $max,
00613             $num_ratings);
00614       }
00615 
00616       if (DMHTTPRequest::getCurrent()->getSession()->hasRatedObject($obj)) {
00617          return $ratings;
00618       }
00619       $radios = "";
00620       foreach (range($min, $max, $multiple_of) as $i) {
00621          $radios .= sprintf('<input type="radio" name="value"
00622                value="%d" id="dmRatingValue%d" class="dmRatingValue">
00623             <label for="dmRatingValue%d">%d</label>',
00624             $i, $i, $i, $i);
00625       }
00626       return sprintf('<form method="post" action="">
00627             <div style="display:none">
00628                <label for="dmFauxRating">Leave this field blank:</label>
00629                <input type="text" name="fauxrating" id="dmFauxRating">
00630             </div>
00631             %s
00632             <p>%s
00633                <input type="hidden" name="max" value="%d">
00634                <input type="hidden" name="action" value="rate">
00635                <input type="submit" value="Rate It" class="dmRateIt">
00636             </p>
00637          </form>',
00638          $ratings,
00639          $radios,
00640          $max);
00641    }
00642 
00653    public function getHtmlResultsAsUL($thumbs = false, $max = 5) {
00654       $obj = $this->getView()->getObject();
00655       $obj = ($obj->isChild()) ? $obj->getParent() : $obj;
00656 
00657       $dxml = new DMDOMDocument("1.0", "utf-8");
00658       $dxml->loadXML("<ol/>");
00659 
00660       $rv = DMHTTPRequest::getCurrent()->getSession()->getResultsView();
00661       $results = array();
00662       if ($rv) {
00663          $results = $rv->getQuery()->getSearchResults();
00664       }
00665 
00666       // slice results array according to the current object being viewed
00667       $count = count($results);
00668       $current_alias = $current_ptr = null;
00669       for ($i = 0; $i < $count; $i++) {
00670          if ((string) $results[$i] == (string) $obj) {
00671             $current_alias = $results[$i]->getCollection()->getAlias();
00672             $current_ptr = $results[$i]->getPtr();
00673             break;
00674          }
00675       }
00676       $slice = $i - floor($max / 2);
00677       if ($slice < 0) $slice = 0;
00678       $results = array_slice($results, $slice, $max);
00679 
00680       // assemble xhtml
00681       foreach ($results as $obj) {
00682          $a = $dxml->createElement("a");
00683          $a->setAttribute("href",
00684                $obj->getURI(DMBridgeComponent::TemplateEngine));
00685 
00686          if ($thumbs) {
00687             $img = $dxml->createElement("img");
00688             $img->setAttribute("class", "dmThumbnail");
00689             $img->setAttribute("src", $obj->getThumbURL());
00690             $img->setAttribute("alt", "Result thumbnail");
00691             $a->appendChild($img);
00692          }
00693 
00694          $text = $dxml->createTextNode(
00695             $obj->getField("title")->getValue());
00696          $a->appendChild($text);
00697 
00698          $li = $dxml->createElement("li");
00699          if ($obj->getCollection()->getAlias() == $current_alias
00700                && $obj->getPtr() == $current_ptr) {
00701             $li->setAttribute("class", "dmCurrentResult");
00702          }
00703          $li->appendChild($a);
00704 
00705          $dxml->documentElement->appendChild($li);
00706       }
00707       $dxml->formatOutput = true;
00708       return $dxml->saveHTML($dxml->documentElement);
00709    }
00710 
00716    public function getHtmlTaggingSection() {
00717       if (!DMConfigXML::getInstance()->isTaggingEnabled()) {
00718          return "<!-- Tagging section not inserted, as tagging is
00719             disabled. -->";
00720       }
00721 
00722       $obj = $this->getView()->getObject();
00723       $this->getView()->getTagForm()->getFieldByName("alias")
00724             ->setValue($obj->getCollection()->getAlias());
00725       $this->getView()->getTagForm()->getFieldByName("ptr")
00726             ->setValue($obj->getPtr());
00727 
00728       return sprintf('<form method="post" action="">
00729             %s%s%s%s
00730             <input type="hidden" name="action" value="tag">
00731             <input type="submit" value="Add Tag">
00732          </form>',
00733          $this->getView()->getTagForm()
00734             ->getFieldByName("value")->getHTMLTag(),
00735          $this->getView()->getTagForm()
00736             ->getFieldByName("alias")->getHTMLTag(),
00737          $this->getView()->getTagForm()
00738             ->getFieldByName("ptr")->getHTMLTag(),
00739          $this->getView()->getTagForm()
00740             ->getFieldByName("fauxtag")->getHTMLTag(),
00741          DMString::websafe(DMHTTPRequest::getCurrent()->getURI()->getParams()));
00742    }
00743 
00753    public function getHtmlViewer($width = null, $height = null, $js = true) {
00754       $obj = $this->getView()->getObject();
00755       if ($obj->isCompound() && $obj->getChild(0) instanceof DMObject) {
00756          $obj = $obj->getChild(0);
00757       }
00758       $viewer = $obj->getViewer($width, $height);
00759       if (!$viewer) {
00760          return sprintf('<p class="dmError">Unable to render the object
00761             viewer; viewer class %s does not exist.</p>',
00762             $obj->getViewerClassName());
00763       }
00764       return $js ? $viewer->getHTMLTag() : $viewer->getHTMLTagNoJS();
00765    }
00766 
00767 }
00768 
 All Data Structures Functions Variables