contact_sync.inc 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /*
  3. *
  4. */
  5. function tripal_contact_sync_form() {
  6. $form['sync_all'] = array(
  7. '#type' => 'item',
  8. '#value' => t('Syncing a contact will create a Drupal page for every contact record in the Chado database. Click the button below to sync all contacts 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 contacts')
  14. );
  15. return $form;
  16. }
  17. /*
  18. *
  19. */
  20. function tripal_contact_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 contacts', 'tripal_contact', 'tripal_contact_sync_contacts', $job_args, $user->uid);
  24. }
  25. /**
  26. *
  27. *
  28. * @ingroup tripal_contact
  29. */
  30. function tripal_contact_sync_contacts($job_id = NULL) {
  31. // get the list of contacts that have not yet been synced
  32. // and ignore the default 'NULL' contact. we don't want
  33. // to sync that one.
  34. $sql = "
  35. SELECT C.*
  36. FROM chado.contact C
  37. LEFT JOIN {chado_contact} CP ON CP.contact_id = C.contact_id
  38. WHERE CP.contact_id IS NULL
  39. ";
  40. $results = db_query($sql);
  41. while ($contact = db_fetch_object($results)) {
  42. $node = tripal_contact_sync_contact($contact);
  43. }
  44. }
  45. /**
  46. * @param $contact
  47. * A contactlication object
  48. *
  49. * @return
  50. * A new Drupal node object on success. FALSE on failure
  51. *
  52. * @ingroup tripal_contact
  53. */
  54. function tripal_contact_sync_contact($contact) {
  55. global $user;
  56. $new_node = new stdClass();
  57. $new_node->contact_id = $contact->contact_id;
  58. $new_node->type = 'chado_contact';
  59. $new_node->title = $contact->name;
  60. $new_node->description = $contact->description;
  61. $new_node->type_id = $contact->type_id;
  62. node_validate($new_node);
  63. $errors = form_get_errors();
  64. if (!$errors) {
  65. $node = node_submit($new_node);
  66. node_save($node);
  67. if ($node->nid) {
  68. print "Added " . $contact->contact_id . "\n";
  69. }
  70. else {
  71. print "ERROR: Unable to create " . $contact->name . "\n";
  72. return FALSE;
  73. }
  74. }
  75. else {
  76. print "ERROR: Unable to create " . $contact->name . "\n" . print_r($errors, TRUE) . "\n";
  77. return FALSE;
  78. }
  79. return $node;
  80. }