You are here

function batch_birthdays_sync_b2p in Birthdays 6

Birthdays to Profile batch copying process

1 string reference to 'batch_birthdays_sync_b2p'
birthdays_sync_birthdays_to_profiles in ./birthdays.sync.inc
Birthdays to Profile submit handler.

File

./birthdays.sync.inc, line 154
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_b2p($field, &$context) {

  // First run, set up the sandbox.
  if (empty($context['sandbox'])) {
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['current_user'] = 0;
    $context['sandbox']['max'] = db_result(db_query('SELECT COUNT(uid) FROM {dob}'));
  }

  // 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;

  // Different syntax for MySQL and Postgresql when getting  theday, month and
  // year parts of the date of birth. Only get the next batch of birthdays.
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $result = db_query_range("SELECT uid, MONTH(birthday) AS month, YEAR(birthday) AS year, DAYOFMONTH(birthday) AS day FROM {dob} WHERE uid > %d ORDER BY uid ASC", $context['sandbox']['current_user'], 0, $limit);
      break;
    case 'pgsql':
      $result = db_query_range("SELECT uid, date_part('month',birthday) AS month, date_part('year',birthday) AS year, date_part('day', birthday) AS day FROM {dob} WHERE uid > %d ORDER BY uid ASC", $context['sandbox']['current_user'], 0, $limit);
      break;
  }

  // For each date of birth.
  while ($birthday = db_fetch_object($result)) {

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

    // Make the date of birth array.
    $dob = array(
      'day' => $birthday->day,
      'month' => $birthday->month,
      'year' => $birthday->year,
    );

    // And save the user.
    user_save($account, array(
      $field->name => $dob,
    ), $field->category);

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

  // Update the message.
  $context['message'] = t('Synchronized @current of @total birthdays.', 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'];
  }
}