pub_sync.inc 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /*
  3. *
  4. */
  5. function tripal_pub_sync_form() {
  6. $form['sync_all'] = array(
  7. '#type' => 'item',
  8. '#value' => t('Syncing a publication will create a Drupal page for every publicatoin record in the Chado database. Click the button below to sync all publications in Chado that currently are not already synced with Drupal.'),
  9. );
  10. $form['submit'] = array(
  11. '#type' => 'submit',
  12. '#weight' => 10,
  13. '#value' => t('Sync Publications')
  14. );
  15. return $form;
  16. }
  17. /*
  18. *
  19. */
  20. function tripal_pub_sync_form_submit($form, $form_state) {
  21. global $user; //needed to make the current users details available so access of user id is available
  22. $job_args = array();
  23. $job_id = tripal_add_job('Sync Publications', 'tripal_pub', 'tripal_pub_sync_pubs', $job_args, $user->uid);
  24. }
  25. /**
  26. *
  27. *
  28. * @ingroup tripal_pub
  29. */
  30. function tripal_pub_sync_pubs($job_id = NULL) {
  31. global $user;
  32. $page_content = '';
  33. // get the list of pubs that have not yet been synced
  34. // and ignore the default 'NULL' pub. we don't want
  35. // to sync that one.
  36. $sql = "
  37. SELECT P.*
  38. FROM chado.pub P
  39. LEFT JOIN {chado_pub} CP ON CP.pub_id = P.pub_id
  40. WHERE CP.pub_id IS NULL and NOT P.title = 'NULL'
  41. ";
  42. $results = db_query($sql);
  43. while ($pub = db_fetch_object($results)) {
  44. $new_node = new stdClass();
  45. $new_node->pub_id = $pub->pub_id;
  46. $new_node->type = 'chado_pub';
  47. $new_node->uid = $user->uid;
  48. $new_node->title = $pub->title;
  49. $new_node->pyear = $pub->pyear;
  50. $new_node->uniquename = $pub->uniquename;
  51. $new_node->type_id = $pub->type_id;
  52. node_validate($new_node);
  53. $errors = form_get_errors();
  54. if (!$errors) {
  55. $node = node_submit($new_node);
  56. node_save($node);
  57. if ($node->nid) {
  58. print "Added " . $pub->pub_id . "\n";
  59. }
  60. else {
  61. print "ERROR: Unable to create " . $pub->name . "\n";
  62. }
  63. }
  64. else {
  65. print "ERROR: Unable to create " . $pub->name . "\n" . print_r($errors, TRUE) . "\n";
  66. }
  67. }
  68. }