Index: ldapdata.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/ldap_integration/ldapdata.module,v
retrieving revision 1.15
diff -u -p -r1.15 ldapdata.module
--- ldapdata.module	22 Jul 2007 20:04:25 -0000	1.15
+++ ldapdata.module	6 Nov 2007 20:22:01 -0000
@@ -21,6 +21,58 @@ if (function_exists('ldapdata_attribute_
   $GLOBALS['ldapdata_ldap']->setOption('attr_filter', 'ldapdata_attribute_filter');
 }
 
+/***************************
+*     CiviCRM Hooks        *
+***************************/
+
+/**
+* Implements hook_civicrm_pre()
+**/
+
+function ldapdata_civicrm_pre($op, $objectName, $objectId, $objectRef) {
+  if ($op == "edit") {
+    $writeout = array();
+    global $ldapdata_ldap;
+    if ($objectName == "Profile") {
+      // This seems to be what happens when the user edits their own profile
+      global $user;
+      $edit_user = $user;
+    } else if ($objectName == "Individual") {
+       // And this seems to be what happens when admins edit the profile for them
+      _ldapdata_init_civicrm();
+      // In this case we need to make sure we deal with the target user's data, not the admin's data.
+      $uid = crm_uf_get_uf_id($objectId); // returns their Drupal uid
+      $edit_user = user_load(array('uid' => $uid)); // call to the Drupal user API
+    }
+
+    // Bail out if this user doesn't auth with LDAP
+    if (!$edit_user->ldap_authentified) {
+      return;
+    }
+
+    $mapping_type = _ldapdata_ldap_info($edit_user, 'mapping_type');  
+    if ($mapping_type != LDAP_MAP_ATTRIBUTES) {
+      return;
+    }
+
+    $ldap_drupal_reverse_mappings = _ldapdata_reverse_mappings($edit_user->ldap_config);
+    foreach ($objectRef as $key => $val) {
+      if (isset($ldap_drupal_reverse_mappings["civicrm-".$key])) {
+        $writeout[$ldap_drupal_reverse_mappings["civicrm-".$key]] = $val;
+      }
+    }
+
+    if ($writeout) {
+      $bind_info = _ldapdata_edition($edit_user);
+      if (!$ldapdata_ldap->connect($bind_info[0], $bind_info[1])) {
+        watchdog('user', "User update: user $user->name's data could not be updated in the LDAP directory", WATCHDOG_NOTICE);
+        return;
+      }
+      $ldapdata_ldap->writeAttributes($edit_user->ldap_dn, $writeout);
+      $ldapdata_ldap->disconnect();
+    }
+  }
+}
 
 /***************************
 *     Drupal Hooks         *
@@ -534,6 +586,29 @@ function ldapdata_user_profile_load(&$us
     else {
       db_query("INSERT INTO {profile_values} (value, fid, uid) VALUES ('%s', '%d', '%d')", $value, $fid, $uid);
     }
+
+    // Handle the field if it is from CiviCRM
+    if (module_exists('civicrm') && substr($field, -10) == " (CiviCRM)") {
+      _ldapdata_init_civicrm();
+      // Clip off the " (CiviCRM)" string trailing the real field name.
+      $sfield = substr($field, 0, -10);
+      // Read get contacr object
+      $cid = crm_uf_get_match_id($uid);
+      $contact = crm_get_contact(array(id => $cid));
+      if (!$contact) {
+        watchdog('user', "User load: user $user->name's contact record could not be read in the CiviCRM database", WATCHDOG_WARNING);
+        return;
+      }
+
+      // This is sort of a roundabout way to do this, since we're only talking about a single field per iteration.
+      // However, the CiviCRM API needs this as an array, so it is what we must do.
+      $vars = get_object_vars($contact->contact_type_object);
+      foreach ($vars as $contactprop => $contactinfo) {
+        if ($contactprop == $sfield) {
+          crm_update_contact(&$contact, array($sfield => $value));
+        }
+      }
+    }
   }
 }
 
@@ -763,9 +838,36 @@ function _ldapdata_retrieve_profile_fiel
   while ($row = db_fetch_object($result)) {
       $fields[$row->fid] = $row->name;
   }
+
+  if (module_exists('civicrm')) {
+    $cfields = _ldapdata_retrieve_civicrm_user_fields();
+    $fields = array_merge($fields, $cfields);
+  }
+
   return $fields;
 }
 
+function _ldapdata_retrieve_civicrm_user_fields() {
+  // Finds all fields that CiviCRM adds to the user profile
+  _ldapdata_init_civicrm();
+
+  $groups = crm_uf_get_profile_groups();
+  $fields = array();
+  foreach ($groups as $key => $val) {
+    $gf = crm_uf_get_profile_fields($key);
+    $fields = array_merge($fields, $gf);
+  }
+
+  $ret = array();
+  foreach ($fields as $key => $val) {
+    // Delineate the CiviCRM-originating fields in the admin form
+    $ret["civicrm-".$key] = $key . " (CiviCRM)";
+    $i++;
+  }
+
+  return $ret;
+}
+
 function _ldapdata_retrieve_standard_user_fields() {
 
   // pablom - 
@@ -890,7 +992,15 @@ function _ldapdata_ldap_init(&$user) {
   }  
 }
 
-
+function _ldapdata_init_civicrm() {
+  // Initialize the CiviCRM API
+  // Change if your civicrm.settings.php lives somewhere else
+  require_once("sites/default/civicrm.settings.php");
+  global $civicrm_root;
+  require_once($civicrm_root . "/CRM/Core/Config.php");
+  require_once($civicrm_root . "/api/crm.php");
+  $config =& CRM_Core_Config::singleton();
+}
 
 /*********************************
  *          4. Themes            *
