tripal_chado.pub_search.inc 28 KB

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