tripal_pub.pub_search.inc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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. $limit = 25;
  16. // generate the search form
  17. $form = drupal_get_form('tripal_pub_search_form');
  18. $output = drupal_render($form);
  19. // retrieve any results
  20. if (array_key_exists('tripal_pub_search_form', $_SESSION) and
  21. $_SESSION['tripal_pub_search_form']['perform_search']) {
  22. $num_criteria = $_SESSION['tripal_pub_search_form']['num_criteria'];
  23. $from_year = $_SESSION['tripal_pub_search_form']['from_year'];
  24. $to_year = $_SESSION['tripal_pub_search_form']['to_year'];
  25. $search_array = array();
  26. $search_array['num_criteria'] = $num_criteria;
  27. $search_array['from_year'] = $from_year;
  28. $search_array['to_year'] = $to_year;
  29. for ($i = 0; $i <= $num_criteria; $i++) {
  30. $search_array['criteria'][$i]['search_terms'] = $_SESSION['tripal_pub_search_form']['criteria'][$i]['search_terms'];
  31. $search_array['criteria'][$i]['scope'] = $_SESSION['tripal_pub_search_form']['criteria'][$i]['scope'];
  32. $search_array['criteria'][$i]['mode'] = $_SESSION['tripal_pub_search_form']['criteria'][$i]['mode'];
  33. $search_array['criteria'][$i]['operation'] = $_SESSION['tripal_pub_search_form']['criteria'][$i]['operation'];
  34. }
  35. // get the list of publications from the remote database using the search criteria.
  36. $page = isset($_GET['page']) ? $_GET['page'] : '0';
  37. $offset = $page * $limit;
  38. $total_records = 0;
  39. $pubs = pub_search($search_array, $offset, $limit, $total_records);
  40. pager_default_initialize($total_records, $limit, 0);
  41. // iterate through the results and construct the table displaying the publications
  42. $rows = array();
  43. $i = $page * $limit + 1;
  44. foreach ($pubs as $pub) {
  45. // get the citation for this publication
  46. $values = array(
  47. 'pub_id' => $pub->pub_id,
  48. 'type_id' => array(
  49. 'name' => 'Citation',
  50. ),
  51. );
  52. $citation_rec = chado_generate_var('pubprop', $values);
  53. $citation_rec = chado_expand_var($citation_rec, 'field', 'pubprop.value');
  54. // if we have the citation then use it, otherwise, just use the title
  55. $title = htmlspecialchars($pub->title);
  56. $result = $title;
  57. if ($pub->nid) {
  58. $result = l($title ,'node/' . $pub->nid, array('attributes' => array('target' => '_blank')));
  59. }
  60. if ($citation_rec->value) {
  61. $citation = htmlspecialchars($citation_rec->value);
  62. $result .= '<br>' . $citation;
  63. }
  64. $rows[] = array(
  65. number_format($i) . ".",
  66. $pub->pyear,
  67. $result
  68. );
  69. $i++;
  70. }
  71. if (count($rows) == 0) {
  72. $rows[] = array(
  73. array(
  74. 'data' => 'No results found',
  75. 'colspan' => 3
  76. )
  77. );
  78. }
  79. $headers = array('', 'Year', 'Publication');
  80. $table = array(
  81. 'header' => $headers,
  82. 'rows' => $rows,
  83. 'attributes' => array(
  84. 'id' => 'tripal-pub-search-results-table',
  85. 'border' => '0',
  86. 'class' => 'tripal-data-table'
  87. ),
  88. 'sticky' => TRUE,
  89. 'caption' => '',
  90. 'colgroups' => array(),
  91. 'empty' => '',
  92. );
  93. $results = theme_table($table);
  94. // generate the pager
  95. $pager = array(
  96. 'tags' => array(),
  97. 'element' => 0,
  98. 'parameters' => array(),
  99. 'quantity' => $limit,
  100. );
  101. $pager = theme_pager($pager);
  102. // join all to form the results
  103. $output .= "<p><b>Found " . number_format($total_records) .
  104. " Results</b></br>" . $results . $pager;
  105. }
  106. return $output;
  107. }
  108. /**
  109. * Provides the form to search pubmed
  110. *
  111. * @ingroup tripal_pub
  112. */
  113. function tripal_pub_search_form($form, &$form_state) {
  114. // Default values can come in the following ways:
  115. //
  116. // 1) as elements of the $pub_importer object. This occurs when editing an existing importer
  117. // 2) in the $form_state['values'] array which occurs on a failed validation or
  118. // ajax callbacks from non submit form elements
  119. // 3) in the $form_state['input'] array which occurs on ajax callbacks from submit
  120. // form elements and the form is being rebuilt
  121. //
  122. // Set the default values. If the pub_import_id isn't already defined by the form values
  123. // and one is provided then look it up in the database
  124. $criteria = NULL;
  125. $num_criteria = 2;
  126. $from_year = '';
  127. $to_year = '';
  128. // if the session has variables then use those. This should only happen when
  129. // the 'Test Criteria' button is clicked.
  130. if (array_key_exists('tripal_pub_search_form', $_SESSION)) {
  131. $num_criteria = $_SESSION['tripal_pub_search_form']['num_criteria'] ? $_SESSION['tripal_pub_search_form']['num_criteria'] : $num_criteria;
  132. $from_year = $_SESSION['tripal_pub_search_form']['from_year'] ? $_SESSION['tripal_pub_search_form']['from_year'] : '';
  133. $to_year = $_SESSION['tripal_pub_search_form']['to_year'] ? $_SESSION['tripal_pub_search_form']['to_year'] : '';
  134. }
  135. if (array_key_exists('values', $form_state)) {
  136. $num_criteria = $form_state['values']['num_criteria'] ? $form_state['values']['num_criteria'] : $num_criteria;
  137. $from_year = $form_state['values']['from_year'] ? $form_state['values']['from_year'] : $from_year;
  138. $to_year = $form_state['values']['to_year'] ? $form_state['values']['to_year'] : $to_year;
  139. }
  140. if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
  141. $num_criteria = $form_state['input']['num_criteria'] ? $form_state['input']['num_criteria'] : $num_criteria;
  142. $from_year = $form_state['input']['from_year'] ? $form_state['input']['from_year'] : $from_year;
  143. $to_year = $form_state['input']['to_year'] ? $form_state['input']['to_year'] : $to_year;
  144. }
  145. if (array_key_exists('triggering_element', $form_state) and
  146. $form_state['triggering_element']['#name'] == 'add') {
  147. $num_criteria++;
  148. }
  149. if (array_key_exists('triggering_element', $form_state) and
  150. $form_state['triggering_element']['#name'] == 'remove') {
  151. $num_criteria--;
  152. }
  153. $form['num_criteria']= array(
  154. '#type' => 'hidden',
  155. '#default_value' => $num_criteria,
  156. );
  157. $form['admin-instructions'] = array(
  158. '#markup' => tripal_set_message(
  159. t('Administrators, you can select the fields with which a user can use to search, by checking the desired fields on the ' .
  160. l('Publication Module Settings Page', 'admin/tripal/chado/tripal_pub/configuration', array('attributes' => array('target' => '_blank'))) . '
  161. in the section titled "Search Options". The selected fields will appear in the dropdowns below.'),
  162. TRIPAL_INFO,
  163. array('return_html' => 1)),
  164. );
  165. $form['instructions'] = array(
  166. '#markup' => t('To search for publications enter keywords in the text boxes below.
  167. You can limit your search by selecting the field in the dropdown box. Click the
  168. add and remove buttons to add additional fields for searching. '),
  169. );
  170. // get publication properties list
  171. $properties = array();
  172. $properties[] = 'Any Field';
  173. $sql = "
  174. SELECT DISTINCT CVTS.cvterm_id, CVTS.name, CVTS.definition
  175. FROM {cvtermpath} CVTP
  176. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  177. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  178. INNER JOIN {cv} ON CVTO.cv_id = CV.cv_id
  179. WHERE CV.name = 'tripal_pub' and
  180. (CVTO.name = 'Publication Details' or CVTS.name = 'Publication Type') and
  181. NOT CVTS.is_obsolete = 1
  182. ORDER BY CVTS.name ASC
  183. ";
  184. $allowed_fields = variable_get('tripal_pub_allowed_search_fields', array());
  185. $prop_types = chado_query($sql);
  186. foreach ($prop_types as $prop) {
  187. if(array_key_exists($prop->cvterm_id, $allowed_fields) and $allowed_fields[$prop->cvterm_id] > 0) {
  188. $properties[$prop->cvterm_id] = $prop->name;
  189. }
  190. }
  191. for($i = 1; $i <= $num_criteria; $i++) {
  192. $search_terms = '';
  193. $scope = '';
  194. $operation = '';
  195. $mode = '';
  196. // first populate defaults using any values in the SESSION variable
  197. if (array_key_exists('tripal_pub_search_form', $_SESSION)) {
  198. $search_terms = $_SESSION['tripal_pub_search_form']['criteria'][$i]['search_terms'] ? $_SESSION['tripal_pub_search_form']['criteria'][$i]['search_terms'] : $search_terms;
  199. $scope = $_SESSION['tripal_pub_search_form']['criteria'][$i]['scope'] ? $_SESSION['tripal_pub_search_form']['criteria'][$i]['scope'] : $scope;
  200. $mode = $_SESSION['tripal_pub_search_form']['criteria'][$i]['mode'] ? $_SESSION['tripal_pub_search_form']['criteria'][$i]['mode'] : $mode;
  201. $operation = $_SESSION['tripal_pub_search_form']['criteria'][$i]['operation'] ? $_SESSION['tripal_pub_search_form']['criteria'][$i]['operation'] : $operation;
  202. }
  203. if (array_key_exists('values', $form_state)) {
  204. $search_terms = array_key_exists("search_terms-$i", $form_state['values']) ? $form_state['values']["search_terms-$i"] : $search_terms;
  205. $scope = array_key_exists("scope-$i", $form_state['values']) ? $form_state['values']["scope-$i"] : $scope;
  206. $mode = array_key_exists("mode-$i", $form_state['values']) ? $form_state['values']["mode-$i"] : $mode;
  207. $operation = array_key_exists("operation-$i", $form_state['values']) ? $form_state['values']["operation-$i"] : $operation;
  208. }
  209. if (array_key_exists('input', $form_state)) {
  210. $search_terms = array_key_exists("search_terms-$i", $form_state['input']) ? $form_state['input']["search_terms-$i"] : $search_terms;
  211. $scope = array_key_exists("scope-$i", $form_state['input']) ? $form_state['input']["scope-$i"] : $scope;
  212. $mode = array_key_exists("mode-$i", $form_state['input']) ? $form_state['input']["mode-$i"] : $mode;
  213. $operation = array_key_exists("operation-$i", $form_state['input']) ? $form_state['input']["operation-$i"] : $operation;
  214. }
  215. // default to searching the title and abstract
  216. if (!$scope) {
  217. $scope = 'abstract';
  218. }
  219. $form['criteria'][$i]["search_terms-$i"] = array(
  220. '#type' => 'textfield',
  221. '#default_value' => $search_terms,
  222. '#required' => FALSE,
  223. );
  224. $form['criteria'][$i]["scope-$i"] = array(
  225. '#type' => 'select',
  226. '#options' => $properties,
  227. '#default_value' => $scope,
  228. '#attributes' => array('class' => array('tripal-pub-search-form-scope-select')),
  229. );
  230. /*
  231. $form['criteria'][$i]["mode-$i"] = array(
  232. '#type' => 'select',
  233. '#options' => array(
  234. 'Contains' => 'Contains',
  235. 'Starts With' => 'Starts With',
  236. 'Ends With' => 'Ends With',
  237. 'Exactly' => 'Exactly'),
  238. '#default_value' => $mode,
  239. );*/
  240. if ($i > 1) {
  241. $form['criteria'][$i]["operation-$i"] = array(
  242. '#type' => 'select',
  243. '#options' => array(
  244. 'AND' => 'AND',
  245. 'OR' => 'OR',
  246. 'NOT' => 'NOT'),
  247. '#default_value' => $operation,
  248. );
  249. }
  250. if ($i == $num_criteria) {
  251. if($i > 1) {
  252. $form['criteria'][$i]["remove-$i"] = array(
  253. '#type' => 'button',
  254. '#name' => 'remove',
  255. '#value' => t('Remove'),
  256. '#ajax' => array(
  257. 'callback' => "tripal_pubs_search_form_ajax_update",
  258. 'wrapper' => 'tripal-pub-search-form-criteria',
  259. 'effect' => 'fade',
  260. 'method' => 'replace',
  261. 'prevent' => 'click'
  262. ),
  263. // When this button is clicked, the form will be validated and submitted.
  264. // Therefore, we set custom submit and validate functions to override the
  265. // default form submit. In the validate function we set the form_state
  266. // to rebuild the form so the submit function never actually gets called,
  267. // but we need it or Drupal will run the default validate anyway.
  268. // we also set #limit_validation_errors to empty so fields that
  269. // are required that don't have values won't generate warnings.
  270. '#submit' => array('tripal_pub_search_form_ajax_button_submit'),
  271. '#validate' => array('tripal_pub_search_form_ajax_button_validate'),
  272. '#limit_validation_errors' => array(),
  273. );
  274. }
  275. $form['criteria'][$i]["add-$i"] = array(
  276. '#type' => 'button',
  277. '#name' => 'add',
  278. '#value' => t('Add'),
  279. '#ajax' => array(
  280. 'callback' => "tripal_pubs_search_form_ajax_update",
  281. 'wrapper' => 'tripal-pub-search-form-criteria',
  282. 'effect' => 'fade',
  283. 'method' => 'replace',
  284. 'prevent' => 'click'
  285. ),
  286. // When this button is clicked, the form will be validated and submitted.
  287. // Therefore, we set custom submit and validate functions to override the
  288. // default form submit. In the validate function we set the form_state
  289. // to rebuild the form so the submit function never actually gets called,
  290. // but we need it or Drupal will run the default validate anyway.
  291. // we also set #limit_validation_errors to empty so fields that
  292. // are required that don't have values won't generate warnings.
  293. '#submit' => array('tripal_pub_search_form_ajax_button_submit'),
  294. '#validate' => array('tripal_pub_search_form_ajax_button_validate'),
  295. '#limit_validation_errors' => array(),
  296. );
  297. }
  298. }
  299. $form['criteria']["date"] = array(
  300. '#type' => 'select',
  301. '#options' => array('Years' => 'Years'),
  302. '#attributes' => array('class' => array('tripal-pub-search-form-scope-select')),
  303. );
  304. $form['criteria']["from_year"] = array(
  305. '#type' => 'textfield',
  306. '#default_value' => $from_year,
  307. '#required' => FALSE,
  308. '#title' => 'from',
  309. '#size' => 4,
  310. '#maxlength' => 4,
  311. );
  312. $form['criteria']["to_year"] = array(
  313. '#type' => 'textfield',
  314. '#default_value' => $to_year,
  315. '#required' => FALSE,
  316. '#title' => 'to',
  317. '#size' => 4,
  318. '#maxlength' => 4,
  319. );
  320. $form['search'] = array(
  321. '#type' => 'submit',
  322. '#value' => t('Search'),
  323. );
  324. $form['reset'] = array(
  325. '#type' => 'submit',
  326. '#value' => t('Reset'),
  327. );
  328. $form['criteria']['#theme'] = 'tripal_pub_search_setup_form_elements';
  329. return $form;
  330. }
  331. /**
  332. * This function is used to rebuild the form if an ajax call is made vai a button.
  333. * The button causes the form to be submitted. We don't want this so we override
  334. * the validate and submit routines on the form button. Therefore, this function
  335. * only needs to tell Drupal to rebuild the form
  336. *
  337. * @ingroup tripal_pub
  338. */
  339. function tripal_pub_search_form_ajax_button_submit() {
  340. $form_state['rebuild'] = TRUE;
  341. }
  342. /**
  343. * This function is just a dummy to override the default form submit on ajax calls for buttons
  344. *
  345. * @ingroup tripal_pub
  346. */
  347. function tripal_pub_search_form_ajax_button_validate() {
  348. // do nothing
  349. }
  350. /**
  351. * Validate the tripal_pub_search_form form
  352. *
  353. * @ingroup tripal_pub
  354. */
  355. function tripal_pub_search_form_validate($form, &$form_state) {
  356. $num_criteria = $form_state['values']['num_criteria'];
  357. $from_year = $form_state['values']['from_year'];
  358. $to_year = $form_state['values']['to_year'];
  359. $op = $form_state['values']['op'];
  360. // no need to vlaidate on a reset
  361. if ($op == 'Reset') {
  362. return;
  363. }
  364. if($from_year and !$to_year) {
  365. form_set_error('to_year', 'Please provide a 4-digit year.');
  366. }
  367. if(!$from_year and $to_year) {
  368. form_set_error('from_year', 'Please provide a 4-digit year.');
  369. }
  370. if($from_year and !preg_match('/\d\d\d\d/' , $from_year)) {
  371. form_set_error('from_year', 'Please provide a 4-digit year.');
  372. }
  373. if($to_year and !preg_match('/\d\d\d\d/' , $to_year)) {
  374. form_set_error('to_year', 'Please provide a 4-digit year.');
  375. }
  376. }
  377. /**
  378. * Submit the tripal_pub_search_form form
  379. *
  380. * @ingroup tripal_pub
  381. */
  382. function tripal_pub_search_form_submit($form, &$form_state) {
  383. $num_criteria = $form_state['values']['num_criteria'];
  384. $from_year = $form_state['values']['from_year'];
  385. $to_year = $form_state['values']['to_year'];
  386. $op = $form_state['values']['op'];
  387. // set the session variables
  388. if($op == 'Search') {
  389. $_SESSION['tripal_pub_search_form']['num_criteria'] = $num_criteria;
  390. unset($_SESSION['tripal_pub_search_form']['criteria']);
  391. for ($i = 0; $i <= $num_criteria; $i++) {
  392. $search_terms = '';
  393. $scope = '';
  394. $mode = 'Contains';
  395. $operation = '';
  396. if (array_key_exists("search_terms-$i", $form_state['values'])) {
  397. $search_terms = trim($form_state['values']["search_terms-$i"]);
  398. }
  399. if (array_key_exists("scope-$i", $form_state['values'])) {
  400. $scope = $form_state['values']["scope-$i"];
  401. }
  402. if (array_key_exists("operation-$i", $form_state['values'])) {
  403. $operation = $form_state['values']["operation-$i"];
  404. }
  405. //$mode = $form_state['values']["mode-$i"];
  406. $_SESSION['tripal_pub_search_form']['criteria'][$i] = array(
  407. 'search_terms' => $search_terms,
  408. 'scope' => $scope,
  409. 'mode' => $mode,
  410. 'operation' => $operation
  411. );
  412. }
  413. $_SESSION['tripal_pub_search_form']['from_year'] = $from_year;
  414. $_SESSION['tripal_pub_search_form']['to_year'] = $to_year;
  415. $_SESSION['tripal_pub_search_form']['perform_search'] = 1;
  416. }
  417. if($op == 'Reset') {
  418. unset($_SESSION['tripal_pub_search_form']);
  419. }
  420. }
  421. /**
  422. * Ajax callback to update the form
  423. *
  424. * @param $form
  425. * The form array
  426. * @param $form_state
  427. * The form state array
  428. *
  429. * @ingroup tripal_pub
  430. */
  431. function tripal_pubs_search_form_ajax_update($form, $form_state) {
  432. return $form['criteria'];
  433. }
  434. /**
  435. * Theme the tripal_pub_search_setup_form form
  436. *
  437. * @ingroup tripal_pub
  438. */
  439. function theme_tripal_pub_search_setup_form_elements($variables) {
  440. $form = $variables['form'];
  441. $rows = array();
  442. // put each criteria element in a single table row
  443. foreach ($form as $i => $element) {
  444. if(is_numeric($i)) {
  445. $rows[] = array(
  446. drupal_render($element["operation-$i"]),
  447. drupal_render($element["scope-$i"]),
  448. //drupal_render($element["mode-$i"]) .
  449. drupal_render($element["search_terms-$i"]),
  450. array(
  451. 'data' => drupal_render($element["add-$i"]) . drupal_render($element["remove-$i"]),
  452. 'nowrap' => 'nowrap',
  453. ),
  454. );
  455. }
  456. }
  457. // add in the from_year and to_year elements as the final row of the table
  458. $rows[] = array(
  459. '&nbsp;',
  460. drupal_render($form['date']),
  461. array(
  462. 'data' =>
  463. "<div id=\"pub-search-form-dates-row\">
  464. <div id=\"pub-search-form-dates\"> ".
  465. drupal_render($form['from_year']) .
  466. drupal_render($form['to_year']) . "
  467. </div>
  468. </div>
  469. ",
  470. ),
  471. ''
  472. );
  473. $headers = array();
  474. $table = array(
  475. 'header' => $headers,
  476. 'rows' => $rows,
  477. 'attributes' => array(
  478. 'id' => 'tripal-pub-search-form-table',
  479. 'border' => '0',
  480. 'class' => 'tripal-data-table'
  481. ),
  482. 'sticky' => TRUE,
  483. 'caption' => '',
  484. 'colgroups' => array(),
  485. 'empty' => '',
  486. );
  487. $results = '<div id="tripal-pub-search-form-criteria">';
  488. $results .= theme_table($table);
  489. $results .= '</div>';
  490. return $results;
  491. }