PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/biblio_list.inc.php

https://gitlab.com/mucill/majalengka
PHP | 294 lines | 204 code | 17 blank | 73 comment | 91 complexity | d9f6490d6f07091dc6d56121a455103f MD5 | raw file
  1. <?php
  2. /**
  3. * biblio_list class
  4. * Class for generating list of bibliographic records
  5. *
  6. * Copyright (C) 2009 Arie Nugraha (dicarve@yahoo.com)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. */
  23. // be sure that this file not accessed directly
  24. if (!defined('INDEX_AUTH')) {
  25. die("can not access this file directly");
  26. } elseif (INDEX_AUTH != 1) {
  27. die("can not access this file directly");
  28. }
  29. class biblio_list extends biblio_list_model {
  30. protected $searchable_fields = array('title', 'author', 'subject', 'isbn',
  31. 'publisher', 'gmd', 'notes', 'colltype', 'publishyear',
  32. 'location', 'itemcode', 'callnumber', 'itemcallnumber', 'notes');
  33. protected $field_join_type = array('title' => 'OR', 'author' => 'OR', 'subject' => 'OR');
  34. /**
  35. * Class Constructor
  36. *
  37. * @param object $obj_db
  38. * @param integer $int_num_show
  39. */
  40. public function __construct($obj_db, $int_num_show) {
  41. parent::__construct($obj_db, $int_num_show);
  42. }
  43. /**
  44. * Method to set search criteria
  45. *
  46. * @param string $str_criteria
  47. * @return void
  48. */
  49. public function setSQLcriteria($str_criteria) {
  50. if (!$str_criteria)
  51. return null;
  52. // defaults
  53. $_sql_criteria = '';
  54. $_searched_fields = array();
  55. $_title_buffer = '';
  56. $_previous_field = '';
  57. $_boolean = '';
  58. // parse query
  59. $this->orig_query = $str_criteria;
  60. $_queries = simbio_tokenizeCQL($str_criteria, $this->searchable_fields, $this->stop_words, $this->queries_word_num_allowed);
  61. // echo '<pre>'; var_dump($_queries); echo '</pre>';
  62. if (count($_queries) < 1) {
  63. return null;
  64. }
  65. // loop each query
  66. foreach ($_queries as $_num => $_query) {
  67. // field
  68. $_field = $_query['f'];
  69. // for debugging purpose only
  70. // echo "<p>$_num. $_field -> $_boolean -> $_sql_criteria</p><p>&nbsp;</p>";
  71. // boolean
  72. if ($_title_buffer == '' && $_field != 'boolean') {
  73. $_sql_criteria .= " $_boolean ";
  74. }
  75. // $_sql_criteria .= " $_boolean ";
  76. // flush title string concatenation
  77. if ($_field != 'title' && $_title_buffer != '') {
  78. $_title_buffer = trim($_title_buffer);
  79. $_sql_criteria .= " biblio.biblio_id IN(SELECT DISTINCT biblio_id FROM biblio WHERE MATCH (title, series_title) AGAINST ('$_title_buffer' IN BOOLEAN MODE)) ";
  80. // reset title buffer
  81. $_title_buffer = '';
  82. }
  83. // break the loop if we meet `cql_end` field
  84. if ($_field == 'cql_end') { break; }
  85. // boolean mode
  86. $_b = isset($_query['b'])?$_query['b']:$_query;
  87. if ($_b == '*') {
  88. $_boolean = 'OR';
  89. } else { $_boolean = 'AND'; }
  90. // search value
  91. $_q = @$this->obj_db->escape_string($_query['q']);
  92. $_searched_word = str_replace(array('+', '-', '*'), '', $_q);
  93. $this->words[$_searched_word] = $_searched_word;
  94. // searched fields flag set
  95. $_searched_fields[$_field] = 1;
  96. $_previous_field = $_field;
  97. // check field
  98. if ($_field == 'title') {
  99. if (strlen($_q) < 4) {
  100. $_previous_field = 'title_short';
  101. $_sql_criteria .= " biblio.title LIKE '%$_q%' ";
  102. $_title_buffer = '';
  103. } else {
  104. if (isset($_query['is_phrase'])) {
  105. $_title_buffer .= ' '.$_b.'"'.$_q.'"';
  106. } else {
  107. $_title_buffer .= ' '.$_b.$_q;
  108. }
  109. }
  110. } else if ($_field == 'author') {
  111. if ($_b == '-') {
  112. $_sql_criteria .= " biblio.biblio_id NOT IN(SELECT ba.biblio_id FROM biblio_author AS ba"
  113. ." LEFT JOIN mst_author AS a ON ba.author_id=a.author_id"
  114. ." WHERE author_name LIKE '%$_q%')";
  115. } else {
  116. $_sql_criteria .= " biblio.biblio_id IN(SELECT ba.biblio_id FROM biblio_author AS ba"
  117. ." LEFT JOIN mst_author AS a ON ba.author_id=a.author_id"
  118. ." WHERE author_name LIKE '%$_q%')";
  119. }
  120. } else if ($_field == 'subject') {
  121. if ($_b == '-') {
  122. $_sql_criteria .= " biblio.biblio_id NOT IN(SELECT bt.biblio_id FROM biblio_topic AS bt"
  123. ." LEFT JOIN mst_topic AS t ON bt.topic_id=t.topic_id"
  124. ." WHERE topic LIKE '%$_q%')";
  125. } else {
  126. $_sql_criteria .= " biblio.biblio_id IN(SELECT bt.biblio_id FROM biblio_topic AS bt"
  127. ." LEFT JOIN mst_topic AS t ON bt.topic_id=t.topic_id"
  128. ." WHERE topic LIKE '%$_q%')";
  129. }
  130. // reset title buffer
  131. $_title_buffer = '';
  132. } else {
  133. switch ($_field) {
  134. case 'location' :
  135. if (!$this->disable_item_data) {
  136. $_subquery = 'SELECT location_id FROM mst_location WHERE location_name=\''.$_q.'\'';
  137. if ($_b == '-') {
  138. $_sql_criteria .= " item.location_id NOT IN ($_subquery)";
  139. } else { $_sql_criteria .= " item.location_id IN ($_subquery)"; }
  140. } else {
  141. if ($_b == '-') {
  142. $_sql_criteria .= " biblio.node_id !='$_q'";
  143. } else { $_sql_criteria .= " biblio.node_id = '$_q'"; }
  144. }
  145. break;
  146. case 'colltype' :
  147. if (!$this->disable_item_data) {
  148. $_subquery = 'SELECT coll_type_id FROM mst_coll_type WHERE coll_type_name=\''.$_q.'\'';
  149. if ($_b == '-') {
  150. $_sql_criteria .= " item.coll_type_id NOT IN ($_subquery)";
  151. } else { $_sql_criteria .= " item.coll_type_id IN ($_subquery)"; }
  152. }
  153. break;
  154. case 'itemcode' :
  155. if (!$this->disable_item_data) {
  156. if ($_b == '-') {
  157. $_sql_criteria .= " item.item_code != '$_q'";
  158. } else { $_sql_criteria .= " item.item_code LIKE '$_q%'"; }
  159. }
  160. break;
  161. case 'callnumber' :
  162. if ($_b == '-') {
  163. $_sql_criteria .= ' biblio.call_number NOT LIKE \''.$_q.'%\'';
  164. } else { $_sql_criteria .= ' biblio.call_number LIKE \''.$_q.'%\''; }
  165. break;
  166. case 'itemcallnumber' :
  167. if (!$this->disable_item_data) {
  168. if ($_b == '-') {
  169. $_sql_criteria .= ' item.call_number NOT LIKE \''.$_q.'%\'';
  170. } else { $_sql_criteria .= ' item.call_number LIKE \''.$_q.'%\''; }
  171. }
  172. break;
  173. case 'class' :
  174. if ($_b == '-') {
  175. $_sql_criteria .= ' biblio.classification NOT LIKE \''.$_q.'%\'';
  176. } else { $_sql_criteria .= ' biblio.classification LIKE \''.$_q.'%\''; }
  177. break;
  178. case 'isbn' :
  179. if ($_b == '-') {
  180. $_sql_criteria .= ' biblio.isbn_issn!=\''.$_q.'\'';
  181. } else { $_sql_criteria .= ' biblio.isbn_issn=\''.$_q.'\''; }
  182. break;
  183. case 'publisher' :
  184. $_subquery = 'SELECT publisher_id FROM mst_publisher WHERE publisher_name LIKE \'%'.$_q.'%\'';
  185. if ($_b == '-') {
  186. $_sql_criteria .= " biblio.publisher_id NOT IN ($_subquery)";
  187. } else { $_sql_criteria .= " biblio.publisher_id IN ($_subquery)"; }
  188. break;
  189. case 'publishyear' :
  190. if ($_b == '-') {
  191. $_sql_criteria .= ' biblio.publish_year!=\''.$_q.'\'';
  192. } else { $_sql_criteria .= ' biblio.publish_year=\''.$_q.'\''; }
  193. break;
  194. case 'gmd' :
  195. $_subquery = 'SELECT gmd_id FROM mst_gmd WHERE gmd_name=\''.$_q.'\'';
  196. if ($_b == '-') {
  197. $_sql_criteria .= " biblio.gmd_id NOT IN ($_subquery)";
  198. } else { $_sql_criteria .= " biblio.gmd_id IN ($_subquery)"; }
  199. break;
  200. case 'notes' :
  201. $_q = isset($_query['is_phrase'])?'"'.$_q.'"':$_q;
  202. if ($_b == '-') {
  203. $_sql_criteria .= " NOT (MATCH (biblio.notes) AGAINST ('".$_q."' IN BOOLEAN MODE))";
  204. } else { $_sql_criteria .= " (MATCH (biblio.notes) AGAINST ('".$_q."' IN BOOLEAN MODE))"; }
  205. break;
  206. }
  207. }
  208. }
  209. // remove boolean's logic symbol prefix and suffix
  210. $_sql_criteria = preg_replace('@^(AND|OR|NOT)\s*|\s+(AND|OR|NOT)$@i', '', trim($_sql_criteria));
  211. // below for debugging purpose only
  212. // echo "<div style=\"border: 1px solid #f00; padding: 5px; color: #f00; margin: 5px;\">$_sql_criteria</div>";
  213. $this->criteria = array('sql_criteria' => $_sql_criteria, 'searched_fields' => $_searched_fields);
  214. return $this->criteria;
  215. }
  216. /**
  217. * Method to print out document records
  218. *
  219. * @param object $obj_db
  220. * @param integer $int_num2show
  221. * @param boolean $bool_return_output
  222. * @return string
  223. */
  224. public function compileSQL() {
  225. global $sysconf;
  226. // get page number from http get var
  227. if (!isset($_GET['page']) OR $_GET['page'] < 1){
  228. $_page = 1;
  229. } else{
  230. $_page = (integer)$_GET['page'];
  231. }
  232. $this->current_page = $_page;
  233. // count the row offset
  234. if ($_page <= 1) {
  235. $_offset = 0;
  236. } else {
  237. $_offset = ($_page*$this->num2show) - $this->num2show;
  238. }
  239. // init sql string
  240. $_sql_str = 'SELECT SQL_CALC_FOUND_ROWS biblio.biblio_id, biblio.title, biblio.image, biblio.isbn_issn,
  241. biblio.publish_year, pbl.publisher_name AS `publisher`, pplc.place_name AS `publish_place`, biblio.labels, biblio.input_date';
  242. // checking custom frontpage fields file
  243. $custom_frontpage_record_file = SB.$sysconf['template']['dir'].'/'.$sysconf['template']['theme'].'/custom_frontpage_record.inc.php';
  244. if (file_exists($custom_frontpage_record_file)) {
  245. include $custom_frontpage_record_file;
  246. $this->enable_custom_frontpage = true;
  247. $this->custom_fields = $custom_fields;
  248. foreach ($this->custom_fields as $_field => $_field_opts) {
  249. if ($_field_opts[0] == 1 && !in_array($_field, array('availability', 'isbn_issn'))) {
  250. $_sql_str .= ", biblio.$_field";
  251. }
  252. }
  253. }
  254. // additional SQL string
  255. $_add_sql_str = ' LEFT JOIN mst_publisher AS pbl ON biblio.publisher_id=pbl.publisher_id ';
  256. $_add_sql_str .= ' LEFT JOIN mst_place AS pplc ON biblio.publish_place_id=pplc.place_id ';
  257. // location
  258. if ($this->criteria) {
  259. if (isset($this->criteria['searched_fields']['location']) || isset($this->criteria['searched_fields']['colltype'])) {
  260. if (!$this->disable_item_data) {
  261. $_add_sql_str .= ' LEFT JOIN item ON biblio.biblio_id=item.biblio_id ';
  262. }
  263. }
  264. }
  265. $_add_sql_str .= ' WHERE opac_hide=0 ';
  266. // promoted flag
  267. // if ($this->only_promoted) { $_add_sql_str .= ' AND promoted=1'; }
  268. // main search criteria
  269. if ($this->criteria) {
  270. $_add_sql_str .= ' AND ('.$this->criteria['sql_criteria'].') ';
  271. }
  272. $_sql_str .= ' FROM biblio '.$_add_sql_str.' ORDER BY biblio.last_update DESC LIMIT '.$_offset.', '.$this->num2show;
  273. // for debugging purpose only
  274. // echo "<div style=\"border: 1px solid navy; padding: 5px; color: navy; margin: 5px;\">$_sql_str</div>";
  275. return $_sql_str;
  276. }
  277. }