pub_search.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. <?php
  2. /*
  3. *
  4. */
  5. function tripal_pub_search_page() {
  6. global $pager_total, $pager_total_items;
  7. $pager_id = 0;
  8. $limit = 25;
  9. // generate the search form
  10. $form = drupal_get_form('tripal_pub_search_form');
  11. $output .= $form;
  12. // retrieve any results
  13. if ($_SESSION['tripal_pub_search_form']['perform_search']) {
  14. $num_criteria = $_SESSION['tripal_pub_search_form']['num_criteria'];
  15. $search_array = array();
  16. $search_array['num_criteria'] = $num_criteria;
  17. for ($i = 0; $i <= $num_criteria; $i++) {
  18. $search_array['criteria'][$i]['search_terms'] = $_SESSION['tripal_pub_search_form']['criteria'][$i]['search_terms'];
  19. $search_array['criteria'][$i]['scope'] = $_SESSION['tripal_pub_search_form']['criteria'][$i]['scope'];
  20. $search_array['criteria'][$i]['mode'] = $_SESSION['tripal_pub_search_form']['criteria'][$i]['mode'];
  21. $search_array['criteria'][$i]['operation'] = $_SESSION['tripal_pub_search_form']['criteria'][$i]['operation'];
  22. }
  23. // get the list of publications from the remote database using the search criteria.
  24. $pubs = tripal_pub_get_search_results($search_array, $limit, $pager_id);
  25. // generate the pager
  26. $total_pages = $pager_total[$pager_id];
  27. $total_items = $pager_total_items[$pager_id];
  28. $page = isset($_GET['page']) ? $_GET['page'] : '0';
  29. $pager = theme('pager');
  30. // iterate through the results and construct the table displaying the publications
  31. $rows = array();
  32. $i = $page * $limit + 1;
  33. while($pub = db_fetch_object($pubs)) {
  34. $title = $pub->title;
  35. if ($pub->nid) {
  36. $title = l($pub->title,'node/' . $pub->nid, array('attributes' => array('target' => '_blank')));
  37. }
  38. $rows[] = array(number_format($i), $pub->pyear, $title);
  39. $i++;
  40. }
  41. $headers = array('', 'Year', 'Title');
  42. $table = theme('table', $headers, $rows);
  43. // join all to form the results
  44. $output .= "<br><p><b>Found " . number_format($total_items) .
  45. ". Page " . ($page + 1) . " of $total_pages. " .
  46. " Results</b></br>" . $table . '</p>' . $pager;
  47. }
  48. return $output;
  49. }
  50. /**
  51. * Purpose: Provides the form to search pubmed
  52. *
  53. * @ingroup tripal_pub
  54. */
  55. function tripal_pub_search_form(&$form_state = NULL) {
  56. tripal_core_ahah_init_form();
  57. // Set the default values. If the pub_import_id isn't already defined by the form values
  58. // and one is provided then look it up in the database
  59. $criteria = NULL;
  60. // if the session has variables then use those. This should only happen when
  61. // the 'Test Criteria' button is clicked.
  62. $num_criteria = $_SESSION['tripal_pub_search_form']['num_criteria'] ? $_SESSION['tripal_pub_search_form']['num_criteria'] : $num_criteria;
  63. // If the form_state has variables then use those. This happens when an error occurs on the form or the
  64. // form is resbumitted using AJAX
  65. $num_criteria = $form_state['values']['num_criteria'] ? $form_state['values']['num_criteria'] : $num_criteria;
  66. // change the number of criteria based on form_state post data.
  67. if (!$num_criteria) {
  68. $num_criteria = 0;
  69. }
  70. if($form_state['post']["add-$num_criteria"]) {
  71. $num_criteria++;
  72. }
  73. if($form_state['post']["remove-$num_criteria"]) {
  74. $num_criteria--;
  75. }
  76. $form['num_criteria']= array(
  77. '#type' => 'hidden',
  78. '#default_value' => $num_criteria,
  79. );
  80. // get publication properties list
  81. $properties = array();
  82. $properties[] = 'Any Field';
  83. $sql = "
  84. SELECT CVTS.cvterm_id, CVTS.name, CVTS.definition
  85. FROM {cvtermpath} CVTP
  86. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  87. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  88. INNER JOIN {cv} ON CVTO.cv_id = CV.cv_id
  89. WHERE CV.name = 'tripal_pub' and CVTO.name = 'Publication Details' and
  90. NOT CVTS.is_obsolete = 1
  91. ORDER BY CVTS.name ASC
  92. ";
  93. $prop_types = chado_query($sql);
  94. while ($prop = db_fetch_object($prop_types)) {
  95. $properties[$prop->cvterm_id] = $prop->name;
  96. }
  97. for($i = 0; $i <= $num_criteria; $i++) {
  98. // if we have criteria supplied from the database then use that, othrewise look from the form_state or the session
  99. if ($criteria) {
  100. $search_terms = $criteria['criteria'][$i]['search_terms'];
  101. $scope = $criteria['criteria'][$i]['scope'];
  102. $operation = $criteria['criteria'][$i]['operation'];
  103. }
  104. // first populate defaults using any values in the SESSION variable
  105. $search_terms = $_SESSION['tripal_pub_search_form']['criteria'][$i]['search_terms'] ? $_SESSION['tripal_pub_search_form']['criteria'][$i]['search_terms'] : $search_terms;
  106. $scope = $_SESSION['tripal_pub_search_form']['criteria'][$i]['scope'] ? $_SESSION['tripal_pub_search_form']['criteria'][$i]['scope'] : $scope;
  107. $operation = $_SESSION['tripal_pub_search_form']['criteria'][$i]['operation'] ? $_SESSION['tripal_pub_search_form']['criteria'][$i]['operation'] : $operation;
  108. // next populate defaults using any form values
  109. $search_terms = $form_state['values']["search_terms-$i"] ? $form_state['values']["search_terms-$i"] : $search_terms;
  110. $scope = $form_state['values']["scope-$i"] ? $form_state['values']["scope-$i"] : $scope;
  111. $operation = $form_state['values']["operation-$i"] ? $form_state['values']["operation-$i"] : $operation;
  112. // default to searching the title and abstract
  113. if (!$scope) {
  114. $scope = 'abstract';
  115. }
  116. $form['criteria'][$i]["search_terms-$i"] = array(
  117. '#type' => 'textfield',
  118. '#description' => t('Please provide a list of words for searching"'),
  119. '#default_value' => $search_terms,
  120. '#required' => TRUE,
  121. );
  122. $form['criteria'][$i]["scope-$i"] = array(
  123. '#type' => 'select',
  124. '#description' => t('Please select the fields to search for this term.'),
  125. '#options' => $properties,
  126. '#default_value' => $scope,
  127. );
  128. $form['criteria'][$i]["mode-$i"] = array(
  129. '#type' => 'select',
  130. '#options' => array(
  131. 'Contains' => 'Contains',
  132. 'Starts With' => 'Starts With',
  133. 'Ends With' => 'Ends With',
  134. 'Exactly' => 'Exactly'),
  135. '#default_value' => $mode,
  136. );
  137. if ($i > 0) {
  138. $form['criteria'][$i]["operation-$i"] = array(
  139. '#type' => 'select',
  140. '#options' => array(
  141. 'AND' => 'AND',
  142. 'OR' => 'OR',
  143. 'NOT' => 'NOT'),
  144. '#default_value' => $operation,
  145. );
  146. }
  147. if ($i == $num_criteria) {
  148. if($i > 0) {
  149. $form['criteria'][$i]["remove-$i"] = array(
  150. '#type' => 'image_button',
  151. '#value' => t('Remove'),
  152. '#src' => drupal_get_path('theme', 'tripal') . '/images/minus.png',
  153. '#ahah' => array(
  154. 'path' => "find/publications/criteria/minus/$i",
  155. 'wrapper' => 'tripal-pub-search-form',
  156. 'event' => 'click',
  157. 'method' => 'replace',
  158. ),
  159. '#attributes' => array('onClick' => 'return false;'),
  160. );
  161. }
  162. $form['criteria'][$i]["add-$i"] = array(
  163. '#type' => 'image_button',
  164. '#value' => t('Add'),
  165. '#src' => drupal_get_path('theme', 'tripal') . '/images/add.png',
  166. '#ahah' => array(
  167. 'path' => "find/publications/criteria/add/$i",
  168. 'wrapper' => 'tripal-pub-search-form',
  169. 'event' => 'click',
  170. 'method' => 'replace',
  171. ),
  172. '#attributes' => array('onClick' => 'return false;'),
  173. );
  174. }
  175. }
  176. $form['search'] = array(
  177. '#type' => 'submit',
  178. '#value' => t('Search'),
  179. );
  180. return $form;
  181. }
  182. /*
  183. *
  184. */
  185. function theme_tripal_pub_search_form($form) {
  186. $rows = array();
  187. foreach ($form['criteria'] as $i => $element) {
  188. if(is_numeric($i)) {
  189. $rows[] = array(
  190. array('data' => drupal_render($element["operation-$i"]), 'width' => '10%'),
  191. array('data' => drupal_render($element["scope-$i"]), 'width' => '10%'),
  192. drupal_render($element["mode-$i"]) . drupal_render($element["search_terms-$i"]),
  193. array('data' => drupal_render($element["add-$i"]) . drupal_render($element["remove-$i"]), 'width' => '5%'),
  194. );
  195. }
  196. }
  197. $headers = array('Operation','Scope', 'Search Terms', '');
  198. $markup = theme('table', $headers, $rows, array('id' => 'tripal-pub-search-table'));
  199. $form['criteria'] = array(
  200. '#type' => 'markup',
  201. '#value' => $markup,
  202. '#weight' => -10,
  203. );
  204. return drupal_render($form);
  205. }
  206. /**
  207. *
  208. */
  209. function tripal_pub_search_form_submit($form, &$form_state) {
  210. $num_criteria = $form_state['values']['num_criteria'];
  211. // set the session variables
  212. $_SESSION['tripal_pub_search_form']['num_criteria'] = $num_criteria;
  213. unset($_SESSION['tripal_pub_search_form']['criteria']);
  214. for ($i = 0; $i <= $num_criteria; $i++) {
  215. $search_terms = trim($form_state['values']["search_terms-$i"]);
  216. $scope = $form_state['values']["scope-$i"];
  217. $mode = $form_state['values']["mode-$i"];
  218. $operation = $form_state['values']["operation-$i"];
  219. $_SESSION['tripal_pub_search_form']['criteria'][$i] = array(
  220. 'search_terms' => $search_terms,
  221. 'scope' => $scope,
  222. 'mode' => $mode,
  223. 'operation' => $operation
  224. );
  225. }
  226. $_SESSION['tripal_pub_search_form']['perform_search'] = 1;
  227. }
  228. /*
  229. * AHAH callback
  230. */
  231. function tripal_pub_search_page_update_criteria($action, $i) {
  232. $status = TRUE;
  233. // prepare and render the form
  234. $form = tripal_core_ahah_prepare_form();
  235. $data = theme('tripal_pub_search_form', $form);
  236. // bind javascript events to the new objects that will be returned
  237. // so that AHAH enabled elements will work.
  238. $settings = tripal_core_ahah_bind_events();
  239. // return the updated JSON
  240. drupal_json(
  241. array(
  242. 'status' => $status,
  243. 'data' => $data,
  244. 'settings' => $settings,
  245. )
  246. );
  247. }
  248. /*
  249. *
  250. */
  251. function tripal_pub_get_search_results($search_array, $limit, $pager_id) {
  252. // build the SQL based on the criteria provided by the user
  253. $select = "SELECT DISTINCT P.*, CP.nid ";
  254. $from = "FROM {pub} P
  255. LEFT JOIN public.chado_pub CP on P.pub_id = CP.pub_id
  256. ";
  257. $where = "WHERE ";
  258. $order = "ORDER BY P.pyear DESC, P.title ASC";
  259. $args = array();
  260. $join = 0;
  261. $num_criteria = $search_array['num_criteria'];
  262. for ($i = 0; $i <= $num_criteria; $i++) {
  263. $value = $search_array['criteria'][$i]['search_terms'];
  264. $type_id = $search_array['criteria'][$i]['scope'];
  265. $mode = $search_array['criteria'][$i]['mode'];
  266. $op = $search_array['criteria'][$i]['operation'];
  267. // to prevent SQL injection make sure our operator is
  268. // what we expect
  269. if ($op and $op != "AND" and $op != "OR" and $op != 'NOT') {
  270. $op = 'AND';
  271. }
  272. $action = "= lower('%s')";
  273. if($mode == 'Contains') {
  274. $action = "LIKE lower('%%%s%%')";
  275. }
  276. if($mode == 'Starts With') {
  277. $action = "= lower('%%%s')";
  278. }
  279. if($mode == 'Ends With') {
  280. $action = "= lower('%s%%')";
  281. }
  282. // get the scope type
  283. $values = array('cvterm_id' => $type_id);
  284. $cvterm = tripal_core_chado_select('cvterm', array('name'), $values);
  285. $type_name = $cvterm[0]->name;
  286. if ($type_name == 'Title') {
  287. $where .= " $op (lower(P.title) $action) ";
  288. $args[] = $value;
  289. }
  290. elseif ($type_name == 'Year') {
  291. $where .= " $op (lower(P.pyear) $action) ";
  292. $args[] = $value;
  293. }
  294. elseif ($type_name == 'Volume') {
  295. $where .= " $op (lower(P.volume) $action) ";
  296. $args[] = $value;
  297. }
  298. elseif ($type_name == 'Issue') {
  299. $where .= " $op (lower(P.issue) $action)";
  300. $args[] = $value;
  301. }
  302. else {
  303. $join = 1;
  304. $where .= " $op (lower(PP.value) $action AND PP.type_id = %d) ";
  305. $args[] = $value;
  306. $args[] = $type_id;
  307. }
  308. }
  309. if ($join) {
  310. $from .= " INNER JOIN {pubprop} PP ON PP.pub_id = P.pub_id ";
  311. }
  312. $sql = "$select $from $where $order";
  313. $count = "SELECT count(*) FROM ($select $from $where $order) as t1";
  314. //dpm(array($mode, $sql, $args));
  315. return chado_pager_query($sql, $limit, $pager_id, $count, $args);
  316. }