remote_search.inc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /*
  3. *
  4. */
  5. function theme_tripal_pub_remote_search_form($form) {
  6. $rows = array(
  7. array(
  8. drupal_render($form['scope']),
  9. drupal_render($form['operation']),
  10. drupal_render($form['search_terms']),
  11. ),
  12. );
  13. $headers = array('Search Terms', 'Scope', 'Operation');
  14. $form['criteria'] = array(
  15. '#type' => 'markup',
  16. '#value' => theme('table', $headers, $rows),
  17. '#weight' => -10,
  18. );
  19. return drupal_render($form);
  20. }
  21. /**
  22. * Purpose: Provides the form to search pubmed
  23. *
  24. * @ingroup tripal_pub
  25. */
  26. function tripal_pub_remote_search_form(&$form_state = NULL) {
  27. $remote_db = $_SESSION['tripal_pub_search_criteria']['remote_db'];
  28. $search_terms = $_SESSION['tripal_pub_search_criteria']['search_terms'];
  29. $days = $_SESSION['tripal_pub_search_criteria']['days'];
  30. $scope = $_SESSION['tripal_pub_search_criteria']['scope'];
  31. $operation = $_SESSION['tripal_pub_search_criteria']['operation'];
  32. $remote_dbs = array('Pubmed' => 'Pubmed');
  33. $form['remote_db'] = array(
  34. '#title' => t('Remote Publication Database'),
  35. '#type' => 'select',
  36. '#options' => $remote_dbs,
  37. '#default_value' => $remote_db,
  38. );
  39. $form['search_terms']= array(
  40. '#type' => 'textfield',
  41. '#description' => t('Please provide a list of words, separated by spaces for searching.'),
  42. '#default_value' => $search_terms,
  43. );
  44. $form['scope']= array(
  45. '#type' => 'select',
  46. '#description' => t('Please select the fields to search for this term.'),
  47. '#options' => array('title' => 'Title',
  48. 'abstract' => 'Title/Abstract',
  49. 'author' => 'Author'),
  50. '#default_value' => $scope,
  51. );
  52. $form['operation']= array(
  53. '#type' => 'select',
  54. '#options' => array('AND' => 'AND',
  55. 'OR' => 'OR',
  56. 'NOT' => 'NOT'),
  57. '#default_value' => $operation,
  58. );
  59. $form['days']= array(
  60. '#type' => 'textfield',
  61. '#title' => t('Number of Days'),
  62. '#description' => t('Please provide the number of days to limit your search. For example, to search only the last 60 days, enter the number 60 in the field.'),
  63. '#default_value' => $days,
  64. );
  65. $form['submit'] = array(
  66. '#type' => 'submit',
  67. '#value' => t('Submit'),
  68. '#executes_submit_callback' => TRUE,
  69. );
  70. return $form;
  71. }
  72. /**
  73. *
  74. */
  75. function tripal_pub_remote_search_form_validate($form, &$form_state) {
  76. $remote_db = $form_state['values']['remote_db'];
  77. $search_terms = $form_state['values']['search_terms'];
  78. $scope = $form_state['values']['scope'];
  79. $operation = $form_state['values']['operation'];
  80. $days = $form_state['values']['days'];
  81. if ($days and !is_numeric($days) or preg_match('/\./', $days)) {
  82. form_set_error('days', "Please enter a numeric, non decimal value, for the number of days.");
  83. }
  84. }
  85. /**
  86. *
  87. */
  88. function tripal_pub_remote_search_form_submit($form, &$form_state) {
  89. $remote_db = $form_state['values']['remote_db'];
  90. $search_terms = $form_state['values']['search_terms'];
  91. $scope = $form_state['values']['scope'];
  92. $operation = $form_state['values']['operation'];
  93. $days = $form_state['values']['days'];
  94. // store the search settings in a session variable. Then when the page
  95. // is refreshed these values will be available for the page to
  96. // generate results.
  97. if ($op == 'Reset') {
  98. unset($_SESSION['tripal_pub_search_criteria']);
  99. unset($form_state['values']);
  100. }
  101. else {
  102. $_SESSION['tripal_pub_search_criteria'] = array(
  103. 'remote_db' => $remote_db,
  104. 'search_terms' => $search_terms,
  105. 'scope' => $scope,
  106. 'operation' => $operation,
  107. 'days' => $days
  108. );
  109. }
  110. }
  111. /*
  112. *
  113. */
  114. function tripal_pub_remote_search_page() {
  115. global $pager_total, $pager_total_items;
  116. $pager_id = 0;
  117. $limit = 10;
  118. // generate the search form
  119. $form = drupal_get_form('tripal_pub_remote_search_form');
  120. // retrieve any results
  121. $remote_db = $_SESSION['tripal_pub_search_criteria']['remote_db'];
  122. $search_array['search_terms'] = $_SESSION['tripal_pub_search_criteria']['search_terms'];
  123. $search_array['days'] = $_SESSION['tripal_pub_search_criteria']['days'];
  124. $search_array['scope'] = $_SESSION['tripal_pub_search_criteria']['scope'];
  125. $search_array['operation'] = $_SESSION['tripal_pub_search_criteria']['operation'];
  126. // get the list of publications from the remote database using the search criteria.
  127. $pubs = tripal_pub_get_remote_search_results($remote_db, $search_array, $limit, $pager_id);
  128. // generate the pager
  129. $total_pages = $pager_total[$pager_id];
  130. $total_items = $pager_total_items[$pager_id];
  131. $page = isset($_GET['page']) ? $_GET['page'] : '0';
  132. $pager = theme('pager');
  133. // iterate through the results and construct the table displaying the publications
  134. $rows = array();
  135. $i = $page * $limit + 1;
  136. if (count($pubs) > 0) {
  137. foreach ($pubs as $pub) {
  138. $rows[] = array(number_format($i), $pub['citation']);
  139. $i++;
  140. }
  141. }
  142. $headers = array('', 'Citation');
  143. $table = theme('table', $headers, $rows);
  144. // join all to form the final page
  145. $output = $form . "<p><b>Found " . number_format($total_items) .
  146. ". Page " . ($page + 1) . " of $total_pages. " .
  147. " Results</b></br>" . $table . '</p>' . $pager;
  148. return $output;
  149. }
  150. /*
  151. *
  152. */
  153. function tripal_pub_get_remote_search_results($remote_db, $search_array, $num_to_retrieve, $pager_id) {
  154. // construct the callback function using the remote database name
  155. $callback = 'tripal_pub_remote_search_' . strtolower($remote_db);
  156. // now call the callback function to get the rsults
  157. $pubs = array();
  158. if (function_exists($callback)) {
  159. $pubs = call_user_func($callback, $search_array, $num_to_retrieve, $pager_id);
  160. }
  161. return $pubs;
  162. }