tripal_pub.pub_search.inc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. <?php
  2. /**
  3. * @file
  4. *
  5. * Functions responsible for creating the publication search form that
  6. * allows a user of the site to search for publications that are currently
  7. * in Chado.
  8. */
  9. /**
  10. * The page that contains the publication search form and the results for the search
  11. *
  12. * @ingroup tripal_pub
  13. */
  14. function tripal_pub_search_page() {
  15. // This line may not be required, but on some sites the $_SESSION
  16. // variable wasn't being set for anonymous users. This line solves that
  17. // problem
  18. drupal_session_start();
  19. $limit = 25;
  20. // generate the search form
  21. $form = drupal_get_form('tripal_pub_search_form');
  22. $output = drupal_render($form);
  23. // retrieve any results
  24. if (array_key_exists('tripal_pub_search_form', $_SESSION) and
  25. $_SESSION['tripal_pub_search_form']['perform_search']) {
  26. $num_criteria = $_SESSION['tripal_pub_search_form']['num_criteria'];
  27. $from_year = $_SESSION['tripal_pub_search_form']['from_year'];
  28. $to_year = $_SESSION['tripal_pub_search_form']['to_year'];
  29. $search_array = array();
  30. $search_array['num_criteria'] = $num_criteria;
  31. $search_array['from_year'] = $from_year;
  32. $search_array['to_year'] = $to_year;
  33. for ($i = 0; $i <= $num_criteria; $i++) {
  34. $search_array['criteria'][$i]['search_terms'] = $_SESSION['tripal_pub_search_form']['criteria'][$i]['search_terms'];
  35. $search_array['criteria'][$i]['scope'] = $_SESSION['tripal_pub_search_form']['criteria'][$i]['scope'];
  36. $search_array['criteria'][$i]['mode'] = $_SESSION['tripal_pub_search_form']['criteria'][$i]['mode'];
  37. $search_array['criteria'][$i]['operation'] = $_SESSION['tripal_pub_search_form']['criteria'][$i]['operation'];
  38. }
  39. // get the list of publications from the remote database using the search criteria.
  40. $page = isset($_GET['page']) ? $_GET['page'] : '0';
  41. $offset = $page * $limit;
  42. $total_records = 0;
  43. $pubs = tripal_search_publications($search_array, $offset, $limit, $total_records);
  44. pager_default_initialize($total_records, $limit, 0);
  45. // iterate through the results and construct the table displaying the publications
  46. $rows = array();
  47. $i = $page * $limit + 1;
  48. foreach ($pubs as $pub) {
  49. // get the citation for this publication
  50. $values = array(
  51. 'pub_id' => $pub->pub_id,
  52. 'type_id' => array(
  53. 'name' => 'Citation',
  54. ),
  55. );
  56. $citation_rec = chado_generate_var('pubprop', $values);
  57. $citation_rec = chado_expand_var($citation_rec, 'field', 'pubprop.value');
  58. // if we have the citation then use it, otherwise, just use the title
  59. $title = htmlspecialchars($pub->title);
  60. $result = $title;
  61. $link = NULL;
  62. // tripal v2 link (node)
  63. $nid = chado_get_nid_from_id ('pub', $pub->pub_id);
  64. if ($nid) {
  65. $link = "/node/$nid";
  66. }
  67. // try tripal v3 link (entity), if it exists, update the link to entity
  68. $entity_id = tripal_get_chado_entity_id ('pub', $pub->pub_id);
  69. if ($entity_id) {
  70. $link = "/bio_data/$entity_id";
  71. }
  72. if ($link) {
  73. $result = l($title , $link, array('attributes' => array('target' => '_blank')));
  74. }
  75. if ($citation_rec->value) {
  76. $citation = htmlspecialchars($citation_rec->value);
  77. $result .= '<br>' . $citation;
  78. }
  79. $rows[] = array(
  80. number_format($i) . ".",
  81. $pub->pyear,
  82. $result
  83. );
  84. $i++;
  85. }
  86. if (count($rows) == 0) {
  87. $rows[] = array(
  88. array(
  89. 'data' => 'No results found',
  90. 'colspan' => 3
  91. )
  92. );
  93. }
  94. $headers = array('', 'Year', 'Publication');
  95. $table = array(
  96. 'header' => $headers,
  97. 'rows' => $rows,
  98. 'attributes' => array(
  99. 'id' => 'tripal-pub-search-results-table',
  100. 'border' => '0',
  101. 'class' => array('tripal-data-table')
  102. ),
  103. 'sticky' => TRUE,
  104. 'caption' => '',
  105. 'colgroups' => array(),
  106. 'empty' => '',
  107. );
  108. $results = theme_table($table);
  109. // generate the pager
  110. $pager = array(
  111. 'tags' => array(),
  112. 'element' => 0,
  113. 'parameters' => array(),
  114. 'quantity' => $limit,
  115. );
  116. $pager = theme_pager($pager);
  117. // join all to form the results
  118. $output .= "<p><b>Found " . number_format($total_records) .
  119. " Results</b></br>" . $results . $pager;
  120. }
  121. return $output;
  122. }
  123. /**
  124. * Provides the form to search pubmed
  125. *
  126. * @ingroup tripal_pub
  127. */
  128. function tripal_pub_search_form($form, &$form_state) {
  129. // Default values can come in the following ways:
  130. //
  131. // 1) as elements of the $pub_importer object. This occurs when editing an existing importer
  132. // 2) in the $form_state['values'] array which occurs on a failed validation or
  133. // ajax callbacks from non submit form elements
  134. // 3) in the $form_state['input'] array which occurs on ajax callbacks from submit
  135. // form elements and the form is being rebuilt
  136. //
  137. // Set the default values. If the pub_import_id isn't already defined by the form values
  138. // and one is provided then look it up in the database
  139. $criteria = NULL;
  140. $num_criteria = 2;
  141. $from_year = '';
  142. $to_year = '';
  143. // if the session has variables then use those. This should only happen when
  144. // the 'Test Criteria' button is clicked.
  145. if (array_key_exists('storage', $form_state)) {
  146. $num_criteria = $form_state['storage']['num_criteria'];
  147. }
  148. if (array_key_exists('tripal_pub_search_form', $_SESSION)) {
  149. $num_criteria = $_SESSION['tripal_pub_search_form']['num_criteria'] ? $_SESSION['tripal_pub_search_form']['num_criteria'] : $num_criteria;
  150. $from_year = $_SESSION['tripal_pub_search_form']['from_year'] ? $_SESSION['tripal_pub_search_form']['from_year'] : '';
  151. $to_year = $_SESSION['tripal_pub_search_form']['to_year'] ? $_SESSION['tripal_pub_search_form']['to_year'] : '';
  152. }
  153. if (array_key_exists('values', $form_state)) {
  154. $from_year = $form_state['values']['from_year'] ? $form_state['values']['from_year'] : $from_year;
  155. $to_year = $form_state['values']['to_year'] ? $form_state['values']['to_year'] : $to_year;
  156. }
  157. if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
  158. $from_year = $form_state['input']['from_year'] ? $form_state['input']['from_year'] : $from_year;
  159. $to_year = $form_state['input']['to_year'] ? $form_state['input']['to_year'] : $to_year;
  160. }
  161. if (array_key_exists('triggering_element', $form_state) and
  162. $form_state['triggering_element']['#name'] == 'add') {
  163. $num_criteria++;
  164. }
  165. if (array_key_exists('triggering_element', $form_state) and
  166. $form_state['triggering_element']['#name'] == 'remove') {
  167. $num_criteria--;
  168. }
  169. $form_state['storage']['num_criteria'] = $num_criteria;
  170. $form['admin-instructions'] = array(
  171. '#markup' => tripal_set_message(
  172. t('Administrators, you can select the fields with which a user can use to search, by checking the desired fields on the ' .
  173. l('Publication Module Settings Page', 'admin/tripal/legacy/tripal_pub/configuration', array('attributes' => array('target' => '_blank'))) . '
  174. in the section titled "Search Options". The selected fields will appear in the dropdowns below.'),
  175. TRIPAL_INFO,
  176. array('return_html' => 1)),
  177. );
  178. $form['instructions'] = array(
  179. '#markup' => t('To search for publications enter keywords in the text boxes below.
  180. You can limit your search by selecting the field in the dropdown box. Click the
  181. add and remove buttons to add additional fields for searching. '),
  182. );
  183. // get publication properties list
  184. $properties = array();
  185. $properties[] = 'Any Field';
  186. $sql = "
  187. SELECT DISTINCT CVTS.cvterm_id, CVTS.name, CVTS.definition
  188. FROM {cvtermpath} CVTP
  189. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  190. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  191. INNER JOIN {cv} ON CVTO.cv_id = CV.cv_id
  192. WHERE CV.name = 'tripal_pub' and
  193. (CVTO.name = 'Publication Details' or CVTS.name = 'Publication Type') and
  194. NOT CVTS.is_obsolete = 1
  195. ORDER BY CVTS.name ASC
  196. ";
  197. $allowed_fields = variable_get('tripal_pub_allowed_search_fields', array());
  198. $prop_types = chado_query($sql);
  199. foreach ($prop_types as $prop) {
  200. if(array_key_exists($prop->cvterm_id, $allowed_fields) and $allowed_fields[$prop->cvterm_id] > 0) {
  201. $properties[$prop->cvterm_id] = $prop->name;
  202. }
  203. }
  204. for($i = 1; $i <= $num_criteria; $i++) {
  205. $search_terms = '';
  206. $scope = '';
  207. $operation = '';
  208. $mode = '';
  209. // first populate defaults using any values in the SESSION variable
  210. if (array_key_exists('tripal_pub_search_form', $_SESSION)) {
  211. $search_terms = $_SESSION['tripal_pub_search_form']['criteria'][$i]['search_terms'] ? $_SESSION['tripal_pub_search_form']['criteria'][$i]['search_terms'] : $search_terms;
  212. $scope = $_SESSION['tripal_pub_search_form']['criteria'][$i]['scope'] ? $_SESSION['tripal_pub_search_form']['criteria'][$i]['scope'] : $scope;
  213. $mode = $_SESSION['tripal_pub_search_form']['criteria'][$i]['mode'] ? $_SESSION['tripal_pub_search_form']['criteria'][$i]['mode'] : $mode;
  214. $operation = $_SESSION['tripal_pub_search_form']['criteria'][$i]['operation'] ? $_SESSION['tripal_pub_search_form']['criteria'][$i]['operation'] : $operation;
  215. }
  216. if (array_key_exists('values', $form_state)) {
  217. $search_terms = array_key_exists("search_terms-$i", $form_state['values']) ? $form_state['values']["search_terms-$i"] : $search_terms;
  218. $scope = array_key_exists("scope-$i", $form_state['values']) ? $form_state['values']["scope-$i"] : $scope;
  219. $mode = array_key_exists("mode-$i", $form_state['values']) ? $form_state['values']["mode-$i"] : $mode;
  220. $operation = array_key_exists("operation-$i", $form_state['values']) ? $form_state['values']["operation-$i"] : $operation;
  221. }
  222. if (array_key_exists('input', $form_state)) {
  223. $search_terms = array_key_exists("search_terms-$i", $form_state['input']) ? $form_state['input']["search_terms-$i"] : $search_terms;
  224. $scope = array_key_exists("scope-$i", $form_state['input']) ? $form_state['input']["scope-$i"] : $scope;
  225. $mode = array_key_exists("mode-$i", $form_state['input']) ? $form_state['input']["mode-$i"] : $mode;
  226. $operation = array_key_exists("operation-$i", $form_state['input']) ? $form_state['input']["operation-$i"] : $operation;
  227. }
  228. // default to searching the title and abstract
  229. if (!$scope) {
  230. $scope = 'abstract';
  231. }
  232. $form['criteria'][$i]["search_terms-$i"] = array(
  233. '#type' => 'textfield',
  234. '#default_value' => $search_terms,
  235. '#required' => FALSE,
  236. );
  237. $form['criteria'][$i]["scope-$i"] = array(
  238. '#type' => 'select',
  239. '#options' => $properties,
  240. '#default_value' => $scope,
  241. '#attributes' => array('class' => array('tripal-pub-search-form-scope-select')),
  242. );
  243. /*
  244. $form['criteria'][$i]["mode-$i"] = array(
  245. '#type' => 'select',
  246. '#options' => array(
  247. 'Contains' => 'Contains',
  248. 'Starts With' => 'Starts With',
  249. 'Ends With' => 'Ends With',
  250. 'Exactly' => 'Exactly'),
  251. '#default_value' => $mode,
  252. );*/
  253. if ($i > 1) {
  254. $form['criteria'][$i]["operation-$i"] = array(
  255. '#type' => 'select',
  256. '#options' => array(
  257. 'AND' => 'AND',
  258. 'OR' => 'OR',
  259. 'NOT' => 'NOT'),
  260. '#default_value' => $operation,
  261. );
  262. }
  263. if ($i == $num_criteria) {
  264. if($i > 1) {
  265. $form['criteria'][$i]["remove-$i"] = array(
  266. '#type' => 'button',
  267. '#name' => 'remove',
  268. '#value' => t('Remove'),
  269. '#ajax' => array(
  270. 'callback' => "tripal_pubs_search_form_ajax_update",
  271. 'wrapper' => 'tripal-pub-search-form-criteria',
  272. 'effect' => 'fade',
  273. 'method' => 'replace',
  274. 'prevent' => 'click'
  275. ),
  276. // When this button is clicked, the form will be validated and submitted.
  277. // Therefore, we set custom submit and validate functions to override the
  278. // default form submit. In the validate function we set the form_state
  279. // to rebuild the form so the submit function never actually gets called,
  280. // but we need it or Drupal will run the default validate anyway.
  281. // we also set #limit_validation_errors to empty so fields that
  282. // are required that don't have values won't generate warnings.
  283. '#submit' => array('tripal_pub_search_form_ajax_button_submit'),
  284. '#validate' => array('tripal_pub_search_form_ajax_button_validate'),
  285. '#limit_validation_errors' => array(),
  286. );
  287. }
  288. $form['criteria'][$i]["add-$i"] = array(
  289. '#type' => 'button',
  290. '#name' => 'add',
  291. '#value' => t('Add'),
  292. '#ajax' => array(
  293. 'callback' => "tripal_pubs_search_form_ajax_update",
  294. 'wrapper' => 'tripal-pub-search-form-criteria',
  295. 'effect' => 'fade',
  296. 'method' => 'replace',
  297. 'prevent' => 'click'
  298. ),
  299. // When this button is clicked, the form will be validated and submitted.
  300. // Therefore, we set custom submit and validate functions to override the
  301. // default form submit. In the validate function we set the form_state
  302. // to rebuild the form so the submit function never actually gets called,
  303. // but we need it or Drupal will run the default validate anyway.
  304. // we also set #limit_validation_errors to empty so fields that
  305. // are required that don't have values won't generate warnings.
  306. '#submit' => array('tripal_pub_search_form_ajax_button_submit'),
  307. '#validate' => array('tripal_pub_search_form_ajax_button_validate'),
  308. '#limit_validation_errors' => array(),
  309. );
  310. }
  311. }
  312. $form['criteria']["date"] = array(
  313. '#type' => 'select',
  314. '#options' => array('Years' => 'Years'),
  315. '#attributes' => array('class' => array('tripal-pub-search-form-scope-select')),
  316. );
  317. $form['criteria']["from_year"] = array(
  318. '#type' => 'textfield',
  319. '#default_value' => $from_year,
  320. '#required' => FALSE,
  321. '#title' => 'from',
  322. '#size' => 4,
  323. '#maxlength' => 4,
  324. );
  325. $form['criteria']["to_year"] = array(
  326. '#type' => 'textfield',
  327. '#default_value' => $to_year,
  328. '#required' => FALSE,
  329. '#title' => 'to',
  330. '#size' => 4,
  331. '#maxlength' => 4,
  332. );
  333. $form['search'] = array(
  334. '#type' => 'submit',
  335. '#value' => t('Search'),
  336. );
  337. $form['reset'] = array(
  338. '#type' => 'submit',
  339. '#value' => t('Reset'),
  340. );
  341. $form['criteria']['#theme'] = 'tripal_pub_search_setup_form_elements';
  342. return $form;
  343. }
  344. /**
  345. * This function is used to rebuild the form if an ajax call is made vai a button.
  346. * The button causes the form to be submitted. We don't want this so we override
  347. * the validate and submit routines on the form button. Therefore, this function
  348. * only needs to tell Drupal to rebuild the form
  349. *
  350. * @ingroup tripal_pub
  351. */
  352. function tripal_pub_search_form_ajax_button_submit() {
  353. $form_state['rebuild'] = TRUE;
  354. }
  355. /**
  356. * This function is just a dummy to override the default form submit on ajax calls for buttons
  357. *
  358. * @ingroup tripal_pub
  359. */
  360. function tripal_pub_search_form_ajax_button_validate() {
  361. // do nothing
  362. }
  363. /**
  364. * Validate the tripal_pub_search_form form
  365. *
  366. * @ingroup tripal_pub
  367. */
  368. function tripal_pub_search_form_validate($form, &$form_state) {
  369. $num_criteria = $form_state['storage']['num_criteria'];
  370. $from_year = $form_state['values']['from_year'];
  371. $to_year = $form_state['values']['to_year'];
  372. $op = $form_state['values']['op'];
  373. // no need to vlaidate on a reset
  374. if ($op == 'Reset') {
  375. return;
  376. }
  377. if($from_year and !$to_year) {
  378. form_set_error('to_year', 'Please provide a 4-digit year.');
  379. }
  380. if(!$from_year and $to_year) {
  381. form_set_error('from_year', 'Please provide a 4-digit year.');
  382. }
  383. if($from_year and !preg_match('/\d\d\d\d/' , $from_year)) {
  384. form_set_error('from_year', 'Please provide a 4-digit year.');
  385. }
  386. if($to_year and !preg_match('/\d\d\d\d/' , $to_year)) {
  387. form_set_error('to_year', 'Please provide a 4-digit year.');
  388. }
  389. }
  390. /**
  391. * Submit the tripal_pub_search_form form
  392. *
  393. * @ingroup tripal_pub
  394. */
  395. function tripal_pub_search_form_submit($form, &$form_state) {
  396. $num_criteria = $form_state['storage']['num_criteria'];
  397. $from_year = $form_state['values']['from_year'];
  398. $to_year = $form_state['values']['to_year'];
  399. $op = $form_state['values']['op'];
  400. // set the session variables
  401. if($op == 'Search') {
  402. $_SESSION['tripal_pub_search_form']['num_criteria'] = $num_criteria;
  403. unset($_SESSION['tripal_pub_search_form']['criteria']);
  404. for ($i = 0; $i <= $num_criteria; $i++) {
  405. $search_terms = '';
  406. $scope = '';
  407. $mode = 'Contains';
  408. $operation = '';
  409. if (array_key_exists("search_terms-$i", $form_state['values'])) {
  410. $search_terms = trim($form_state['values']["search_terms-$i"]);
  411. }
  412. if (array_key_exists("scope-$i", $form_state['values'])) {
  413. $scope = $form_state['values']["scope-$i"];
  414. }
  415. if (array_key_exists("operation-$i", $form_state['values'])) {
  416. $operation = $form_state['values']["operation-$i"];
  417. }
  418. //$mode = $form_state['values']["mode-$i"];
  419. $_SESSION['tripal_pub_search_form']['criteria'][$i] = array(
  420. 'search_terms' => $search_terms,
  421. 'scope' => $scope,
  422. 'mode' => $mode,
  423. 'operation' => $operation
  424. );
  425. }
  426. $_SESSION['tripal_pub_search_form']['from_year'] = $from_year;
  427. $_SESSION['tripal_pub_search_form']['to_year'] = $to_year;
  428. $_SESSION['tripal_pub_search_form']['perform_search'] = 1;
  429. }
  430. if($op == 'Reset') {
  431. unset($_SESSION['tripal_pub_search_form']);
  432. }
  433. }
  434. /**
  435. * Ajax callback to update the form
  436. *
  437. * @param $form
  438. * The form array
  439. * @param $form_state
  440. * The form state array
  441. *
  442. * @ingroup tripal_pub
  443. */
  444. function tripal_pubs_search_form_ajax_update($form, $form_state) {
  445. return $form['criteria'];
  446. }
  447. /**
  448. * Theme the tripal_pub_search_setup_form form
  449. *
  450. * @ingroup tripal_pub
  451. */
  452. function theme_tripal_pub_search_setup_form_elements($variables) {
  453. $form = $variables['form'];
  454. $rows = array();
  455. // put each criteria element in a single table row
  456. foreach ($form as $i => $element) {
  457. if(is_numeric($i)) {
  458. $rows[] = array(
  459. drupal_render($element["operation-$i"]),
  460. drupal_render($element["scope-$i"]),
  461. //drupal_render($element["mode-$i"]) .
  462. drupal_render($element["search_terms-$i"]),
  463. array(
  464. 'data' => drupal_render($element["add-$i"]) . drupal_render($element["remove-$i"]),
  465. 'nowrap' => 'nowrap',
  466. ),
  467. );
  468. }
  469. }
  470. // add in the from_year and to_year elements as the final row of the table
  471. $rows[] = array(
  472. '&nbsp;',
  473. drupal_render($form['date']),
  474. array(
  475. 'data' =>
  476. "<div id=\"pub-search-form-dates-row\">
  477. <div id=\"pub-search-form-dates\"> ".
  478. drupal_render($form['from_year']) .
  479. drupal_render($form['to_year']) . "
  480. </div>
  481. </div>
  482. ",
  483. ),
  484. ''
  485. );
  486. $headers = array();
  487. $table = array(
  488. 'header' => $headers,
  489. 'rows' => $rows,
  490. 'attributes' => array(
  491. 'id' => 'tripal-pub-search-form-table',
  492. 'border' => '0',
  493. 'class' => 'tripal-data-table'
  494. ),
  495. 'sticky' => TRUE,
  496. 'caption' => '',
  497. 'colgroups' => array(),
  498. 'empty' => '',
  499. );
  500. $results = '<div id="tripal-pub-search-form-criteria">';
  501. $results .= theme_table($table);
  502. $results .= '</div>';
  503. return $results;
  504. }
  505. /**
  506. * Builds the SQL statement need to search Chado for the publications
  507. * that match the user supplied criteria. Tpyically, this function is
  508. * called by the search form generated by the tripal_pub_search_form() function
  509. * but this function is included in the API for calling by anyone.
  510. *
  511. * @param $search_array
  512. * An array of search criteria provided by the user. The search array is
  513. * an associative array with the following keys:
  514. * 'num_criteria': an integer indicating the number of search criteria supplied
  515. * 'from_year': filters records by a start year
  516. * 'to_year': filters records by an end year
  517. * 'criteria': an array of criteria. Each criteria is an associative
  518. * array with the following keys:
  519. * 'search_terms': The text used for searching
  520. * 'scope': The cvterm_id of the property used for filtering
  521. * 'mode': The operation (e.g. AND, OR or NOT)
  522. * @param $offset
  523. * The offset for paging records. The first record returned will be
  524. * at the offset indicated here, and the next $limit number of records
  525. * will be returned.
  526. *
  527. * @param $limit
  528. * The number of records to retrieve
  529. *
  530. * @param total_records
  531. * A value passed by reference. This value will get set to the total
  532. * number of matching records
  533. *
  534. * @return
  535. * a PDO database object of the query results.
  536. *
  537. * @ingroup tripal_pub
  538. */
  539. function tripal_search_publications($search_array, $offset, $limit, &$total_records) {
  540. // build the SQL based on the criteria provided by the user
  541. $select = "SELECT DISTINCT P.*, CP.nid ";
  542. $from = "FROM {pub} P
  543. LEFT JOIN [chado_pub] CP on P.pub_id = CP.pub_id
  544. INNER JOIN {cvterm} CVT on CVT.cvterm_id = P.type_id
  545. ";
  546. $where = "WHERE (NOT P.title = 'null') "; // always exclude the dummy pub
  547. $order = "ORDER BY P.pyear DESC, P.title ASC";
  548. $args = array(); // arguments for where clause
  549. $join = 0;
  550. $num_criteria = $search_array['num_criteria'];
  551. $from_year = $search_array['from_year'];
  552. $to_year = $search_array['to_year'];
  553. for ($i = 1; $i <= $num_criteria; $i++) {
  554. $value = $search_array['criteria'][$i]['search_terms'];
  555. $type_id = $search_array['criteria'][$i]['scope'];
  556. $mode = $search_array['criteria'][$i]['mode'];
  557. $op = $search_array['criteria'][$i]['operation'];
  558. // skip criteria with no values
  559. if(!$value) {
  560. continue;
  561. }
  562. // to prevent SQL injection make sure our operator is
  563. // what we expect
  564. if ($op and $op != "AND" and $op != "OR" and $op != 'NOT') {
  565. $op = 'AND';
  566. }
  567. if ($op == 'NOT') {
  568. $op = 'AND NOT';
  569. }
  570. if (!$op) {
  571. $op = 'AND';
  572. }
  573. // get the scope type
  574. $values = array('cvterm_id' => $type_id);
  575. $cvterm = chado_select_record('cvterm', array('name'), $values);
  576. $type_name = '';
  577. if (count($cvterm) > 0) {
  578. $type_name = $cvterm[0]->name;
  579. }
  580. if ($type_name == 'Title') {
  581. $where .= " $op (lower(P.title) LIKE lower(:crit$i)) ";
  582. $args[":crit$i"] = '%' . $value . '%';
  583. }
  584. elseif ($type_name == 'Year') {
  585. $where .= " $op (lower(P.pyear) = lower(:crit$i)) ";
  586. $args[":crit$i"] = '%' . $value . '%';
  587. }
  588. elseif ($type_name == 'Volume') {
  589. $where .= " $op (lower(P.volume) = lower(:crit$i)) ";
  590. $args[":crit$i"] = '%' . $value . '%';
  591. }
  592. elseif ($type_name == 'Issue') {
  593. $where .= " $op (lower(P.issue) = lower(:crit$i)) ";
  594. $args[":crit$i"] = '%' . $value . '%';
  595. }
  596. elseif ($type_name == 'Journal Name') {
  597. $from .= " LEFT JOIN {pubprop} PP$i ON PP$i.pub_id = P.pub_id AND PP$i.type_id = :crit$i ";
  598. $where .= " $op ((lower(P.series_name) = lower(:crit$i) and CVT.name = 'Journal Article') OR
  599. (lower(PP$i.value) = lower(:crit$i))) ";
  600. $args[":crit$i"] = $type_id;
  601. }
  602. elseif ($type_name == 'Conference Name') {
  603. $from .= " LEFT JOIN {pubprop} PP$i ON PP$i.pub_id = P.pub_id AND PP$i.type_id = :crit$i ";
  604. $where .= " $op ((lower(P.series_name) = lower(:crit$i) and CVT.name = 'Conference Proceedings') OR
  605. (lower(PP$i.value) = lower(:crit$i))) ";
  606. $args[":crit$i"] = $type_id;
  607. }
  608. elseif ($type_name == 'Publication Type') {
  609. $where .= " $op (lower(CVT.name) = lower(:crit$i))";
  610. $args[":crit$i"] = $value;
  611. }
  612. elseif ($type_id == 0) { //'Any Field'
  613. $from .= " LEFT JOIN {pubprop} PP$i ON PP$i.pub_id = P.pub_id ";
  614. $where .= " $op (lower(PP$i.value) LIKE lower(:crit$i) OR
  615. lower(P.title) LIKE lower(:crit$i) OR
  616. lower(P.volumetitle) LIKE lower(:crit$i) OR
  617. lower(P.publisher) LIKE lower(:crit$i) OR
  618. lower(P.uniquename) LIKE lower(:crit$i) OR
  619. lower(P.pubplace) LIKE lower(:crit$i) OR
  620. lower(P.miniref) LIKE lower(:crit$i) OR
  621. lower(P.series_name) LIKE lower(:crit$i)) ";
  622. $args[":crit$i"] = '%' . $value . '%';
  623. }
  624. // for all other properties
  625. else {
  626. $from .= " LEFT JOIN {pubprop} PP$i ON PP$i.pub_id = P.pub_id AND PP$i.type_id = :type_id$i ";
  627. $where .= " $op (lower(PP$i.value) LIKE lower(:crit$i)) ";
  628. $args[":crit$i"] = '%' . $value . '%';
  629. $args[":type_id$i"] = $type_id;
  630. }
  631. }
  632. if($from_year and $to_year) {
  633. $where .= " AND (P.pyear ~ '....' AND to_number(P.pyear,'9999') >= :from$i AND to_number(P.pyear,'9999') <= :to$i) ";
  634. $args[":from$i"] = $from_year;
  635. $args[":to$i"] = $to_year;
  636. }
  637. $sql = "$select $from $where $order LIMIT " . (int) $limit . ' OFFSET ' . (int) $offset;
  638. $count = "SELECT count(*) FROM ($select $from $where $order) as t1";
  639. // first get the total number of matches
  640. $total_records = chado_query($count, $args)->fetchField();
  641. $results = chado_query($sql, $args);
  642. return $results;
  643. }