pub_search.inc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. $from_year = $_SESSION['tripal_pub_search_form']['from_year'];
  16. $to_year = $_SESSION['tripal_pub_search_form']['to_year'];
  17. $search_array = array();
  18. $search_array['num_criteria'] = $num_criteria;
  19. $search_array['from_year'] = $from_year;
  20. $search_array['to_year'] = $to_year;
  21. for ($i = 0; $i <= $num_criteria; $i++) {
  22. $search_array['criteria'][$i]['search_terms'] = $_SESSION['tripal_pub_search_form']['criteria'][$i]['search_terms'];
  23. $search_array['criteria'][$i]['scope'] = $_SESSION['tripal_pub_search_form']['criteria'][$i]['scope'];
  24. $search_array['criteria'][$i]['mode'] = $_SESSION['tripal_pub_search_form']['criteria'][$i]['mode'];
  25. $search_array['criteria'][$i]['operation'] = $_SESSION['tripal_pub_search_form']['criteria'][$i]['operation'];
  26. }
  27. // get the list of publications from the remote database using the search criteria.
  28. $pubs = tripal_pub_get_search_results($search_array, $limit, $pager_id);
  29. // generate the pager
  30. $total_pages = $pager_total[$pager_id];
  31. $total_items = $pager_total_items[$pager_id];
  32. $page = isset($_GET['page']) ? $_GET['page'] : '0';
  33. $pager = theme('pager');
  34. // iterate through the results and construct the table displaying the publications
  35. $rows = array();
  36. $i = $page * $limit + 1;
  37. while($pub = db_fetch_object($pubs)) {
  38. // get the citation for this publication
  39. $values = array(
  40. 'pub_id' => $pub->pub_id,
  41. 'type_id' => array(
  42. 'name' => 'Citation',
  43. ),
  44. );
  45. $citation_rec = tripal_core_generate_chado_var('pubprop', $values);
  46. $citation_rec = tripal_core_expand_chado_vars($citation_rec, 'field', 'pubprop.value');
  47. // if we have the citation then use it, otherwise, just use the title
  48. if ($citation_rec->value) {
  49. $pub_info = $citation_rec->value;
  50. }
  51. else {
  52. $pub_info = $pub->title;
  53. }
  54. // if the publication has a node then link to it
  55. if ($pub->nid) {
  56. $pub_info = l($pub_info ,'node/' . $pub->nid, array('attributes' => array('target' => '_blank')));
  57. }
  58. $rows[] = array(number_format($i) . ".", $pub->pyear, $pub_info);
  59. $i++;
  60. }
  61. $headers = array('', 'Year', 'Publication');
  62. $table = theme('table', $headers, $rows);
  63. // join all to form the results
  64. $output .= "<br><p><b>Found " . number_format($total_items) .
  65. ". Page " . ($page + 1) . " of $total_pages. " .
  66. " Results</b></br>" . $table . '</p>' . $pager;
  67. }
  68. return $output;
  69. }
  70. /**
  71. * Purpose: Provides the form to search pubmed
  72. *
  73. * @ingroup tripal_pub
  74. */
  75. function tripal_pub_search_form(&$form_state = NULL) {
  76. tripal_core_ahah_init_form();
  77. // Set the default values. If the pub_import_id isn't already defined by the form values
  78. // and one is provided then look it up in the database
  79. $criteria = NULL;
  80. // if the session has variables then use those. This should only happen when
  81. // the 'Test Criteria' button is clicked.
  82. $num_criteria = $_SESSION['tripal_pub_search_form']['num_criteria'] ? $_SESSION['tripal_pub_search_form']['num_criteria'] : $num_criteria;
  83. $from_year = $_SESSION['tripal_pub_search_form']['from_year'] ? $_SESSION['tripal_pub_search_form']['from_year'] : '';
  84. $to_year = $_SESSION['tripal_pub_search_form']['to_year'] ? $_SESSION['tripal_pub_search_form']['to_year'] : '';
  85. // If the form_state has variables then use those. This happens when an error occurs on the form or the
  86. // form is resbumitted using AJAX
  87. $num_criteria = $form_state['values']['num_criteria'] ? $form_state['values']['num_criteria'] : $num_criteria;
  88. // change the number of criteria based on form_state post data.
  89. if (!$num_criteria) {
  90. $num_criteria = 0;
  91. }
  92. if($form_state['post']["add-$num_criteria"]) {
  93. $num_criteria++;
  94. }
  95. if($form_state['post']["remove-$num_criteria"]) {
  96. $num_criteria--;
  97. }
  98. $form['num_criteria']= array(
  99. '#type' => 'hidden',
  100. '#default_value' => $num_criteria,
  101. );
  102. // get publication properties list
  103. $properties = array();
  104. $properties[] = 'Any Field';
  105. $sql = "
  106. SELECT DISTINCT CVTS.cvterm_id, CVTS.name, CVTS.definition
  107. FROM {cvtermpath} CVTP
  108. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  109. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  110. INNER JOIN {cv} ON CVTO.cv_id = CV.cv_id
  111. WHERE CV.name = 'tripal_pub' and
  112. (CVTO.name = 'Publication Details' or CVTS.name = 'Publication Type') and
  113. NOT CVTS.is_obsolete = 1
  114. ORDER BY CVTS.name ASC
  115. ";
  116. $prop_types = chado_query($sql);
  117. while ($prop = db_fetch_object($prop_types)) {
  118. $properties[$prop->cvterm_id] = $prop->name;
  119. }
  120. for($i = 0; $i <= $num_criteria; $i++) {
  121. $search_terms = '';
  122. $scope = '';
  123. $operation = '';
  124. $mode = '';
  125. // if we have criteria supplied from the database then use that, othrewise look from the form_state or the session
  126. if ($criteria) {
  127. $search_terms = $criteria['criteria'][$i]['search_terms'];
  128. $scope = $criteria['criteria'][$i]['scope'];
  129. $mode = $criteria['criteria'][$i]['mode'];
  130. $operation = $criteria['criteria'][$i]['operation'];
  131. }
  132. // first populate defaults using any values in the SESSION variable
  133. $search_terms = $_SESSION['tripal_pub_search_form']['criteria'][$i]['search_terms'] ? $_SESSION['tripal_pub_search_form']['criteria'][$i]['search_terms'] : $search_terms;
  134. $scope = $_SESSION['tripal_pub_search_form']['criteria'][$i]['scope'] ? $_SESSION['tripal_pub_search_form']['criteria'][$i]['scope'] : $scope;
  135. $mode = $_SESSION['tripal_pub_search_form']['criteria'][$i]['mode'] ? $_SESSION['tripal_pub_search_form']['criteria'][$i]['mode'] : $mode;
  136. $operation = $_SESSION['tripal_pub_search_form']['criteria'][$i]['operation'] ? $_SESSION['tripal_pub_search_form']['criteria'][$i]['operation'] : $operation;
  137. // next populate defaults using any form values
  138. $search_terms = $form_state['values']["search_terms-$i"] ? $form_state['values']["search_terms-$i"] : $search_terms;
  139. $scope = $form_state['values']["scope-$i"] ? $form_state['values']["scope-$i"] : $scope;
  140. $mode = $form_state['values']["mode-$i"] ? $form_state['values']["mode-$i"] : $mode;
  141. $operation = $form_state['values']["operation-$i"] ? $form_state['values']["operation-$i"] : $operation;
  142. // default to searching the title and abstract
  143. if (!$scope) {
  144. $scope = 'abstract';
  145. }
  146. $form['criteria'][$i]["search_terms-$i"] = array(
  147. '#type' => 'textfield',
  148. '#description' => t('Please provide a list of words for searching"'),
  149. '#default_value' => $search_terms,
  150. '#required' => TRUE,
  151. );
  152. $form['criteria'][$i]["scope-$i"] = array(
  153. '#type' => 'select',
  154. '#description' => t('Please select the fields to search for this term.'),
  155. '#options' => $properties,
  156. '#default_value' => $scope,
  157. );
  158. $form['criteria'][$i]["mode-$i"] = array(
  159. '#type' => 'select',
  160. '#options' => array(
  161. 'Contains' => 'Contains',
  162. 'Starts With' => 'Starts With',
  163. 'Ends With' => 'Ends With',
  164. 'Exactly' => 'Exactly'),
  165. '#default_value' => $mode,
  166. );
  167. if ($i > 0) {
  168. $form['criteria'][$i]["operation-$i"] = array(
  169. '#type' => 'select',
  170. '#options' => array(
  171. 'AND' => 'AND',
  172. 'OR' => 'OR',
  173. 'NOT' => 'NOT'),
  174. '#default_value' => $operation,
  175. );
  176. }
  177. if ($i == $num_criteria) {
  178. if($i > 0) {
  179. $form['criteria'][$i]["remove-$i"] = array(
  180. '#type' => 'image_button',
  181. '#value' => t('Remove'),
  182. '#src' => drupal_get_path('theme', 'tripal') . '/images/minus.png',
  183. '#ahah' => array(
  184. 'path' => "find/publications/criteria/minus/$i",
  185. 'wrapper' => 'tripal-pub-search-form',
  186. 'event' => 'click',
  187. 'method' => 'replace',
  188. ),
  189. '#attributes' => array('onClick' => 'return false;'),
  190. );
  191. }
  192. $form['criteria'][$i]["add-$i"] = array(
  193. '#type' => 'image_button',
  194. '#value' => t('Add'),
  195. '#src' => drupal_get_path('theme', 'tripal') . '/images/add.png',
  196. '#ahah' => array(
  197. 'path' => "find/publications/criteria/add/$i",
  198. 'wrapper' => 'tripal-pub-search-form',
  199. 'event' => 'click',
  200. 'method' => 'replace',
  201. ),
  202. '#attributes' => array('onClick' => 'return false;'),
  203. );
  204. }
  205. }
  206. $form['criteria']["from_year"] = array(
  207. '#type' => 'textfield',
  208. '#default_value' => $from_year,
  209. '#required' => FALSE,
  210. '#title' => 'Years from',
  211. '#size' => 4,
  212. '#maxlength' => 4,
  213. );
  214. $form['criteria']["to_year"] = array(
  215. '#type' => 'textfield',
  216. '#default_value' => $to_year,
  217. '#required' => FALSE,
  218. '#title' => 'to',
  219. '#size' => 4,
  220. '#maxlength' => 4,
  221. );
  222. $form['search'] = array(
  223. '#type' => 'submit',
  224. '#value' => t('Search'),
  225. );
  226. $form['reset'] = array(
  227. '#type' => 'submit',
  228. '#value' => t('Reset'),
  229. );
  230. return $form;
  231. }
  232. /*
  233. *
  234. */
  235. function theme_tripal_pub_search_form($form) {
  236. $rows = array();
  237. foreach ($form['criteria'] as $i => $element) {
  238. if(is_numeric($i)) {
  239. $rows[] = array(
  240. array('data' => drupal_render($element["operation-$i"]), 'width' => '10%'),
  241. array('data' => drupal_render($element["scope-$i"]), 'width' => '10%'),
  242. drupal_render($element["mode-$i"]) . drupal_render($element["search_terms-$i"]),
  243. array('data' => drupal_render($element["add-$i"]) . drupal_render($element["remove-$i"]), 'width' => '5%'),
  244. );
  245. }
  246. }
  247. $headers = array('Operation','Scope', 'Search Terms', '');
  248. $markup = "
  249. <div id=\"pub-search-form-row1\"> ".
  250. theme('table', $headers, $rows, array('id' => 'tripal-pub-search-table')) . "
  251. </div>
  252. <div id=\"pub-search-form-row2\">
  253. <div id=\"pub-search-form-dates\"> ".
  254. drupal_render($form['criteria']['from_year']) .
  255. drupal_render($form['criteria']['to_year']) . '
  256. </div>
  257. </div>
  258. <div style="clear: both;">
  259. ';
  260. $form['criteria'] = array(
  261. '#type' => 'markup',
  262. '#value' => $markup,
  263. '#weight' => -10,
  264. );
  265. return drupal_render($form);
  266. }
  267. /**
  268. *
  269. */
  270. function tripal_pub_search_form_validate($form, &$form_state) {
  271. $from_year = $form_state['values']['from_year'];
  272. $to_year = $form_state['values']['to_year'];
  273. if($from_year and !$to_year) {
  274. form_set_error('to_year', 'Please provide a 4-digit year.');
  275. }
  276. if(!$from_year and $to_year) {
  277. form_set_error('from_year', 'Please provide a 4-digit year.');
  278. }
  279. if($from_year and !preg_match('/\d\d\d\d/' , $from_year)) {
  280. form_set_error('from_year', 'Please provide a 4-digit year.');
  281. }
  282. if($to_year and !preg_match('/\d\d\d\d/' , $to_year)) {
  283. form_set_error('to_year', 'Please provide a 4-digit year.');
  284. }
  285. }
  286. /**
  287. *
  288. */
  289. function tripal_pub_search_form_submit($form, &$form_state) {
  290. $num_criteria = $form_state['values']['num_criteria'];
  291. $from_year = $form_state['values']['from_year'];
  292. $to_year = $form_state['values']['to_year'];
  293. $op = $form_state['values']['op'];
  294. // set the session variables
  295. if($op == 'Search') {
  296. $_SESSION['tripal_pub_search_form']['num_criteria'] = $num_criteria;
  297. unset($_SESSION['tripal_pub_search_form']['criteria']);
  298. for ($i = 0; $i <= $num_criteria; $i++) {
  299. $search_terms = trim($form_state['values']["search_terms-$i"]);
  300. $scope = $form_state['values']["scope-$i"];
  301. $mode = $form_state['values']["mode-$i"];
  302. $operation = $form_state['values']["operation-$i"];
  303. $_SESSION['tripal_pub_search_form']['criteria'][$i] = array(
  304. 'search_terms' => $search_terms,
  305. 'scope' => $scope,
  306. 'mode' => $mode,
  307. 'operation' => $operation
  308. );
  309. }
  310. $_SESSION['tripal_pub_search_form']['from_year'] = $from_year;
  311. $_SESSION['tripal_pub_search_form']['to_year'] = $to_year;
  312. $_SESSION['tripal_pub_search_form']['perform_search'] = 1;
  313. }
  314. if($op == 'Reset') {
  315. unset($_SESSION['tripal_pub_search_form']);
  316. }
  317. }
  318. /*
  319. * AHAH callback
  320. */
  321. function tripal_pub_search_page_update_criteria($action, $i) {
  322. $status = TRUE;
  323. // prepare and render the form
  324. $form = tripal_core_ahah_prepare_form();
  325. $data = theme('tripal_pub_search_form', $form);
  326. // bind javascript events to the new objects that will be returned
  327. // so that AHAH enabled elements will work.
  328. $settings = tripal_core_ahah_bind_events();
  329. // return the updated JSON
  330. drupal_json(
  331. array(
  332. 'status' => $status,
  333. 'data' => $data,
  334. 'settings' => $settings,
  335. )
  336. );
  337. }
  338. /*
  339. *
  340. */
  341. function tripal_pub_get_search_results($search_array, $limit, $pager_id) {
  342. // build the SQL based on the criteria provided by the user
  343. $select = "SELECT DISTINCT P.*, CP.nid ";
  344. $from = "FROM {pub} P
  345. LEFT JOIN public.chado_pub CP on P.pub_id = CP.pub_id
  346. ";
  347. $where = "WHERE ";
  348. $order = "ORDER BY P.pyear DESC, P.title ASC";
  349. $fargs = array(); // arguments for from, inner join clause
  350. $wargs = array(); // arguments for where clause
  351. $join = 0;
  352. $num_criteria = $search_array['num_criteria'];
  353. $from_year = $search_array['from_year'];
  354. $to_year = $search_array['to_year'];
  355. for ($i = 0; $i <= $num_criteria; $i++) {
  356. $value = $search_array['criteria'][$i]['search_terms'];
  357. $type_id = $search_array['criteria'][$i]['scope'];
  358. $mode = $search_array['criteria'][$i]['mode'];
  359. $op = $search_array['criteria'][$i]['operation'];
  360. // to prevent SQL injection make sure our operator is
  361. // what we expect
  362. if ($op and $op != "AND" and $op != "OR" and $op != 'NOT') {
  363. $op = 'AND';
  364. }
  365. if ($op == 'NOT') {
  366. $op = 'AND NOT';
  367. }
  368. $action = "= lower('%s')";
  369. if($mode == 'Contains') {
  370. $action = 'LIKE lower(\'%%%s%%\')';
  371. }
  372. if($mode == 'Starts With') {
  373. $action = '= lower(\'%%%s\')';
  374. }
  375. if($mode == 'Ends With') {
  376. $action = '= lower(\'%s%%\')';
  377. }
  378. // get the scope type
  379. $values = array('cvterm_id' => $type_id);
  380. $cvterm = tripal_core_chado_select('cvterm', array('name'), $values);
  381. $type_name = $cvterm[0]->name;
  382. if ($type_name == 'Title') {
  383. $where .= " $op (lower(P.title) $action) ";
  384. $wargs[] = $value;
  385. }
  386. elseif ($type_name == 'Year') {
  387. $where .= " $op (lower(P.pyear) $action) ";
  388. $wargs[] = $value;
  389. }
  390. elseif ($type_name == 'Volume') {
  391. $where .= " $op (lower(P.volume) $action) ";
  392. $wargs[] = $value;
  393. }
  394. elseif ($type_name == 'Issue') {
  395. $where .= " $op (lower(P.issue) $action)";
  396. $wargs[] = $value;
  397. }
  398. elseif ($type_name == 'Journal Name') {
  399. $where .= " $op (lower(P.series_name) $action) ";
  400. $wargs[] = $value;
  401. }
  402. elseif ($type_id == 0) { //'Any Field'
  403. $from .= " LEFT JOIN {pubprop} PP$i ON PP$i.pub_id = P.pub_id ";
  404. $where .= " $op (lower(PP$i.value) $action OR
  405. lower(P.title) $action OR
  406. lower(P.volumetitle) $action OR
  407. lower(P.publisher) $action OR
  408. lower(P.pubplace) $action OR
  409. lower(P.miniref) $action OR
  410. lower(P.series_name) $action) ";
  411. $wargs[] = $value;
  412. $wargs[] = $value;
  413. $wargs[] = $value;
  414. $wargs[] = $value;
  415. $wargs[] = $value;
  416. $wargs[] = $value;
  417. $wargs[] = $value;
  418. }
  419. // for all other properties
  420. else {
  421. $from .= " LEFT JOIN {pubprop} PP$i ON PP$i.pub_id = P.pub_id AND PP$i.type_id = %d ";
  422. $where .= " $op (lower(PP$i.value) $action) ";
  423. $fargs[] = $type_id;
  424. $wargs[] = $value;
  425. }
  426. }
  427. if($from_year and $to_year) {
  428. $where .= " AND (P.pyear ~ '\d\d\d\d' AND to_number(P.pyear,'9999') >= %d AND to_number(P.pyear,'9999') <= %d) ";
  429. $wargs[] = $from_year;
  430. $wargs[] = $to_year;
  431. }
  432. $sql = "$select $from $where $order";
  433. $count = "SELECT count(*) FROM ($select $from $where $order) as t1";
  434. $args = array_merge($fargs, $wargs);
  435. //dpm(array($mode, $sql, $args));
  436. return chado_pager_query($sql, $limit, $pager_id, $count, $args);
  437. }