You are here

function usermerge_usermerge_account_properties in User Merge 7.2

Implement hook_usermerge_account_properties().

File

./usermerge.usermerge.inc, line 19
Implements User merge hooks for core-related user properties.

Code

function usermerge_usermerge_account_properties($user_to_delete, $user_to_keep, $action) {
  $user_entity_properties['core'] = usermerge_get_user_core_properties();

  // Define an array of properties that can be used to display the data-review table(s)
  $account_properties = array(
    'core' => array(
      'title' => t('Core properties'),
      'items' => array(),
    ),
  );

  // Define list of all user properties (including fields)
  // Using $user_to_delete insures that all properties are accounted for
  $user_all_properties = array_keys((array) $user_to_delete);

  // Find custom fields
  $user_entity_properties['fields'] = preg_grep("/^field_/", $user_all_properties);
  if (count($user_entity_properties['fields'])) {
    $account_properties['fields'] = array(
      'title' => t('Fields'),
      'description' => t('Please note that single-value fields cannot be merged.'),
      'items' => array(),
    );
  }

  // Find other user properties
  $user_noncore_properties = array_diff($user_all_properties, $user_entity_properties['core']);
  $user_entity_properties['other'] = array_diff($user_noncore_properties, $user_entity_properties['fields']);
  if (count($user_entity_properties['other'])) {
    $account_properties['other'] = array(
      'title' => t('Other properties'),
      'items' => array(),
    );
  }
  foreach ($user_entity_properties as $type => $properties) {
    foreach (array_flip($properties) as $property_name => $delta) {

      // Exclude properties that shouldn't be merged, like password
      // These could be defined via settings page
      if (!in_array($property_name, array(
        'pass',
      ))) {
        $account_properties[$type]['items'][$property_name] = array(
          'name' => $property_name,
          'criterion' => 'merge',
        );
      }
    }
  }

  // Set default choices for core properties
  // This could be defined via settings page
  $usermerge_settings = variable_get('usermerge_settings', usermerge_settings_default());
  foreach ($usermerge_settings['core'] as $property_name => $setting_value) {
    if (empty($setting_value)) {

      // If the property is not checked in settings, set a default
      $account_properties['core']['items'][$property_name]['default'] = $user_to_keep->{$property_name};
    }
  }

  // Always show uid, mail, and name, regarless of whether they can be editted.
  foreach (array(
    'uid',
    'mail',
    'name',
  ) as $property_name) {
    if (isset($account_properties['core']['items'][$property_name]['default']) || $action != 'delete') {
      $account_properties['core']['items'][$property_name]['disabled'] = TRUE;
    }
    unset($account_properties['core']['items'][$property_name]['default']);
  }

  // Automatically handle properties that can't be exposed via settings:
  // Choose older created date
  if ($user_to_delete->created < $user_to_keep->created) {
    $account_properties['core']['items']['created']['default'] = $user_to_delete->created;
  }
  else {
    $account_properties['core']['items']['created']['default'] = $user_to_keep->created;
  }

  // Choose newer access date
  if ($user_to_delete->access > $user_to_keep->access) {
    $account_properties['core']['items']['access']['default'] = $user_to_delete->access;
  }
  else {
    $account_properties['core']['items']['access']['default'] = $user_to_keep->access;
  }

  // Choose newer login date
  if ($user_to_delete->login > $user_to_keep->login) {
    $account_properties['core']['items']['login']['default'] = $user_to_delete->login;
  }
  else {
    $account_properties['core']['items']['login']['default'] = $user_to_keep->login;
  }
  $account_properties['core']['items']['init']['default'] = $user_to_keep->init;
  $account_properties['core']['items']['data']['default'] = $user_to_keep->data;
  foreach ($account_properties['core']['items'] as $property_name => $settings) {
    if ($property_name == 'roles') {

      // Keep the ability to choose between roles of the two users, but set the default option to merge
      $account_properties['core']['items'][$property_name]['default_option'] = 'merge';
    }
    else {

      // All other properties should not have a "both" option
      $account_properties['core']['items'][$property_name]['criterion'] = 'no_merge';
    }
  }

  // Special settings for fields
  if (!empty($account_properties['fields']['items'])) {
    foreach ($account_properties['fields']['items'] as $field_name => $properties) {
      $field_settings = field_info_field($field_name);

      // If the field's cardinality is not 1, do not allow merging
      // This could pose problems for fields whose cardinality is greater than one, but not unlimited
      if ($field_settings['cardinality'] != FIELD_CARDINALITY_UNLIMITED) {
        $account_properties['fields']['items'][$field_name]['criterion'] = 'force_select';
      }
    }
  }

  // Authored entitites
  $account_properties['entities']['title'] = t('Authored entities');
  foreach (usermerge_get_authorable_entities() as $entity_name => $entity) {
    $account_properties['entities']['items'][$entity_name] = array(
      'name' => $entity_name,
      'criterion' => 'no_merge',
    );
  }
  return $account_properties;
}