dmBridge PHP API
DMResultsTemplateHelper.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 DMResultsTemplateHelper extends DMGenericTemplateHelper
00020 implements DMTemplateHelper {
00021 
00034    public function getHtmlFacetsAsUL($term_cutoff = 0) {
00035       $query = $this->getView()->getQuery();
00036       if (!$query || count($query->getFacetTerms()) < 1) {
00037          return "";
00038       }
00039 
00040       $dxml = new DMDOMDocument("1.0", "utf-8");
00041       $dxml->loadXML("<ul/>");
00042       $dxml->documentElement->setAttribute("class", "dmFacetList");
00043 
00044       // preprocess the facet terms structure into something easier to get
00045       // into a DOM tree
00046       $facets = array();
00047       foreach ($query->getFacetTerms() as $term) {
00048          $facets[$term->getField()->getName()][] = array(
00049             'value' => $term->getField()->getValue(),
00050             'count' => $term->getCount(),
00051             'nick' => $term->getField()->getNick(),
00052             'uri' => (string) $term->getURI()
00053          );
00054       }
00055 
00056       foreach ($facets as $name => $terms) {
00057          $fnode = $dxml->createElement("ul");
00058          $fnode->setAttribute("class", "dmFacet");
00059 
00060          for ($i = 0; $i < count($terms); $i++) {
00061             if ($term_cutoff > 0 && $i > $term_cutoff) {
00062                break;
00063             }
00064             $uri = DMHTTPRequest::getCurrent()->getURI();
00065             // skip any facets that are already part of the search query
00066             foreach ($query->getPredicates() as $st) {
00067                if (DMString::paranoid($terms[$i]['value'])
00068                      == DMString::paranoid($st->getString())) {
00069                   continue(2);
00070                }
00071             }
00072 
00073             // assemble target href
00074             for ($j = 1; $j < 5; $j++) {
00075                if (!$uri->getQueryValue("CISOBOX" . $j)) {
00076                   if ($j <= count($terms) && $terms[$i]) {
00077                      $uri->addQueryValue("CISOBOX" . $j,
00078                            DMString::paranoid(
00079                               $terms[$i]['value'],
00080                               array(' ', '-')));
00081                      $uri->addQueryValue("CISOFIELD" . $j,
00082                            $terms[$i]['nick']);
00083                      $uri->addQueryValue('CISOOP' . $j, "all");
00084                      break;
00085                   }
00086                }
00087             }
00088 
00089             // assemble html elements
00090             $term = $dxml->createElement("a",
00091                   DMString::websafe($terms[$i]['value']));
00092             $term->setAttribute("href", $uri);
00093             $count = $dxml->createElement(
00094                "span", sprintf("(%d)", $terms[$i]['count']));
00095             $count->setAttribute("class", "dmFacetTermCount");
00096             $li = $dxml->createElement("li");
00097             $li->setAttribute("class", "dmFacetTerm");
00098             $li->appendChild($term);
00099             $li->appendChild($dxml->createTextNode(" "));
00100             $li->appendChild($count);
00101             $fnode->appendChild($li);
00102          }
00103 
00104          if ($fnode->childNodes->length) {
00105             $flnode = $dxml->createElement('li', DMString::websafe($name));
00106             $flnode->setAttribute('class', 'dmFacetTitle');
00107             $dxml->documentElement->appendChild($flnode);
00108             $dxml->documentElement->appendChild($fnode);
00109          }
00110 
00111       }
00112       if ($dxml->documentElement->childNodes->length) {
00113          return $dxml->saveHTML($dxml->documentElement);
00114       }
00115       return "";
00116    }
00117 
00125    public function getHtmlLinkedSuggestion() {
00126       $query = $this->getView()->getQuery();
00127       if ($query->getSuggestion()) {
00128          $term = $query->getSuggestion();
00129          $qt = new DMQueryPredicate();
00130          $qt->setString($query->getSuggestion());
00131          $qt->setField(new DMDCElement("all"));
00132          $qt->setMode("all");
00133          $query->setPredicates(array($qt));
00134          return $this->getHtmlTermLinkedToSearch($term, $query);
00135       }
00136       return null;
00137    }
00138 
00153    public function getHtmlPageLinks(
00154          $max_page_links = 10, $page_list_separator = ' ',
00155          $current_page_element = 'strong',
00156          $first_symbol = '<<', $prev_symbol = '<',
00157          $next_symbol = '>', $last_symbol = '>>') {
00158 
00159       // we are going to build the URI by cloning our current DMObjectQuery,
00160       // modifying it appropriately, and inserting the return value of its
00161       // getURI() method within the page number <a> tags.
00162       $query = $this->getView()->getQuery();
00163       $query_copy = clone $query;
00164       
00165       $current_uri = DMHTTPRequest::getCurrent()->getURI();
00166 
00167       $ts = $this->getView()->getTemplate()->getTemplateSet();
00168 
00169       $current_page_open_tag = sprintf('<%s>',
00170          trim($current_page_element, '<>'));
00171       $current_page_close_tag = sprintf('</%s>',
00172          trim($current_page_element, '<>'));
00173 
00174       $num_pages = $query->getNumPages();
00175 
00176       if ($num_pages < 1
00177             || $this->getView() instanceof DMFavoritesView) {
00178          return $current_page_open_tag . "1" . $current_page_close_tag;
00179       }
00180 
00181       $page_links = array();
00182       // "first" link, before all other links
00183       if ($query->getPage() > 1) {
00184          $query_copy->setPage(1);
00185          $page_links[] = sprintf('<a href="%s">%s</a>',
00186             $query_copy->getURI(DMBridgeComponent::TemplateEngine, "html",
00187                $ts, $current_uri),
00188                $first_symbol);
00189       } else {
00190          $page_links[0] = $first_symbol;
00191       }
00192 
00193       // "previous" link, after "first" link, before page # links
00194       if ($query->getPage() > 1) {
00195          $query_copy->setPage($query->getPage() - 1);
00196          $page_links[] = sprintf('<a href="%s">%s</a>',
00197             $query_copy->getURI(DMBridgeComponent::TemplateEngine, "html",
00198                $ts, $current_uri),
00199                $prev_symbol);
00200       } else {
00201          $page_links[] = $prev_symbol;
00202       }
00203 
00204       // enforce $max_page_links
00205       $startpage = 1;
00206       $stoppage = $query->getNumPages();
00207       if ($query->getNumPages() > $max_page_links) {
00208          $startpage = $query->getPage() - floor($max_page_links / 2);
00209          if ($startpage < 1) {
00210             $startpage = 1;
00211          }
00212          $stoppage = $query->getPage() + floor($max_page_links / 2);
00213          if ($stoppage - $startpage < $max_page_links) {
00214             $stoppage = $startpage + $max_page_links;
00215          }
00216          if ($stoppage > $query->getNumPages()) {
00217             $startpage = $query->getNumPages()
00218                   - $max_page_links;
00219             $stoppage = $query->getNumPages();
00220          }
00221       }
00222 
00223       // build page number links
00224       for ($page = $startpage; $page <= $stoppage; $page++) {
00225          if ($page == $query->getPage()) {
00226             $page_links[] = $page;
00227          } else if ($page == 1 || $page <> $query->getPage()) {
00228             $query_copy->setPage($page);
00229             $page_links[] = sprintf('<a href="%s">%s</a>',
00230                $query_copy->getURI(DMBridgeComponent::TemplateEngine,
00231                   "html", $ts, $current_uri),
00232                   $page);
00233          } else {
00234             $page_links[] = $current_page_open_tag
00235                . $page . $current_page_close_tag;
00236          }
00237       }
00238 
00239       // "next" link, after page # links, before "last" link
00240       if ($query->getPage() < $query->getNumPages()) {
00241          $query_copy->setPage($query->getPage() + 1);
00242          $page_links[] = sprintf('<a href="%s">%s</a>',
00243             $query_copy->getURI(DMBridgeComponent::TemplateEngine, "html",
00244                $ts, $current_uri),
00245                $next_symbol);
00246       } else {
00247          $page_links[] = $next_symbol;
00248       }
00249 
00250       // "last" link, after all other links
00251       if ($query->getPage() < $query->getNumPages()) {
00252          $query_copy->setPage($query->getNumPages());
00253          $page_links[] = sprintf('<a href="%s">%s</a>',
00254             $query_copy->getURI(DMBridgeComponent::TemplateEngine, "html", $ts,
00255                $current_uri),
00256                $last_symbol);
00257       } else {
00258          $page_links[] = $last_symbol;
00259       }
00260       return implode($page_list_separator, $page_links);
00261    }
00262 
00272    public function getHtmlResults($view = "grid") {
00273       $uri = DMHTTPRequest::getCurrent()->getURI();
00274 
00275       $valid_views = array('grid', 'list', 'tile');
00276       if (in_array($uri->getQueryValue("view"), $valid_views)) {
00277          $view = $uri->getQueryValue("view");
00278       }
00279 
00280       switch ($view) {
00281       case 'tile':
00282          return $this->getHtmlResultsAsTiles();
00283          break;
00284       case 'list':
00285          return $this->getHtmlResultsAsList();
00286          break;
00287       default:
00288          return $this->getHtmlResultsAsGrid();
00289          break;
00290       }
00291    }
00292 
00296    public function getHtmlResultsAsGrid() {
00297       $query = $this->getView()->getQuery();
00298       $objects = $query->getSearchResults();
00299       $view = $this->getView();
00300       $fields = $view->getFields();
00301       $count = count($objects);
00302 
00303       if ($count < 1) {
00304          return null;
00305       }
00306 
00307       $dxml = $this->getResultsForm();
00308 
00309       $table = $dxml->documentElement->childNodes->item(0);
00310       $table->setAttribute("cellspacing", 0);
00311       $table->setAttribute("cellpadding", 0);
00312       $table->setAttribute("border", 0);
00313       $table->setAttribute("class", "dmResults dmGridResults");
00314 
00315       // header row
00316       $tr = $dxml->createElement("tr");
00317       $th = $dxml->createElement("th", "&nbsp;");
00318       $tr->appendChild($th);
00319       foreach ($fields as $f) {
00320          // IE allegedly needs the nbsps
00321          $th = $dxml->createElement("th",
00322             ($f->getName()) ? DMString::websafe($f->getName()) : "&nbsp;");
00323          $tr->appendChild($th);
00324       }
00325       $table->appendChild($tr);
00326 
00327       // rows
00328       for ($i = 0; $i < $count; $i++) {
00329          $tr = $dxml->createElement("tr");
00330          if (!($i & 1)) {
00331             $tr->setAttribute("class", "odd");
00332          }
00333          $td = $dxml->createElement("td");
00334          $input = $dxml->createElement("input");
00335          $input->setAttribute("type", "checkbox");
00336          $input->setAttribute("name", "data[]");
00337          $input->setAttribute("value",
00338             DMString::websafe($objects[$i]->getCollection()->getAlias()
00339             . DMAbstractForm::ALIAS_PTR_SEPARATOR . $objects[$i]->getPtr()));
00340          if ($objects[$i]->isFavorite()
00341                && !$this instanceof DMFavoritesTemplateHelper) {
00342             $input->setAttribute("disabled", "disabled");
00343          }
00344          $td->appendChild($input);
00345          $tr->appendChild($td);
00346 
00347          $url = $objects[$i]->getURI();
00348 
00349          // columns
00350          foreach ($fields as $f) {
00351             if ($f->getNick() == "index") { // tack on index
00352                $td = $dxml->createElement("td",
00353                   ($query->getPage() - 1) * $query->getNumResultsPerPage()
00354                      + $i + 1);
00355                $tr->appendChild($td);
00356             } else if ($f->getNick() == "thumb") { // tack on thumbnail
00357                $td = $dxml->createElement("td");
00358                $a = $dxml->createElement("a");
00359                $a->setAttribute("href", $url);
00360 
00361                $img = $dxml->createElement("img");
00362                $img->setAttribute("src", $objects[$i]->getThumbURL());
00363                $img->setAttribute("class", "dmThumbnail");
00364                $img->setAttribute("alt", "Object thumbnail");
00365 
00366                $a->appendChild($img);
00367                $td->appendChild($a);
00368 
00369                $div = $dxml->createElement("div");
00370                $div->setAttribute("class", "dmResultIcons");
00371                $td->appendChild($div);
00372                $this->appendIconSpanElementsToParent($objects[$i], $dxml, $div);
00373 
00374                $tr->appendChild($td);
00375             } else {
00376                $td = $dxml->createElement("td");
00377                $td->setAttribute("class",
00378                   sprintf("dmGrid%sField", ucfirst($f->getNick())));
00379                $tmp = $objects[$i]->getMetadata($f->getNick());
00380                if ($tmp) {
00381                   $value = rtrim($tmp->getValue(), "/");
00382                   // truncate field, if maxwords specified
00383                   if ($f->getMaxWords() > 0) {
00384                      $value = DMString::truncate($value, $f->getMaxWords());
00385                   }
00386                   if ($f->getNick() == "title") {
00387                      $a = $dxml->createElement("a", DMString::websafe($value));
00388                      $a->setAttribute("href", $url);
00389                      $td->appendChild($a);
00390                   } else {
00391                      $td->nodeValue = DMString::websafe($value);
00392                   }
00393                }
00394             }
00395             $tr->appendChild($td);
00396          } // foreach
00397          $table->appendChild($tr);
00398       } // for
00399 
00400       $dxml->formatOutput = true;
00401       return $dxml->saveHTML($dxml->documentElement);
00402    }
00403 
00407    public function getHtmlResultsAsList() {
00408       $view = $this->getView();
00409       $query = $view->getQuery();
00410       $objects = $query->getSearchResults();
00411       $fields = $view->getFields();
00412       $count = count($objects);
00413 
00414       if ($count < 1) {
00415          return null;
00416       }
00417 
00418       $dxml = $this->getResultsForm();
00419 
00420       $table = $dxml->documentElement->childNodes->item(0);
00421       $table->setAttribute("cellspacing", 0);
00422       $table->setAttribute("cellpadding", 0);
00423       $table->setAttribute("border", 0);
00424       $table->setAttribute("class", "dmResults dmListResults");
00425 
00426       // object rows
00427       for ($i = 0; $i < $count; $i++) {
00428          $tr = $dxml->createElement("tr");
00429          if (!($i & 1)) {
00430             $tr->setAttribute("class", "odd");
00431          }
00432          $td = $dxml->createElement("td");
00433          $input = $dxml->createElement("input");
00434          $input->setAttribute("type", "checkbox");
00435          $input->setAttribute("name", "data[]");
00436          $input->setAttribute("value",
00437             DMString::websafe($objects[$i]->getCollection()->getAlias()
00438                   . DMAbstractForm::ALIAS_PTR_SEPARATOR
00439                   . $objects[$i]->getPtr()));
00440          if ($objects[$i]->isFavorite()
00441                && !$this instanceof DMFavoritesTemplateHelper) {
00442             $input->setAttribute("disabled", "disabled");
00443          }
00444          $td->appendChild($input);
00445          $tr->appendChild($td);
00446 
00447          $url = $objects[$i]->getURI();
00448 
00449          // tack on index & thumbnail
00450          foreach ($fields as $f) {
00451             if ($f->getNick() == "index") {
00452                $td = $dxml->createElement("td",
00453                   ($query->getPage() - 1) * $query->getNumResultsPerPage()
00454                      + $i + 1);
00455                $tr->appendChild($td);
00456             } else if ($f->getNick() == "thumb") {
00457                $td = $dxml->createElement("td");
00458                $a = $dxml->createElement("a");
00459                $a->setAttribute("href", $url);
00460                $img = $dxml->createElement("img");
00461                $img->setAttribute("src", $objects[$i]->getThumbURL());
00462                $img->setAttribute("class", "dmThumbnail");
00463                $img->setAttribute("alt", "Object thumbnail");
00464                $a->appendChild($img);
00465                $td->appendChild($a);
00466                $tr->appendChild($td);
00467             }
00468          }
00469 
00470          $td = $dxml->createElement("td");
00471          $mtable = $dxml->createElement("table");
00472          $mtable->setAttribute("class", "dmListResultMetadata");
00473          $mtable->setAttribute("cellpadding", 0);
00474          $mtable->setAttribute("cellspacing", 2);
00475          $mtable->setAttribute("border", 0);
00476 
00477          // append icon spans
00478          $mtr = $dxml->createElement("tr");
00479          $mtd = $dxml->createElement("td");
00480          $this->appendIconSpanElementsToParent($objects[$i], $dxml, $mtd);
00481          $mtr->appendChild($mtd);
00482          $mtable->appendChild($mtr);
00483 
00484          // fill in fields
00485          $j = 0;
00486          foreach ($fields as $f) {
00487             if ($f->getNick() == "index" || $f->getNick() == "thumb") {
00488                continue;
00489             }
00490 
00491             $mtr = $dxml->createElement("tr");
00492             if (!($j & 1)) {
00493                $mtr->setAttribute("class", "odd");
00494             }
00495 
00496             $mth = $dxml->createElement("th",
00497                DMString::websafe($f->getName()));
00498             $mth->setAttribute("class",
00499                sprintf("dmListFieldName dmList%sFieldName",
00500                   DMString::websafe(ucfirst($f->getNick()))));
00501 
00502             $mtd = $dxml->createElement("td");
00503             $mtd->setAttribute("class",
00504                sprintf("dmListFieldValue dmList%sFieldValue",
00505                   DMString::websafe(ucfirst($f->getNick()))));
00506 
00507             $tmp = $objects[$i]->getMetadata($f->getNick());
00508 
00509             $value = ($tmp instanceof DMDCElement) ? $tmp->getValue() : "";
00510             // truncate field, if maxwords specified
00511             if ($f->getMaxWords() > 0) {
00512                $value = DMString::truncate($value, $f->getMaxWords());
00513             }
00514             if ($f->getNick() == "title") {
00515                $a = $dxml->createElement("a", DMString::websafe($value));
00516                $a->setAttribute("href", $url);
00517                $mtd->appendChild($a);
00518             } else {
00519                $mtd->nodeValue = DMString::websafe($value);
00520             }
00521             $mtr->appendChild($mth);
00522             $mtr->appendChild($mtd);
00523             $mtable->appendChild($mtr);
00524             $j++;
00525          } // foreach
00526          $td->appendChild($mtable);
00527          $tr->appendChild($td);
00528          $table->appendChild($tr);
00529       } // for
00530 
00531       $dxml->formatOutput = true;
00532       return $dxml->saveHTML($dxml->documentElement);
00533    }
00534 
00538    public function getHtmlResultsAsTiles() {
00539       $view = $this->getView();
00540       $query = $view->getQuery();
00541       $objects = $query->getSearchResults();
00542       $count = count($objects);
00543       $num_columns = $view->getNumTileViewColumns()
00544             ? $view->getNumTileViewColumns() : 3;
00545 
00546       if ($count < 1) {
00547          return null;
00548       }
00549 
00550       $dxml = $this->getResultsForm();
00551 
00552       $table = $dxml->documentElement->childNodes->item(0);
00553       $table->setAttribute("cellspacing", 0);
00554       $table->setAttribute("cellpadding", 0);
00555       $table->setAttribute("border", 0);
00556       $table->setAttribute("class", "dmResults dmTileResults");
00557 
00558       $tr = $dxml->createElement("tr");
00559       $j = 0;
00560       for ($i = 0; $i < $count; $i++) {
00561          if ($i % $num_columns == 0 && $i != 0) {
00562             $j++;
00563             $tr = $dxml->createElement("tr");
00564          }
00565          if (!($j & 1)) {
00566             $tr->setAttribute("class", "odd");
00567          }
00568 
00569          $td = $dxml->createElement("td");
00570          $url = $objects[$i]->getURI();
00571 
00572          // thumbnail
00573          $a = $dxml->createElement("a");
00574          $a->setAttribute("href", $url);
00575          $img = $dxml->createElement("img");
00576          $img->setAttribute("src", $objects[$i]->getThumbURL());
00577          $img->setAttribute("class", "dmThumbnail");
00578          $img->setAttribute("alt", "Object thumbnail");
00579          $a->appendChild($img);
00580          $td->appendChild($a);
00581          $br = $dxml->createElement("br");
00582          $td->appendChild($br);
00583 
00584          // span icons
00585          $div = $dxml->createElement("div");
00586          $div->setAttribute("class", "dmResultIcons");
00587          $td->appendChild($div);
00588          $this->appendIconSpanElementsToParent($objects[$i], $dxml, $div);
00589 
00590          // checkbox
00591          $input = $dxml->createElement("input");
00592          $input->setAttribute("type", "checkbox");
00593          $input->setAttribute("name", "data[]");
00594          $input->setAttribute("value",
00595             DMString::websafe($objects[$i]->getCollection()->getAlias()
00596                . DMAbstractForm::ALIAS_PTR_SEPARATOR . $objects[$i]->getPtr()));
00597          if ($objects[$i]->isFavorite()
00598                && !$this instanceof DMFavoritesTemplateHelper) {
00599             $input->setAttribute("disabled", "disabled");
00600          }
00601          $td->appendChild($input);
00602 
00603          // index
00604          $idx = $dxml->createElement("span",
00605             (($query->getPage() - 1)
00606                * $query->getNumResultsPerPage() + $i + 1) . ". ");
00607          $idx->setAttribute("class", "dmIndex");
00608          $td->appendChild($idx);
00609 
00610          // title
00611          $field = $objects[$i]->getMetadata("title");
00612          $value = $field->getValue();
00613          // truncate field, if maxwords specified
00614          if ($field->getMaxWords() > 0) {
00615             $value = DMString::truncate($value, $field->getMaxWords());
00616          }
00617          $a = $dxml->createElement("a", DMString::websafe($value));
00618          $a->setAttribute("href", $url);
00619          $td->appendChild($a);
00620 
00621          $tr->appendChild($td);
00622          if ($i % $num_columns == 0) {
00623             $table->appendChild($tr);
00624          }
00625       } // for
00626 
00627       $dxml->formatOutput = true;
00628       return $dxml->saveHTML($dxml->documentElement);
00629    }
00630 
00631    private function appendIconSpanElementsToParent(DMObject $obj,
00632          DOMDocument $dxml, DOMNode $parent) {
00633       // metadata icon
00634       $a = $dxml->createElement("a");
00635       $a->setAttribute("href", DMString::xmlentities(
00636             $obj->getURI(DMBridgeComponent::TemplateEngine, null, false)));
00637       $span = $dxml->createElement("span");
00638       $span->setAttribute("class", "dmResultIcon dmResultViewMetadata");
00639       $span->setAttribute("title", "View object metadata");
00640       $a->appendChild($span);
00641       $parent->appendChild($a);
00642 
00643       // URL-type object icon
00644       $span = $dxml->createElement("span");
00645       if ($obj->isURLType()) {
00646          $a = $dxml->createElement("a");
00647          $a->setAttribute("href", DMString::xmlentities($obj->getFileURL()));
00648          $span->setAttribute("class", "dmResultIsURL");
00649          $span->setAttribute("title", "This is a pointer to another object");
00650          $a->appendChild($span);
00651          $parent->appendChild($a);
00652       } else {
00653          $span->setAttribute("class", "dmResultIsNotURL");
00654          $span->setAttribute("title", "This object does not redirect anywhere");
00655          $parent->appendChild($span);
00656       }
00657 
00658       // favorite icon
00659       $span = $dxml->createElement("span");
00660       if ($obj->isFavorite()) {
00661          $span->setAttribute("class", "dmResultIsFavorite");
00662          $span->setAttribute("title", "This object is in your favorites");
00663       } else {
00664          $span->setAttribute("class", "dmResultIsNotFavorite");
00665          $span->setAttribute("title", "This object is not in your favorites");
00666       }
00667       $parent->appendChild($span);
00668 
00669       // comments icon
00670       $span = $dxml->createElement("span");
00671       if ($obj->hasComments()) {
00672          $span->setAttribute("class", "dmResultHasComments");
00673          $span->setAttribute("title", "This object has comments");
00674       } else {
00675          $span->setAttribute("class", "dmResultDoesNotHaveComments");
00676          $span->setAttribute("title", "This object has no comments");
00677       }
00678       $parent->appendChild($span);
00679 
00680       // rating icon
00681       $span = $dxml->createElement("span");
00682       if ($obj->getRating()) {
00683          $span->setAttribute("class", "dmResultHasBeenRated");
00684          $span->setAttribute("title", "This object has been rated");
00685       } else {
00686          $span->setAttribute("class", "dmResultHasNotBeenRated");
00687          $span->setAttribute("title", "This object has not yet been rated");
00688       }
00689       $parent->appendChild($span);
00690    }
00691 
00696    public function getHtmlSearchTerms() {
00697       $mode_labels = array(
00698          'all' => 'all of',
00699          'exact' => 'exactly',
00700          'any' => 'any of',
00701          'none' => 'none of'
00702       );
00703       $formatted = array();
00704       foreach ($this->getView()->getQuery()->getPredicates() as $t) {
00705          if ($t->getField()->getNick() == 'date') {
00706             $tmp = explode('-', $t->getString());
00707             if (count($tmp) > 1) { // "before", "after", or "from" search
00708                if ($tmp[0] == '00000000' && $tmp[1] == '99999999') {
00709                   $formatted[] = 'any date';
00710                } else if ($tmp[0] == '00000000') {
00711                   $dt = new DMDateTime($tmp[1]);
00712                   $formatted[] = 'before ' . $dt->format('Y-m-d');
00713                } else if ($tmp[1] == '99999999') {
00714                   $dt = new DMDateTime($tmp[0]);
00715                   $formatted[] = 'after ' . $dt->format('Y-m-d');
00716                } else {
00717                   $from = new DMDateTime($tmp[0]);
00718                   $to = new DMDateTime($tmp[1]);
00719                   $formatted[] = $from->format('Y-m-d') . ' to '
00720                      . $to->format('Y-m-d');
00721                }
00722             } else { // "on" search
00723                $dt = new DMDateTime($tmp[0]);
00724                $formatted[] = 'exactly '. $dt->format('Y-m-d');
00725             }
00726          } else $formatted[] = $mode_labels[$t->getMode()]
00727             . ' "' . $t->getString() . '"';
00728       }
00729       return DMString::websafe(implode(', ', $formatted));
00730    }
00731 
00739    public function getHtmlSortLinksAsUL($current_sort_tag = 'strong') {
00740       // if we are on the favorites page, nothing is sortable because the
00741       // favorites are being pulled from cookies, not a search engine, and
00742       // we are too lazy to write in favorites sorting
00743       if ($this instanceof DMFavoritesTemplateHelper) {
00744          return;
00745       }
00746 
00747       $query = array();
00748       $dxml = new DMDOMDocument('1.0', 'utf-8');
00749       $dxml->loadXML('<ul/>');
00750       $dxml->documentElement->setAttribute('class', 'dmSortLinks');
00751 
00752       foreach ($this->getView()->getCollection()->getFields() as $f) {
00753          if (!$f->isSortable()) {
00754             continue;
00755          }
00756          $query['sort'] = $f->getNick();
00757          $li = $dxml->createElement('li');
00758          $sort = DMHTTPRequest::getCurrent()->getURI()->getQueryValue("sort");
00759 
00760          if ($f->isDefaultSort() && strlen($sort) < 1) {
00761             $a = $dxml->createElement('a');
00762             $inner = $dxml->createElement(
00763                $current_sort_tag,
00764                DMString::websafe($f->getName()));
00765             $a->setAttribute('href', DMInternalURI::getURIWithParams(null, $query));
00766             $a->appendChild($inner);
00767             $li->appendChild($a);
00768          } else if ($f->getNick() == $sort) {
00769             $value = $dxml->createElement(
00770                $current_sort_tag,
00771                DMString::websafe($f->getName()));
00772             $li->appendChild($value);
00773          } else if (strlen($sort) < 1) {
00774             $a = $dxml->createElement('a', DMString::xmlentities($f->getName()));
00775             $a->setAttribute('href', DMInternalURI::getURIWithParams(null, $query));
00776             $li->appendChild($a);
00777          } else {
00778             $a = $dxml->createElement('a', DMString::xmlentities($f->getName()));
00779             $a->setAttribute('href', DMInternalURI::getURIWithParams(null, $query));
00780             $li->appendChild($a);
00781          }
00782          $dxml->documentElement->appendChild($li);
00783       }
00784       if ($dxml->documentElement->childNodes->length < 2) {
00785          return "<!-- Not enough sortable fields -->";
00786       }
00787       $dxml->formatOutput = true;
00788       return $dxml->saveHTML($dxml->documentElement);
00789    }
00790 
00805    public function getHtmlViewLinks($default_view = "grid",
00806          $separator = " | ",
00807          array $views = array()) {
00808       if (count($views) < 1) {
00809          $views = array(
00810             array(
00811                'name' => 'Grid',
00812                'url' => 'grid'
00813             ),
00814             array(
00815                'name' => 'List',
00816                'url' => 'list'
00817             ),
00818             array(
00819                'name' => 'Tile',
00820                'url' => 'tile'
00821             )
00822          );
00823       }
00824 
00825       $req = DMHTTPRequest::getCurrent();
00826       $view_links = array();
00827 
00828       $current_view = $req->getURI()->getQueryValue("view");
00829       $current_view = $current_view ? $current_view : $default_view;
00830 
00831       foreach ($views as $view) {
00832          $uri = clone $req->getURI();
00833          $uri->unsetQueryKey("view");
00834          // assign CSS classes to the views
00835          $view['css'] = sprintf("dm%sViewLink",
00836             ucwords(str_replace(" ", "", $view['name'])));
00837 
00838          if ($view['url'] == $current_view) {
00839             $view_links[] = sprintf('<span class="%s">%s</span>',
00840                $view['css'], $view['name']);
00841          } else {
00842             if ($default_view && $view['url'] != $default_view) {
00843                $uri->setQueryValue("view", $view['url']);
00844             }
00845             $view_links[] = sprintf('<a href="%s" class="%s">%s</a>',
00846                $uri, $view['css'], $view['name']);
00847          }
00848       }
00849       return implode($separator, $view_links);
00850    }
00851 
00855    protected function getResultsForm() {
00856       $alias = $this->getView()->getCollection()->getAlias();
00857       $alias = ($alias == "/dmdefault") ? "" : $alias;
00858 
00859       $dxml = new DMDOMDocument('1.0', 'utf-8');
00860       $dxml->loadXML('<form/>');
00861       $dxml->documentElement->setAttribute('class', 'dmResults');
00862       $table = $dxml->createElement('table');
00863       $dxml->documentElement->appendChild($table);
00864 
00865       $params = $alias ? "objects" . $alias . "/favorites" : "objects/favorites";
00866       $dxml->documentElement->setAttribute("action",
00867             DMInternalURI::getURIWithParams($params));
00868       $dxml->documentElement->setAttribute("method", "post");
00869 
00870       $input_div = $dxml->createElement('div');
00871 
00872       $input = $dxml->createElement('input');
00873       $input->setAttribute('type', 'hidden');
00874       $input->setAttribute('name', 'params');
00875       $input->setAttribute('value',
00876             sprintf("objects%s/favorites", rtrim($alias, "/")));
00877       $input_div->appendChild($input);
00878 
00879       $input = $dxml->createElement('input');
00880       $input->setAttribute('type', 'submit');
00881       $input->setAttribute('class', 'dmAddFavorites');
00882       $input->setAttribute('value', 'Add Checked To Favorites');
00883       $input_div->appendChild($input);
00884 
00885       $dxml->documentElement->appendChild($input_div);
00886       return $dxml;
00887    }
00888 
00889 }
 All Data Structures Functions Variables