pub_importers.inc 25 KB

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