You are here

public function FeedsUserProcessor::process in Feeds 7

Same name and namespace in other branches
  1. 6 plugins/FeedsUserProcessor.inc \FeedsUserProcessor::process()

Implements FeedsProcessor::process().

Overrides FeedsProcessor::process

File

plugins/FeedsUserProcessor.inc, line 16
FeedsUserProcessor class.

Class

FeedsUserProcessor
Feeds processor plugin. Create users from feed items.

Code

public function process(FeedsImportBatch $batch, FeedsSource $source) {

  // Count number of created and updated nodes.
  $created = $updated = $failed = 0;
  while ($item = $batch
    ->shiftItem()) {
    if (!($uid = $this
      ->existingItemId($batch, $source)) || $this->config['update_existing']) {

      // Map item to a term.
      $account = $this
        ->map($batch);

      // Check if user name and mail are set, otherwise continue.
      if (empty($account->name) || empty($account->mail) || !valid_email_address($account->mail)) {
        $failed++;
        continue;
      }

      // Add term id if available.
      if (!empty($uid)) {
        $account->uid = $uid;
      }

      // Save the user.
      user_save($account, (array) $account);
      if ($account->uid && $account->openid) {
        $authmap = array(
          'uid' => $account->uid,
          'module' => 'openid',
          'authname' => $account->openid,
        );
        if (SAVED_UPDATED != drupal_write_record('authmap', $authmap, array(
          'uid',
          'module',
        ))) {
          drupal_write_record('authmap', $authmap);
        }
      }
      if ($uid) {
        $updated++;
      }
      else {
        $created++;
      }
    }
  }

  // Set messages.
  if ($failed) {
    drupal_set_message(format_plural($failed, 'There was @number user that could not be imported because either their name or their email was empty or not valid. Check import data and mapping settings on User processor.', 'There were @number users that could not be imported because either their name or their email was empty or not valid. Check import data and mapping settings on User processor.', array(
      '@number' => $failed,
    )), 'error');
  }
  if ($created) {
    drupal_set_message(format_plural($created, 'Created @number user.', 'Created @number users.', array(
      '@number' => $created,
    )));
  }
  elseif ($updated) {
    drupal_set_message(format_plural($updated, 'Updated @number user.', 'Updated @number users.', array(
      '@number' => $updated,
    )));
  }
  else {
    drupal_set_message(t('There are no new users.'));
  }
}