You are here

function crm_core_user_sync_sync in CRM Core 8.2

Same name and namespace in other branches
  1. 7 modules/crm_core_user_sync/crm_core_user_sync.module \crm_core_user_sync_sync()

Synchronizes user amd contact.

Parameters

$account to be synchronized:

$contact to be associated with $account:

Return value

contact object

3 calls to crm_core_user_sync_sync()
crm_core_user_sync_user_insert in modules/crm_core_user_sync/crm_core_user_sync.module
Implements hook_user_insert()
crm_core_user_sync_user_update in modules/crm_core_user_sync/crm_core_user_sync.module
Implements hook_user_update()
_crm_core_user_sync_batch_processing in modules/crm_core_user_sync/crm_core_user_sync.admin.inc
Helper function for batch processing of users synchronization.

File

modules/crm_core_user_sync/crm_core_user_sync.module, line 250

Code

function crm_core_user_sync_sync($account, $contact = NULL) {

  // Property crm_core_no_auto_sync skips creation of contact.
  if (empty($contact) && empty($account->crm_core_no_auto_sync)) {

    // Get corresponding contact type
    $contact_type = crm_core_user_sync_get_contact_type_for_account($account);
    if (!$contact_type) {
      return;
    }

    // Create the contact.
    $contact = Individual::create(array(
      'type' => $contact_type,
    ));
    $contact
      ->setOwner($account->uid);

    // For now we just add the name.
    $contact_name = field_info_instance('crm_core_contact', 'contact_name', $contact_type);
    if (!empty($contact_name)) {
      $contact->contact_name[LANGUAGE_NONE][0] = array(
        'title' => '',
        'family' => '',
        'generational' => '',
        'credentials' => '',
        'given' => $account->name,
      );
    }
    $contact
      ->save();
  }
  else {
    $contact_type = $contact->type;

    // Check if contact can be synchronized to a contact.
    if (!crm_core_user_sync_validate($account, $contact)) {
      return;
    }
  }

  // Check if crm_core_user_sync relation exists for any of endpoint.
  if (crm_core_user_sync_get_contact_from_uid($account->uid) || crm_core_user_sync_get_user_from_contact_id($contact->contact_id)) {
    return;
  }

  // Create the relation
  $endpoints = array(
    array(
      'entity_type' => 'user',
      'entity_bundle' => 'user',
      'entity_id' => $account->uid,
    ),
    array(
      'entity_type' => 'crm_core_contact',
      'entity_bundle' => $contact_type,
      'entity_id' => $contact->contact_id,
    ),
  );
  $relation = Relation::create([
    'relation_type' => 'crm_core_user_sync',
    'endpoints' => $endpoints,
  ])
    ->save();
  watchdog('crm_core_user_sync', 'User @user has been synchronized to the contact @contact_id, relation @rid has been created.', array(
    '@user' => $account->name,
    '@contact_id' => $contact->contact_id,
    '@rid' => $relation
      ->id(),
  ));
  return $contact;
}