You are here

function redhen_dedupe_list_page in RedHen CRM 7

Page callback for listing duplicate contacts.

Return value

array Render array for a table of duplicates.

1 string reference to 'redhen_dedupe_list_page'
redhen_dedupe_menu in modules/redhen_dedupe/redhen_dedupe.module
Implements hook_menu().

File

modules/redhen_dedupe/redhen_dedupe.module, line 31

Code

function redhen_dedupe_list_page() {
  $results = FALSE;
  $contacts = array();
  $properties = array();
  $fields = array();
  $active = TRUE;
  if (!isset($_POST['form_id'])) {
    if (isset($_GET['properties'])) {
      $properties = $_GET['properties'];
    }
    if (isset($_GET['fields'])) {
      $fields = $_GET['fields'];
    }
    if (empty($properties) && empty($fields)) {
      drupal_set_message(t('Please select at least on Property or Field to match on.'), 'warning', FALSE);
    }
    if (!empty($properties) || !empty($fields)) {
      $active = isset($_GET['active']) ? $_GET['active'] : TRUE;
      $results = redhen_dedupe_get_duplicates($properties, $fields, $active);
    }
  }
  if (!empty($results)) {
    $message = t('The following sets of duplicate contacts have been found. Select the corresponding merge action to merge contact records.');
    $info = entity_get_property_info('redhen_contact');
    $rows = array();
    $header = array();

    // Build our header array from the selected properties.
    foreach ($properties as $property) {
      $header[] = $info['properties'][$property]['label'];
    }
    $rh_entity_info = entity_get_property_info('redhen_contact');
    foreach ($fields as $field) {
      $field_pieces = explode(":", $field);
      $field = array_shift($field_pieces);
      $info = field_info_field($field);
      $instance = field_info_instance('redhen_contact', $field, $info['bundles']['redhen_contact'][0]);
      $label = $instance['label'];
      if (count($field_pieces)) {
        $label .= ' -- ' . $rh_entity_info['bundles'][$info['bundles']['redhen_contact'][0]]['properties'][$field]['property info'][$field_pieces[0]]['label'];
      }
      $header[] = $label;
    }
    $header[] = t('Count (IDs)');
    $header[] = '';

    // Display each result basing our row on the selected properties.
    foreach ($results as $result) {
      $ids = explode(',', $result->ids);

      // Dedupe by values:
      $ids = array_flip(array_flip($ids));
      if (count($ids) > 1) {
        $result->ids = implode(',', $ids);
        $col = array();
        foreach ($properties as $property) {
          $col[] = $result->{$property};
        }
        foreach ($fields as $field) {
          $field_pieces = explode(':', $field);
          $name = array_shift($field_pieces);
          if (count($field_pieces)) {
            foreach ($field_pieces as $piece) {
              $name .= '_' . $piece;
            }
          }
          else {
            reset($info['columns']);
            $name .= "_" . key($info['columns']);
          }
          $col[] = $result->{$name};
        }
        $id_links = array();
        foreach ($ids as $id) {
          $id_links[] = l($id, 'redhen/contact/' . $id);
        }
        $count = $result->count . ' (' . implode(', ', $id_links) . ')';
        $col[] = $count;
        $col[] = l(t('merge'), 'redhen/dedupe/merge/' . $result->ids);
        $rows[] = $col;
      }
    }
    $contacts = array(
      '#theme' => 'table',
      '#header' => $header,
      '#rows' => $rows,
    );
  }
  else {
    $message = t('There are no duplicate contacts based on the selected properties. Expand your search or relax, you have no duplicates!');
  }
  return array(
    'form' => drupal_get_form('redhen_dedupe_filter_form', $properties, $fields, $active),
    'message' => array(
      '#markup' => $message,
    ),
    'contacts' => $contacts,
  );
}