pub_sync.inc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. if(!$pub->pyear) {
  57. watchdog('tpub_sync', "Skipping pub without published year: %title.", array('%title' => $pub->title), WATCHDOG_WARNING);
  58. return FALSE;
  59. }
  60. $new_node = new stdClass();
  61. $new_node->pub_id = $pub->pub_id;
  62. $new_node->type = 'chado_pub';
  63. $new_node->uid = $user->uid;
  64. $new_node->title = substr($pub->title, 0 ,255); // node titles can't be longer than 255 characters
  65. $new_node->pubtitle = $pub->title;
  66. $new_node->pyear = $pub->pyear;
  67. $new_node->uniquename = $pub->uniquename;
  68. $new_node->type_id = $pub->type_id;
  69. $new_node->series_name = $pub->series_name;
  70. node_validate($new_node);
  71. $errors = form_get_errors();
  72. if (!$errors) {
  73. $node = node_submit($new_node);
  74. node_save($node);
  75. if ($node->nid) {
  76. print "Added " . $pub->pub_id . "\n";
  77. }
  78. else {
  79. watchdog('tpub_sync', "Unable to create publication node: %title.", array('%title' => $pub->title), WATCHDOG_ERROR);
  80. return FALSE;
  81. }
  82. }
  83. // if there are form errors then we need to reset the form errors cache, print a message and return
  84. else {
  85. form_set_error(NULL,'',TRUE);
  86. watchdog('tpub_sync', "Unable to create publication node: %title\n%errs",
  87. array('%title' => $pub->title, '%errs' => print_r($errors, TRUE)), WATCHDOG_ERROR);
  88. return FALSE;
  89. }
  90. return $node;
  91. }