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