pub_importers.inc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. <?php
  2. /**
  3. * A function to render a listing of all 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($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 .= $form;
  68. // retrieve any results
  69. $remote_db = $_SESSION['tripal_pub_import']['remote_db'];
  70. $num_criteria = $_SESSION['tripal_pub_import']['num_criteria'];
  71. $days = $_SESSION['tripal_pub_import']['days'];
  72. $search_array = array();
  73. $search_array['remote_db'] = $remote_db;
  74. $search_array['num_criteria'] = $num_criteria;
  75. $search_array['days'] = $days;
  76. for ($i = 1; $i <= $num_criteria; $i++) {
  77. $search_array['criteria'][$i]['search_terms'] = $_SESSION['tripal_pub_import']['criteria'][$i]['search_terms'];
  78. $search_array['criteria'][$i]['scope'] = $_SESSION['tripal_pub_import']['criteria'][$i]['scope'];
  79. $search_array['criteria'][$i]['is_phrase'] = $_SESSION['tripal_pub_import']['criteria'][$i]['is_phrase'];
  80. $search_array['criteria'][$i]['operation'] = $_SESSION['tripal_pub_import']['criteria'][$i]['operation'];
  81. }
  82. if ($_SESSION['tripal_pub_import']['perform_search']) {
  83. // get the list of publications from the remote database using the search criteria.
  84. $pubs = tripal_pub_get_remote_search_results($remote_db, $search_array, $limit, $pager_id);
  85. //dpm($pubs);
  86. // generate the pager
  87. $total_pages = $pager_total[$pager_id];
  88. $total_items = $pager_total_items[$pager_id];
  89. $page = isset($_GET['page']) ? $_GET['page'] : '0';
  90. $pager = theme('pager');
  91. // iterate through the results and construct the table displaying the publications
  92. $rows = array();
  93. $i = $page * $limit + 1;
  94. if (count($pubs) > 0) {
  95. foreach ($pubs as $pub) {
  96. $citation = htmlspecialchars($pub['Citation']);
  97. $raw_link = '';
  98. if($pub['Publication Dbxref']) {
  99. $raw_link = l('raw', 'admin/tripal/chado/tripal_pub/import/raw/' . $pub['Publication Dbxref'], array('attributes' => array('target' => '_blank')));
  100. }
  101. $rows[] = array(
  102. number_format($i),
  103. $citation,
  104. $raw_link,
  105. );
  106. $i++;
  107. }
  108. }
  109. $headers = array('', 'Publication', '');
  110. $table = theme('table', array('header' => $headers, 'rows' => $rows));
  111. // join all to form the results
  112. $output .= "<br><p><b>Found " . number_format($total_items) .
  113. ". Page " . ($page + 1) . " of $total_pages. " .
  114. " Results</b></br>" . $table . '</p>' . $pager;
  115. }
  116. return $output;
  117. }
  118. /*
  119. *
  120. */
  121. function theme_tripal_pub_importer_setup_form($form) {
  122. $rows = array();
  123. foreach ($form['criteria'] as $i => $element) {
  124. if(is_numeric($i)) {
  125. $rows[] = array(
  126. drupal_render($element["operation-$i"]),
  127. drupal_render($element["scope-$i"]),
  128. drupal_render($element["search_terms-$i"]),
  129. drupal_render($element["is_phrase-$i"]),
  130. drupal_render($element["add-$i"]) . drupal_render($element["remove-$i"]),
  131. );
  132. }
  133. }
  134. $headers = array('Operation','Scope', 'Search Terms', '','');
  135. $markup = '';
  136. $markup .= '<div>' . drupal_render($form['remote_db']) . '</div>';
  137. $markup .= '<div id="pub-search-form-row1">';
  138. $markup .= ' <div id="pub-search-form-col1">' . drupal_render($form['loader_name']) . '</div>';
  139. $markup .= ' <div id="pub-search-form-col3">' . drupal_render($form['days']) . '</div>';
  140. $markup .= '</div>';
  141. $markup .= '<div id="pub-search-form-row2">' . drupal_render($form['disabled']) . '</div>';
  142. $markup .= '<div id="pub-search-form-row3">' . drupal_render($form['do_contact']) . '</div>';
  143. $markup .= theme('table', array('header' => $headers, 'rows' => $rows, 'attributes' => array('id' => 'tripal-pub-importer-table')));
  144. $form['criteria'] = array(
  145. '#type' => 'markup',
  146. '#value' => $markup,
  147. '#weight' => -10,
  148. );
  149. return drupal_render($form);
  150. }
  151. /**
  152. * Purpose: Provides the form to search pubmed
  153. *
  154. * @ingroup tripal_pub
  155. */
  156. function tripal_pub_importer_setup_form(&$form_state = NULL, $pub_import_id = NULL, $action = 'new') {
  157. tripal_core_ahah_init_form();
  158. // Set the default values. If the pub_import_id isn't already defined by the form values
  159. // and one is provided then look it up in the database
  160. $criteria = NULL;
  161. if ($action == "edit" and !$form_state['values']) {
  162. $sql = "SELECT * FROM {tripal_pub_import} WHERE pub_import_id = :pub_import_id";
  163. $importer = db_query($sql, array(':pub_import_id' => $pub_import_id))->fetchObject();
  164. $criteria = unserialize($importer->criteria);
  165. $remote_db = $criteria['remote_db'];
  166. $days = $criteria['days'];
  167. $disabled = $criteria['disabled'];
  168. $do_contact = $criteria['do_contact'];
  169. $num_criteria = $criteria['num_criteria'];
  170. $loader_name = $criteria['loader_name'];
  171. }
  172. // if we're here because the form was posted then load from the session variable (we lost the form state)
  173. $num_criteria = isset($_SESSION['tripal_pub_import']['num_criteria']) ? $_SESSION['tripal_pub_import']['num_criteria'] : $num_criteria;
  174. $loader_name = isset($_SESSION['tripal_pub_import']['loader_name']) ? $_SESSION['tripal_pub_import']['loader_name'] : $loader_name;
  175. $remote_db = isset($_SESSION['tripal_pub_import']['remote_db']) ? $_SESSION['tripal_pub_import']['remote_db'] : $remote_db;
  176. $disabled = isset($_SESSION['tripal_pub_import']['disabled']) ? $_SESSION['tripal_pub_import']['disabled'] : $disabled;
  177. $do_contact = isset($_SESSION['tripal_pub_import']['do_contact']) ? $_SESSION['tripal_pub_import']['do_contact'] : $do_contact;
  178. $days = isset($_SESSION['tripal_pub_import']['days']) ? $_SESSION['tripal_pub_import']['days'] : $days;
  179. // If the form_state has variables then use those. This happens when an error occurs on the form or the
  180. // form is resbumitted using AJAX
  181. if ($form_state['values']) {
  182. $num_criteria = $form_state['values']['num_criteria'] ? $form_state['values']['num_criteria'] : $num_criteria;
  183. $loader_name = $form_state['values']['loader_name'] ? $form_state['values']['loader_name'] : $loader_name;
  184. $remote_db = $form_state['values']['remote_db'] ? $form_state['values']['remote_db'] : $remote_db;
  185. $disabled = $form_state['values']['disabled'] ? $form_state['values']['disabled'] : $disabled;
  186. $do_contact = $form_state['values']['do_contact'] ? $form_state['values']['do_contact'] : $do_contact;
  187. $days = $form_state['values']['days'] ? $form_state['values']['days'] : $days;
  188. }
  189. // check if the pub_import_id in the session variable is not the same as the one we've been provided
  190. // if so, then clear the session variable
  191. if ($pub_import_id and $pub_import_id != $_SESSION['tripal_pub_import']['pub_import_id']) {
  192. unset($_SESSION['tripal_pub_import']);
  193. }
  194. // change the number of criteria based on form_state post data.
  195. if (!$num_criteria) {
  196. $num_criteria = 1;
  197. }
  198. if($form_state['post']["add-$num_criteria"]) {
  199. $num_criteria++;
  200. }
  201. if($form_state['post']["remove-$num_criteria"]) {
  202. $num_criteria--;
  203. }
  204. $form['pub_import_id'] = array(
  205. '#type' => 'hidden',
  206. '#value' => $pub_import_id,
  207. '#required' => TRUE,
  208. );
  209. $form['action'] = array(
  210. '#type' => 'hidden',
  211. '#value' => $action,
  212. '#required' => TRUE,
  213. );
  214. $form['loader_name'] = array(
  215. '#type' => 'textfield',
  216. '#title' => t('Loader Name'),
  217. '#description' => t('Please provide a name for this loader setup..'),
  218. '#default_value' => $loader_name,
  219. '#required' => TRUE,
  220. );
  221. $supported_dbs = variable_get('tripal_pub_supported_dbs', array());
  222. $remote_dbs = array();
  223. $values = array(
  224. 'name' => $supported_dbs,
  225. );
  226. $dbs = tripal_core_chado_select('db', array('*'), $values);
  227. foreach ($dbs as $index => $db) {
  228. $remote_dbs[$db->name] = $db->description;
  229. };
  230. // use PubMed as the default
  231. if (!$remote_db) {
  232. $remote_db = 'PMID';
  233. }
  234. $form['remote_db'] = array(
  235. '#title' => t('Remote Database'),
  236. '#type' => 'select',
  237. '#options' => $remote_dbs,
  238. '#default_value' => $remote_db,
  239. '#ahah' => array(
  240. 'path' => "admin/tripal/chado/tripal_pub/import/changedb",
  241. 'wrapper' => 'tripal-pub-importer-setup-form',
  242. 'event' => 'click',
  243. 'method' => 'replace',
  244. ),
  245. );
  246. $form['num_criteria']= array(
  247. '#type' => 'hidden',
  248. '#default_value' => $num_criteria,
  249. );
  250. $form['pub_import_id']= array(
  251. '#type' => 'hidden',
  252. '#default_value' => $pub_import_id,
  253. );
  254. $form['days'] = array(
  255. '#type' => 'textfield',
  256. '#title' => t('Days since record modified'),
  257. '#description' => t('Limit the search to include pubs that have been added no more than this many days before today.'),
  258. '#default_value' => $days,
  259. '#size' => 5,
  260. );
  261. $form['disabled'] = array(
  262. '#type' => 'checkbox',
  263. '#title' => t('Disabled'),
  264. '#description' => t('Check to disable this importer.'),
  265. '#default_value' => $disabled,
  266. );
  267. $form['do_contact'] = array(
  268. '#type' => 'checkbox',
  269. '#title' => t('Create Contact'),
  270. '#description' => t('Check to create an entry in the contact table for each author of a matching publication during import. This allows storage of
  271. additional information such as affilation, etc. Otherwise, only authors names are retrieved.'),
  272. '#default_value' => $do_contact,
  273. );
  274. // choices array
  275. $scope_choices = array(
  276. 'any' => 'Any Field',
  277. 'abstract' => 'Abstract',
  278. 'author' => 'Author',
  279. 'id' => 'Accession',
  280. 'title' => 'Title',
  281. 'journal' => 'Journal Name'
  282. );
  283. $first_op_choices = array(
  284. '' => '',
  285. 'NOT' => 'NOT'
  286. );
  287. $op_choices = array(
  288. 'AND' => 'AND',
  289. 'OR' => 'OR',
  290. 'NOT' => 'NOT'
  291. );
  292. for($i = 1; $i <= $num_criteria; $i++) {
  293. $is_phrase = 1;
  294. // if we have criteria supplied from the database then use that as the initial defaults
  295. if ($criteria) {
  296. $search_terms = $criteria['criteria'][$i]['search_terms'];
  297. $scope = $criteria['criteria'][$i]['scope'];
  298. $is_phrase = $criteria['criteria'][$i]['is_phrase'];
  299. $operation = $criteria['criteria'][$i]['operation'];
  300. }
  301. // if we're here because the form was posted then load from the session variable (we lost the form state)
  302. $search_terms = isset($_SESSION['tripal_pub_import']['criteria'][$i]['search_terms']) ? $_SESSION['tripal_pub_import']['criteria'][$i]['search_terms'] : $search_terms;
  303. $scope = isset($_SESSION['tripal_pub_import']['criteria'][$i]['scope']) ? $_SESSION['tripal_pub_import']['criteria'][$i]['scope'] : $scope;
  304. $is_phrase = isset($_SESSION['tripal_pub_import']['criteria'][$i]['is_phrase']) ? $_SESSION['tripal_pub_import']['criteria'][$i]['is_phrase'] : $is_phrase;
  305. $operation = isset($_SESSION['tripal_pub_import']['criteria'][$i]['operation']) ? $_SESSION['tripal_pub_import']['criteria'][$i]['operation'] : $operation;
  306. // If the form_state has variables then use those. This happens when an error occurs on the form or the
  307. // form is resbumitted using AJAX
  308. if ($form_state['values']) {
  309. $search_terms = $form_state['values']["search_terms-$i"];
  310. $scope = $form_state['values']["scope-$i"];
  311. $is_phrase = $form_state['values']["is_phrase-$i"];
  312. $operation = $form_state['values']["operation-$i"];
  313. }
  314. $form['criteria'][$i]["search_terms-$i"] = array(
  315. '#type' => 'textfield',
  316. '#description' => t('Please provide a list of words for searching. You may use
  317. conjunctions such as "AND" or "OR" to separate words if they are expected in
  318. the same scope, but do not mix ANDs and ORs. Uncheck the "Is Phrase" checkbox to use conjunctions'),
  319. '#default_value' => $search_terms,
  320. '#required' => TRUE,
  321. '#maxlength' => 2048,
  322. );
  323. $form['criteria'][$i]["scope-$i"] = array(
  324. '#type' => 'select',
  325. '#description' => t('Please select the fields to search for this term.'),
  326. '#options' => $scope_choices,
  327. '#default_value' => $scope,
  328. );
  329. $form['criteria'][$i]["is_phrase-$i"] = array(
  330. '#type' => 'checkbox',
  331. '#title' => t('Is Phrase?'),
  332. '#default_value' => $is_phrase,
  333. );
  334. if ($i == 1) {
  335. /*
  336. $form['criteria'][$i]["operation-$i"] = array(
  337. '#type' => 'select',
  338. '#options' => $first_op_choices,
  339. '#default_value' => $operation,
  340. );*/
  341. }
  342. if ($i > 1) {
  343. $form['criteria'][$i]["operation-$i"] = array(
  344. '#type' => 'select',
  345. '#options' => $op_choices,
  346. '#default_value' => $operation,
  347. );
  348. }
  349. if ($i == $num_criteria) {
  350. if($i > 1) {
  351. $form['criteria'][$i]["remove-$i"] = array(
  352. '#type' => 'image_button',
  353. '#value' => t('Remove'),
  354. '#src' => drupal_get_path('theme', 'tripal') . '/images/minus.png',
  355. '#ahah' => array(
  356. 'path' => "admin/tripal/chado/tripal_pub/import/criteria/minus/$i",
  357. 'wrapper' => 'tripal-pub-importer-setup-form',
  358. 'event' => 'click',
  359. 'method' => 'replace',
  360. ),
  361. '#attributes' => array('onClick' => 'return false;'),
  362. );
  363. }
  364. $form['criteria'][$i]["add-$i"] = array(
  365. '#type' => 'image_button',
  366. '#value' => t('Add'),
  367. '#src' => drupal_get_path('theme', 'tripal') . '/images/add.png',
  368. '#ahah' => array(
  369. 'path' => "admin/tripal/chado/tripal_pub/import/criteria/add/$i",
  370. 'wrapper' => 'tripal-pub-importer-setup-form',
  371. 'event' => 'click',
  372. 'method' => 'replace',
  373. ),
  374. '#attributes' => array('onClick' => 'return false;'),
  375. );
  376. }
  377. }
  378. $form['test'] = array(
  379. '#type' => 'submit',
  380. '#value' => t('Test Importer'),
  381. );
  382. $form['save'] = array(
  383. '#type' => 'submit',
  384. '#value' => t('Save Importer'),
  385. );
  386. /*
  387. $form['import'] = array(
  388. '#type' => 'submit',
  389. '#value' => t('Save & Import Now'),
  390. );*/
  391. $form['delete'] = array(
  392. '#type' => 'submit',
  393. '#value' => t('Delete Importer'),
  394. );
  395. // allow the selected remote database to make changes to the form if needed
  396. $callback = "tripal_pub_remote_alter_form_$remote_db";
  397. $form = call_user_func($callback, $form, $form_state);
  398. return $form;
  399. }
  400. /**
  401. *
  402. */
  403. function tripal_pub_importer_setup_form_validate($form, &$form_state) {
  404. $num_criteria = $form_state['values']['num_criteria'];
  405. $remote_db = $form_state['values']["remote_db"];
  406. $days = trim($form_state['values']["days"]);
  407. $disabled = $form_state['values']["disabled"];
  408. $do_contact = $form_state['values']["do_contact"];
  409. $loader_name = trim($form_state['values']["loader_name"]);
  410. for ($i = 1; $i <= $num_criteria; $i++) {
  411. $search_terms = trim($form_state['values']["search_terms-$i"]);
  412. $scope = $form_state['values']["scope-$i"];
  413. $is_phrase = $form_state['values']["is_phrase-$i"];
  414. $operation = $form_state['values']["operation-$i"];
  415. if (!$is_phrase) {
  416. if (preg_match('/and/i', $search_terms) and preg_match('/or/i', $search_terms)) {
  417. 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.");
  418. $_SESSION['tripal_pub_import']['perform_search'] = 0;
  419. }
  420. }
  421. }
  422. if ($days and !is_numeric($days) or preg_match('/\./', $days)) {
  423. form_set_error("days", "Please enter a numeric, non decimal value, for the number of days.");
  424. $_SESSION['tripal_pub_import']['perform_search'] = 0;
  425. }
  426. // allow the selected remote database to validate any changes to the form if needed
  427. $callback = "tripal_pub_remote_validate_form_$remote_db";
  428. $form = call_user_func($callback, $form, $form_state);
  429. }
  430. /**
  431. *
  432. */
  433. function tripal_pub_importer_setup_form_submit($form, &$form_state) {
  434. $pub_import_id = $form_state['values']['pub_import_id'];
  435. $num_criteria = $form_state['values']['num_criteria'];
  436. $remote_db = $form_state['values']["remote_db"];
  437. $days = trim($form_state['values']["days"]);
  438. $loader_name = trim($form_state['values']["loader_name"]);
  439. $disabled = $form_state['values']["disabled"];
  440. $do_contact = $form_state['values']["do_contact"];
  441. // set the session variables
  442. $_SESSION['tripal_pub_import']['remote_db'] = $remote_db;
  443. $_SESSION['tripal_pub_import']['days'] = $days;
  444. $_SESSION['tripal_pub_import']['num_criteria'] = $num_criteria;
  445. $_SESSION['tripal_pub_import']['loader_name'] = $loader_name;
  446. $_SESSION['tripal_pub_import']['disabled'] = $disabled;
  447. $_SESSION['tripal_pub_import']['do_contact'] = $do_contact;
  448. $_SESSION['tripal_pub_import']['pub_import_id'] = $pub_import_id;
  449. unset($_SESSION['tripal_pub_import']['criteria']);
  450. for ($i = 1; $i <= $num_criteria; $i++) {
  451. $search_terms = trim($form_state['values']["search_terms-$i"]);
  452. $scope = $form_state['values']["scope-$i"];
  453. $is_phrase = $form_state['values']["is_phrase-$i"];
  454. $operation = $form_state['values']["operation-$i"];
  455. $_SESSION['tripal_pub_import']['criteria'][$i] = array(
  456. 'search_terms' => $search_terms,
  457. 'scope' => $scope,
  458. 'is_phrase' => $is_phrase,
  459. 'operation' => $operation
  460. );
  461. }
  462. // now perform the appropriate action for the button clicked
  463. if ($form_state['values']['op'] == 'Test Importer') {
  464. $_SESSION['tripal_pub_import']['perform_search'] = 1;
  465. }
  466. if ($form_state['values']['op'] == 'Save Importer' or
  467. $form_state['values']['op'] == 'Save & Import Now') {
  468. $record = array(
  469. 'name' => $loader_name,
  470. 'criteria' => serialize($_SESSION['tripal_pub_import']),
  471. 'disabled' => $disabled,
  472. 'do_contact' => $do_contact
  473. );
  474. // first check to see if this pub_import_id is already present. If so,
  475. // do an update rather than an insert
  476. $sql = "SELECT * FROM {tripal_pub_import} WHERE pub_import_id = :pub_import_id";
  477. $importer = db_query($sql, array(':pub_import_id' => $pub_import_id))->fetchObject();
  478. if($importer) {
  479. // do the update
  480. $record['pub_import_id'] = $pub_import_id;
  481. if(drupal_write_record('tripal_pub_import', $record, 'pub_import_id')){
  482. unset($_SESSION['tripal_pub_import']);
  483. drupal_set_message('Publication import settings updated.');
  484. drupal_goto('admin/tripal/chado/tripal_pub/import_list');
  485. }
  486. else {
  487. drupal_set_message('Could not update publication import settings.', 'error');
  488. }
  489. }
  490. else {
  491. // do the insert
  492. if(drupal_write_record('tripal_pub_import', $record)){
  493. unset($_SESSION['tripal_pub_import']);
  494. drupal_set_message('Publication import settings saved.');
  495. // if the user wants to do the import now then do it (may time out
  496. // for long jobs)
  497. if ($form_state['values']['op'] == 'Save & Import Now') {
  498. tripal_pub_import_publications($record['pub_import_id']);
  499. }
  500. drupal_goto('admin/tripal/chado/tripal_pub/import_list');
  501. }
  502. else {
  503. drupal_set_message('Could not save publication import settings.', 'error');
  504. }
  505. }
  506. }
  507. if ($form_state['values']['op'] == 'Delete Importer') {
  508. $sql = "DELETE FROM {tripal_pub_import} WHERE pub_import_id = :pub_import_id";
  509. $success = db_query($sql, array(':pub_import_id' => $pub_import_id));
  510. if ($success) {
  511. drupal_set_message('Publication importer deleted.');
  512. drupal_goto('admin/tripal/chado/tripal_pub/import_list');
  513. }
  514. else {
  515. drupal_set_message('Could not delete publication importer.', 'error');
  516. }
  517. }
  518. }
  519. /*
  520. *
  521. */
  522. function tripal_pub_importer_delete($pub_import_id) {
  523. $sql = "DELETE FROM {tripal_pub_import} WHERE pub_import_id = :pub_import_id";
  524. $success = db_query($sql, array(':pub_import_id' => $pub_import_id));
  525. if ($success) {
  526. drupal_set_message('Publication importer deleted.');
  527. drupal_goto('admin/tripal/chado/tripal_pub/import_list');
  528. }
  529. else {
  530. drupal_set_message('Could not delete publication importer.', 'error');
  531. }
  532. }
  533. /*
  534. * AHAH callback
  535. */
  536. function tripal_pub_importer_setup_page_update_remotedb() {
  537. $status = TRUE;
  538. // prepare and render the form
  539. $form = tripal_core_ahah_prepare_form();
  540. $data = theme('tripal_pub_importer_setup_form', $form);
  541. // bind javascript events to the new objects that will be returned
  542. // so that AHAH enabled elements will work.
  543. $settings = tripal_core_ahah_bind_events();
  544. // return the updated JSON
  545. drupal_json(
  546. array(
  547. 'status' => $status,
  548. 'data' => $data,
  549. 'settings' => $settings,
  550. )
  551. );
  552. }
  553. /*
  554. * AHAH callback
  555. */
  556. function tripal_pub_importer_setup_page_update_criteria($action, $i) {
  557. $status = TRUE;
  558. // prepare and render the form
  559. $form = tripal_core_ahah_prepare_form();
  560. $data = theme('tripal_pub_importer_setup_form', $form);
  561. // bind javascript events to the new objects that will be returned
  562. // so that AHAH enabled elements will work.
  563. $settings = tripal_core_ahah_bind_events();
  564. // return the updated JSON
  565. drupal_json(
  566. array(
  567. 'status' => $status,
  568. 'data' => $data,
  569. 'settings' => $settings,
  570. )
  571. );
  572. }