You are here

function uif_plus_update_user in User Import Framework Plus 7

Common function for updating users based on types of fields as provided by contrib modules.

Parameters

object $account - User account to be updated:

array $user_data:

2 calls to uif_plus_update_user()
uif_plus_uif_post_create in ./uif_plus.module
Implementats hook_uif_post_create().
uif_plus_uif_post_update in ./uif_plus.module
Implements hook_uif_post_update().

File

./uif_plus.module, line 130
Advanced user import from a CSV file.

Code

function uif_plus_update_user($account, $user_data, $form_state) {
  $fields = uif_plus_get_field_info(array_keys($user_data));
  $value_delimiter = $form_state['storage']['value_delimiter'];
  $user_fields = $profile_fields = array();
  foreach ($fields as $label => $info) {
    if (!$info['supported']) {
      continue;
    }
    $parser = uif_isset_or($info['import']['parser']) ? $info['import']['parser'] : 'uif_get_raw_value';
    if ($label == 'picture') {
      if (drupal_strlen($user_data[$label])) {
        $user_fields[$label] = $parser($account, $info['data'], $user_data[$label]);
        user_save($account, $user_fields);
      }
    }
    elseif ($label == 'groups') {
      if (isset($user_data[$label])) {
        $parser($account, $info['data'], $user_data[$label]);
      }
    }
    elseif ($info['type'] == 'entity') {
      $field_values = array();
      $values = explode($value_delimiter, $user_data[$label]);
      $key = uif_isset_or($info['import']['key']) ? $info['import']['key'] : 'value';
      foreach ($values as $value) {
        $value = trim($value);
        if (drupal_strlen($value)) {
          $parsed_value = $parser($account, $info['data'], $value);
          if (!is_null($parsed_value)) {
            $field_values[] = $parsed_value;
          }
        }
      }
      if (isset($info['data']) && array_key_exists('bundles', $info['data']) && count($info['data']['bundles']['profile2'])) {
        foreach ($info['data']['bundles']['profile2'] as $profile_type) {
          for ($delta = 0; $delta < count($field_values); $delta++) {
            if ($info['data']['cardinality'] == 1 && $delta > 0) {
              break;
            }
            if (drupal_strlen($field_values[$delta] || count($field_values[$delta]))) {
              switch ($info['data']['type']) {
                case 'addressfield':
                  foreach ($field_values[$delta] as $field_value) {
                    foreach ($field_value as $key => $value) {
                      $profile_fields[$profile_type][$label][LANGUAGE_NONE][$delta][$key] = $value;
                    }
                  }
                  break;
                case 'file':
                  $profile_fields[$profile_type][$label][LANGUAGE_NONE][$delta] = $field_values[$delta];
                  break;
                default:
                  $profile_fields[$profile_type][$label][LANGUAGE_NONE][$delta][$key] = $field_values[$delta];
                  break;
              }
            }
          }
        }
      }
    }
  }
  if (count($profile_fields)) {
    uif_plus_save_profile2_fields($account, $profile_fields);
  }
}