pub_search.inc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. <?php
  2. /*
  3. *
  4. */
  5. function tripal_pub_search_page() {
  6. $limit = 25;
  7. // generate the search form
  8. $form = drupal_get_form('tripal_pub_search_form');
  9. $output = drupal_render($form);
  10. // retrieve any results
  11. if (array_key_exists('tripal_pub_search_form', $_SESSION) and
  12. $_SESSION['tripal_pub_search_form']['perform_search']) {
  13. $num_criteria = $_SESSION['tripal_pub_search_form']['num_criteria'];
  14. $from_year = $_SESSION['tripal_pub_search_form']['from_year'];
  15. $to_year = $_SESSION['tripal_pub_search_form']['to_year'];
  16. $search_array = array();
  17. $search_array['num_criteria'] = $num_criteria;
  18. $search_array['from_year'] = $from_year;
  19. $search_array['to_year'] = $to_year;
  20. for ($i = 0; $i <= $num_criteria; $i++) {
  21. $search_array['criteria'][$i]['search_terms'] = $_SESSION['tripal_pub_search_form']['criteria'][$i]['search_terms'];
  22. $search_array['criteria'][$i]['scope'] = $_SESSION['tripal_pub_search_form']['criteria'][$i]['scope'];
  23. $search_array['criteria'][$i]['mode'] = $_SESSION['tripal_pub_search_form']['criteria'][$i]['mode'];
  24. $search_array['criteria'][$i]['operation'] = $_SESSION['tripal_pub_search_form']['criteria'][$i]['operation'];
  25. }
  26. // get the list of publications from the remote database using the search criteria.
  27. $pubs = tripal_pub_get_search_results($search_array, $limit);
  28. $page = isset($_GET['page']) ? $_GET['page'] : '0';
  29. $total_records = $_SESSION['chado_pager'][0]['total_records'];
  30. $total_pages = (int) ($total_records / $limit) + 1;
  31. // iterate through the results and construct the table displaying the publications
  32. $rows = array();
  33. $i = $page * $limit + 1;
  34. foreach ($pubs as $pub) {
  35. // get the citation for this publication
  36. $values = array(
  37. 'pub_id' => $pub->pub_id,
  38. 'type_id' => array(
  39. 'name' => 'Citation',
  40. ),
  41. );
  42. $citation_rec = tripal_core_generate_chado_var('pubprop', $values);
  43. $citation_rec = tripal_core_expand_chado_vars($citation_rec, 'field', 'pubprop.value');
  44. // if we have the citation then use it, otherwise, just use the title
  45. $title = htmlspecialchars($pub->title);
  46. $result = $title;
  47. if ($pub->nid) {
  48. $result = l($title ,'node/' . $pub->nid, array('attributes' => array('target' => '_blank')));
  49. }
  50. if ($citation_rec->value) {
  51. $citation = htmlspecialchars($citation_rec->value);
  52. $result .= '<br>' . $citation;
  53. }
  54. $rows[] = array(
  55. number_format($i) . ".",
  56. $pub->pyear,
  57. $result
  58. );
  59. $i++;
  60. }
  61. if (count($rows) == 0) {
  62. $rows[] = array(
  63. array(
  64. 'data' => 'No results found',
  65. 'colspan' => 3
  66. )
  67. );
  68. }
  69. $headers = array('', 'Year', 'Publication');
  70. $table = array(
  71. 'header' => $headers,
  72. 'rows' => $rows,
  73. 'attributes' => array('id' => 'tripal-pub-search-results-table', 'border' => '0'),
  74. 'sticky' => TRUE,
  75. 'caption' => '',
  76. 'colgroups' => array(),
  77. 'empty' => '',
  78. );
  79. $results = theme_table($table);
  80. // generate the pager
  81. pager_default_initialize($total_records, $limit);
  82. $pager = array(
  83. 'tags' => array(),
  84. 'element' => 0,
  85. 'parameters' => array(),
  86. 'quantity' => $limit,
  87. );
  88. $pager = theme_pager($pager);
  89. // join all to form the results
  90. $output .= "<br><p><b>Found " . number_format($total_records) .
  91. ". Page " . ($page + 1) . " of $total_pages. " .
  92. " Results</b></br>" . $results . $pager;
  93. }
  94. return $output;
  95. }
  96. /**
  97. * Purpose: Provides the form to search pubmed
  98. *
  99. * @ingroup tripal_pub
  100. */
  101. function tripal_pub_search_form($form, &$form_state) {
  102. // Default values can come in the following ways:
  103. //
  104. // 1) as elements of the $pub_importer object. This occurs when editing an existing importer
  105. // 2) in the $form_state['values'] array which occurs on a failed validation or
  106. // ajax callbacks from non submit form elements
  107. // 3) in the $form_state['input'] array which occurs on ajax callbacks from submit
  108. // form elements and the form is being rebuilt
  109. //
  110. // Set the default values. If the pub_import_id isn't already defined by the form values
  111. // and one is provided then look it up in the database
  112. $criteria = NULL;
  113. $num_criteria = 2;
  114. $from_year = '';
  115. $to_year = '';
  116. // if the session has variables then use those. This should only happen when
  117. // the 'Test Criteria' button is clicked.
  118. if (array_key_exists('tripal_pub_search_form', $_SESSION)) {
  119. $num_criteria = $_SESSION['tripal_pub_search_form']['num_criteria'] ? $_SESSION['tripal_pub_search_form']['num_criteria'] : $num_criteria;
  120. $from_year = $_SESSION['tripal_pub_search_form']['from_year'] ? $_SESSION['tripal_pub_search_form']['from_year'] : '';
  121. $to_year = $_SESSION['tripal_pub_search_form']['to_year'] ? $_SESSION['tripal_pub_search_form']['to_year'] : '';
  122. }
  123. if (array_key_exists('values', $form_state)) {
  124. $num_criteria = $form_state['values']['num_criteria'] ? $form_state['values']['num_criteria'] : $num_criteria;
  125. $from_year = $form_state['values']['from_year'] ? $form_state['values']['from_year'] : $from_year;
  126. $to_year = $form_state['values']['to_year'] ? $form_state['values']['to_year'] : $to_year;
  127. }
  128. if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
  129. $num_criteria = $form_state['input']['num_criteria'] ? $form_state['input']['num_criteria'] : $num_criteria;
  130. $from_year = $form_state['input']['from_year'] ? $form_state['input']['from_year'] : $from_year;
  131. $to_year = $form_state['input']['to_year'] ? $form_state['input']['to_year'] : $to_year;
  132. }
  133. if (array_key_exists('triggering_element', $form_state) and
  134. $form_state['triggering_element']['#name'] == 'add') {
  135. $num_criteria++;
  136. }
  137. if (array_key_exists('triggering_element', $form_state) and
  138. $form_state['triggering_element']['#name'] == 'remove') {
  139. $num_criteria--;
  140. }
  141. $form['num_criteria']= array(
  142. '#type' => 'hidden',
  143. '#default_value' => $num_criteria,
  144. );
  145. if (user_access('access administration pages')) {
  146. $form['admin-instructions'] = array(
  147. '#prefix' => '<div class="tripal-no-results">',
  148. '#markup' => t('Administrators, you can select the fields with which a user can use to search,
  149. by checking the desired fields on the ' .
  150. l('Publication Module Settings Page', 'admin/tripal/chado/tripal_pub/configuration'). ' in
  151. the section titled "Search Options". The selected fields will appear in the dropdowns below.'),
  152. '#suffix' => '</div>'
  153. );
  154. }
  155. $form['instructions'] = array(
  156. '#markup' => t('To search for publications enter keywords in the text boxes below.
  157. You can limit your search by selecting the field in the dropdown box. Click the
  158. add and remove buttons to add additional fields for searching. '),
  159. );
  160. // get publication properties list
  161. $properties = array();
  162. $properties[] = 'Any Field';
  163. $sql = "
  164. SELECT DISTINCT CVTS.cvterm_id, CVTS.name, CVTS.definition
  165. FROM {cvtermpath} CVTP
  166. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  167. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  168. INNER JOIN {cv} ON CVTO.cv_id = CV.cv_id
  169. WHERE CV.name = 'tripal_pub' and
  170. (CVTO.name = 'Publication Details' or CVTS.name = 'Publication Type') and
  171. NOT CVTS.is_obsolete = 1
  172. ORDER BY CVTS.name ASC
  173. ";
  174. $allowed_fields = variable_get('tripal_pub_allowed_search_fields', array());
  175. $prop_types = chado_query($sql);
  176. foreach ($prop_types as $prop) {
  177. if(array_key_exists($prop->cvterm_id, $allowed_fields) and $allowed_fields[$prop->cvterm_id] > 0) {
  178. $properties[$prop->cvterm_id] = $prop->name;
  179. }
  180. }
  181. for($i = 1; $i <= $num_criteria; $i++) {
  182. $search_terms = '';
  183. $scope = '';
  184. $operation = '';
  185. $mode = '';
  186. // first populate defaults using any values in the SESSION variable
  187. if (array_key_exists('tripal_pub_search_form', $_SESSION)) {
  188. $search_terms = $_SESSION['tripal_pub_search_form']['criteria'][$i]['search_terms'] ? $_SESSION['tripal_pub_search_form']['criteria'][$i]['search_terms'] : $search_terms;
  189. $scope = $_SESSION['tripal_pub_search_form']['criteria'][$i]['scope'] ? $_SESSION['tripal_pub_search_form']['criteria'][$i]['scope'] : $scope;
  190. $mode = $_SESSION['tripal_pub_search_form']['criteria'][$i]['mode'] ? $_SESSION['tripal_pub_search_form']['criteria'][$i]['mode'] : $mode;
  191. $operation = $_SESSION['tripal_pub_search_form']['criteria'][$i]['operation'] ? $_SESSION['tripal_pub_search_form']['criteria'][$i]['operation'] : $operation;
  192. }
  193. if (array_key_exists('values', $form_state)) {
  194. $search_terms = array_key_exists("search_terms-$i", $form_state['values']) ? $form_state['values']["search_terms-$i"] : $search_terms;
  195. $scope = array_key_exists("scope-$i", $form_state['values']) ? $form_state['values']["scope-$i"] : $scope;
  196. $mode = array_key_exists("mode-$i", $form_state['values']) ? $form_state['values']["mode-$i"] : $mode;
  197. $operation = array_key_exists("operation-$i", $form_state['values']) ? $form_state['values']["operation-$i"] : $operation;
  198. }
  199. if (array_key_exists('input', $form_state)) {
  200. $search_terms = array_key_exists("search_terms-$i", $form_state['input']) ? $form_state['input']["search_terms-$i"] : $search_terms;
  201. $scope = array_key_exists("scope-$i", $form_state['input']) ? $form_state['input']["scope-$i"] : $scope;
  202. $mode = array_key_exists("mode-$i", $form_state['input']) ? $form_state['input']["mode-$i"] : $mode;
  203. $operation = array_key_exists("operation-$i", $form_state['input']) ? $form_state['input']["operation-$i"] : $operation;
  204. }
  205. // default to searching the title and abstract
  206. if (!$scope) {
  207. $scope = 'abstract';
  208. }
  209. $form['criteria'][$i]["search_terms-$i"] = array(
  210. '#type' => 'textfield',
  211. '#default_value' => $search_terms,
  212. '#required' => FALSE,
  213. );
  214. $form['criteria'][$i]["scope-$i"] = array(
  215. '#type' => 'select',
  216. '#options' => $properties,
  217. '#default_value' => $scope,
  218. '#attributes' => array('class' => array('tripal-pub-search-form-scope-select')),
  219. );
  220. /*
  221. $form['criteria'][$i]["mode-$i"] = array(
  222. '#type' => 'select',
  223. '#options' => array(
  224. 'Contains' => 'Contains',
  225. 'Starts With' => 'Starts With',
  226. 'Ends With' => 'Ends With',
  227. 'Exactly' => 'Exactly'),
  228. '#default_value' => $mode,
  229. );*/
  230. if ($i > 1) {
  231. $form['criteria'][$i]["operation-$i"] = array(
  232. '#type' => 'select',
  233. '#options' => array(
  234. 'AND' => 'AND',
  235. 'OR' => 'OR',
  236. 'NOT' => 'NOT'),
  237. '#default_value' => $operation,
  238. );
  239. }
  240. if ($i == $num_criteria) {
  241. if($i > 1) {
  242. $form['criteria'][$i]["remove-$i"] = array(
  243. '#type' => 'button',
  244. '#name' => 'remove',
  245. '#value' => t('Remove'),
  246. '#ajax' => array(
  247. 'callback' => "tripal_pubs_search_form_ajax_update",
  248. 'wrapper' => 'tripal-pub-search-form-criteria',
  249. 'effect' => 'fade',
  250. 'method' => 'replace',
  251. 'prevent' => 'click'
  252. ),
  253. // When this button is clicked, the form will be validated and submitted.
  254. // Therefore, we set custom submit and validate functions to override the
  255. // default form submit. In the validate function we set the form_state
  256. // to rebuild the form so the submit function never actually gets called,
  257. // but we need it or Drupal will run the default validate anyway.
  258. // we also set #limit_validation_errors to empty so fields that
  259. // are required that don't have values won't generate warnings.
  260. '#submit' => array('tripal_pub_search_form_ajax_button_submit'),
  261. '#validate' => array('tripal_pub_search_form_ajax_button_validate'),
  262. '#limit_validation_errors' => array(),
  263. );
  264. }
  265. $form['criteria'][$i]["add-$i"] = array(
  266. '#type' => 'button',
  267. '#name' => 'add',
  268. '#value' => t('Add'),
  269. '#ajax' => array(
  270. 'callback' => "tripal_pubs_search_form_ajax_update",
  271. 'wrapper' => 'tripal-pub-search-form-criteria',
  272. 'effect' => 'fade',
  273. 'method' => 'replace',
  274. 'prevent' => 'click'
  275. ),
  276. // When this button is clicked, the form will be validated and submitted.
  277. // Therefore, we set custom submit and validate functions to override the
  278. // default form submit. In the validate function we set the form_state
  279. // to rebuild the form so the submit function never actually gets called,
  280. // but we need it or Drupal will run the default validate anyway.
  281. // we also set #limit_validation_errors to empty so fields that
  282. // are required that don't have values won't generate warnings.
  283. '#submit' => array('tripal_pub_search_form_ajax_button_submit'),
  284. '#validate' => array('tripal_pub_search_form_ajax_button_validate'),
  285. '#limit_validation_errors' => array(),
  286. );
  287. }
  288. }
  289. $form['criteria']["date"] = array(
  290. '#type' => 'select',
  291. '#options' => array('Years' => 'Years'),
  292. '#attributes' => array('class' => array('tripal-pub-search-form-scope-select')),
  293. );
  294. $form['criteria']["from_year"] = array(
  295. '#type' => 'textfield',
  296. '#default_value' => $from_year,
  297. '#required' => FALSE,
  298. '#title' => 'from',
  299. '#size' => 4,
  300. '#maxlength' => 4,
  301. );
  302. $form['criteria']["to_year"] = array(
  303. '#type' => 'textfield',
  304. '#default_value' => $to_year,
  305. '#required' => FALSE,
  306. '#title' => 'to',
  307. '#size' => 4,
  308. '#maxlength' => 4,
  309. );
  310. $form['search'] = array(
  311. '#type' => 'submit',
  312. '#value' => t('Search'),
  313. );
  314. $form['reset'] = array(
  315. '#type' => 'submit',
  316. '#value' => t('Reset'),
  317. );
  318. $form['criteria']['#theme'] = 'tripal_pub_search_setup_form_elements';
  319. return $form;
  320. }
  321. /**
  322. * This function is used to rebuild the form if an ajax call is made vai a button.
  323. * The button causes the form to be submitted. We don't want this so we override
  324. * the validate and submit routines on the form button. Therefore, this function
  325. * only needs to tell Drupal to rebuild the form
  326. */
  327. function tripal_pub_search_form_ajax_button_submit() {
  328. $form_state['rebuild'] = TRUE;
  329. }
  330. /**
  331. * This function is just a dummy to override the default form submit on ajax calls for buttons
  332. */
  333. function tripal_pub_search_form_ajax_button_validate() {
  334. // do nothing
  335. }
  336. /**
  337. *
  338. */
  339. function tripal_pub_search_form_validate($form, &$form_state) {
  340. $num_criteria = $form_state['values']['num_criteria'];
  341. $from_year = $form_state['values']['from_year'];
  342. $to_year = $form_state['values']['to_year'];
  343. $op = $form_state['values']['op'];
  344. // no need to vlaidate on a reset
  345. if ($op == 'Reset') {
  346. return;
  347. }
  348. if($from_year and !$to_year) {
  349. form_set_error('to_year', 'Please provide a 4-digit year.');
  350. }
  351. if(!$from_year and $to_year) {
  352. form_set_error('from_year', 'Please provide a 4-digit year.');
  353. }
  354. if($from_year and !preg_match('/\d\d\d\d/' , $from_year)) {
  355. form_set_error('from_year', 'Please provide a 4-digit year.');
  356. }
  357. if($to_year and !preg_match('/\d\d\d\d/' , $to_year)) {
  358. form_set_error('to_year', 'Please provide a 4-digit year.');
  359. }
  360. }
  361. /**
  362. *
  363. */
  364. function tripal_pub_search_form_submit($form, &$form_state) {
  365. $num_criteria = $form_state['values']['num_criteria'];
  366. $from_year = $form_state['values']['from_year'];
  367. $to_year = $form_state['values']['to_year'];
  368. $op = $form_state['values']['op'];
  369. // set the session variables
  370. if($op == 'Search') {
  371. $_SESSION['tripal_pub_search_form']['num_criteria'] = $num_criteria;
  372. unset($_SESSION['tripal_pub_search_form']['criteria']);
  373. for ($i = 0; $i <= $num_criteria; $i++) {
  374. $search_terms = '';
  375. $scope = '';
  376. $mode = 'Contains';
  377. $operation = '';
  378. if (array_key_exists("search_terms-$i", $form_state['values'])) {
  379. $search_terms = trim($form_state['values']["search_terms-$i"]);
  380. }
  381. if (array_key_exists("scope-$i", $form_state['values'])) {
  382. $scope = $form_state['values']["scope-$i"];
  383. }
  384. if (array_key_exists("operation-$i", $form_state['values'])) {
  385. $operation = $form_state['values']["operation-$i"];
  386. }
  387. //$mode = $form_state['values']["mode-$i"];
  388. $_SESSION['tripal_pub_search_form']['criteria'][$i] = array(
  389. 'search_terms' => $search_terms,
  390. 'scope' => $scope,
  391. 'mode' => $mode,
  392. 'operation' => $operation
  393. );
  394. }
  395. $_SESSION['tripal_pub_search_form']['from_year'] = $from_year;
  396. $_SESSION['tripal_pub_search_form']['to_year'] = $to_year;
  397. $_SESSION['tripal_pub_search_form']['perform_search'] = 1;
  398. }
  399. if($op == 'Reset') {
  400. unset($_SESSION['tripal_pub_search_form']);
  401. }
  402. }
  403. /*
  404. * AHAH callback
  405. */
  406. function tripal_pub_search_page_update_criteria($action, $i) {
  407. $status = TRUE;
  408. // prepare and render the form
  409. $form = tripal_core_ahah_prepare_form();
  410. $data = theme('tripal_pub_search_form', $form);
  411. // bind javascript events to the new objects that will be returned
  412. // so that AHAH enabled elements will work.
  413. $settings = tripal_core_ahah_bind_events();
  414. // return the updated JSON
  415. drupal_json(
  416. array(
  417. 'status' => $status,
  418. 'data' => $data,
  419. 'settings' => $settings,
  420. )
  421. );
  422. }
  423. /**
  424. *
  425. */
  426. function tripal_pub_get_search_results($search_array, $limit) {
  427. // build the SQL based on the criteria provided by the user
  428. $select = "SELECT DISTINCT P.*, CP.nid ";
  429. $from = "FROM {pub} P
  430. LEFT JOIN public.chado_pub CP on P.pub_id = CP.pub_id
  431. INNER JOIN {cvterm} CVT on CVT.cvterm_id = P.type_id
  432. ";
  433. $where = "WHERE (NOT P.title = 'null') "; // always exclude the dummy pub
  434. $order = "ORDER BY P.pyear DESC, P.title ASC";
  435. $args = array(); // arguments for where clause
  436. $join = 0;
  437. $num_criteria = $search_array['num_criteria'];
  438. $from_year = $search_array['from_year'];
  439. $to_year = $search_array['to_year'];
  440. for ($i = 1; $i <= $num_criteria; $i++) {
  441. $value = $search_array['criteria'][$i]['search_terms'];
  442. $type_id = $search_array['criteria'][$i]['scope'];
  443. $mode = $search_array['criteria'][$i]['mode'];
  444. $op = $search_array['criteria'][$i]['operation'];
  445. // skip criteria with no values
  446. if(!$value) {
  447. continue;
  448. }
  449. // to prevent SQL injection make sure our operator is
  450. // what we expect
  451. if ($op and $op != "AND" and $op != "OR" and $op != 'NOT') {
  452. $op = 'AND';
  453. }
  454. if ($op == 'NOT') {
  455. $op = 'AND NOT';
  456. }
  457. if (!$op) {
  458. $op = 'AND';
  459. }
  460. // get the scope type
  461. $values = array('cvterm_id' => $type_id);
  462. $cvterm = tripal_core_chado_select('cvterm', array('name'), $values);
  463. $type_name = '';
  464. if (count($cvterm) > 0) {
  465. $type_name = $cvterm[0]->name;
  466. }
  467. if ($type_name == 'Title') {
  468. $where .= " $op (lower(P.title) LIKE lower(:crit$i)) ";
  469. $args[":crit$i"] = '%' . $value . '%';
  470. }
  471. elseif ($type_name == 'Year') {
  472. $where .= " $op (lower(P.pyear) = lower(:crit$i)) ";
  473. $args[":crit$i"] = '%' . $value . '%';
  474. }
  475. elseif ($type_name == 'Volume') {
  476. $where .= " $op (lower(P.volume) = lower(:crit$i)) ";
  477. $args[":crit$i"] = '%' . $value . '%';
  478. }
  479. elseif ($type_name == 'Issue') {
  480. $where .= " $op (lower(P.issue) = lower(:crit$i)) ";
  481. $args[":crit$i"] = '%' . $value . '%';
  482. }
  483. elseif ($type_name == 'Journal Name') {
  484. $from .= " LEFT JOIN {pubprop} PP$i ON PP$i.pub_id = P.pub_id AND PP$i.type_id = :crit$i ";
  485. $where .= " $op ((lower(P.series_name) = lower(:crit$i) and CVT.name = 'Journal Article') OR
  486. (lower(PP$i.value) = lower(:crit$i))) ";
  487. $args[":crit$i"] = $type_id;
  488. }
  489. elseif ($type_name == 'Conference Name') {
  490. $from .= " LEFT JOIN {pubprop} PP$i ON PP$i.pub_id = P.pub_id AND PP$i.type_id = :crit$i ";
  491. $where .= " $op ((lower(P.series_name) = lower(:crit$i) and CVT.name = 'Conference Proceedings') OR
  492. (lower(PP$i.value) = lower(:crit$i))) ";
  493. $args[":crit$i"] = $type_id;
  494. }
  495. elseif ($type_name == 'Publication Type') {
  496. $where .= " $op (lower(CVT.name) = lower(:crit$i))";
  497. $args[":crit$i"] = $value;
  498. }
  499. elseif ($type_id == 0) { //'Any Field'
  500. $from .= " LEFT JOIN {pubprop} PP$i ON PP$i.pub_id = P.pub_id ";
  501. $where .= " $op (lower(PP$i.value) LIKE lower(:crit$i) OR
  502. lower(P.title) LIKE lower(:crit$i) OR
  503. lower(P.volumetitle) LIKE lower(:crit$i) OR
  504. lower(P.publisher) LIKE lower(:crit$i) OR
  505. lower(P.uniquename) LIKE lower(:crit$i) OR
  506. lower(P.pubplace) LIKE lower(:crit$i) OR
  507. lower(P.miniref) LIKE lower(:crit$i) OR
  508. lower(P.series_name) LIKE lower(:crit$i)) ";
  509. $args[":crit$i"] = '%' . $value . '%';
  510. }
  511. // for all other properties
  512. else {
  513. $from .= " LEFT JOIN {pubprop} PP$i ON PP$i.pub_id = P.pub_id AND PP$i.type_id = :type_id$i ";
  514. $where .= " $op (lower(PP$i.value) LIKE lower(:crit$i)) ";
  515. $args[":crit$i"] = '%' . $value . '%';
  516. $args[":type_id$i"] = $type_id;
  517. }
  518. }
  519. if($from_year and $to_year) {
  520. $where .= " AND (P.pyear ~ '....' AND to_number(P.pyear,'9999') >= :from$i AND to_number(P.pyear,'9999') <= :to$i) ";
  521. $args[":from$i"] = $from_year;
  522. $args[":to$i"] = $to_year;
  523. }
  524. $sql = "$select $from $where $order";
  525. $count = "SELECT count(*) FROM ($select $from $where $order) as t1";
  526. return chado_pager_query($sql, $args, $limit, 0, $count);
  527. }
  528. /**
  529. *
  530. */
  531. function tripal_pubs_search_form_ajax_update($form, $form_state) {
  532. return $form['criteria'];
  533. }
  534. /**
  535. *
  536. * @param unknown $form
  537. */
  538. function theme_tripal_pub_search_setup_form_elements($variables) {
  539. $form = $variables['form'];
  540. $rows = array();
  541. // put each criteria element in a single table row
  542. foreach ($form as $i => $element) {
  543. if(is_numeric($i)) {
  544. $rows[] = array(
  545. drupal_render($element["operation-$i"]),
  546. drupal_render($element["scope-$i"]),
  547. //drupal_render($element["mode-$i"]) .
  548. drupal_render($element["search_terms-$i"]),
  549. array(
  550. 'data' => drupal_render($element["add-$i"]) . drupal_render($element["remove-$i"]),
  551. 'nowrap' => 'nowrap',
  552. ),
  553. );
  554. }
  555. }
  556. // add in the from_year and to_year elements as the final row of the table
  557. $rows[] = array(
  558. '&nbsp;',
  559. drupal_render($form['date']),
  560. array(
  561. 'data' =>
  562. "<div id=\"pub-search-form-dates-row\">
  563. <div id=\"pub-search-form-dates\"> ".
  564. drupal_render($form['from_year']) .
  565. drupal_render($form['to_year']) . "
  566. </div>
  567. </div>
  568. ",
  569. ),
  570. ''
  571. );
  572. $headers = array();
  573. $table = array(
  574. 'header' => $headers,
  575. 'rows' => $rows,
  576. 'attributes' => array('id' => 'tripal-pub-search-form-table', 'border' => '0'),
  577. 'sticky' => TRUE,
  578. 'caption' => '',
  579. 'colgroups' => array(),
  580. 'empty' => '',
  581. );
  582. $results = '<div id="tripal-pub-search-form-criteria">';
  583. $results .= theme_table($table);
  584. $results .= '</div>';
  585. return $results;
  586. }