contact_sync.inc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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
  68. ";
  69. $results = db_query($sql);
  70. while ($contact = $results->fetchObject()) {
  71. $new_node = new stdClass();
  72. $new_node->contact_id = $contact->contact_id;
  73. $new_node->type = 'chado_contact';
  74. $new_node->title = $contact->name;
  75. $new_node->description = $contact->description;
  76. $new_node->type_id = $contact->type_id;
  77. $form = array(); // dummy variable
  78. $form_state = array(); // dummy variable
  79. node_validate($new_node, $form, $form_state);
  80. if (!form_get_errors()) {
  81. $node = node_submit($new_node);
  82. node_save($node);
  83. if ($node->nid) {
  84. print "Added contact: $new_node->title\n";
  85. }
  86. }
  87. else {
  88. print "ERROR: Unable to create " . $contact->name . "\n" . print_r($errors, TRUE) . "\n";
  89. return FALSE;
  90. }
  91. }
  92. }
  93. /**
  94. * Remove orphaned drupal nodes
  95. *
  96. * @param $dummy
  97. * Not Used -kept for backwards compatibility
  98. * @param $job_id
  99. * The id of the tripal job executing this function
  100. *
  101. * @ingroup tripal_contact
  102. */
  103. function tripal_contact_cleanup($dummy = NULL, $job_id = NULL) {
  104. return tripal_core_clean_orphaned_nodes('contact', $job_id);
  105. }