pub_sync.inc 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. // get the list of pubs that have not yet been synced
  32. // and ignore the default 'NULL' pub. we don't want
  33. // to sync that one.
  34. $sql = "
  35. SELECT P.*
  36. FROM chado.pub P
  37. LEFT JOIN {chado_pub} CP ON CP.pub_id = P.pub_id
  38. WHERE CP.pub_id IS NULL and NOT P.title = 'NULL'
  39. ";
  40. $results = db_query($sql);
  41. while ($pub = db_fetch_object($results)) {
  42. $node = tripal_pub_sync_pub($pub);
  43. }
  44. }
  45. /**
  46. * @param $pub
  47. * A publication object
  48. *
  49. * @return
  50. * A new Drupal node object on success. FALSE on failure
  51. *
  52. * @ingroup tripal_pub
  53. */
  54. function tripal_pub_sync_pub($pub) {
  55. global $user;
  56. $new_node = new stdClass();
  57. $new_node->pub_id = $pub->pub_id;
  58. $new_node->type = 'chado_pub';
  59. $new_node->uid = $user->uid;
  60. $new_node->title = $pub->title;
  61. $new_node->pyear = $pub->pyear;
  62. $new_node->uniquename = $pub->uniquename;
  63. $new_node->type_id = $pub->type_id;
  64. node_validate($new_node);
  65. $errors = form_get_errors();
  66. if (!$errors) {
  67. $node = node_submit($new_node);
  68. node_save($node);
  69. if ($node->nid) {
  70. print "Added " . $pub->pub_id . "\n";
  71. }
  72. else {
  73. print "ERROR: Unable to create " . $pub->name . "\n";
  74. return FALSE;
  75. }
  76. }
  77. else {
  78. print "ERROR: Unable to create " . $pub->name . "\n" . print_r($errors, TRUE) . "\n";
  79. return FALSE;
  80. }
  81. return $node;
  82. }