pub_search.inc 13 KB

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