You are here

private function RedhenDedupeMergeForm::redhenDedupeMerge in RedHen CRM 8

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

Parameters

\Drupal\redhen_contact\Entity\Contact $master: The master RedHen Contact.

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

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

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

Return value

bool Result of the merge attempt.

1 call to RedhenDedupeMergeForm::redhenDedupeMerge()
RedhenDedupeMergeForm::submitForm in modules/redhen_dedupe/src/Form/RedhenDedupeMergeForm.php
Form submission handler.

File

modules/redhen_dedupe/src/Form/RedhenDedupeMergeForm.php, line 352

Class

RedhenDedupeMergeForm
Form controller for Dedupe merge tool.

Namespace

Drupal\redhen_dedupe\Form

Code

private function redhenDedupeMerge(Contact $master, $values, $related_entities, $contacts = []) {
  $master_id = $master
    ->id();
  $transaction = \Drupal::database()
    ->startTransaction(__FUNCTION__);
  try {

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

      // Update related entities:
      foreach ($related_entities as $entity_type) {
        switch ($entity_type) {
          case 'redhen_note':
          case 'redhen_engagement':
          case 'redhen_membership':

            // TODO redhen_notes, redhen_engagement, 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 = \Drupal::entityTypeManager()
                ->getStorage($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 'redhen_connection':

            // Look for connections w/ one end point including dupe contact.
            $results = \Drupal::service('redhen_connection.connections')
              ->getConnections($contact);
            if ($results) {
              foreach ($results as $connection) {
                $connection_type = ConnectionType::load($connection
                  ->bundle());
                $contact_endpoint_fields = $connection_type
                  ->getEndpointFields('redhen_contact');
                foreach ($contact_endpoint_fields as $contact_endpoint_field) {

                  // Iterate through endpoints and replace the endpoint that
                  // matches with the master contact.
                  if ($connection
                    ->get($contact_endpoint_field)->entity
                    ->id() == $contact_id) {
                    $connection
                      ->get($contact_endpoint_field)
                      ->setValue($master);
                  }
                }
                $connection
                  ->save();
              }
            }
            break;
        }
      }
    }

    // Delete old contacts.
    \Drupal::entityTypeManager()
      ->getStorage('redhen_contact')
      ->delete($contacts);

    // Set the new values on the master contact.
    foreach ($values as $id => $value) {
      if ($value['type'] == 'direct') {
        $master
          ->get($id)
          ->setValue($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 = [];
        }
        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);
        }
        $master
          ->get($id)
          ->setValue($all_vals);
      }
    }
    $master
      ->save();
    return TRUE;
  } catch (Exception $e) {
    $transaction
      ->rollback();
    watchdog_exception('redhen_dedupe', $e);
    return FALSE;
  }
}