You are here

function logintoboggan_update_5 in LoginToboggan 5

Same name and namespace in other branches
  1. 6 logintoboggan.install \logintoboggan_update_5()

Implementation of hook_update_5()

File

./logintoboggan.install, line 63

Code

function logintoboggan_update_5() {

  // get all profile fields.
  $fields = db_query('SELECT fid, name FROM {profile_fields}');
  while ($field = db_fetch_object($fields)) {
    $fids[$field->fid] = $field->name;
    $where[] = "data LIKE '%%%s%%'";
  }
  if (isset($fids)) {

    // get all users with any profile fields in their user data.
    $users = db_query('SELECT uid, data FROM {users} WHERE ' . implode(' OR ', $where), $fids);
    while ($user = db_fetch_object($users)) {
      $data = unserialize($user->data);
      $updated_data = array();

      // Extract any profile values from the user's data.
      if (is_array($data)) {
        foreach ($data as $key => $value) {
          if (!in_array($key, $fids)) {
            $updated_data[$key] = $value;
          }
          elseif ($value) {

            // reinsert profile data into profile_values table.
            db_query("INSERT INTO {profile_values} VALUES (%d, %d, '%s')", array_search($key, $fids), $user->uid, $value);
          }
        }

        // reinsert the cleaned data for the user
        $updated_data = serialize($updated_data);
        db_query("UPDATE {users} SET data = '%s' WHERE uid = %d", $updated_data, $user->uid);
      }
    }
  }
  drupal_set_message(t('logintoboggan cleaning of user/profile data successful'));
  return array();
}