tripal_pub.pub_search.inc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  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 = chado_generate_var('pubprop', $values);
  43. $citation_rec = chado_expand_var($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. $form['admin-instructions'] = array(
  146. '#markup' => theme('tripal_admin_message', array('message' =>
  147. t('Administrators, you can select the fields with which a user can use to search,
  148. by checking the desired fields on the ' .
  149. l('Publication Module Settings Page', 'admin/tripal/chado/tripal_pub/configuration'). ' in
  150. the section titled "Search Options". The selected fields will appear in the dropdowns below.'))),
  151. );
  152. $form['instructions'] = array(
  153. '#markup' => t('To search for publications enter keywords in the text boxes below.
  154. You can limit your search by selecting the field in the dropdown box. Click the
  155. add and remove buttons to add additional fields for searching. '),
  156. );
  157. // get publication properties list
  158. $properties = array();
  159. $properties[] = 'Any Field';
  160. $sql = "
  161. SELECT DISTINCT CVTS.cvterm_id, CVTS.name, CVTS.definition
  162. FROM {cvtermpath} CVTP
  163. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  164. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  165. INNER JOIN {cv} ON CVTO.cv_id = CV.cv_id
  166. WHERE CV.name = 'tripal_pub' and
  167. (CVTO.name = 'Publication Details' or CVTS.name = 'Publication Type') and
  168. NOT CVTS.is_obsolete = 1
  169. ORDER BY CVTS.name ASC
  170. ";
  171. $allowed_fields = variable_get('tripal_pub_allowed_search_fields', array());
  172. $prop_types = chado_query($sql);
  173. foreach ($prop_types as $prop) {
  174. if(array_key_exists($prop->cvterm_id, $allowed_fields) and $allowed_fields[$prop->cvterm_id] > 0) {
  175. $properties[$prop->cvterm_id] = $prop->name;
  176. }
  177. }
  178. for($i = 1; $i <= $num_criteria; $i++) {
  179. $search_terms = '';
  180. $scope = '';
  181. $operation = '';
  182. $mode = '';
  183. // first populate defaults using any values in the SESSION variable
  184. if (array_key_exists('tripal_pub_search_form', $_SESSION)) {
  185. $search_terms = $_SESSION['tripal_pub_search_form']['criteria'][$i]['search_terms'] ? $_SESSION['tripal_pub_search_form']['criteria'][$i]['search_terms'] : $search_terms;
  186. $scope = $_SESSION['tripal_pub_search_form']['criteria'][$i]['scope'] ? $_SESSION['tripal_pub_search_form']['criteria'][$i]['scope'] : $scope;
  187. $mode = $_SESSION['tripal_pub_search_form']['criteria'][$i]['mode'] ? $_SESSION['tripal_pub_search_form']['criteria'][$i]['mode'] : $mode;
  188. $operation = $_SESSION['tripal_pub_search_form']['criteria'][$i]['operation'] ? $_SESSION['tripal_pub_search_form']['criteria'][$i]['operation'] : $operation;
  189. }
  190. if (array_key_exists('values', $form_state)) {
  191. $search_terms = array_key_exists("search_terms-$i", $form_state['values']) ? $form_state['values']["search_terms-$i"] : $search_terms;
  192. $scope = array_key_exists("scope-$i", $form_state['values']) ? $form_state['values']["scope-$i"] : $scope;
  193. $mode = array_key_exists("mode-$i", $form_state['values']) ? $form_state['values']["mode-$i"] : $mode;
  194. $operation = array_key_exists("operation-$i", $form_state['values']) ? $form_state['values']["operation-$i"] : $operation;
  195. }
  196. if (array_key_exists('input', $form_state)) {
  197. $search_terms = array_key_exists("search_terms-$i", $form_state['input']) ? $form_state['input']["search_terms-$i"] : $search_terms;
  198. $scope = array_key_exists("scope-$i", $form_state['input']) ? $form_state['input']["scope-$i"] : $scope;
  199. $mode = array_key_exists("mode-$i", $form_state['input']) ? $form_state['input']["mode-$i"] : $mode;
  200. $operation = array_key_exists("operation-$i", $form_state['input']) ? $form_state['input']["operation-$i"] : $operation;
  201. }
  202. // default to searching the title and abstract
  203. if (!$scope) {
  204. $scope = 'abstract';
  205. }
  206. $form['criteria'][$i]["search_terms-$i"] = array(
  207. '#type' => 'textfield',
  208. '#default_value' => $search_terms,
  209. '#required' => FALSE,
  210. );
  211. $form['criteria'][$i]["scope-$i"] = array(
  212. '#type' => 'select',
  213. '#options' => $properties,
  214. '#default_value' => $scope,
  215. '#attributes' => array('class' => array('tripal-pub-search-form-scope-select')),
  216. );
  217. /*
  218. $form['criteria'][$i]["mode-$i"] = array(
  219. '#type' => 'select',
  220. '#options' => array(
  221. 'Contains' => 'Contains',
  222. 'Starts With' => 'Starts With',
  223. 'Ends With' => 'Ends With',
  224. 'Exactly' => 'Exactly'),
  225. '#default_value' => $mode,
  226. );*/
  227. if ($i > 1) {
  228. $form['criteria'][$i]["operation-$i"] = array(
  229. '#type' => 'select',
  230. '#options' => array(
  231. 'AND' => 'AND',
  232. 'OR' => 'OR',
  233. 'NOT' => 'NOT'),
  234. '#default_value' => $operation,
  235. );
  236. }
  237. if ($i == $num_criteria) {
  238. if($i > 1) {
  239. $form['criteria'][$i]["remove-$i"] = array(
  240. '#type' => 'button',
  241. '#name' => 'remove',
  242. '#value' => t('Remove'),
  243. '#ajax' => array(
  244. 'callback' => "tripal_pubs_search_form_ajax_update",
  245. 'wrapper' => 'tripal-pub-search-form-criteria',
  246. 'effect' => 'fade',
  247. 'method' => 'replace',
  248. 'prevent' => 'click'
  249. ),
  250. // When this button is clicked, the form will be validated and submitted.
  251. // Therefore, we set custom submit and validate functions to override the
  252. // default form submit. In the validate function we set the form_state
  253. // to rebuild the form so the submit function never actually gets called,
  254. // but we need it or Drupal will run the default validate anyway.
  255. // we also set #limit_validation_errors to empty so fields that
  256. // are required that don't have values won't generate warnings.
  257. '#submit' => array('tripal_pub_search_form_ajax_button_submit'),
  258. '#validate' => array('tripal_pub_search_form_ajax_button_validate'),
  259. '#limit_validation_errors' => array(),
  260. );
  261. }
  262. $form['criteria'][$i]["add-$i"] = array(
  263. '#type' => 'button',
  264. '#name' => 'add',
  265. '#value' => t('Add'),
  266. '#ajax' => array(
  267. 'callback' => "tripal_pubs_search_form_ajax_update",
  268. 'wrapper' => 'tripal-pub-search-form-criteria',
  269. 'effect' => 'fade',
  270. 'method' => 'replace',
  271. 'prevent' => 'click'
  272. ),
  273. // When this button is clicked, the form will be validated and submitted.
  274. // Therefore, we set custom submit and validate functions to override the
  275. // default form submit. In the validate function we set the form_state
  276. // to rebuild the form so the submit function never actually gets called,
  277. // but we need it or Drupal will run the default validate anyway.
  278. // we also set #limit_validation_errors to empty so fields that
  279. // are required that don't have values won't generate warnings.
  280. '#submit' => array('tripal_pub_search_form_ajax_button_submit'),
  281. '#validate' => array('tripal_pub_search_form_ajax_button_validate'),
  282. '#limit_validation_errors' => array(),
  283. );
  284. }
  285. }
  286. $form['criteria']["date"] = array(
  287. '#type' => 'select',
  288. '#options' => array('Years' => 'Years'),
  289. '#attributes' => array('class' => array('tripal-pub-search-form-scope-select')),
  290. );
  291. $form['criteria']["from_year"] = array(
  292. '#type' => 'textfield',
  293. '#default_value' => $from_year,
  294. '#required' => FALSE,
  295. '#title' => 'from',
  296. '#size' => 4,
  297. '#maxlength' => 4,
  298. );
  299. $form['criteria']["to_year"] = array(
  300. '#type' => 'textfield',
  301. '#default_value' => $to_year,
  302. '#required' => FALSE,
  303. '#title' => 'to',
  304. '#size' => 4,
  305. '#maxlength' => 4,
  306. );
  307. $form['search'] = array(
  308. '#type' => 'submit',
  309. '#value' => t('Search'),
  310. );
  311. $form['reset'] = array(
  312. '#type' => 'submit',
  313. '#value' => t('Reset'),
  314. );
  315. $form['criteria']['#theme'] = 'tripal_pub_search_setup_form_elements';
  316. return $form;
  317. }
  318. /**
  319. * This function is used to rebuild the form if an ajax call is made vai a button.
  320. * The button causes the form to be submitted. We don't want this so we override
  321. * the validate and submit routines on the form button. Therefore, this function
  322. * only needs to tell Drupal to rebuild the form
  323. */
  324. function tripal_pub_search_form_ajax_button_submit() {
  325. $form_state['rebuild'] = TRUE;
  326. }
  327. /**
  328. * This function is just a dummy to override the default form submit on ajax calls for buttons
  329. */
  330. function tripal_pub_search_form_ajax_button_validate() {
  331. // do nothing
  332. }
  333. /**
  334. *
  335. */
  336. function tripal_pub_search_form_validate($form, &$form_state) {
  337. $num_criteria = $form_state['values']['num_criteria'];
  338. $from_year = $form_state['values']['from_year'];
  339. $to_year = $form_state['values']['to_year'];
  340. $op = $form_state['values']['op'];
  341. // no need to vlaidate on a reset
  342. if ($op == 'Reset') {
  343. return;
  344. }
  345. if($from_year and !$to_year) {
  346. form_set_error('to_year', 'Please provide a 4-digit year.');
  347. }
  348. if(!$from_year and $to_year) {
  349. form_set_error('from_year', 'Please provide a 4-digit year.');
  350. }
  351. if($from_year and !preg_match('/\d\d\d\d/' , $from_year)) {
  352. form_set_error('from_year', 'Please provide a 4-digit year.');
  353. }
  354. if($to_year and !preg_match('/\d\d\d\d/' , $to_year)) {
  355. form_set_error('to_year', 'Please provide a 4-digit year.');
  356. }
  357. }
  358. /**
  359. *
  360. */
  361. function tripal_pub_search_form_submit($form, &$form_state) {
  362. $num_criteria = $form_state['values']['num_criteria'];
  363. $from_year = $form_state['values']['from_year'];
  364. $to_year = $form_state['values']['to_year'];
  365. $op = $form_state['values']['op'];
  366. // set the session variables
  367. if($op == 'Search') {
  368. $_SESSION['tripal_pub_search_form']['num_criteria'] = $num_criteria;
  369. unset($_SESSION['tripal_pub_search_form']['criteria']);
  370. for ($i = 0; $i <= $num_criteria; $i++) {
  371. $search_terms = '';
  372. $scope = '';
  373. $mode = 'Contains';
  374. $operation = '';
  375. if (array_key_exists("search_terms-$i", $form_state['values'])) {
  376. $search_terms = trim($form_state['values']["search_terms-$i"]);
  377. }
  378. if (array_key_exists("scope-$i", $form_state['values'])) {
  379. $scope = $form_state['values']["scope-$i"];
  380. }
  381. if (array_key_exists("operation-$i", $form_state['values'])) {
  382. $operation = $form_state['values']["operation-$i"];
  383. }
  384. //$mode = $form_state['values']["mode-$i"];
  385. $_SESSION['tripal_pub_search_form']['criteria'][$i] = array(
  386. 'search_terms' => $search_terms,
  387. 'scope' => $scope,
  388. 'mode' => $mode,
  389. 'operation' => $operation
  390. );
  391. }
  392. $_SESSION['tripal_pub_search_form']['from_year'] = $from_year;
  393. $_SESSION['tripal_pub_search_form']['to_year'] = $to_year;
  394. $_SESSION['tripal_pub_search_form']['perform_search'] = 1;
  395. }
  396. if($op == 'Reset') {
  397. unset($_SESSION['tripal_pub_search_form']);
  398. }
  399. }
  400. /*
  401. * AHAH callback
  402. */
  403. function tripal_pub_search_page_update_criteria($action, $i) {
  404. $status = TRUE;
  405. // prepare and render the form
  406. $form = tripal_core_ahah_prepare_form();
  407. $data = theme('tripal_pub_search_form', $form);
  408. // bind javascript events to the new objects that will be returned
  409. // so that AHAH enabled elements will work.
  410. $settings = tripal_core_ahah_bind_events();
  411. // return the updated JSON
  412. drupal_json(
  413. array(
  414. 'status' => $status,
  415. 'data' => $data,
  416. 'settings' => $settings,
  417. )
  418. );
  419. }
  420. /**
  421. *
  422. */
  423. function tripal_pub_get_search_results($search_array, $limit) {
  424. // build the SQL based on the criteria provided by the user
  425. $select = "SELECT DISTINCT P.*, CP.nid ";
  426. $from = "FROM {pub} P
  427. LEFT JOIN public.chado_pub CP on P.pub_id = CP.pub_id
  428. INNER JOIN {cvterm} CVT on CVT.cvterm_id = P.type_id
  429. ";
  430. $where = "WHERE (NOT P.title = 'null') "; // always exclude the dummy pub
  431. $order = "ORDER BY P.pyear DESC, P.title ASC";
  432. $args = array(); // arguments for where clause
  433. $join = 0;
  434. $num_criteria = $search_array['num_criteria'];
  435. $from_year = $search_array['from_year'];
  436. $to_year = $search_array['to_year'];
  437. for ($i = 1; $i <= $num_criteria; $i++) {
  438. $value = $search_array['criteria'][$i]['search_terms'];
  439. $type_id = $search_array['criteria'][$i]['scope'];
  440. $mode = $search_array['criteria'][$i]['mode'];
  441. $op = $search_array['criteria'][$i]['operation'];
  442. // skip criteria with no values
  443. if(!$value) {
  444. continue;
  445. }
  446. // to prevent SQL injection make sure our operator is
  447. // what we expect
  448. if ($op and $op != "AND" and $op != "OR" and $op != 'NOT') {
  449. $op = 'AND';
  450. }
  451. if ($op == 'NOT') {
  452. $op = 'AND NOT';
  453. }
  454. if (!$op) {
  455. $op = 'AND';
  456. }
  457. // get the scope type
  458. $values = array('cvterm_id' => $type_id);
  459. $cvterm = chado_select_record('cvterm', array('name'), $values);
  460. $type_name = '';
  461. if (count($cvterm) > 0) {
  462. $type_name = $cvterm[0]->name;
  463. }
  464. if ($type_name == 'Title') {
  465. $where .= " $op (lower(P.title) LIKE lower(:crit$i)) ";
  466. $args[":crit$i"] = '%' . $value . '%';
  467. }
  468. elseif ($type_name == 'Year') {
  469. $where .= " $op (lower(P.pyear) = lower(:crit$i)) ";
  470. $args[":crit$i"] = '%' . $value . '%';
  471. }
  472. elseif ($type_name == 'Volume') {
  473. $where .= " $op (lower(P.volume) = lower(:crit$i)) ";
  474. $args[":crit$i"] = '%' . $value . '%';
  475. }
  476. elseif ($type_name == 'Issue') {
  477. $where .= " $op (lower(P.issue) = lower(:crit$i)) ";
  478. $args[":crit$i"] = '%' . $value . '%';
  479. }
  480. elseif ($type_name == 'Journal Name') {
  481. $from .= " LEFT JOIN {pubprop} PP$i ON PP$i.pub_id = P.pub_id AND PP$i.type_id = :crit$i ";
  482. $where .= " $op ((lower(P.series_name) = lower(:crit$i) and CVT.name = 'Journal Article') OR
  483. (lower(PP$i.value) = lower(:crit$i))) ";
  484. $args[":crit$i"] = $type_id;
  485. }
  486. elseif ($type_name == 'Conference Name') {
  487. $from .= " LEFT JOIN {pubprop} PP$i ON PP$i.pub_id = P.pub_id AND PP$i.type_id = :crit$i ";
  488. $where .= " $op ((lower(P.series_name) = lower(:crit$i) and CVT.name = 'Conference Proceedings') OR
  489. (lower(PP$i.value) = lower(:crit$i))) ";
  490. $args[":crit$i"] = $type_id;
  491. }
  492. elseif ($type_name == 'Publication Type') {
  493. $where .= " $op (lower(CVT.name) = lower(:crit$i))";
  494. $args[":crit$i"] = $value;
  495. }
  496. elseif ($type_id == 0) { //'Any Field'
  497. $from .= " LEFT JOIN {pubprop} PP$i ON PP$i.pub_id = P.pub_id ";
  498. $where .= " $op (lower(PP$i.value) LIKE lower(:crit$i) OR
  499. lower(P.title) LIKE lower(:crit$i) OR
  500. lower(P.volumetitle) LIKE lower(:crit$i) OR
  501. lower(P.publisher) LIKE lower(:crit$i) OR
  502. lower(P.uniquename) LIKE lower(:crit$i) OR
  503. lower(P.pubplace) LIKE lower(:crit$i) OR
  504. lower(P.miniref) LIKE lower(:crit$i) OR
  505. lower(P.series_name) LIKE lower(:crit$i)) ";
  506. $args[":crit$i"] = '%' . $value . '%';
  507. }
  508. // for all other properties
  509. else {
  510. $from .= " LEFT JOIN {pubprop} PP$i ON PP$i.pub_id = P.pub_id AND PP$i.type_id = :type_id$i ";
  511. $where .= " $op (lower(PP$i.value) LIKE lower(:crit$i)) ";
  512. $args[":crit$i"] = '%' . $value . '%';
  513. $args[":type_id$i"] = $type_id;
  514. }
  515. }
  516. if($from_year and $to_year) {
  517. $where .= " AND (P.pyear ~ '....' AND to_number(P.pyear,'9999') >= :from$i AND to_number(P.pyear,'9999') <= :to$i) ";
  518. $args[":from$i"] = $from_year;
  519. $args[":to$i"] = $to_year;
  520. }
  521. $sql = "$select $from $where $order";
  522. $count = "SELECT count(*) FROM ($select $from $where $order) as t1";
  523. return chado_pager_query($sql, $args, $limit, 0, $count);
  524. }
  525. /**
  526. *
  527. */
  528. function tripal_pubs_search_form_ajax_update($form, $form_state) {
  529. return $form['criteria'];
  530. }
  531. /**
  532. *
  533. * @param unknown $form
  534. */
  535. function theme_tripal_pub_search_setup_form_elements($variables) {
  536. $form = $variables['form'];
  537. $rows = array();
  538. // put each criteria element in a single table row
  539. foreach ($form as $i => $element) {
  540. if(is_numeric($i)) {
  541. $rows[] = array(
  542. drupal_render($element["operation-$i"]),
  543. drupal_render($element["scope-$i"]),
  544. //drupal_render($element["mode-$i"]) .
  545. drupal_render($element["search_terms-$i"]),
  546. array(
  547. 'data' => drupal_render($element["add-$i"]) . drupal_render($element["remove-$i"]),
  548. 'nowrap' => 'nowrap',
  549. ),
  550. );
  551. }
  552. }
  553. // add in the from_year and to_year elements as the final row of the table
  554. $rows[] = array(
  555. '&nbsp;',
  556. drupal_render($form['date']),
  557. array(
  558. 'data' =>
  559. "<div id=\"pub-search-form-dates-row\">
  560. <div id=\"pub-search-form-dates\"> ".
  561. drupal_render($form['from_year']) .
  562. drupal_render($form['to_year']) . "
  563. </div>
  564. </div>
  565. ",
  566. ),
  567. ''
  568. );
  569. $headers = array();
  570. $table = array(
  571. 'header' => $headers,
  572. 'rows' => $rows,
  573. 'attributes' => array('id' => 'tripal-pub-search-form-table', 'border' => '0'),
  574. 'sticky' => TRUE,
  575. 'caption' => '',
  576. 'colgroups' => array(),
  577. 'empty' => '',
  578. );
  579. $results = '<div id="tripal-pub-search-form-criteria">';
  580. $results .= theme_table($table);
  581. $results .= '</div>';
  582. return $results;
  583. }