tripal_pub.pub_search.inc 25 KB

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