tripal_pub.pub_search.inc 24 KB

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