You are here

function sf_user_user in Salesforce Suite 6.2

Same name and namespace in other branches
  1. 5.2 sf_user/sf_user.module \sf_user_user()

Implementation of hook_user().

File

sf_user/sf_user.module, line 51
Integrates the core user object and profile module with the Salesforce API.

Code

function sf_user_user($op, &$edit, &$account, $category = NULL) {
  static $sf_user_skip_export;
  switch ($op) {
    case 'load':
      $account->salesforce = salesforce_api_id_load('user', $account->uid);
      break;
    case 'delete':
      $maps = salesforce_api_salesforce_field_map_load_by(array(
        'drupal' => 'user',
      ));
      $map = current($maps);
      if ($map->automatic & SALESFORCE_AUTO_SYNC_DELETE) {
        $continue = TRUE;
        $continues = module_invoke_all('salesforce_api_delete', $account->salesforce->sfid, $map, $account->uid);
        if (!empty($continues)) {
          foreach ($continues as $continue) {
            if ($continue === FALSE) {
              break;
            }
          }
        }
        if ($continue) {
          salesforce_api_delete_salesforce_objects($account->salesforce->sfid);
        }
      }
      salesforce_api_delete_object_map('user', $account->uid);
      break;
    case 'update':

      // Set a static variable during 'update' to prevent extranesous API calls.
      // In 'after_update', $account has been reloaded from the database, and
      // sf_user_skip_export will have been unset. Also set the same property in
      // $edit array to NULL so that it won't get written to the database, and
      // to remove any "legacy" data.
      if ($account->sf_user_skip_export) {
        $sf_user_skip_export = TRUE;
        $edit['sf_user_skip_export'] = NULL;
      }
      break;
    case 'insert':
    case 'after_update':

      // When importing *from* Salesforce, don't export *back* to Salesforce.
      if (isset($account->sf_user_skip_export) || $sf_user_skip_export || isset($edit['sf_user_skip_export'])) {

        // We don't want this value to get into the $account->data array, so set
        // it to null in the $edit array to make sure that doesn't happen. This
        // will also unset any "legacy" data.
        $edit['sf_user_skip_export'] = NULL;
        unset($sf_user_skip_export);
        break;
      }
      $salesforce = (object) array(
        'name' => NULL,
        'sfid' => NULL,
      );
      if ($account->uid) {
        $salesforce = salesforce_api_id_load('user', $account->uid);
      }

      // If we have an existing link, attempt to load the assoc'd map.
      if (!empty($salesforce->name)) {
        $map = salesforce_api_fieldmap_load($salesforce->name);
      }

      // If the sf link wasn't found, or if it was found but assoc'd to a
      // non-existent map, grab any maps assoc'd with this node type.
      if (empty($salesforce->name) || empty($map)) {
        $maps = salesforce_api_salesforce_field_map_load_by(array(
          'drupal' => 'user',
        ));
        if (empty($maps)) {
          break;
        }
      }
      else {
        $maps = array(
          $map->name => $map,
        );
      }
      foreach ($maps as $map) {
        $auto_create = $map->automatic & SALESFORCE_AUTO_SYNC_CREATE;
        $auto_update = $map->automatic & SALESFORCE_AUTO_SYNC_UPDATE;
        if (!$auto_create && $op == 'insert' || !$auto_update && $op == 'after_update') {
          unset($maps[$map->name]);
        }
      }

      // If all our maps were unset, abort this procedure.
      if (empty($maps)) {
        break;
      }

      // Otherwise, use the first fieldmap.
      $map = reset($maps);
      $salesforce->name = $map->name;

      // Check if there is more than one fieldmap in the result.
      if (user_access('administer salesforce') and next($maps)) {
        if (!empty($map->description)) {
          $description = '(' . $map->description . ')';
        }
        drupal_set_message(t('Warning: more than one "automatic" salesforce mapping detected. Used fieldmap !map_name @map_description.', array(
          '!map_name' => l($map->name, SALESFORCE_PATH_FIELDMAPS . '/' . $map->name . '/edit'),
          '@map_description' => $description,
        )), 'warning');
      }

      // Finally export the user to salesforce
      try {
        sf_user_export($account->uid, $salesforce->name, $salesforce->sfid);
      } catch (Exception $e) {
        salesforce_api_log(SALESFORCE_LOG_SOME, 'Exception while attempting to export user: ' . $e
          ->getMessage(), array(), WATCHDOG_ERROR, l('user ' . $account->uid, 'user/' . $account->uid));
      }
      break;
  }
}