pub_search.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. $citation = $pub->uniquename;
  35. if ($pub->nid) {
  36. $citation = l($pub->uniquename,'node/' . $pub->nid, array('attributes' => array('target' => '_blank')));
  37. }
  38. $rows[] = array(number_format($i), $pub->pyear, $citation);
  39. $i++;
  40. }
  41. $headers = array('', 'Year', 'Citation');
  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. $search_terms = '';
  99. $scope = '';
  100. $operation = '';
  101. $mode = '';
  102. // if we have criteria supplied from the database then use that, othrewise look from the form_state or the session
  103. if ($criteria) {
  104. $search_terms = $criteria['criteria'][$i]['search_terms'];
  105. $scope = $criteria['criteria'][$i]['scope'];
  106. $mode = $criteria['criteria'][$i]['mode'];
  107. $operation = $criteria['criteria'][$i]['operation'];
  108. }
  109. // first populate defaults using any values in the SESSION variable
  110. $search_terms = $_SESSION['tripal_pub_search_form']['criteria'][$i]['search_terms'] ? $_SESSION['tripal_pub_search_form']['criteria'][$i]['search_terms'] : $search_terms;
  111. $scope = $_SESSION['tripal_pub_search_form']['criteria'][$i]['scope'] ? $_SESSION['tripal_pub_search_form']['criteria'][$i]['scope'] : $scope;
  112. $mode = $_SESSION['tripal_pub_search_form']['criteria'][$i]['mode'] ? $_SESSION['tripal_pub_search_form']['criteria'][$i]['mode'] : $mode;
  113. $operation = $_SESSION['tripal_pub_search_form']['criteria'][$i]['operation'] ? $_SESSION['tripal_pub_search_form']['criteria'][$i]['operation'] : $operation;
  114. // next populate defaults using any form values
  115. $search_terms = $form_state['values']["search_terms-$i"] ? $form_state['values']["search_terms-$i"] : $search_terms;
  116. $scope = $form_state['values']["scope-$i"] ? $form_state['values']["scope-$i"] : $scope;
  117. $mode = $form_state['values']["mode-$i"] ? $form_state['values']["mode-$i"] : $mode;
  118. $operation = $form_state['values']["operation-$i"] ? $form_state['values']["operation-$i"] : $operation;
  119. // default to searching the title and abstract
  120. if (!$scope) {
  121. $scope = 'abstract';
  122. }
  123. $form['criteria'][$i]["search_terms-$i"] = array(
  124. '#type' => 'textfield',
  125. '#description' => t('Please provide a list of words for searching"'),
  126. '#default_value' => $search_terms,
  127. '#required' => TRUE,
  128. );
  129. $form['criteria'][$i]["scope-$i"] = array(
  130. '#type' => 'select',
  131. '#description' => t('Please select the fields to search for this term.'),
  132. '#options' => $properties,
  133. '#default_value' => $scope,
  134. );
  135. $form['criteria'][$i]["mode-$i"] = array(
  136. '#type' => 'select',
  137. '#options' => array(
  138. 'Contains' => 'Contains',
  139. 'Starts With' => 'Starts With',
  140. 'Ends With' => 'Ends With',
  141. 'Exactly' => 'Exactly'),
  142. '#default_value' => $mode,
  143. );
  144. if ($i > 0) {
  145. $form['criteria'][$i]["operation-$i"] = array(
  146. '#type' => 'select',
  147. '#options' => array(
  148. 'AND' => 'AND',
  149. 'OR' => 'OR',
  150. 'NOT' => 'NOT'),
  151. '#default_value' => $operation,
  152. );
  153. }
  154. if ($i == $num_criteria) {
  155. if($i > 0) {
  156. $form['criteria'][$i]["remove-$i"] = array(
  157. '#type' => 'image_button',
  158. '#value' => t('Remove'),
  159. '#src' => drupal_get_path('theme', 'tripal') . '/images/minus.png',
  160. '#ahah' => array(
  161. 'path' => "find/publications/criteria/minus/$i",
  162. 'wrapper' => 'tripal-pub-search-form',
  163. 'event' => 'click',
  164. 'method' => 'replace',
  165. ),
  166. '#attributes' => array('onClick' => 'return false;'),
  167. );
  168. }
  169. $form['criteria'][$i]["add-$i"] = array(
  170. '#type' => 'image_button',
  171. '#value' => t('Add'),
  172. '#src' => drupal_get_path('theme', 'tripal') . '/images/add.png',
  173. '#ahah' => array(
  174. 'path' => "find/publications/criteria/add/$i",
  175. 'wrapper' => 'tripal-pub-search-form',
  176. 'event' => 'click',
  177. 'method' => 'replace',
  178. ),
  179. '#attributes' => array('onClick' => 'return false;'),
  180. );
  181. }
  182. }
  183. $form['search'] = array(
  184. '#type' => 'submit',
  185. '#value' => t('Search'),
  186. );
  187. return $form;
  188. }
  189. /*
  190. *
  191. */
  192. function theme_tripal_pub_search_form($form) {
  193. $rows = array();
  194. foreach ($form['criteria'] as $i => $element) {
  195. if(is_numeric($i)) {
  196. $rows[] = array(
  197. array('data' => drupal_render($element["operation-$i"]), 'width' => '10%'),
  198. array('data' => drupal_render($element["scope-$i"]), 'width' => '10%'),
  199. drupal_render($element["mode-$i"]) . drupal_render($element["search_terms-$i"]),
  200. array('data' => drupal_render($element["add-$i"]) . drupal_render($element["remove-$i"]), 'width' => '5%'),
  201. );
  202. }
  203. }
  204. $headers = array('Operation','Scope', 'Search Terms', '');
  205. $markup = theme('table', $headers, $rows, array('id' => 'tripal-pub-search-table'));
  206. $form['criteria'] = array(
  207. '#type' => 'markup',
  208. '#value' => $markup,
  209. '#weight' => -10,
  210. );
  211. return drupal_render($form);
  212. }
  213. /**
  214. *
  215. */
  216. function tripal_pub_search_form_submit($form, &$form_state) {
  217. $num_criteria = $form_state['values']['num_criteria'];
  218. // set the session variables
  219. $_SESSION['tripal_pub_search_form']['num_criteria'] = $num_criteria;
  220. unset($_SESSION['tripal_pub_search_form']['criteria']);
  221. for ($i = 0; $i <= $num_criteria; $i++) {
  222. $search_terms = trim($form_state['values']["search_terms-$i"]);
  223. $scope = $form_state['values']["scope-$i"];
  224. $mode = $form_state['values']["mode-$i"];
  225. $operation = $form_state['values']["operation-$i"];
  226. $_SESSION['tripal_pub_search_form']['criteria'][$i] = array(
  227. 'search_terms' => $search_terms,
  228. 'scope' => $scope,
  229. 'mode' => $mode,
  230. 'operation' => $operation
  231. );
  232. }
  233. $_SESSION['tripal_pub_search_form']['perform_search'] = 1;
  234. }
  235. /*
  236. * AHAH callback
  237. */
  238. function tripal_pub_search_page_update_criteria($action, $i) {
  239. $status = TRUE;
  240. // prepare and render the form
  241. $form = tripal_core_ahah_prepare_form();
  242. $data = theme('tripal_pub_search_form', $form);
  243. // bind javascript events to the new objects that will be returned
  244. // so that AHAH enabled elements will work.
  245. $settings = tripal_core_ahah_bind_events();
  246. // return the updated JSON
  247. drupal_json(
  248. array(
  249. 'status' => $status,
  250. 'data' => $data,
  251. 'settings' => $settings,
  252. )
  253. );
  254. }
  255. /*
  256. *
  257. */
  258. function tripal_pub_get_search_results($search_array, $limit, $pager_id) {
  259. // build the SQL based on the criteria provided by the user
  260. $select = "SELECT DISTINCT P.*, CP.nid ";
  261. $from = "FROM {pub} P
  262. LEFT JOIN public.chado_pub CP on P.pub_id = CP.pub_id
  263. ";
  264. $where = "WHERE ";
  265. $order = "ORDER BY P.pyear DESC, P.title ASC";
  266. $args = array();
  267. $join = 0;
  268. $num_criteria = $search_array['num_criteria'];
  269. for ($i = 0; $i <= $num_criteria; $i++) {
  270. $value = $search_array['criteria'][$i]['search_terms'];
  271. $type_id = $search_array['criteria'][$i]['scope'];
  272. $mode = $search_array['criteria'][$i]['mode'];
  273. $op = $search_array['criteria'][$i]['operation'];
  274. // to prevent SQL injection make sure our operator is
  275. // what we expect
  276. if ($op and $op != "AND" and $op != "OR" and $op != 'NOT') {
  277. $op = 'AND';
  278. }
  279. $action = "= lower('%s')";
  280. if($mode == 'Contains') {
  281. $action = "LIKE lower('%%%s%%')";
  282. }
  283. if($mode == 'Starts With') {
  284. $action = "= lower('%%%s')";
  285. }
  286. if($mode == 'Ends With') {
  287. $action = "= lower('%s%%')";
  288. }
  289. // get the scope type
  290. $values = array('cvterm_id' => $type_id);
  291. $cvterm = tripal_core_chado_select('cvterm', array('name'), $values);
  292. $type_name = $cvterm[0]->name;
  293. if ($type_name == 'Title') {
  294. $where .= " $op (lower(P.title) $action) ";
  295. $args[] = $value;
  296. }
  297. elseif ($type_name == 'Year') {
  298. $where .= " $op (lower(P.pyear) $action) ";
  299. $args[] = $value;
  300. }
  301. elseif ($type_name == 'Volume') {
  302. $where .= " $op (lower(P.volume) $action) ";
  303. $args[] = $value;
  304. }
  305. elseif ($type_name == 'Issue') {
  306. $where .= " $op (lower(P.issue) $action)";
  307. $args[] = $value;
  308. }
  309. elseif ($type_id == 0) { //'Any Field'
  310. $join = 1;
  311. $where .= " $op ((lower(PP.value) $action) OR (lower(P.title) $action)) ";
  312. $args[] = $value;
  313. $args[] = $value;
  314. }
  315. else {
  316. $join = 1;
  317. $where .= " $op (lower(PP.value) $action AND PP.type_id = %d) ";
  318. $args[] = $value;
  319. $args[] = $type_id;
  320. }
  321. }
  322. if ($join) {
  323. $from .= " INNER JOIN {pubprop} PP ON PP.pub_id = P.pub_id ";
  324. }
  325. $sql = "$select $from $where $order";
  326. $count = "SELECT count(*) FROM ($select $from $where $order) as t1";
  327. //dpm(array($mode, $sql, $args));
  328. return chado_pager_query($sql, $limit, $pager_id, $count, $args);
  329. }