pub_importers.inc 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. <?php
  2. /**
  3. * A function to generate a table containing the list of publication importers
  4. *
  5. * @ingroup tripal_pub
  6. */
  7. function tripal_pub_importers_list() {
  8. // clear out the session variable when we view the list.
  9. unset($_SESSION['tripal_pub_import']);
  10. $header = array('', 'Importer Name', 'Database', 'Search String', 'Disabled', 'Create Contact', '');
  11. $rows = array();
  12. $importers = db_query("SELECT * FROM {tripal_pub_import} ORDER BY name");
  13. while ($importer = $importers->fetchObject()) {
  14. $criteria = unserialize($importer->criteria);
  15. $num_criteria = $criteria['num_criteria'];
  16. $criteria_str = '';
  17. for ($i = 1; $i <= $num_criteria; $i++) {
  18. $search_terms = $criteria['criteria'][$i]['search_terms'];
  19. $scope = $criteria['criteria'][$i]['scope'];
  20. $is_phrase = $criteria['criteria'][$i]['is_phrase'];
  21. $operation = $criteria['criteria'][$i]['operation'];
  22. $criteria_str .= "$operation ($scope: $search_terms) ";
  23. }
  24. $rows[] = array(
  25. l(t('Edit/Test'), "admin/tripal/chado/tripal_pub/import/edit/$importer->pub_import_id"),
  26. $importer->name,
  27. $criteria['remote_db'],
  28. $criteria_str,
  29. $importer->disabled ? 'Yes' : 'No',
  30. $importer->do_contact ? 'Yes' : 'No',
  31. l(t('Delete'), "admin/tripal/chado/tripal_pub/import/delete/$importer->pub_import_id"),
  32. );
  33. }
  34. $page = "<ul class='action-links'>";
  35. $page .= ' <li>' . l('New Importer', 'admin/tripal/chado/tripal_pub/import/new') . '</li>';
  36. $page .= '</ul>';
  37. $page .= '<p>' . t(
  38. "A publication importer is used to create a set of search criteria that can be used
  39. to query a remote database, find publications that match the specified criteria
  40. and then import those publications into the Chado database. An example use case would
  41. be to peridocially add new publications to this Tripal site that have appeared in PubMed
  42. in the last 30 days. See the " .
  43. l("Pub Module help instructions", "admin/tripal/chado/tripal_pub/help") . " to learn how to
  44. set the importers to run automatically.") . '</p>';
  45. $page .= theme('table', array('header' => $header, 'rows' => $rows));
  46. return $page;
  47. }
  48. /**
  49. *
  50. * @param action
  51. * @param $pub_import_id
  52. *
  53. *
  54. */
  55. function tripal_pub_importer_setup_page($action = 'new', $pub_import_id = NULL) {
  56. global $base_path;
  57. // make sure the tripal_pub and tripal_contact ontologies are loaded
  58. $values = array('name' => 'tripal_pub');
  59. $tpub_cv = tripal_core_chado_select('cv', array('cv_id'), $values);
  60. if (count($tpub_cv) == 0) {
  61. drupal_set_message(t('Before importing publications you must first ') . l(t('load the Tripal Pub Ontology'), 'admin/tripal/tripal_cv/obo_loader'), 'error');
  62. }
  63. $values = array('name' => 'tripal_contact');
  64. $tpub_cv = tripal_core_chado_select('cv', array('cv_id'), $values);
  65. if (count($tpub_cv) == 0) {
  66. drupal_set_message(t('If you want to create contact pages for authors, you must first ') . l(t('load the Tripal Contact Ontology'), 'admin/tripal/tripal_cv/obo_loader'), 'error');
  67. }
  68. if(!extension_loaded ('yaz')){
  69. drupal_set_message(t('<b>Note:</b> In order to create an importer using the USDA National Agricultural Library (AGL) you must install the yaz libraries. See the ') . l(t('Pub Module help page'), 'admin/tripal/chado/tripal_pub/help') . ' for assistance. If you do not want to use AGL you can ignore this warning.', 'warning');
  70. }
  71. // generate the search form
  72. $form = drupal_get_form('tripal_pub_importer_setup_form', $pub_import_id, $action);
  73. $output = l("Return to publication importers list", "admin/tripal/chado/tripal_pub/import_list");
  74. $output .= drupal_render($form);
  75. // retrieve any results
  76. if (array_key_exists('tripal_pub_import', $_SESSION)) {
  77. $remote_db = $_SESSION['tripal_pub_import']['remote_db'];
  78. $num_criteria = $_SESSION['tripal_pub_import']['num_criteria'];
  79. $days = $_SESSION['tripal_pub_import']['days'];
  80. $search_array = array();
  81. $search_array['remote_db'] = $remote_db;
  82. $search_array['num_criteria'] = $num_criteria;
  83. $search_array['days'] = $days;
  84. for ($i = 1; $i <= $num_criteria; $i++) {
  85. $search_array['criteria'][$i]['search_terms'] = $_SESSION['tripal_pub_import']['criteria'][$i]['search_terms'];
  86. $search_array['criteria'][$i]['scope'] = $_SESSION['tripal_pub_import']['criteria'][$i]['scope'];
  87. $search_array['criteria'][$i]['is_phrase'] = $_SESSION['tripal_pub_import']['criteria'][$i]['is_phrase'];
  88. $search_array['criteria'][$i]['operation'] = $_SESSION['tripal_pub_import']['criteria'][$i]['operation'];
  89. }
  90. // if the form has been submitted with the 'test' button then get the results
  91. if ($_SESSION['tripal_pub_import']['perform_search']) {
  92. $limit = 25;
  93. // get the list of publications from the remote database using the search criteria.
  94. $page = isset($_GET['page']) ? $_GET['page'] : '0';
  95. $results = tripal_pub_get_remote_search_results($remote_db, $search_array, $limit, $page);
  96. $total_records = $results['total_records'];
  97. $search_str = $results['search_str'];
  98. $pubs = $results['pubs'];
  99. // iterate through the results and construct the table displaying the publications
  100. $rows = array();
  101. $i = $page * $limit + 1;
  102. if (count($pubs) > 0) {
  103. foreach ($pubs as $pub) {
  104. $citation = htmlspecialchars($pub['Citation']);
  105. $raw_link = '';
  106. if($pub['Publication Dbxref']) {
  107. $raw_link = l('raw', 'admin/tripal/chado/tripal_pub/import/raw/' . $pub['Publication Dbxref'], array('attributes' => array('target' => '_blank')));
  108. }
  109. $rows[] = array(
  110. number_format($i),
  111. $citation,
  112. $raw_link,
  113. );
  114. $i++;
  115. }
  116. }
  117. if (count($rows) == 0) {
  118. $rows[] = array(
  119. array(
  120. 'data' => 'No results found',
  121. 'colspan' => 3,
  122. ),
  123. );
  124. }
  125. $headers = array('', 'Publication', 'Raw Results');
  126. $table = array(
  127. 'header' => $headers,
  128. 'rows' => $rows,
  129. 'attributes' => array(
  130. 'id' => 'tripal_pub-importer-test',
  131. ),
  132. 'sticky' => FALSE,
  133. 'caption' => '',
  134. 'colgroups' => array(),
  135. 'empty' => '',
  136. );
  137. // once we have our table array structure defined, we call Drupal's theme_table()
  138. // function to generate the table.
  139. $table = theme_table($table);
  140. // generate the pager
  141. pager_default_initialize($total_records, $limit);
  142. $pager = array(
  143. 'tags' => array(),
  144. 'element' => 0,
  145. 'parameters' => array(),
  146. 'quantity' => $limit,
  147. );
  148. $pager = theme_pager($pager);
  149. // because this is an ajax callback, the theme_pager will set the URL to be
  150. // "system/ajax", so we need to reset that
  151. $pager = str_replace($base_path . "system/ajax", "", $pager) ;
  152. // join all to form the results
  153. $total_pages = (int) ($total_records / $limit) + 1;
  154. $page = isset($_GET['page']) ? $_GET['page'] : '0';
  155. $output .= "$pager<br><b>Found " . number_format($total_records) . " publications. Page " . ($page + 1) . " of $total_pages.</b> " .
  156. "<br>$remote_db Search String: $search_str $table<br>$pager";
  157. }
  158. }
  159. return $output;
  160. }
  161. /**
  162. * Purpose: Provides the form to search pubmed
  163. *
  164. * @ingroup tripal_pub
  165. */
  166. function tripal_pub_importer_setup_form($form, &$form_state = NULL, $pub_import_id = NULL, $action = 'new') {
  167. // Default values can come in the following ways:
  168. //
  169. // 1) as elements of the $pub_importer object. This occurs when editing an existing importer
  170. // 2) in the $form_state['values'] array which occurs on a failed validation or
  171. // ajax callbacks from non submit form elements
  172. // 3) in the $form_state['input'] array which occurs on ajax callbacks from submit
  173. // form elements and the form is being rebuilt
  174. //
  175. // set form field defaults
  176. // Set the default values. If the pub_import_id isn't already defined by the form values
  177. // and one is provided then look it up in the database
  178. $criteria = NULL;
  179. $remote_db = '';
  180. $days = '';
  181. $disabled = '';
  182. $do_contact = '';
  183. $num_criteria = 1;
  184. $loader_name = '';
  185. // if this is an edit the we are pulling an import object from the database
  186. if ($action == "edit") {
  187. $sql = "SELECT * FROM {tripal_pub_import} WHERE pub_import_id = :pub_import_id";
  188. $importer = db_query($sql, array(':pub_import_id' => $pub_import_id))->fetchObject();
  189. $criteria = unserialize($importer->criteria);
  190. $remote_db = $criteria['remote_db'];
  191. $days = $criteria['days'];
  192. $disabled = $criteria['disabled'];
  193. $do_contact = $criteria['do_contact'];
  194. $num_criteria = $criteria['num_criteria'];
  195. $loader_name = $criteria['loader_name'];
  196. }
  197. // if there are any session variables then use those
  198. if (array_key_exists('tripal_pub_import', $_SESSION)) {
  199. $remote_db = $_SESSION['tripal_pub_import']['remote_db'];
  200. $days = $_SESSION['tripal_pub_import']['days'];
  201. $disabled = $_SESSION['tripal_pub_import']['disabled'];
  202. $do_contact = $_SESSION['tripal_pub_import']['do_contact'];
  203. $num_criteria = $_SESSION['tripal_pub_import']['num_criteria'];
  204. $loader_name = $_SESSION['tripal_pub_import']['loader_name'];
  205. }
  206. // if we are re constructing the form from a failed validation or ajax callback
  207. // then use the $form_state['values'] values
  208. if (array_key_exists('values', $form_state)) {
  209. $remote_db = $form_state['values']['remote_db'];
  210. $days = $form_state['values']['days'];
  211. $disabled = $form_state['values']['disabled'];
  212. $do_contact = $form_state['values']['do_contact'];
  213. $num_criteria = $form_state['values']['num_criteria'];
  214. $loader_name = $form_state['values']['loader_name'];
  215. }
  216. // if we are re building the form from after submission (from ajax call) then
  217. // the values are in the $form_state['input'] array
  218. if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
  219. $remote_db = $form_state['input']['remote_db'];
  220. $days = $form_state['input']['days'];
  221. $disabled = $form_state['input']['disabled'];
  222. $do_contact = $form_state['input']['do_contact'];
  223. $loader_name = $form_state['input']['loader_name'];
  224. // because the num_criteria is a value and not a visible or hidden form
  225. // element it is not part of the ['input'] array, so we need to get it from the form
  226. $num_criteria = $form_state['complete form']['num_criteria']['#value'];
  227. }
  228. if (array_key_exists('triggering_element', $form_state) and
  229. $form_state['triggering_element']['#name'] == 'add') {
  230. $num_criteria++;
  231. }
  232. if (array_key_exists('triggering_element', $form_state) and
  233. $form_state['triggering_element']['#name'] == 'remove') {
  234. $num_criteria--;
  235. }
  236. // check if the pub_import_id in the session variable is not the same as the one we've been provided
  237. // if so, then clear the session variable
  238. if ($pub_import_id and $pub_import_id != $_SESSION['tripal_pub_import']['pub_import_id']) {
  239. unset($_SESSION['tripal_pub_import']);
  240. }
  241. // set the values we need for later but that should not be shown on the form
  242. $form['num_criteria']= array(
  243. '#type' => 'value',
  244. '#value' => $num_criteria,
  245. );
  246. $form['pub_import_id'] = array(
  247. '#type' => 'value',
  248. '#value' => $pub_import_id,
  249. );
  250. $form['action'] = array(
  251. '#type' => 'value',
  252. '#value' => $action,
  253. );
  254. // add in the elements that will be organized via a theme function
  255. $form['themed_element']['loader_name'] = array(
  256. '#type' => 'textfield',
  257. '#title' => t('Loader Name'),
  258. '#description' => t('Please provide a name for this loader setup.'),
  259. '#default_value' => $loader_name,
  260. '#required' => TRUE,
  261. );
  262. $supported_dbs = variable_get('tripal_pub_supported_dbs', array());
  263. $remote_dbs = array();
  264. $values = array(
  265. 'name' => $supported_dbs,
  266. );
  267. $dbs = tripal_core_chado_select('db', array('*'), $values);
  268. foreach ($dbs as $index => $db) {
  269. $remote_dbs[$db->name] = $db->description;
  270. };
  271. // use PubMed as the default
  272. if (!$remote_db) {
  273. $remote_db = 'PMID';
  274. }
  275. $form['themed_element']['remote_db'] = array(
  276. '#title' => t('Remote Database'),
  277. '#type' => 'select',
  278. '#options' => $remote_dbs,
  279. '#default_value' => $remote_db,
  280. '#ajax' => array(
  281. 'callback' => "tripal_pubs_setup_form_ajax_update",
  282. 'wrapper' => 'tripal-pubs-importer-setup',
  283. 'effect' => 'fade',
  284. 'method' => 'replace',
  285. ),
  286. );
  287. $form['themed_element']['days'] = array(
  288. '#type' => 'textfield',
  289. '#title' => t('Days since record modified'),
  290. '#description' => t('Limit the search to include pubs that have been added no more than this many days before today.'),
  291. '#default_value' => $days,
  292. '#size' => 5,
  293. );
  294. $form['themed_element']['disabled'] = array(
  295. '#type' => 'checkbox',
  296. '#title' => t('Disabled'),
  297. '#description' => t('Check to disable this importer.'),
  298. '#default_value' => $disabled,
  299. );
  300. $form['themed_element']['do_contact'] = array(
  301. '#type' => 'checkbox',
  302. '#title' => t('Create Contact'),
  303. '#description' => t('Check to create an entry in the contact table for each author of a matching publication during import. This allows storage of
  304. additional information such as affilation, etc. Otherwise, only authors names are retrieved.'),
  305. '#default_value' => $do_contact,
  306. );
  307. // add in the form for the criteria
  308. tripal_pub_importer_setup_add_criteria_fields($form, $form_state, $num_criteria, $criteria);
  309. // add in the buttons
  310. $form['save'] = array(
  311. '#type' => 'submit',
  312. '#value' => t('Save Importer'),
  313. );
  314. $form['test'] = array(
  315. '#type' => 'submit',
  316. '#value' => t('Test Importer'),
  317. );
  318. $form['delete'] = array(
  319. '#type' => 'submit',
  320. '#value' => t('Delete Importer'),
  321. '#attributes' => array('style' => 'float: right;')
  322. );
  323. // add in the section where the test results will appear
  324. $form['results'] = array(
  325. '#markup' => '<div id="tripal-pub-importer-test-section"></div>',
  326. );
  327. // allow the selected remote database to make changes to the form if needed
  328. $callback = "tripal_pub_remote_alter_form_$remote_db";
  329. $form = call_user_func($callback, $form, $form_state, $num_criteria);
  330. $form['themed_element']['#theme'] = 'tripal_pub_importer_setup_form_elements';
  331. return $form;
  332. }
  333. /**
  334. *
  335. * @param $form
  336. * @param $form_state
  337. * @param $num_criteria
  338. * @param $criteria
  339. */
  340. function tripal_pub_importer_setup_add_criteria_fields(&$form, &$form_state, $num_criteria, $criteria){
  341. // choices array
  342. $scope_choices = array(
  343. 'any' => 'Any Field',
  344. 'abstract' => 'Abstract',
  345. 'author' => 'Author',
  346. 'id' => 'Accession',
  347. 'title' => 'Title',
  348. 'journal' => 'Journal Name'
  349. );
  350. $first_op_choices = array(
  351. '' => '',
  352. 'NOT' => 'NOT'
  353. );
  354. $op_choices = array(
  355. 'AND' => 'AND',
  356. 'OR' => 'OR',
  357. 'NOT' => 'NOT'
  358. );
  359. for($i = 1; $i <= $num_criteria; $i++) {
  360. $is_phrase = 1;
  361. $search_terms = '';
  362. $scope = '';
  363. $is_phrase = '';
  364. $operation = '';
  365. // if we have criteria supplied from the database then use that as the initial defaults
  366. if ($criteria) {
  367. $search_terms = $criteria['criteria'][$i]['search_terms'];
  368. $scope = $criteria['criteria'][$i]['scope'];
  369. $is_phrase = $criteria['criteria'][$i]['is_phrase'];
  370. $operation = $criteria['criteria'][$i]['operation'];
  371. }
  372. // if the criteria comes the session
  373. if (array_key_exists('tripal_pub_import', $_SESSION)) {
  374. $search_terms = isset($_SESSION['tripal_pub_import']['criteria'][$i]['search_terms']) ? $_SESSION['tripal_pub_import']['criteria'][$i]['search_terms'] : $search_terms;
  375. $scope = isset($_SESSION['tripal_pub_import']['criteria'][$i]['scope']) ? $_SESSION['tripal_pub_import']['criteria'][$i]['scope'] : $scope;
  376. $is_phrase = isset($_SESSION['tripal_pub_import']['criteria'][$i]['is_phrase']) ? $_SESSION['tripal_pub_import']['criteria'][$i]['is_phrase'] : $is_phrase;
  377. $operation = isset($_SESSION['tripal_pub_import']['criteria'][$i]['operation']) ? $_SESSION['tripal_pub_import']['criteria'][$i]['operation'] : $operation;
  378. }
  379. // If the form_state has variables then use those. This happens when an error occurs on the form or the
  380. // form is resbumitted using AJAX
  381. if (array_key_exists('values', $form_state)) {
  382. $search_terms = $form_state['values']["search_terms-$i"];
  383. $scope = $form_state['values']["scope-$i"];
  384. $is_phrase = $form_state['values']["is_phrase-$i"];
  385. $operation = $form_state['values']["operation-$i"];
  386. }
  387. $form['themed_element']['criteria'][$i]["scope-$i"] = array(
  388. '#type' => 'select',
  389. '#description' => t('Please select the fields to search for this term.'),
  390. '#options' => $scope_choices,
  391. '#default_value' => $scope,
  392. );
  393. $form['themed_element']['criteria'][$i]["search_terms-$i"] = array(
  394. '#type' => 'textfield',
  395. '#description' => t('<span style="white-space: normal">Please provide a list of words for searching. You may use
  396. conjunctions such as "AND" or "OR" to separate words if they are expected in
  397. the same scope, but do not mix ANDs and ORs. Check the "Is Phrase" checkbox to use conjunctions as part of the text to search</span>'),
  398. '#default_value' => $search_terms,
  399. '#required' => TRUE,
  400. '#maxlength' => 2048,
  401. );
  402. $form['themed_element']['criteria'][$i]["is_phrase-$i"] = array(
  403. '#type' => 'checkbox',
  404. '#title' => t('Is Phrase?'),
  405. '#default_value' => $is_phrase,
  406. );
  407. if ($i == 1) {
  408. /*
  409. $form['criteria'][$i]["operation-$i"] = array(
  410. '#type' => 'select',
  411. '#options' => $first_op_choices,
  412. '#default_value' => $operation,
  413. );*/
  414. }
  415. if ($i > 1) {
  416. $form['themed_element']['criteria'][$i]["operation-$i"] = array(
  417. '#type' => 'select',
  418. '#options' => $op_choices,
  419. '#default_value' => $operation,
  420. );
  421. }
  422. if ($i == $num_criteria) {
  423. if($i > 1) {
  424. $form['themed_element']['criteria'][$i]["remove-$i"] = array(
  425. '#type' => 'button',
  426. '#name' => 'remove',
  427. '#value' => t('Remove'),
  428. '#ajax' => array(
  429. 'callback' => "tripal_pubs_setup_form_ajax_update",
  430. 'wrapper' => 'tripal-pubs-importer-setup',
  431. 'effect' => 'fade',
  432. 'method' => 'replace',
  433. 'prevent' => 'click'
  434. ),
  435. // When this button is clicked, the form will be validated and submitted.
  436. // Therefore, we set custom submit and validate functions to override the
  437. // default form submit. In the validate function we set the form_state
  438. // to rebuild the form so the submit function never actually gets called,
  439. // but we need it or Drupal will run the default validate anyway.
  440. // we also set #limit_validation_errors to empty so fields that
  441. // are required that don't have values won't generate warnings.
  442. '#submit' => array('tripal_pub_setup_form_ajax_button_submit'),
  443. '#validate' => array('tripal_pub_setup_form_ajax_button_validate'),
  444. '#limit_validation_errors' => array(),
  445. );
  446. }
  447. $form['themed_element']['criteria'][$i]["add-$i"] = array(
  448. '#type' => 'button',
  449. '#name' => 'add',
  450. '#value' => t('Add'),
  451. '#ajax' => array(
  452. 'callback' => "tripal_pubs_setup_form_ajax_update",
  453. 'wrapper' => 'tripal-pubs-importer-setup',
  454. 'effect' => 'fade',
  455. 'method' => 'replace',
  456. 'prevent' => 'click'
  457. ),
  458. // When this button is clicked, the form will be validated and submitted.
  459. // Therefore, we set custom submit and validate functions to override the
  460. // default form submit. In the validate function we set the form_state
  461. // to rebuild the form so the submit function never actually gets called,
  462. // but we need it or Drupal will run the default validate anyway.
  463. // we also set #limit_validation_errors to empty so fields that
  464. // are required that don't have values won't generate warnings.
  465. '#submit' => array('tripal_pub_setup_form_ajax_button_submit'),
  466. '#validate' => array('tripal_pub_setup_form_ajax_button_validate'),
  467. '#limit_validation_errors' => array(),
  468. );
  469. }
  470. }
  471. }
  472. /**
  473. * This function is used to rebuild the form if an ajax call is made vai a button.
  474. * The button causes the form to be submitted. We don't want this so we override
  475. * the validate and submit routines on the form button. Therefore, this function
  476. * only needs to tell Drupal to rebuild the form
  477. */
  478. function tripal_pub_setup_form_ajax_button_validate($form, &$form_state){
  479. $form_state['rebuild'] = TRUE;
  480. }
  481. /**
  482. * This function is just a dummy to override the default form submit on ajax calls for buttons
  483. */
  484. function tripal_pub_setup_form_ajax_button_submit($form, &$form_state){
  485. // do nothing
  486. }
  487. /**
  488. *
  489. */
  490. function tripal_pub_importer_setup_form_validate($form, &$form_state) {
  491. $num_criteria = $form_state['values']['num_criteria'];
  492. $remote_db = $form_state['values']["remote_db"];
  493. $days = trim($form_state['values']["days"]);
  494. $disabled = $form_state['values']["disabled"];
  495. $do_contact = $form_state['values']["do_contact"];
  496. $loader_name = trim($form_state['values']["loader_name"]);
  497. for ($i = 1; $i <= $num_criteria; $i++) {
  498. $search_terms = trim($form_state['values']["search_terms-$i"]);
  499. $scope = $form_state['values']["scope-$i"];
  500. $is_phrase = $form_state['values']["is_phrase-$i"];
  501. $operation = '';
  502. if($i > 1) {
  503. $operation = $form_state['values']["operation-$i"];
  504. }
  505. if (!$is_phrase) {
  506. if (preg_match('/and/i', $search_terms) and preg_match('/or/i', $search_terms)) {
  507. form_set_error("search_terms-$i", "You may use 'AND' or 'OR' but cannot use both. Add a new entry below with the same scope for the other conunction.");
  508. $_SESSION['tripal_pub_import']['perform_search'] = 0;
  509. }
  510. }
  511. }
  512. if ($days and !is_numeric($days) or preg_match('/\./', $days)) {
  513. form_set_error("days", "Please enter a numeric, non decimal value, for the number of days.");
  514. $_SESSION['tripal_pub_import']['perform_search'] = 0;
  515. }
  516. // allow the selected remote database to validate any changes to the form if needed
  517. $callback = "tripal_pub_remote_validate_form_$remote_db";
  518. $form = call_user_func($callback, $form, $form_state);
  519. }
  520. /**
  521. *
  522. */
  523. function tripal_pub_importer_setup_form_submit($form, &$form_state) {
  524. $pub_import_id = $form_state['values']['pub_import_id'];
  525. $num_criteria = $form_state['values']['num_criteria'];
  526. $remote_db = $form_state['values']["remote_db"];
  527. $days = trim($form_state['values']["days"]);
  528. $loader_name = trim($form_state['values']["loader_name"]);
  529. $disabled = $form_state['values']["disabled"];
  530. $do_contact = $form_state['values']["do_contact"];
  531. // set the session variables
  532. $_SESSION['tripal_pub_import']['remote_db'] = $remote_db;
  533. $_SESSION['tripal_pub_import']['days'] = $days;
  534. $_SESSION['tripal_pub_import']['num_criteria'] = $num_criteria;
  535. $_SESSION['tripal_pub_import']['loader_name'] = $loader_name;
  536. $_SESSION['tripal_pub_import']['disabled'] = $disabled;
  537. $_SESSION['tripal_pub_import']['do_contact'] = $do_contact;
  538. $_SESSION['tripal_pub_import']['pub_import_id'] = $pub_import_id;
  539. unset($_SESSION['tripal_pub_import']['criteria']);
  540. for ($i = 1; $i <= $num_criteria; $i++) {
  541. $search_terms = trim($form_state['values']["search_terms-$i"]);
  542. $scope = $form_state['values']["scope-$i"];
  543. $is_phrase = $form_state['values']["is_phrase-$i"];
  544. $operation = '';
  545. if ($i > 1) {
  546. $operation = $form_state['values']["operation-$i"];
  547. }
  548. $_SESSION['tripal_pub_import']['criteria'][$i] = array(
  549. 'search_terms' => $search_terms,
  550. 'scope' => $scope,
  551. 'is_phrase' => $is_phrase,
  552. 'operation' => $operation
  553. );
  554. }
  555. // now perform the appropriate action for the button clicked
  556. if ($form_state['values']['op'] == 'Test Importer') {
  557. $_SESSION['tripal_pub_import']['perform_search'] = 1;
  558. }
  559. if ($form_state['values']['op'] == 'Save Importer' or
  560. $form_state['values']['op'] == 'Save & Import Now') {
  561. $record = array(
  562. 'name' => $loader_name,
  563. 'criteria' => serialize($_SESSION['tripal_pub_import']),
  564. 'disabled' => $disabled,
  565. 'do_contact' => $do_contact
  566. );
  567. // first check to see if this pub_import_id is already present. If so,
  568. // do an update rather than an insert
  569. $sql = "SELECT * FROM {tripal_pub_import} WHERE pub_import_id = :pub_import_id";
  570. $importer = db_query($sql, array(':pub_import_id' => $pub_import_id))->fetchObject();
  571. if($importer) {
  572. // do the update
  573. $record['pub_import_id'] = $pub_import_id;
  574. if(drupal_write_record('tripal_pub_import', $record, 'pub_import_id')){
  575. unset($_SESSION['tripal_pub_import']);
  576. drupal_set_message('Publication import settings updated.');
  577. drupal_goto('admin/tripal/chado/tripal_pub/import_list');
  578. }
  579. else {
  580. drupal_set_message('Could not update publication import settings.', 'error');
  581. }
  582. }
  583. else {
  584. // do the insert
  585. if(drupal_write_record('tripal_pub_import', $record)){
  586. unset($_SESSION['tripal_pub_import']);
  587. drupal_set_message('Publication import settings saved.');
  588. // if the user wants to do the import now then do it (may time out
  589. // for long jobs)
  590. if ($form_state['values']['op'] == 'Save & Import Now') {
  591. tripal_pub_import_publications($record['pub_import_id']);
  592. }
  593. drupal_goto('admin/tripal/chado/tripal_pub/import_list');
  594. }
  595. else {
  596. drupal_set_message('Could not save publication import settings.', 'error');
  597. }
  598. }
  599. }
  600. if ($form_state['values']['op'] == 'Delete Importer') {
  601. $sql = "DELETE FROM {tripal_pub_import} WHERE pub_import_id = :pub_import_id";
  602. $success = db_query($sql, array(':pub_import_id' => $pub_import_id));
  603. if ($success) {
  604. drupal_set_message('Publication importer deleted.');
  605. drupal_goto('admin/tripal/chado/tripal_pub/import_list');
  606. }
  607. else {
  608. drupal_set_message('Could not delete publication importer.', 'error');
  609. }
  610. }
  611. }
  612. /**
  613. * AJAX callback for updating the form.
  614. */
  615. function tripal_pubs_setup_form_ajax_update($form, $form_state) {
  616. return $form['themed_element'];
  617. }
  618. /**
  619. *
  620. * @param $form
  621. */
  622. function theme_tripal_pub_importer_setup_form_elements($variables) {
  623. $form = $variables['form'];
  624. // first render the fields at the top of the form
  625. $markup = '';
  626. $markup .= '<div id="pub-search-form-row0">';
  627. $markup .= ' <div id="pub-search-form-row0-col1" style="float: left">' . drupal_render($form['remote_db']) . '</div>';
  628. $markup .= ' <div id="pub-search-form-row0-col2" style="float: left; margin-left: 10px">' . drupal_render($form['loader_name']) . '</div>';
  629. $markup .= '</div>';
  630. $markup .= '<div id="pub-search-form-row1" style="clear:both">';
  631. $markup .= ' <div id="pub-search-form-row1-col1">' . drupal_render($form['days']) . '</div>';
  632. $markup .= '</div>';
  633. $markup .= '<div id="pub-search-form-row2">' . drupal_render($form['disabled']) . '</div>';
  634. $markup .= '<div id="pub-search-form-row3">' . drupal_render($form['do_contact']) . '</div>';
  635. // next render the criteria fields into a table format
  636. $rows = array();
  637. foreach ($form['criteria'] as $i => $element) {
  638. if(is_numeric($i)) {
  639. $rows[] = array(
  640. drupal_render($element["operation-$i"]),
  641. drupal_render($element["scope-$i"]),
  642. drupal_render($element["search_terms-$i"]),
  643. drupal_render($element["is_phrase-$i"]),
  644. drupal_render($element["add-$i"]) . drupal_render($element["remove-$i"]),
  645. );
  646. }
  647. }
  648. $headers = array('Operation','Scope', 'Search Terms', '','');
  649. $table = array(
  650. 'header' => $headers,
  651. 'rows' => $rows,
  652. 'attributes' => array(),
  653. 'sticky' => TRUE,
  654. 'caption' => '',
  655. 'colgroups' => array(),
  656. 'empty' => '',
  657. );
  658. $criteria_table = theme_table($table);
  659. $markup .= $criteria_table;
  660. // add the rendered form
  661. $form = array(
  662. '#markup' => $markup,
  663. '#prefix' => '<div id="tripal-pubs-importer-setup">',
  664. '#suffix' => '</div>',
  665. );
  666. return drupal_render($form);
  667. }