remote_search.inc 11 KB

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