pub_sync.inc

File

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