tripal_pub.pub_search.inc 24 KB

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