remote_search.inc 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. /*
  3. *
  4. */
  5. function tripal_pub_remote_search_page() {
  6. global $pager_total, $pager_total_items;
  7. $pager_id = 0;
  8. $limit = 10;
  9. // generate the search form
  10. $form = drupal_get_form('tripal_pub_remote_search_form');
  11. // retrieve any results
  12. $remote_db = $_SESSION['tripal_pub_search']['remote_db'];
  13. $num_criteria = $_SESSION['tripal_pub_search']['num_criteria'];
  14. $days = $_SESSION['tripal_pub_search']['days'];
  15. $search_array = array();
  16. $search_array['remote_db'] = $remote_db;
  17. $search_array['num_criteria'] = $num_criteria;
  18. $search_array['days'] = $days;
  19. for ($i = 0; $i <= $num_criteria; $i++) {
  20. $search_array['criteria'][$i]['search_terms'] = $_SESSION['tripal_pub_search']['criteria'][$i]['search_terms'];
  21. $search_array['criteria'][$i]['scope'] = $_SESSION['tripal_pub_search']['criteria'][$i]['scope'];
  22. $search_array['criteria'][$i]['operation'] = $_SESSION['tripal_pub_search']['criteria'][$i]['operation'];
  23. }
  24. // get the list of publications from the remote database using the search criteria.
  25. $pubs = tripal_pub_get_remote_search_results($remote_db, $search_array, $limit, $pager_id);
  26. // generate the pager
  27. $total_pages = $pager_total[$pager_id];
  28. $total_items = $pager_total_items[$pager_id];
  29. $page = isset($_GET['page']) ? $_GET['page'] : '0';
  30. $pager = theme('pager');
  31. // iterate through the results and construct the table displaying the publications
  32. $rows = array();
  33. $i = $page * $limit + 1;
  34. if (count($pubs) > 0) {
  35. foreach ($pubs as $pub) {
  36. $rows[] = array(number_format($i), $pub['citation']);
  37. $i++;
  38. }
  39. }
  40. $headers = array('', 'Citation');
  41. $table = theme('table', $headers, $rows);
  42. // join all to form the final page
  43. $output = $form . "<p><b>Found " . number_format($total_items) .
  44. ". Page " . ($page + 1) . " of $total_pages. " .
  45. " Results</b></br>" . $table . '</p>' . $pager;
  46. return $output;
  47. }
  48. /*
  49. *
  50. */
  51. function theme_tripal_pub_remote_search_form($form) {
  52. $rows = array();
  53. foreach ($form['criteria'] as $i => $element) {
  54. if(is_numeric($i)) {
  55. $rows[] = array(
  56. array('data' => drupal_render($element["operation-$i"]), 'width' => '10%'),
  57. array('data' => drupal_render($element["scope-$i"]), 'width' => '10%'),
  58. drupal_render($element["search_terms-$i"]),
  59. array('data' => drupal_render($element["add-$i"]) . drupal_render($element["remove-$i"]), 'width' => '5%'),
  60. );
  61. }
  62. }
  63. $headers = array('Operation','Scope', 'Search Terms', '');
  64. $form['criteria'] = array(
  65. '#type' => 'markup',
  66. '#value' => theme('table', $headers, $rows),
  67. '#weight' => -10,
  68. );
  69. return drupal_render($form);
  70. }
  71. /**
  72. * Purpose: Provides the form to search pubmed
  73. *
  74. * @ingroup tripal_pub
  75. */
  76. function tripal_pub_remote_search_form(&$form_state = NULL) {
  77. tripal_core_ahah_init_form();
  78. // set default values. First use what is in the form_state['values'] array. If nothing there then use the session variable.
  79. $remote_db = $form_state['values']['remote_db'] ? $form_state['values']['remote_db'] : $_SESSION['tripal_pub_search']['remote_db'];
  80. $days = $form_state['values']['days'] ? $form_state['values']['days'] : $_SESSION['tripal_pub_search']['days'];
  81. $num_criteria = $form_state['values']['num_criteria'] ? $form_state['values']['num_criteria'] : $_SESSION['tripal_pub_search']['num_criteria'];
  82. $loader_name = $form_state['values']['loader_name'];
  83. // change the number of criteria based on form_state post data.
  84. if (!$num_criteria) {
  85. $num_criteria = 0;
  86. }
  87. if($form_state['post']["add-$num_criteria"]) {
  88. $num_criteria++;
  89. }
  90. if($form_state['post']["remove-$num_criteria"]) {
  91. $num_criteria--;
  92. }
  93. $form['loader_name'] = array(
  94. '#type' => 'textfield',
  95. '#title' => t('Loader Name'),
  96. '#description' => t('Please provide a name for this loader setup..'),
  97. '#default_value' => $loader_name,
  98. );
  99. $remote_dbs = array('Pubmed' => 'Pubmed');
  100. $form['remote_db'] = array(
  101. '#title' => t('Remote Publication Database'),
  102. '#type' => 'select',
  103. '#options' => $remote_dbs,
  104. '#default_value' => $remote_db,
  105. );
  106. $form['num_criteria']= array(
  107. '#type' => 'hidden',
  108. '#default_value' => $num_criteria,
  109. );
  110. for($i = 0; $i <= $num_criteria; $i++) {
  111. $search_terms = $form_state['values']["search_terms-$i"] ? $form_state['values']["search_terms-$i"] : $_SESSION['tripal_pub_search']['criteria'][$i]['search_terms'];
  112. $scope = $form_state['values']["scope-$i"] ? $form_state['values']["scope-$i"] : $_SESSION['tripal_pub_search']['criteria'][$i]['scope'];
  113. $operation = $form_state['values']["operation-$i"] ? $form_state['values']["operation-$i"] : $_SESSION['tripal_pub_search']['criteria'][$i]['operation'];
  114. // default to searching the title and abstract
  115. if (!$scope) {
  116. $scope = 'abstract';
  117. }
  118. $form['criteria'][$i]["search_terms-$i"] = array(
  119. '#type' => 'textfield',
  120. '#description' => t('Please provide a list of words, separated by spaces for searching.'),
  121. '#default_value' => $search_terms,
  122. );
  123. $form['criteria'][$i]["scope-$i"] = array(
  124. '#type' => 'select',
  125. '#description' => t('Please select the fields to search for this term.'),
  126. '#options' => array(
  127. 'any' => 'Any Field',
  128. 'title' => 'Title',
  129. 'abstract' => 'Title/Abstract',
  130. 'author' => 'Author'),
  131. '#default_value' => $scope,
  132. );
  133. if ($i > 0) {
  134. $form['criteria'][$i]["operation-$i"] = array(
  135. '#type' => 'select',
  136. '#options' => array(
  137. 'AND' => 'AND',
  138. 'OR' => 'OR',
  139. 'NOT' => 'NOT'),
  140. '#default_value' => $operation,
  141. );
  142. }
  143. if ($i == $num_criteria) {
  144. if($i > 0) {
  145. $form['criteria'][$i]["remove-$i"] = array(
  146. '#type' => 'image_button',
  147. '#value' => t('Remove'),
  148. '#src' => drupal_get_path('theme', 'tripal') . '/images/minus.png',
  149. '#ahah' => array(
  150. 'path' => "admin/tripal/tripal_pub/import_setup/criteria/minus/$i",
  151. 'wrapper' => 'tripal-pub-remote-search-form',
  152. 'event' => 'click',
  153. 'method' => 'replace',
  154. ),
  155. '#attributes' => array('onClick' => 'return false;'),
  156. );
  157. }
  158. $form['criteria'][$i]["add-$i"] = array(
  159. '#type' => 'image_button',
  160. '#value' => t('Add'),
  161. '#src' => drupal_get_path('theme', 'tripal') . '/images/add.png',
  162. '#ahah' => array(
  163. 'path' => "admin/tripal/tripal_pub/import_setup/criteria/add/$i",
  164. 'wrapper' => 'tripal-pub-remote-search-form',
  165. 'event' => 'click',
  166. 'method' => 'replace',
  167. ),
  168. '#attributes' => array('onClick' => 'return false;'),
  169. );
  170. }
  171. }
  172. $form['days'] = array(
  173. '#type' => 'textfield',
  174. '#title' => t('Number of Days'),
  175. '#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.'),
  176. '#default_value' => $days,
  177. );
  178. $form['submit'] = array(
  179. '#type' => 'submit',
  180. '#value' => t('Test Criteria'),
  181. );
  182. $form['submit'] = array(
  183. '#type' => 'submit',
  184. '#value' => t('Save Criteria'),
  185. );
  186. return $form;
  187. }
  188. /**
  189. *
  190. */
  191. function tripal_pub_remote_search_form_validate($form, &$form_state) {
  192. $num_criteria = $form_state['values']['num_criteria'];
  193. $remote_db = $form_state['values']["remote_db"];
  194. $days = trim($form_state['values']["days"]);
  195. for ($i = 0; $i <= $num_criteria; $i++) {
  196. $search_terms = trim($form_state['values']["search_terms-$i"]);
  197. $scope = $form_state['values']["scope-$i"];
  198. $operation = $form_state['values']["operation-$i"];
  199. if ($days and !is_numeric($days) or preg_match('/\./', $days)) {
  200. form_set_error("days-$i", "Please enter a numeric, non decimal value, for the number of days.");
  201. }
  202. if (!$search_terms) {
  203. form_set_error("search_terms-$i", "Please enter a value for searching.");
  204. }
  205. }
  206. }
  207. /**
  208. *
  209. */
  210. function tripal_pub_remote_search_form_submit($form, &$form_state) {
  211. $num_criteria = $form_state['values']['num_criteria'];
  212. $remote_db = $form_state['values']["remote_db"];
  213. $days = trim($form_state['values']["days"]);
  214. $_SESSION['tripal_pub_search']['remote_db'] = $remote_db;
  215. $_SESSION['tripal_pub_search']['days'] = $days;
  216. $_SESSION['tripal_pub_search']['num_criteria'] = $num_criteria;
  217. for ($i = 0; $i <= $num_criteria; $i++) {
  218. $search_terms = trim($form_state['values']["search_terms-$i"]);
  219. $scope = $form_state['values']["scope-$i"];
  220. $operation = $form_state['values']["operation-$i"];
  221. $_SESSION['tripal_pub_search']['criteria'][$i] = array(
  222. 'search_terms' => $search_terms,
  223. 'scope' => $scope,
  224. 'operation' => $operation
  225. );
  226. }
  227. }
  228. /*
  229. *
  230. */
  231. function tripal_pub_get_remote_search_results($remote_db, $search_array, $num_to_retrieve, $pager_id) {
  232. // construct the callback function using the remote database name
  233. $callback = 'tripal_pub_remote_search_' . strtolower($remote_db);
  234. // now call the callback function to get the rsults
  235. $pubs = array();
  236. if (function_exists($callback)) {
  237. $pubs = call_user_func($callback, $search_array, $num_to_retrieve, $pager_id);
  238. }
  239. return $pubs;
  240. }
  241. /*
  242. * AHAH callback
  243. */
  244. function tripal_pub_remote_search_page_update_criteria($action, $i) {
  245. $status = TRUE;
  246. // prepare and render the form
  247. $form = tripal_core_ahah_prepare_form();
  248. $data = theme('tripal_pub_remote_search_form', $form);
  249. // bind javascript events to the new objects that will be returned
  250. // so that AHAH enabled elements will work.
  251. $settings = tripal_core_ahah_bind_events();
  252. // return the updated JSON
  253. drupal_json(
  254. array(
  255. 'status' => $status,
  256. 'data' => $data,
  257. 'settings' => $settings,
  258. )
  259. );
  260. }