PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/hotri/classes/BiblioSearchQuery.php

https://github.com/aimakun/odlib
PHP | 266 lines | 180 code | 26 blank | 60 comment | 28 complexity | e46d381e831e1c482ad42fc8ef5fe0e0 MD5 | raw file
  1. <?php
  2. /* This file is part of a copyrighted work; it is distributed with NO WARRANTY.
  3. * See the file COPYRIGHT.html for more details.
  4. */
  5. require_once("../shared/global_constants.php");
  6. require_once("../classes/Query.php");
  7. require_once("../classes/BiblioField.php");
  8. require_once("../classes/Localize.php");
  9. /******************************************************************************
  10. * BiblioQuery data access component for library bibliographies
  11. *
  12. * @author David Stevens <dave@stevens.name>;
  13. * @version 1.0
  14. * @access public
  15. ******************************************************************************
  16. */
  17. class BiblioSearchQuery extends Query {
  18. var $_itemsPerPage = 1;
  19. var $_rowNmbr = 0;
  20. var $_currentRowNmbr = 0;
  21. var $_currentPageNmbr = 0;
  22. var $_rowCount = 0;
  23. var $_pageCount = 0;
  24. var $_loc;
  25. function BiblioSearchQuery() {
  26. $this->Query();
  27. $this->_loc = new Localize(OBIB_LOCALE,"classes");
  28. }
  29. function setItemsPerPage($value) {
  30. $this->_itemsPerPage = $value;
  31. }
  32. function getLineNmbr() {
  33. return $this->_rowNmbr;
  34. }
  35. function getCurrentRowNmbr() {
  36. return $this->_currentRowNmbr;
  37. }
  38. function getRowCount() {
  39. return $this->_rowCount;
  40. }
  41. function getPageCount() {
  42. return $this->_pageCount;
  43. }
  44. /****************************************************************************
  45. * Executes a query
  46. * @param string $type one of the global constants
  47. * OBIB_SEARCH_BARCODE,
  48. * OBIB_SEARCH_TITLE,
  49. * OBIB_SEARCH_AUTHOR,
  50. * or OBIB_SEARCH_SUBJECT
  51. * @param string @$words pointer to an array containing words to search for
  52. * @param integer $page What page should be returned if results are more than one page
  53. * @param string $sortBy column name to sort by. Can be title or author
  54. * @return boolean returns false, if error occurs
  55. * @access public
  56. ****************************************************************************
  57. */
  58. function search($type, &$words, $page, $sortBy, $opacFlg=true) {
  59. # reset stats
  60. $this->_rowNmbr = 0;
  61. $this->_currentRowNmbr = 0;
  62. $this->_currentPageNmbr = $page;
  63. $this->_rowCount = 0;
  64. $this->_pageCount = 0;
  65. # setting sql join clause
  66. $join = "from biblio left join biblio_copy on biblio.bibid=biblio_copy.bibid ";
  67. # setting sql where clause
  68. $criteria = "";
  69. if ((sizeof($words) == 0) || ($words[0] == "")) {
  70. if ($opacFlg) $criteria = "where opac_flg = 'Y' ";
  71. } else {
  72. if ($type == OBIB_SEARCH_BARCODE) {
  73. $criteria = $this->_getCriteria(array("biblio_copy.barcode_nmbr"),$words);
  74. } elseif ($type == OBIB_SEARCH_AUTHOR) {
  75. $join .= "left join biblio_field on biblio_field.bibid=biblio.bibid "
  76. . "and biblio_field.tag='700' "
  77. . "and (biblio_field.subfield_cd='a' or biblio_field.subfield_cd='b') ";
  78. $criteria = $this->_getCriteria(array("biblio.author","biblio.responsibility_stmt","biblio_field.field_data"),$words);
  79. } elseif ($type == OBIB_SEARCH_SUBJECT) {
  80. $criteria = $this->_getCriteria(array("biblio.topic1","biblio.topic2","biblio.topic3","biblio.topic4","biblio.topic5"),$words);
  81. } else {
  82. $criteria = $this->_getCriteria(array("biblio.title"),$words);
  83. }
  84. if ($opacFlg) $criteria = $criteria."and opac_flg = 'Y' ";
  85. }
  86. # setting count query
  87. $sqlcount = "select count(*) as rowcount ";
  88. $sqlcount = $sqlcount.$join;
  89. $sqlcount = $sqlcount.$criteria;
  90. # setting query that will return all the data
  91. $sql = "select biblio.* ";
  92. $sql .= ",biblio_copy.copyid ";
  93. $sql .= ",biblio_copy.barcode_nmbr ";
  94. $sql .= ",biblio_copy.status_cd ";
  95. $sql .= ",biblio_copy.due_back_dt ";
  96. $sql .= ",biblio_copy.mbrid ";
  97. $sql .= $join;
  98. $sql .= $criteria;
  99. $sql .= $this->mkSQL(" order by %C ", $sortBy);
  100. # setting limit so we can page through the results
  101. $offset = ($page - 1) * $this->_itemsPerPage;
  102. $limit = $this->_itemsPerPage;
  103. $sql .= $this->mkSQL(" limit %N, %N", $offset, $limit);
  104. //echo "sqlcount=[".$sqlcount."]<br>\n";
  105. //exit("sql=[".$sql."]<br>\n");
  106. # Running row count sql statement
  107. if (!$this->_query($sqlcount, $this->_loc->getText("biblioSearchQueryErr1"))) {
  108. return false;
  109. }
  110. # Calculate stats based on row count
  111. $array = $this->_conn->fetchRow();
  112. $this->_rowCount = $array["rowcount"];
  113. $this->_pageCount = ceil($this->_rowCount / $this->_itemsPerPage);
  114. # Running search sql statement
  115. return $this->_query($sql, $this->_loc->getText("biblioSearchQueryErr2"));
  116. }
  117. /****************************************************************************
  118. * Utility function to get the selection criteria for a given column and set of values
  119. * @param string $col bibid of bibliography to select
  120. * @param array reference &$words array of words to search for
  121. * @return string returns SQL criteria syntax for the given column and set of values
  122. * @access private
  123. ****************************************************************************
  124. */
  125. function _getCriteria($cols,&$words) {
  126. # setting selection criteria sql
  127. $prefix = "where ";
  128. $criteria = "";
  129. for ($i = 0; $i < count($words); $i++) {
  130. $criteria .= $prefix.$this->_getLike($cols,$words[$i]);
  131. $prefix = " and ";
  132. }
  133. return $criteria;
  134. }
  135. function _getLike(&$cols,$word) {
  136. $prefix = "";
  137. $suffix = "";
  138. if (count($cols) > 1) {
  139. $prefix = "(";
  140. $suffix = ")";
  141. }
  142. $like = "";
  143. for ($i = 0; $i < count($cols); $i++) {
  144. $like .= $prefix;
  145. $like .= $this->mkSQL("%C like %Q ", $cols[$i], "%".$word."%");
  146. $prefix = " or ";
  147. }
  148. $like .= $suffix;
  149. return $like;
  150. }
  151. /****************************************************************************
  152. * Executes a query to select ONLY ONE SUBFIELD
  153. * @param string $bibid bibid of bibliography copy to select
  154. * @param string $fieldid copyid of bibliography copy to select
  155. * @return BiblioField returns subfield or false, if error occurs
  156. * @access public
  157. ****************************************************************************
  158. */
  159. function doQuery($statusCd,$mbrid="") {
  160. $sql = "select biblio.* ";
  161. $sql .= ",biblio_copy.copyid ";
  162. $sql .= ",biblio_copy.barcode_nmbr ";
  163. $sql .= ",biblio_copy.status_cd ";
  164. $sql .= ",biblio_copy.status_begin_dt ";
  165. $sql .= ",biblio_copy.due_back_dt ";
  166. $sql .= ",biblio_copy.mbrid ";
  167. $sql .= ",biblio_copy.renewal_count ";
  168. $sql .= ",greatest(0,to_days(sysdate()) - to_days(biblio_copy.due_back_dt)) days_late ";
  169. $sql .= "from biblio, biblio_copy ";
  170. $sql .= "where biblio.bibid = biblio_copy.bibid ";
  171. if ($mbrid != "") {
  172. $sql .= $this->mkSQL("and biblio_copy.mbrid = %N ", $mbrid);
  173. }
  174. $sql .= $this->mkSQL(" and biblio_copy.status_cd=%Q ", $statusCd);
  175. $sql .= " order by biblio_copy.status_begin_dt desc";
  176. if (!$this->_query($sql, $this->_loc->getText("biblioSearchQueryErr3"))) {
  177. return false;
  178. }
  179. $this->_rowCount = $this->_conn->numRows();
  180. return true;
  181. }
  182. /****************************************************************************
  183. * Fetches a row from the query result and populates the BiblioSearch object.
  184. * @return BiblioSearch returns bibliography search record or false if no more bibliographies to fetch
  185. * @access public
  186. ****************************************************************************
  187. */
  188. function fetchRow() {
  189. $array = $this->_conn->fetchRow();
  190. if ($array == false) {
  191. return false;
  192. }
  193. # increment rowNmbr
  194. $this->_rowNmbr = $this->_rowNmbr + 1;
  195. $this->_currentRowNmbr = $this->_rowNmbr + (($this->_currentPageNmbr - 1) * $this->_itemsPerPage);
  196. $bib = new BiblioSearch();
  197. $bib->setBibid($array["bibid"]);
  198. $bib->setCopyid($array["copyid"]);
  199. $bib->setCreateDt($array["create_dt"]);
  200. $bib->setLastChangeDt($array["last_change_dt"]);
  201. $bib->setLastChangeUserid($array["last_change_userid"]);
  202. $bib->setMaterialCd($array["material_cd"]);
  203. $bib->setCollectionCd($array["collection_cd"]);
  204. $bib->setCallNmbr1($array["call_nmbr1"]);
  205. $bib->setCallNmbr2($array["call_nmbr2"]);
  206. $bib->setCallNmbr3($array["call_nmbr3"]);
  207. $bib->setTitle($array["title"]);
  208. $bib->setTitleRemainder($array["title_remainder"]);
  209. $bib->setResponsibilityStmt($array["responsibility_stmt"]);
  210. $bib->setAuthor($array["author"]);
  211. $bib->setTopic1($array["topic1"]);
  212. $bib->setTopic2($array["topic2"]);
  213. $bib->setTopic3($array["topic3"]);
  214. $bib->setTopic4($array["topic4"]);
  215. $bib->setTopic5($array["topic5"]);
  216. if (isset($array["barcode_nmbr"])) {
  217. $bib->setBarcodeNmbr($array["barcode_nmbr"]);
  218. }
  219. if (isset($array["status_cd"])) {
  220. $bib->setStatusCd($array["status_cd"]);
  221. }
  222. if (isset($array["status_begin_dt"])) {
  223. $bib->setStatusBeginDt($array["status_begin_dt"]);
  224. }
  225. if (isset($array["status_mbrid"])) {
  226. $bib->setStatusMbrid($array["status_mbrid"]);
  227. }
  228. if (isset($array["due_back_dt"])) {
  229. $bib->setDueBackDt($array["due_back_dt"]);
  230. }
  231. if (isset($array["days_late"])) {
  232. $bib->setDaysLate($array["days_late"]);
  233. }
  234. if (isset($array["renewal_count"])) {
  235. $bib->setRenewalCount($array["renewal_count"]);
  236. }
  237. return $bib;
  238. }
  239. }
  240. ?>