You are here

function batch_birthdays_sync_p2b in Birthdays 6

Profile to Birthdays batch copying process.

1 string reference to 'batch_birthdays_sync_p2b'
birthdays_sync_profiles_to_birthdays in ./birthdays.sync.inc
Profile to Birthdays submit handler.

File

./birthdays.sync.inc, line 109
Because the Birthdays module uses 2 tables for the birthdays, they have to be kept in sync with each other. Normally that will be no problem, but in some cases there is the need to manually synchronize the two tables. These cases are for example:

Code

function batch_birthdays_sync_p2b($field, &$context) {

  // First run, set up the sandbox.
  if (empty($context['sandbox'])) {
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['current_user'] = 0;

    // Count how many users are going to be processed.
    $context['sandbox']['max'] = db_result(db_query('SELECT COUNT(DISTINCT uid) FROM {users} WHERE uid > 0'));
  }

  // I'm not sure how many a slow server can process within the batch execution
  // limit, but 25 seems not too much. Faster machines won't notice the limit
  // thanks to the Batch API.
  $limit = 25;

  // Get next batch of users.
  $result = db_query_range('SELECT uid FROM {users} WHERE uid > %d ORDER BY uid ASC', $context['sandbox']['current_user'], 0, $limit);

  // For each user id.
  while ($uid = db_fetch_object($result)) {

    // Get the user object.
    $account = user_load(array(
      'uid' => $uid->uid,
    ));

    // If the date of birth was set
    if ($account->{$field->name}) {

      // Save it, so birthdays_user() will be called and all necessary actions
      // will be performed.
      user_save($account, array(
        $field->name => $account->{$field->name},
      ));
    }

    // Updating the sandbox.
    $context['sandbox']['progress']++;
    $context['sandbox']['current_user'] = $account->uid;
  }

  // Update the message.
  $context['message'] = t('Synchronized @current of @total users.', array(
    '@current' => $context['sandbox']['progress'],
    '@total' => $context['sandbox']['max'],
  ));

  // If not yet finished, calculate the proportion of items that are.
  if ($context['sandbox']['progress'] < $context['sandbox']['max']) {
    $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
  }
}