You are here

function uc_file_update_6005 in Ubercart 6.2

Iterate over all the existing file_user objects and replace them with the defaults for their product feature.

File

uc_file/uc_file.install, line 348
Install, update and uninstall functions for the uc_file module.

Code

function uc_file_update_6005(&$sandbox) {
  $ret = array();
  if (!isset($sandbox['progress'])) {
    $sandbox['progress'] = 0;
    $sandbox['current_fuid'] = 0;
    $sandbox['max'] = db_result(db_query('SELECT COUNT(DISTINCT fuid) FROM {uc_file_users}'));
  }

  // This could probably go higher, but this will be fine for now.
  $limit = 100;
  $result = db_query_range("SELECT * FROM {uc_file_users} WHERE fuid > %d ORDER BY fuid ASC", $sandbox['current_fuid'], 0, $limit);
  while ($file_user = db_fetch_object($result)) {
    $file_product = db_fetch_object(db_query("SELECT * FROM {uc_file_products} WHERE pfid = %d", $file_user->pfid));

    // Get the higher limits. Work around the 5 -> 6 upgrade path.
    if (function_exists('uc_file_get_download_limit') && $file_product) {
      $file_user->download_limit = uc_file_get_download_limit($file_product);
      $file_user->address_limit = uc_file_get_address_limit($file_product);
      $time_limit = uc_file_get_time_limit($file_product);
    }
    else {
      $file_user->download_limit = variable_get('uc_file_download_limit_number', NULL);
      $file_user->address_limit = variable_get('uc_file_download_limit_addresses', NULL);
      $time_limit = array(
        'time_polarity' => '+',
        'time_granularity' => variable_get('uc_file_download_limit_duration_granularity', 'never'),
        'time_quantity' => variable_get('uc_file_download_limit_duration_qty', NULL),
      );
    }
    $granted = $file_user->granted;
    if ($time_limit['time_polarity'] == 'never') {
      $expiration = NULL;
    }
    else {
      $expiration = strtotime($time_limit['time_polarity'] . $time_limit['time_quantity'] . ' ' . $time_limit['time_granularity'], $file_user->granted);
    }

    // drupal_write_record() doesn't work here for some odd reason.
    db_query("UPDATE {uc_file_users} SET expiration = %d WHERE fuid = %d", $expiration, $file_user->fuid);
    $sandbox['progress']++;
    $sandbox['current_fuid'] = $file_user->fuid;
  }
  $ret['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];
  return $ret;
}