You are here

function uc_file_user_renew in Ubercart 8.4

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

Adds file(s) 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.

\Drupal\Core\Session\AccountInterface $user: A Drupal user object.

int $pfid: An Ubercart product feature ID.

$file_limits: The limits inherited from this file.

bool $force_overwrite: Don't accumulate, assign.

Return value

array An array of uc_file objects.

3 calls to uc_file_user_renew()
RenewFile::doExecute in uc_file/src/Plugin/RulesAction/RenewFile.php
Renews an order's product files.
uc_file_action_order_renew in uc_file/uc_file.rules.inc
Renews an orders product files.
UserForm::submitForm in uc_file/src/Form/UserForm.php
Form submission handler.

File

uc_file/uc_file.module, line 883
Allows products to be associated with downloadable files.

Code

function uc_file_user_renew($fid, AccountInterface $user, $pfid, $file_limits, $force_overwrite) {
  $result = [];
  $connection = \Drupal::database();

  // Data shared between all files passed.
  $user_file_global = [
    'uid' => $user
      ->id(),
    '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 = [
        'granted' => \Drupal::time()
          ->getRequestTime(),
        'accessed' => 0,
        'addresses' => '',
      ];
      $force_overwrite = TRUE;
    }
    else {
      $file_user = (array) $file_user;
      $key = $file_user['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'] ? $file_user['file_key'] : \Drupal::csrfToken()
      ->get(serialize($file_user));
    unset($file_user['filename']);

    // Insert or update (if $key is already in table) uc_file_users table.
    $connection
      ->merge('uc_file_users')
      ->key([
      'fuid' => $key,
    ])
      ->fields($file_user)
      ->execute();
    if ($key) {
      \Drupal::logger('uc_file')
        ->notice('%user has had download privileges of %file renewed.', [
        '%user' => $user
          ->getUsername(),
        '%file' => $file_user['filename'],
      ]);
    }
    else {
      \Drupal::logger('uc_file')
        ->notice('%user has been allowed to download %file.', [
        '%user' => $user
          ->getUsername(),
        '%file' => $file_user['filename'],
      ]);
    }
    $result[] = (object) $file_user;
  }
  return $result;
}