You are here

function redhen_dedupe_merge_form_submit in RedHen CRM 7

Submit handler for dedupe merge form.

File

modules/redhen_dedupe/includes/redhen_dedupe.form.inc, line 193
Forms for creating, editing, and deleting contacts.

Code

function redhen_dedupe_merge_form_submit($form, &$form_state) {
  $master_id = $form_state['values']['master'];
  $contacts = $form_state['contacts'];
  $master = $contacts[$master_id];
  $values = array();

  // Pull the actual data out of the #value array constructed for the form:
  if (isset($form_state['values']['values'])) {
    foreach ($form_state['values']['values'] as $name => $val) {
      if (is_array($val)) {
        $values[$name] = array(
          'type' => 'combine',
          'value' => array(),
        );
        foreach ($val as $ent_id => $selected) {
          if ($selected) {
            $wrapper = entity_metadata_wrapper('redhen_contact', $ent_id);
            $values[$name]['value'][$ent_id] = $wrapper->{$name}
              ->value();
          }
        }
      }
      else {
        $wrapper = entity_metadata_wrapper('redhen_contact', $contacts[$val]);
        $values[$name] = array(
          'type' => 'direct',
          'value' => $wrapper->{$name}
            ->value(),
        );
      }
    }
  }
  unset($contacts[$master_id]);
  $merge_status = redhen_dedupe_merge($master, $contacts, $values, array_filter($form_state['values']['related_entities']));
  if ($merge_status) {
    drupal_set_message(t('Contacts have successfully been merged into %master and deleted.', array(
      '%master' => $master
        ->label(),
    )));
    $uri = $master
      ->uri();
    $form_state['redirect'] = $uri['path'];
  }
  else {
    drupal_set_message(t('Error attempting to merge these contacts. Check the error log for more details.'), 'error');
  }
}