tripal_pub.pub_sync.inc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /*
  3. *
  4. */
  5. function tripal_pub_sync_form() {
  6. $form['sync'] = array(
  7. '#type' => 'fieldset',
  8. '#title' => t('Sync')
  9. );
  10. $form['sync']['sync_all'] = array(
  11. '#markup' => t('<p>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.</p>'),
  12. );
  13. $form['sync']['submit'] = array(
  14. '#type' => 'submit',
  15. '#weight' => 10,
  16. '#value' => t('Sync Publications')
  17. );
  18. $form['cleanup'] = array(
  19. '#type' => 'fieldset',
  20. '#title' => t('Clean Up')
  21. );
  22. $form['cleanup']['description'] = array(
  23. '#markup' => t("<p>With Drupal and chado residing in different databases ".
  24. "it is possible that nodes in Drupal and publications in Chado become ".
  25. "\"orphaned\". This can occur if an pub node in Drupal is ".
  26. "deleted but the corresponding chado pub is not and/or vice ".
  27. "versa. Click the button below to resolve these discrepancies.</p>"),
  28. '#weight' => 1,
  29. );
  30. $form['cleanup']['button'] = array(
  31. '#type' => 'submit',
  32. '#value' => t('Clean up orphaned publications'),
  33. '#weight' => 2,
  34. );
  35. return $form;
  36. }
  37. /*
  38. *
  39. */
  40. function tripal_pub_sync_form_submit($form, $form_state) {
  41. global $user; //needed to make the current users details available so access of user id is available
  42. if ($form_state['values']['op'] == t('Sync Publications')) {
  43. $job_args = array();
  44. $job_id = tripal_add_job('Sync Publications', 'tripal_pub', 'tripal_pub_sync_pubs', $job_args, $user->uid);
  45. }
  46. // -------------------------------------
  47. // Submit the Cleanup Job if selected
  48. if ($form_state['values']['op'] == t('Clean up orphaned publications')) {
  49. $job_args = array();
  50. tripal_add_job('Cleanup orphaned publications', 'tripal_pub',
  51. 'tripal_pub_cleanup', $job_args, $user->uid);
  52. }
  53. }
  54. /**
  55. *
  56. *
  57. * @ingroup tripal_pub
  58. */
  59. function tripal_pub_sync_pubs($job_id = NULL) {
  60. global $user;
  61. // get the list of pubs that have not yet been synced
  62. // and ignore the default 'NULL' pub. we don't want
  63. // to sync that one.
  64. $sql = "
  65. SELECT P.*
  66. FROM {pub} P
  67. LEFT JOIN public.chado_pub CP ON CP.pub_id = P.pub_id
  68. WHERE CP.pub_id IS NULL and NOT P.title = 'NULL'
  69. ";
  70. $results = chado_query($sql);
  71. // We'll use the following SQL statement for checking if the pub
  72. // already exists as a drupal node.
  73. $sql = "SELECT * FROM {chado_pub} WHERE pub_id = :pub_id";
  74. while ($pub = $results->fetchObject()) {
  75. // check if this pub already exists in the drupal database. if it
  76. // does then skip this pub and go to the next one.
  77. if (!db_query($sql, array(':pub_id' => $pub->pub_id))->fetchObject()) {
  78. if(!$pub->pyear) {
  79. watchdog('tpub_sync', "Skipping pub without published year: %title.",
  80. array('%title' => $pub->title), WATCHDOG_WARNING);
  81. return FALSE;
  82. }
  83. $new_node = new stdClass();
  84. $new_node->pub_id = $pub->pub_id;
  85. $new_node->type = 'chado_pub';
  86. $new_node->uid = $user->uid;
  87. $new_node->title = substr($pub->title, 0 ,255); // node titles can't be longer than 255 characters
  88. $new_node->pubtitle = $pub->title;
  89. $new_node->pyear = $pub->pyear;
  90. $new_node->uniquename = $pub->uniquename;
  91. $new_node->type_id = $pub->type_id;
  92. $new_node->series_name = $pub->series_name;
  93. $form = array(); // dummy variable
  94. $form_state = array(); // dummy variable
  95. node_validate($new_node, $form, $form_state);
  96. if (!form_get_errors()) {
  97. $node = node_submit($new_node);
  98. node_save($node);
  99. if ($node->nid) {
  100. print "Added $new_node->title\n";
  101. }
  102. }
  103. else {
  104. watchdog('tpub_sync', "Unable to create publication node. ID: %pub_id, Title: %title",
  105. array('%pub_id' => $pub->pub_id, '%title' => $pub->title), WATCHDOG_WARNING);
  106. }
  107. }
  108. }
  109. }
  110. /**
  111. * Remove orphaned drupal nodes
  112. *
  113. * @param $dummy
  114. * Not Used -kept for backwards compatibility
  115. * @param $job_id
  116. * The id of the tripal job executing this function
  117. *
  118. * @ingroup tripal_pub
  119. */
  120. function tripal_pub_cleanup($dummy = NULL, $job_id = NULL) {
  121. return tripal_core_chado_node_cleanup_orphaned('pub', $job_id);
  122. }