pub_search.inc 18 KB

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