contact_sync.inc 3.9 KB

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