You are here

function redhen_dedupe_merge in RedHen CRM 7

Merge values from contacts into master contact and handle related entities.

Parameters

RedhenContact $master: The master RedHen Contact.

array $contacts: The contacts being merged into the master.

array $values: Values to update the master contact with.

array $related_entities: Array of entity types to update to the master contact.

Return value

bool Result of the merge attempt.

1 call to redhen_dedupe_merge()
redhen_dedupe_merge_form_submit in modules/redhen_dedupe/includes/redhen_dedupe.form.inc
Submit handler for dedupe merge form.

File

modules/redhen_dedupe/redhen_dedupe.module, line 281

Code

function redhen_dedupe_merge(RedhenContact $master, $contacts, $values, $related_entities) {
  $master_wrapper = entity_metadata_wrapper('redhen_contact', $master);
  $master_id = $master_wrapper
    ->getIdentifier();
  $transaction = db_transaction();
  try {

    // Iterate through all contacts and update or delete related entities.
    foreach ($contacts as $contact) {
      $contact_id = $contact
        ->internalIdentifier();

      // Update related entities:
      foreach ($related_entities as $entity_type) {
        switch ($entity_type) {
          case 'redhen_note':
          case 'redhen_engagement':
          case 'redhen_membership':
            $query = new EntityFieldQuery();
            $query
              ->entityCondition('entity_type', $entity_type);
            $query
              ->propertyCondition('entity_type', 'redhen_contact');
            $query
              ->propertyCondition('entity_id', $contact_id);
            $result = $query
              ->execute();
            if (!empty($result)) {
              $rel_entities = entity_load($entity_type, array_keys($result[$entity_type]));

              // Determine the property to change.
              $entity_key = $entity_type == 'redhen_engagement' ? 'contact_id' : 'entity_id';
              foreach ($rel_entities as $rel_entity) {
                $rel_entity->{$entity_key} = $master_id;
                $rel_entity
                  ->save();
              }
            }
            break;
          case 'relation':

            // Look for relations with one end point including the dupe contact.
            $query = relation_query('redhen_contact', $contact_id);
            $results = $query
              ->execute();
            if ($results) {
              $relations = relation_load_multiple(array_keys($results));
              foreach ($relations as $relation) {
                $endoints = field_get_items('relation', $relation, 'endpoints');
                foreach ($endoints as $key => $endpoint) {

                  // Iterate through endpoints and replace the endpoint that
                  // matches with the master contact.
                  if ($endpoint['entity_type'] == 'redhen_contact' && $endpoint['entity_id'] == $contact_id) {
                    $relation->endpoints[LANGUAGE_NONE][$key]['entity_id'] = $master_id;
                  }
                }
                relation_update($relation);
              }
            }
            break;
        }
      }
    }

    // Delete old contacts.
    redhen_contact_delete_multiple(array_keys($contacts));

    // Set the new values on the master contact.
    foreach ($values as $id => $value) {
      if ($value['type'] == 'direct') {
        if (!isset($master_wrapper->{$id}->itemType) || $master_wrapper->{$id}->itemType != 'field_collection') {
          $master_wrapper->{$id}
            ->set($value['value']);
        }
        else {
          _redhen_dedupe_set_field_collection_value($master, $id, $value['value']);
        }
      }
      if ($value['type'] == 'combine') {
        if (isset($value['value'][$master_id])) {

          // This assures that the "Master" record value is at the 0-index:
          $all_vals = $value['value'][$master_id];
          unset($value['value'][$master_id]);
        }
        else {
          $all_vals = array();
        }
        foreach ($value['value'] as $val) {
          $all_vals = array_merge($all_vals, $val);
        }
        if (!is_array(reset($all_vals)) && !is_object(reset($all_vals))) {
          $all_vals = array_unique($all_vals);
        }
        $field_info = field_info_field($id);
        if ($field_info['type'] != 'field_collection') {
          $master_wrapper->{$id}
            ->set($all_vals);
        }
        else {

          // Field Collections are completely obnoxious.
          $originals = array();
          foreach ($master_wrapper->{$id} as $original_val) {
            $originals[] = $original_val->item_id
              ->value();
          }
          foreach ($all_vals as $val) {
            _redhen_dedupe_set_field_collection_value($master, $id, $val);
          }
          entity_delete_multiple('field_collection_item', $originals);
        }
      }
    }
    $master_wrapper
      ->save();
    return TRUE;
  } catch (Exception $e) {
    $transaction
      ->rollback();
    watchdog_exception('redhen_dedupe', $e);
    return FALSE;
  }
}