You are here

function usermerge_usermerge_merge_accounts in User Merge 7.2

Implements hook_usermerge_merge_accounts().

File

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

Code

function usermerge_usermerge_merge_accounts($user_to_delete, $user_to_keep, $review) {
  $merged_account = unserialize($review['account']);
  unset($review['account']);

  // Process core, fields, other properties
  foreach ($review as $property_type => $properties) {
    if (in_array($property_type, array(
      'core',
      'fields',
      'other',
    ))) {
      foreach ($properties as $property_name => $options) {
        if ($options['options'] != NULL) {
          if ($options['options'] != 'merge') {
            $chosen_account = $options['options'];
            $merged_account[$property_name] = ${$chosen_account}->{$property_name};
          }
          else {
            switch ($property_type) {
              case 'core':

                // Other modules can enforce merging of properties this module won't merge, so we won't object to that here
                // This module deals natively only with roles
                if ($property_name == 'roles') {
                  $merged_account[$property_name] = $user_to_delete->{$property_name} + $user_to_keep->{$property_name};
                  ksort($merged_account[$property_name]);
                }
                break;
              case 'fields':

                // $user_to_delete has data in the field
                if (count($user_to_delete->{$property_name})) {
                  foreach ($user_to_delete->{$property_name} as $language => $items) {

                    // $user_to_keep has data in the field
                    if (isset($user_to_keep->{$property_name}[$language])) {
                      $merged_account[$property_name][$language] = array_merge($user_to_delete->{$property_name}[$language], $user_to_keep->{$property_name}[$language]);
                    }
                    else {
                      $merged_account[$property_name][$language] = $user_to_delete->{$property_name}[$language];
                    }
                  }
                }
                elseif (count($user_to_keep->{$property_name})) {
                  $merged_account[$property_name] = $user_to_keep->{$property_name};
                }
                break;
            }
          }
        }
      }
    }
  }

  // Operate on entities
  // Rebuild the list of entities here instead of passing as form values
  $authorable_entities = usermerge_get_authorable_entities();
  $entities_user_to_delete = usermerge_query_authored_entities($authorable_entities, $user_to_delete->uid);

  // No need to find entities for $user_to_keep
  foreach ($entities_user_to_delete as $entity_type => $entities) {

    // We take for granted that the base table has a uid column, because of usermerge_get_authorable_entities()
    // We also don't need to select specific entities, because we're replacing all the entities belonging to $user_to_delete
    db_update($authorable_entities[$entity_type]['base table'])
      ->fields(array(
      'uid' => $user_to_keep->uid,
    ))
      ->condition('uid', $user_to_delete->uid)
      ->execute();

    // Check if the entity has a revision table
    if (isset($authorable_entities[$entity_type]['revision table'])) {

      // Find the correct column name
      // Not all revision tables have the uid stored in uid (like node_revision)
      // Some store it in revision_uid
      // Could be taking a chance here
      $uid_column = preg_grep("/uid/", $authorable_entities[$entity_type]['schema_fields_sql']['revision table']);
      $uid_column = reset($uid_column);
      db_update($authorable_entities[$entity_type]['revision table'])
        ->fields(array(
        $uid_column => $user_to_keep->uid,
      ))
        ->condition($uid_column, $user_to_delete->uid)
        ->execute();
    }
  }
  return $merged_account;
}