You are here

function uc_file_user_renew in Ubercart 6.2

Same name and namespace in other branches
  1. 8.4 uc_file/uc_file.module \uc_file_user_renew()
  2. 7.3 uc_file/uc_file.module \uc_file_user_renew()

Adds a file (or files) to a user's list of downloadable files, accumulating limits.

First the function sees if the given file ID is a file or a directory, if it's a directory, it gathers all the files under it recursively. Then all the gathered IDs are iterated over, loading each file and aggregating all the data necessary to save a file_user object. Limits derived from the file are accumulated with the current limits for this user on this file (if an association exists yet). The data is then hashed, and the hash is stored in the file_user object. The object is then written to the file_users table.

Parameters

$fid: A file ID.

$user: A Drupal user object.

$pfid: An Ubercart product feature ID.

$file_limits: The limits inherited from this file.

$force_overwrite: Don't accumulate, assign.

Return value

An array of uc_file objects.

2 calls to uc_file_user_renew()
uc_file_action_order_renew in uc_file/uc_file.ca.inc
Renews an orders product files.
uc_file_user_form_submit in uc_file/uc_file.module
Submit handler for per-user file download administration.

File

uc_file/uc_file.module, line 1704

Code

function uc_file_user_renew($fid, $user, $pfid, $file_limits, $force_overwrite) {
  $result = array();

  // Data shared between all files passed.
  $user_file_global = array(
    'uid' => $user->uid,
    'pfid' => $pfid,
  );

  // Get the file(s).
  $fids = _uc_file_get_dir_file_ids($fid, TRUE);
  foreach ($fids as $fid) {
    $file_user = _uc_file_user_get($user, $fid);

    // Doesn't exist yet?
    $key = NULL;
    if (!$file_user) {
      $file_user = array(
        'granted' => time(),
        'accessed' => 0,
        'addresses' => array(),
      );
      $force_overwrite = TRUE;
    }
    else {
      $file_user = (array) $file_user;
      $key = 'fuid';
    }

    // Add file data in as well.
    $file_info = (array) uc_file_get_by_id($fid);
    $file_user += $user_file_global + $file_info;
    _uc_file_accumulate_limits($file_user, $file_limits, $force_overwrite);

    // Workaround for d#226264 ...
    $file_user['download_limit'] = $file_user['download_limit'] ? $file_user['download_limit'] : 0;
    $file_user['address_limit'] = $file_user['address_limit'] ? $file_user['address_limit'] : 0;
    $file_user['expiration'] = $file_user['expiration'] ? $file_user['expiration'] : 0;

    // Calculate hash
    $file_user['file_key'] = isset($file_user['file_key']) ? $file_user['file_key'] : drupal_get_token(serialize($file_user));

    // Write and queue the file_user object.
    drupal_write_record('uc_file_users', $file_user, $key);
    if ($key) {
      watchdog('uc_file', '%user has had download privileges of %file renewed.', array(
        '%user' => $user->name,
        '%file' => $file_user['filename'],
      ));
    }
    else {
      watchdog('uc_file', '%user has been allowed to download %file.', array(
        '%user' => $user->name,
        '%file' => $file_user['filename'],
      ));
    }
    $result[] = (object) $file_user;
  }
  return $result;
}