You are here

function ip_login_update_7300 in IP Login 7.3

Create field_ip_login on user entity. Migrate data from ip_login_user to new field

File

./ip_login.install, line 60
Install file of the IP Login module.

Code

function ip_login_update_7300(&$sandbox) {
  if (!module_exists('field_ipaddress')) {
    throw new DrupalUpdateException(t('IP Login 7.3.x requires the module field_ipaddress to be enabled.', array(
      'field_ipaddress' => l('field_ip_address', 'http://drupal.org/project/field_ipaddress', array(
        'external' => TRUE,
      )),
    )));
    return;
  }

  // Create the IP Address field
  // field_ip_login
  // on the User entity
  if (!isset($sandbox['field_created'])) {

    // Check if our field is not already created.
    if (!field_info_field('field_ip_login')) {
      $field = array(
        'field_name' => 'field_ip_login',
        'type' => 'field_ipaddress',
        'cardinality' => FIELD_CARDINALITY_UNLIMITED,
      );
      field_create_field($field);

      // Create the instance on the bundle.
      $instance = array(
        'field_name' => 'field_ip_login',
        'entity_type' => 'user',
        'bundle' => 'user',
        'label' => 'IP address',
        'description' => ip_login_help_ranges(),
        'required' => FALSE,
        'settings' => array(
          'user_register_form' => 0,
        ),
        'widget' => array(
          'type' => 'field_ipaddress_shorthand',
        ),
      );
      field_create_instance($instance);
      $sandbox['field_created'] = TRUE;
    }
  }

  // Migrate the data
  // from ip_login_user
  // to field_ip_login data tables
  if (db_table_exists('ip_login_user')) {
    if (!isset($sandbox['progress'])) {
      $sandbox['progress'] = 0;
      $sandbox['current_uid'] = 0;
      $sandbox['max'] = db_query('SELECT COUNT(DISTINCT uid) FROM {ip_login_user}')
        ->fetchField();
    }

    // Select 10 users to process
    $result = db_select('ip_login_user', 'u')
      ->fields('u', array(
      'uid',
      'ip_match',
    ))
      ->condition('u.uid', $sandbox['current_uid'], '>')
      ->range(0, 10)
      ->orderBy('u.uid', 'ASC')
      ->execute();
    foreach ($result as $row) {
      $sandbox['current_uid'] = $row->uid;
      $sandbox['progress']++;
      if (empty($row->ip_match)) {
        continue;
      }

      // Try shorthand match
      $value = _field_ipaddress_shorthand2long(trim($row->ip_match));

      // Try CIDR match
      if (!$value && strpos($row->ip_match, '/') !== FALSE) {
        $value = _field_ipaddress_cidr2long(trim($row->ip_match));
      }
      if ($value) {
        $account = user_load($row->uid);
        $language = empty($account->language) ? LANGUAGE_NONE : $account->language;
        if (empty($account->field_ip_login[$language][0])) {
          $edit = array(
            'field_ip_login' => array(
              $language => array(
                0 => $value,
              ),
            ),
          );
          user_save($account, $edit);
        }
      }
      else {
        watchdog('ip_login', 'Invalid IP range or address @range for user id (@uid). IP address match _NOT_ migrated for this user.', array(
          '@range' => $row->ip_match,
          '@uid' => $row->uid,
        ));
        $sandbox['errors'] = TRUE;
      }
    }
    $sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];

    // Tell user about watchdog log if any errors
    if ($sandbox['#finished'] && !empty($sandbox['#errors'])) {
      drupal_set_message('Some errors were encountered during IP address migration. Check the watchdog log for more details.');
    }
    elseif ($sandbox['#finished']) {
      return t('IP Login field migration completed successfully.');
    }
  }
}