pub_search.inc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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. dpm($allowed_fields);
  123. $prop_types = chado_query($sql);
  124. while ($prop = db_fetch_object($prop_types)) {
  125. if($allowed_fields[$prop->cvterm_id] > 0) {
  126. $properties[$prop->cvterm_id] = $prop->name;
  127. }
  128. }
  129. for($i = 1; $i <= $num_criteria; $i++) {
  130. $search_terms = '';
  131. $scope = '';
  132. $operation = '';
  133. $mode = '';
  134. // if we have criteria supplied from the database then use that, othrewise look from the form_state or the session
  135. if ($criteria) {
  136. $search_terms = $criteria['criteria'][$i]['search_terms'];
  137. $scope = $criteria['criteria'][$i]['scope'];
  138. $mode = $criteria['criteria'][$i]['mode'];
  139. $operation = $criteria['criteria'][$i]['operation'];
  140. }
  141. // first populate defaults using any values in the SESSION variable
  142. $search_terms = $_SESSION['tripal_pub_search_form']['criteria'][$i]['search_terms'] ? $_SESSION['tripal_pub_search_form']['criteria'][$i]['search_terms'] : $search_terms;
  143. $scope = $_SESSION['tripal_pub_search_form']['criteria'][$i]['scope'] ? $_SESSION['tripal_pub_search_form']['criteria'][$i]['scope'] : $scope;
  144. $mode = $_SESSION['tripal_pub_search_form']['criteria'][$i]['mode'] ? $_SESSION['tripal_pub_search_form']['criteria'][$i]['mode'] : $mode;
  145. $operation = $_SESSION['tripal_pub_search_form']['criteria'][$i]['operation'] ? $_SESSION['tripal_pub_search_form']['criteria'][$i]['operation'] : $operation;
  146. // next populate defaults using any form values
  147. $search_terms = $form_state['values']["search_terms-$i"] ? $form_state['values']["search_terms-$i"] : $search_terms;
  148. $scope = $form_state['values']["scope-$i"] ? $form_state['values']["scope-$i"] : $scope;
  149. $mode = $form_state['values']["mode-$i"] ? $form_state['values']["mode-$i"] : $mode;
  150. $operation = $form_state['values']["operation-$i"] ? $form_state['values']["operation-$i"] : $operation;
  151. // default to searching the title and abstract
  152. if (!$scope) {
  153. $scope = 'abstract';
  154. }
  155. $form['criteria'][$i]["search_terms-$i"] = array(
  156. '#type' => 'textfield',
  157. '#default_value' => $search_terms,
  158. '#required' => FALSE,
  159. );
  160. $form['criteria'][$i]["scope-$i"] = array(
  161. '#type' => 'select',
  162. '#options' => $properties,
  163. '#default_value' => $scope,
  164. );
  165. /*
  166. $form['criteria'][$i]["mode-$i"] = array(
  167. '#type' => 'select',
  168. '#options' => array(
  169. 'Contains' => 'Contains',
  170. 'Starts With' => 'Starts With',
  171. 'Ends With' => 'Ends With',
  172. 'Exactly' => 'Exactly'),
  173. '#default_value' => $mode,
  174. );*/
  175. if ($i > 1) {
  176. $form['criteria'][$i]["operation-$i"] = array(
  177. '#type' => 'select',
  178. '#options' => array(
  179. 'AND' => 'AND',
  180. 'OR' => 'OR',
  181. 'NOT' => 'NOT'),
  182. '#default_value' => $operation,
  183. );
  184. }
  185. if ($i == $num_criteria) {
  186. if($i > 1) {
  187. $form['criteria'][$i]["remove-$i"] = array(
  188. '#type' => 'image_button',
  189. '#value' => t('Remove'),
  190. '#src' => drupal_get_path('theme', 'tripal') . '/images/minus.png',
  191. '#ahah' => array(
  192. 'path' => "find/publications/criteria/minus/$i",
  193. 'wrapper' => 'tripal-pub-search-form',
  194. 'event' => 'click',
  195. 'method' => 'replace',
  196. ),
  197. '#attributes' => array('onClick' => 'return false;'),
  198. );
  199. }
  200. $form['criteria'][$i]["add-$i"] = array(
  201. '#type' => 'image_button',
  202. '#value' => t('Add'),
  203. '#src' => drupal_get_path('theme', 'tripal') . '/images/add.png',
  204. '#ahah' => array(
  205. 'path' => "find/publications/criteria/add/$i",
  206. 'wrapper' => 'tripal-pub-search-form',
  207. 'event' => 'click',
  208. 'method' => 'replace',
  209. ),
  210. '#attributes' => array('onClick' => 'return false;'),
  211. );
  212. }
  213. }
  214. $form['criteria']["date"] = array(
  215. '#type' => 'select',
  216. '#options' => array(
  217. 'Years' => 'Years',
  218. ),
  219. '#default_value' => $date,
  220. );
  221. $form['criteria']["from_year"] = array(
  222. '#type' => 'textfield',
  223. '#default_value' => $from_year,
  224. '#required' => FALSE,
  225. '#title' => 'from',
  226. '#size' => 4,
  227. '#maxlength' => 4,
  228. );
  229. $form['criteria']["to_year"] = array(
  230. '#type' => 'textfield',
  231. '#default_value' => $to_year,
  232. '#required' => FALSE,
  233. '#title' => 'to',
  234. '#size' => 4,
  235. '#maxlength' => 4,
  236. );
  237. $form['search'] = array(
  238. '#type' => 'submit',
  239. '#value' => t('Search'),
  240. );
  241. $form['reset'] = array(
  242. '#type' => 'submit',
  243. '#value' => t('Reset'),
  244. );
  245. return $form;
  246. }
  247. /*
  248. *
  249. */
  250. function theme_tripal_pub_search_form($form) {
  251. $rows = array();
  252. foreach ($form['criteria'] as $i => $element) {
  253. if(is_numeric($i)) {
  254. $rows[] = array(
  255. drupal_render($element["operation-$i"]),
  256. drupal_render($element["scope-$i"]),
  257. //drupal_render($element["mode-$i"]) .
  258. drupal_render($element["search_terms-$i"]),
  259. drupal_render($element["add-$i"]) . drupal_render($element["remove-$i"]),
  260. );
  261. }
  262. }
  263. $rows[] = array(
  264. drupal_render($form['criteria']['date']),
  265. array(
  266. 'data' =>
  267. "<div id=\"pub-search-form-dates-row\">
  268. <div id=\"pub-search-form-dates\"> ".
  269. drupal_render($form['criteria']['from_year']) .
  270. drupal_render($form['criteria']['to_year']) . "
  271. </div>
  272. </div>",
  273. 'colspan' => 2,
  274. ),
  275. ''
  276. );
  277. $table = theme('table', $headers, $rows, array('id' => 'tripal-pub-search-form-table', 'border' => '0'));
  278. $headers = array();
  279. $markup = drupal_render($form['instructions']) . "
  280. <div id=\"pub-search-form-row1\">$table</div>
  281. <div style=\"clear: both;\">
  282. ";
  283. $form['criteria'] = array(
  284. '#type' => 'markup',
  285. '#value' => $markup,
  286. '#weight' => -10,
  287. );
  288. return drupal_render($form);
  289. }
  290. /**
  291. *
  292. */
  293. function tripal_pub_search_form_validate($form, &$form_state) {
  294. $num_criteria = $form_state['values']['num_criteria'];
  295. $from_year = $form_state['values']['from_year'];
  296. $to_year = $form_state['values']['to_year'];
  297. $op = $form_state['values']['op'];
  298. // no need to vlaidate on a reset
  299. if ($op == 'Reset') {
  300. return;
  301. }
  302. if($from_year and !$to_year) {
  303. form_set_error('to_year', 'Please provide a 4-digit year.');
  304. }
  305. if(!$from_year and $to_year) {
  306. form_set_error('from_year', 'Please provide a 4-digit year.');
  307. }
  308. if($from_year and !preg_match('/\d\d\d\d/' , $from_year)) {
  309. form_set_error('from_year', 'Please provide a 4-digit year.');
  310. }
  311. if($to_year and !preg_match('/\d\d\d\d/' , $to_year)) {
  312. form_set_error('to_year', 'Please provide a 4-digit year.');
  313. }
  314. }
  315. /**
  316. *
  317. */
  318. function tripal_pub_search_form_submit($form, &$form_state) {
  319. $num_criteria = $form_state['values']['num_criteria'];
  320. $from_year = $form_state['values']['from_year'];
  321. $to_year = $form_state['values']['to_year'];
  322. $op = $form_state['values']['op'];
  323. // set the session variables
  324. if($op == 'Search') {
  325. $_SESSION['tripal_pub_search_form']['num_criteria'] = $num_criteria;
  326. unset($_SESSION['tripal_pub_search_form']['criteria']);
  327. for ($i = 0; $i <= $num_criteria; $i++) {
  328. $search_terms = trim($form_state['values']["search_terms-$i"]);
  329. $scope = $form_state['values']["scope-$i"];
  330. //$mode = $form_state['values']["mode-$i"];
  331. $mode = 'Contains';
  332. $operation = $form_state['values']["operation-$i"];
  333. $_SESSION['tripal_pub_search_form']['criteria'][$i] = array(
  334. 'search_terms' => $search_terms,
  335. 'scope' => $scope,
  336. 'mode' => $mode,
  337. 'operation' => $operation
  338. );
  339. }
  340. $_SESSION['tripal_pub_search_form']['from_year'] = $from_year;
  341. $_SESSION['tripal_pub_search_form']['to_year'] = $to_year;
  342. $_SESSION['tripal_pub_search_form']['perform_search'] = 1;
  343. }
  344. if($op == 'Reset') {
  345. unset($_SESSION['tripal_pub_search_form']);
  346. }
  347. }
  348. /*
  349. * AHAH callback
  350. */
  351. function tripal_pub_search_page_update_criteria($action, $i) {
  352. $status = TRUE;
  353. // prepare and render the form
  354. $form = tripal_core_ahah_prepare_form();
  355. $data = theme('tripal_pub_search_form', $form);
  356. // bind javascript events to the new objects that will be returned
  357. // so that AHAH enabled elements will work.
  358. $settings = tripal_core_ahah_bind_events();
  359. // return the updated JSON
  360. drupal_json(
  361. array(
  362. 'status' => $status,
  363. 'data' => $data,
  364. 'settings' => $settings,
  365. )
  366. );
  367. }
  368. /*
  369. *
  370. */
  371. function tripal_pub_get_search_results($search_array, $limit, $pager_id) {
  372. // build the SQL based on the criteria provided by the user
  373. $select = "SELECT DISTINCT P.*, CP.nid ";
  374. $from = "FROM {pub} P
  375. LEFT JOIN public.chado_pub CP on P.pub_id = CP.pub_id
  376. ";
  377. $where = "WHERE ";
  378. $order = "ORDER BY P.pyear DESC, P.title ASC";
  379. $fargs = array(); // arguments for from, inner join clause
  380. $wargs = array(); // arguments for where clause
  381. $join = 0;
  382. $num_criteria = $search_array['num_criteria'];
  383. $from_year = $search_array['from_year'];
  384. $to_year = $search_array['to_year'];
  385. for ($i = 1; $i <= $num_criteria; $i++) {
  386. $value = $search_array['criteria'][$i]['search_terms'];
  387. $type_id = $search_array['criteria'][$i]['scope'];
  388. $mode = $search_array['criteria'][$i]['mode'];
  389. $op = $search_array['criteria'][$i]['operation'];
  390. // skip criteria with no values
  391. if(!$value) {
  392. continue;
  393. }
  394. // to prevent SQL injection make sure our operator is
  395. // what we expect
  396. if ($op and $op != "AND" and $op != "OR" and $op != 'NOT') {
  397. $op = 'AND';
  398. }
  399. if ($op == 'NOT') {
  400. $op = 'AND NOT';
  401. }
  402. $action = "= lower('%s')";
  403. if($mode == 'Contains') {
  404. $action = 'LIKE lower(\'%%%s%%\')';
  405. }
  406. if($mode == 'Starts With') {
  407. $action = '= lower(\'%%%s\')';
  408. }
  409. if($mode == 'Ends With') {
  410. $action = '= lower(\'%s%%\')';
  411. }
  412. // get the scope type
  413. $values = array('cvterm_id' => $type_id);
  414. $cvterm = tripal_core_chado_select('cvterm', array('name'), $values);
  415. $type_name = $cvterm[0]->name;
  416. if ($type_name == 'Title') {
  417. $where .= " $op (lower(P.title) $action) ";
  418. $wargs[] = $value;
  419. }
  420. elseif ($type_name == 'Year') {
  421. $where .= " $op (lower(P.pyear) $action) ";
  422. $wargs[] = $value;
  423. }
  424. elseif ($type_name == 'Volume') {
  425. $where .= " $op (lower(P.volume) $action) ";
  426. $wargs[] = $value;
  427. }
  428. elseif ($type_name == 'Issue') {
  429. $where .= " $op (lower(P.issue) $action)";
  430. $wargs[] = $value;
  431. }
  432. elseif ($type_name == 'Journal Name') {
  433. $where .= " $op (lower(P.series_name) $action) ";
  434. $wargs[] = $value;
  435. }
  436. elseif ($type_id == 0) { //'Any Field'
  437. $from .= " LEFT JOIN {pubprop} PP$i ON PP$i.pub_id = P.pub_id ";
  438. $where .= " $op (lower(PP$i.value) $action OR
  439. lower(P.title) $action OR
  440. lower(P.volumetitle) $action OR
  441. lower(P.publisher) $action OR
  442. lower(P.pubplace) $action OR
  443. lower(P.miniref) $action OR
  444. lower(P.series_name) $action) ";
  445. $wargs[] = $value;
  446. $wargs[] = $value;
  447. $wargs[] = $value;
  448. $wargs[] = $value;
  449. $wargs[] = $value;
  450. $wargs[] = $value;
  451. $wargs[] = $value;
  452. }
  453. // for all other properties
  454. else {
  455. $from .= " LEFT JOIN {pubprop} PP$i ON PP$i.pub_id = P.pub_id AND PP$i.type_id = %d ";
  456. $where .= " $op (lower(PP$i.value) $action) ";
  457. $fargs[] = $type_id;
  458. $wargs[] = $value;
  459. }
  460. }
  461. if($from_year and $to_year) {
  462. $where .= " AND (P.pyear ~ '....' AND to_number(P.pyear,'9999') >= %d AND to_number(P.pyear,'9999') <= %d) ";
  463. $wargs[] = $from_year;
  464. $wargs[] = $to_year;
  465. }
  466. $sql = "$select $from $where $order";
  467. $count = "SELECT count(*) FROM ($select $from $where $order) as t1";
  468. $args = array_merge($fargs, $wargs);
  469. //dpm(array($mode, $sql, $args));
  470. return chado_pager_query($sql, $limit, $pager_id, $count, $args);
  471. }