dmBridge PHP API
DMSearchTemplateHelper.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 DMSearchTemplateHelper extends DMGenericTemplateHelper
00020 implements DMTemplateHelper {
00021 
00022    public function helperWillRender() {
00023       $this->addStylesheetTag(
00024             dirname($_SERVER['PHP_SELF']) . "/includes/css/search_view.css");
00025       $this->addBodyScriptTag(
00026             "http://yui.yahooapis.com/3.3.0/build/yui/yui-min.js");
00027       $this->addBodyScriptTag(
00028             dirname($_SERVER['PHP_SELF']) . "/includes/js/search_view.js");
00029    }
00030 
00036    public function getHtmlDateSearch(array $get_parameters = array()) {
00037       $selected_year1 = $selected_year2 = $selected_month1 = $selected_month2
00038          = $selected_day1 = $selected_day2 = $earliest_year = $latest_year
00039          = $year1_options = $month1_options = $day1_options
00040          = $year2_options = $month2_options = $day2_options = $mode = $string
00041          = $field = $date_qualifier = null;
00042       $collection = $this->getView()->getCollection();
00043       $earliest_year = $collection->getDateSearchBeginYear();
00044       $latest_year = $collection->getDateSearchEndYear();
00045 
00046       // retrieve saved search input
00047       $input = DMHTTPRequest::getCurrent()->getSession()->getSearchInput();
00048       if ($input instanceof DMInput) {
00049          $date_qualifier = $input->getDateQualifier();
00050          if ($input->getPredicate(0) instanceof DMQueryPredicate) {
00051             if (!$input->getPredicate(0)->isProximity()) {
00052                $field = $input->getPredicate(0)->getField()->getNick();
00053                $mode = $input->getPredicate(0)->getMode();
00054                $string = $input->getPredicate(0)->getString();
00055             }
00056             if ($input->getBeginDate() instanceof DMDateTime) {
00057                $selected_year1 = $input->getBeginDate()->format('Y');
00058                $selected_month1 = $input->getBeginDate()->format('n');
00059                $selected_day1 = $input->getBeginDate()->format('j');
00060             }
00061             if ($input->getEndDate() instanceof DMDateTime) {
00062                $selected_year2 = $input->getEndDate()->format('Y');
00063                $selected_month2 = $input->getEndDate()->format('n');
00064                $selected_day2 = $input->getEndDate()->format('j');
00065             }
00066          }
00067       }
00068       $selected_year1 = ($selected_year1) ? $selected_year1 : $earliest_year;
00069       $selected_year2 = ($selected_year2) ? $selected_year2 : $latest_year;
00070 
00071       // assemble year1 <option> elements
00072       foreach (range($earliest_year, $latest_year, 1) as $y) {
00073          $year1_options .= sprintf('<option value="%d" %s>%d</option>',
00074             $y,
00075             ($y == $selected_year1) ? 'selected="selected"' : '',
00076             $y
00077          );
00078       }
00079 
00080       // assemble month1 <option> elements
00081       foreach (range(1, 12) as $m) {
00082          $month1_options .= sprintf('<option value="%d" %s>%s</option>',
00083             $m,
00084             ($m == $selected_month1) ? 'selected="selected"' : '',
00085             date('F', mktime(1, 1, 1, $m, 5))
00086          );
00087       }
00088 
00089       // assemble day1 <option> elements
00090       foreach (range(1, 31) as $d) {
00091          $day1_options .= sprintf('<option value="%d" %s>%d</option>',
00092             $d,
00093             ($d == $selected_day1) ? 'selected="selected"' : '',
00094             $d
00095          );
00096       }
00097 
00098       // assemble year2 <option> elements
00099       foreach (range($earliest_year, $latest_year, 1) as $y) {
00100          $year2_options .= sprintf('<option value="%d" %s>%d</option>',
00101             $y,
00102             ($y == $selected_year2) ? 'selected="selected"' : '',
00103             $y
00104          );
00105       }
00106 
00107       // assemble month2 <option> elements
00108       foreach (range(1, 12) as $m) {
00109          $month2_options .= sprintf('<option value="%d" %s>%s</option>',
00110             $m,
00111             ($m == $selected_month2) ? 'selected="selected"' : '',
00112             date('F', mktime(1, 1, 1, $m, 5))
00113          );
00114       }
00115 
00116       // assemble day2 <option> elements
00117       foreach (range(1, 31) as $d) {
00118          $day2_options .= sprintf('<option value="%d" %s>%d</option>',
00119             $d,
00120             ($d == $selected_day2) ? 'selected="selected"' : '',
00121             $d
00122          );
00123       }
00124 
00125       $alias = ($collection) ? $collection->getAlias() : "";
00126 
00127       return sprintf('<form method="get" action="%s" name="dmDateSearch">
00128             <fieldset id="dmDateSearch">
00129                <ol>
00130                   <li class="dmTerms">
00131                      <select name="CISOOP1">
00132                         <option value="all" %s>All of the words</option>
00133                         <option value="exact" %s>The exact phrase</option>
00134                         <option value="any" %s>Any of the words</option>
00135                         <option value="none" %s>None of the words</option>
00136                      </select>
00137 
00138                      <input type="search" size="30" name="CISOBOX1"
00139                         value="%s" class="dmSearchField autofill"> in this field:
00140 
00141                      <select id="dmDateSearchField" class="dmSearchFieldSelect" name="CISOFIELD1">
00142                         %s
00143                      </select>
00144                   </li>
00145 
00146                   <li id="dmDateSelect">
00147                      <select id="dmDateQualifier" name="date_qualifier">
00148                         <option value="from" %s>From</option>
00149                         <option value="after" %s>After</option>
00150                         <option value="before" %s>Before</option>
00151                         <option value="on" %s>On</option>
00152                      </select>
00153                   </li>
00154 
00155                   <li id="dmBeginDateSelect">
00156                      <select name="y1" id="y1">%s</select>
00157                      <select name="m1" id="m1">%s</select>
00158                      <select name="d1" id="d1">%s</select>
00159                   </li>
00160 
00161                   <li id="dmEndDateSelect">
00162                      <label id="dmEndDateLabel">To:</label>
00163                      <select name="y2" id="y2">%s</select>
00164                      <select name="m2" id="m2">%s</select>
00165                      <select name="d2" id="d2">%s</select>
00166                   </li>
00167                </ol>
00168 
00169                <div class="dmSearchCollectionList">
00170                   <ul>%s</ul>
00171                   %s
00172                </div>
00173                <p>
00174                   %s
00175                   %s
00176                   <input type="submit" value="Search" class="dmSubmit">
00177                </p>
00178             </fieldset>
00179          </form>',
00180          DMInternalURI::getURIWithParams("objects" . $alias),
00181          ($mode == 'all') ? 'selected="selected"' : '',
00182          ($mode == 'exact') ? 'selected="selected"' : '',
00183          ($mode == 'any') ? 'selected="selected"' : '',
00184          ($mode == 'none') ? 'selected="selected"' : '',
00185          $string,
00186          $this->getFieldOptionElements($field),
00187          ($date_qualifier == 'from') ? 'selected="selected"' : '',
00188          ($date_qualifier == 'after') ? 'selected="selected"' : '',
00189          ($date_qualifier == 'before') ? 'selected="selected"' : '',
00190          ($date_qualifier == 'on') ? 'selected="selected"' : '',
00191          $year1_options, $month1_options, $day1_options,
00192          $year2_options, $month2_options, $day2_options,
00193          $this->getCollectionListElements(),
00194          $this->getCheckAllLinks(),
00195          $this->getHiddenCollectionElement(),
00196          $this->getHiddenElements($get_parameters)
00197       );
00198    }
00199 
00205    public function getHtmlFieldSearch(array $get_parameters = array()) {
00206       $input = DMHTTPRequest::getCurrent()->getSession()->getSearchInput();
00207       $collection = $this->getView()->getCollection();
00208 
00209       $fields = array();
00210       $modes = array();
00211       $strings = array();
00212       $field_lis = "";
00213       for ($i = 0; $i < 3; $i++) {
00214          if ($input instanceof DMInput) {
00215             $term = $input->getPredicate($i);
00216             if ($term instanceof DMQueryPredicate) {
00217                if (!$term->isDate() && !$term->isProximity()) {
00218                   $fields[$i] = $term->getField()->getNick();
00219                   $modes[$i] = $term->getMode();
00220                   $strings[$i] = $term->getString();
00221                }
00222             }
00223          }
00224          
00225          $field_lis .= sprintf(
00226             '<li>
00227                <select name="CISOOP%d">
00228                   <option value="all" %s>All of the words</option>
00229                   <option value="exact" %s>The exact phrase</option>
00230                   <option value="any" %s>Any of the words</option>
00231                   <option value="none" %s>None of the words</option>
00232                </select>
00233                <input type="search" size="30" name="CISOBOX%d" id="dmFieldSearchTextBox%d"
00234                   value="%s" class="dmSearchField autofill"> in this field:
00235                <select name="CISOFIELD%d" id="dmSearchField%d" class="dmSearchFieldSelect">
00236                   <option value="any">Any Field</option>
00237                   %s
00238                </select>
00239             </li>',
00240             $i+1,
00241             (array_key_exists($i, $modes) && $modes[$i] == 'all') ? 'selected="selected"' : '',
00242             (array_key_exists($i, $modes) && $modes[$i] == 'exact') ? 'selected="selected"' : '',
00243             (array_key_exists($i, $modes) && $modes[$i] == 'any') ? 'selected="selected"' : '',
00244             (array_key_exists($i, $modes) && $modes[$i] == 'none') ? 'selected="selected"' : '',
00245             $i+1,
00246             $i,
00247             (array_key_exists($i, $strings)) ? DMString::websafe($strings[$i]) : '',
00248             $i+1,
00249             $i,
00250             (array_key_exists($i, $fields))
00251                ? $this->getFieldOptionElements($fields[$i])
00252                : $this->getFieldOptionElements()
00253          );
00254       }
00255 
00256       $alias = ($collection) ? $collection->getAlias() : "";
00257 
00258       return sprintf('<input class="dmBridgeBaseURI" type="hidden" name="dmBridgeBaseURI" value="%s">
00259             <input class="dmBridgeSelectedCollection" type="hidden" name="selectedCollection" value="%s">
00260             <form method="get" action="%s" name="dmFieldSearch">
00261             <fieldset id="dmTermSearch">
00262                <ol>%s</ol>
00263                <div class="dmSearchCollectionList">
00264                   <ul>%s</ul>
00265                   %s
00266                </div>
00267                <p>
00268                   %s
00269                   %s
00270                   <input type="submit" value="Search" class="dmSubmit">
00271                </p>
00272             </fieldset>
00273          </form>',
00274          DMConfigIni::getInstance()->getString("dmbridge.base_uri_path"),
00275          $this->getView()->getCollection()->getAlias(),
00276          DMInternalURI::getURIWithParams("objects" . $alias),
00277          $field_lis,
00278          $this->getCollectionListElements(),
00279          $this->getCheckAllLinks(),
00280          $this->getHiddenCollectionElement(),
00281          $this->getHiddenElements($get_parameters)
00282       );
00283    }
00284 
00290    public function getHtmlProximitySearch(array $get_parameters = array()) {
00291       $collection = $this->getView()->getCollection();
00292       $strings = $nick = null;
00293       // retrieve saved search input
00294       $input = DMHTTPRequest::getCurrent()->getSession()->getSearchInput();
00295       if ($input instanceof DMInput) {
00296          $term0 = $input->getPredicate(0);
00297          $term1 = $input->getPredicate(1);
00298          if ($term0 instanceof DMQueryPredicate) {
00299             if (!$term0->isDate()) {
00300                $strings = array();
00301                $field = $term0->getField();
00302                $nick = $field->getNick();
00303                $strings[0] = $term0->getString();
00304                if ($term1 instanceof DMQueryPredicate) {
00305                   if (!$term1->isDate()) {
00306                      $strings[1] = $term1->getString();
00307                   }
00308                }
00309             }
00310          }
00311       }
00312 
00313       $alias = ($collection) ? $collection->getAlias() : "";
00314 
00315       return sprintf(
00316          '<form method="get" action="%s" name="dmProximitySearch">
00317             <fieldset id="dmProximitySearch">
00318                <ol>
00319                   <li>
00320                      <label for="dmProximityString0">The word:</label>
00321                      <input id="dmProximityString0" type="search" size="30"
00322                         name="CISOBOX1" value="%s"
00323                         class="dmSearchField autofill">
00324                   </li>
00325                   <li>
00326                      <label for="dmProximity">Within</label>
00327                      <select name="proximity" id="dmProximity">
00328                         <option value="2">2</option>
00329                         <option value="3">3</option>
00330                         <option value="4">4</option>
00331                         <option value="5">5</option>
00332                         <option value="10">10</option>
00333                         <option value="20">20</option>
00334                         <option value="50">50</option>
00335                         <option value="100">100</option>
00336                      </select>
00337 
00338                      <label for="dmProximityString1">words of:</label>
00339                      <input id="dmProximityString1" type="search" size="30"
00340                         name="CISOBOX2" value="%s"
00341                         class="dmSearchField autofill">
00342                   </li>
00343                   <li>
00344                      <label for="dmProximityField">In this field:</label>
00345                      <select name="CISOFIELD1" id="dmProximityField" class="dmSearchFieldSelect">
00346                         <option value="any">Any Field</option>
00347                         %s
00348                      </select>
00349                   </li>
00350                </ol>
00351 
00352                <div class="dmSearchCollectionList">
00353                   <ul>%s</ul>
00354                   %s
00355                </div>
00356                <p>
00357                   <input type="hidden" name="CISOOP1" value="exact">
00358                   <input type="hidden" name="CISOOP2" value="exact">
00359                   %s
00360                   %s
00361                   <input type="submit" value="Search" class="dmSubmit">
00362                </p>
00363             </fieldset>
00364          </form>',
00365          DMInternalURI::getURIWithParams("objects" . $alias),
00366          (count($strings)) ? DMString::websafe($strings[0]) : "",
00367          (count($strings) > 1) ? DMString::websafe($strings[1]) : "",
00368          $this->getFieldOptionElements($nick),
00369          $this->getCollectionListElements(),
00370          $this->getCheckAllLinks(),
00371          $this->getHiddenCollectionElement(),
00372          $this->getHiddenElements($get_parameters)
00373       );
00374    }
00375 
00376    private function getCheckAllLinks() {
00377       $tpl_set = $this->getView()->getTemplate()->getTemplateSet();
00378       if (count($tpl_set->getAuthorizedCollections()) == 1) {
00379          return false;
00380       }
00381       return '<div class="dmCheckAll">
00382             <a class="dmCheckAll">Check All</a> |
00383             <a class="dmUncheckAll">Uncheck All</a>
00384          </div>';
00385    }
00386 
00387    private function getCollectionListElements() {
00388       $tpl_set = $this->getView()->getTemplate()->getTemplateSet();
00389       if (count($tpl_set->getAuthorizedCollections()) == 1) {
00390          return false;
00391       }
00392       $collection_list = "";
00393       foreach (DMCollection::getAuthorized() as $c) {
00394          if (count($tpl_set->getAuthorizedCollections()) > 1
00395                && !in_array($c, $tpl_set->getAuthorizedCollections())) {
00396             continue;
00397          }
00398 
00399          $input = DMHTTPRequest::getCurrent()->getSession()->getSearchInput();
00400          $checked = false;
00401          if ($input instanceof DMInput) {
00402             if (in_array($c->getAlias(), $input->getCollections())) {
00403                $checked = true;
00404             }
00405          } else {
00406             if ($this->getView()->getCollection()
00407                   && $this->getView()->getCollection()->getAlias()
00408                   != "/dmdefault") {
00409                $checked = ($c->equals($this->getView()->getCollection()));
00410             } else {
00411                $checked = true;
00412             }
00413          }
00414          $checked = $checked ? 'checked="checked"' : null;
00415          $collection_list .= sprintf(
00416             '<li>
00417                <label>
00418                   <input type="checkbox" name="CISOROOT" value="%s" %s>
00419                   %s
00420                </label>
00421             </li>',
00422             DMString::websafe($c->getAlias()),
00423             $checked,
00424             DMString::websafe($c->getName())
00425          );
00426          $checked = '';
00427       }
00428       return $collection_list;
00429    }
00430 
00431    private function getFieldOptionElements($selected = null) {
00432       $field_options = "";
00433       foreach ($this->getView()->getCollection()->getFields() as $f) {
00434          if (!$f->isSearchable()) {
00435             continue;
00436          }
00437          // disallow field searches for date; this won't affect date searches
00438          if ($f->getNick() == "date") {
00439             continue;
00440          }
00441          $field_options .= sprintf('<option value="%s"%s>%s</option>',
00442             DMString::paranoid($f->getNick()),
00443             ($f->getNick() == $selected) ? ' selected="selected"' : '',
00444             DMString::websafe($f->getName()));
00445       }
00446       return $field_options;
00447    }
00448 
00449    private function getHiddenCollectionElement() {
00450       $allowed_collections = $this->getView()->getTemplate()
00451             ->getTemplateSet()->getAuthorizedCollections();
00452       if (sizeof($allowed_collections) <> 1) {
00453          return "";
00454       }
00455       $elem = sprintf('<input type="hidden" name="CISOROOT" value="%s">',
00456          DMString::paranoid($allowed_collections[0], array('/')));
00457       return $elem;
00458    }
00459 
00460    private function getHiddenElements(array $get_parameters) {
00461       $hidden_elements = "";
00462       foreach ($get_parameters as $key => $value) {
00463          $hidden_elements .= sprintf(
00464                '<input type="hidden" name="%s" value="%s">',
00465                DMString::websafe($key),
00466                DMString::websafe($value));
00467       }
00468       return $hidden_elements;
00469    }
00470 
00471 }
 All Data Structures Functions Variables