function commerce_file_get_license in Commerce File 7.2
Returns an eligible active license for the given file.
A file could be sold from multiple products. The user's active licenses for all of them are loaded, and the first eligible one is returned.
Parameters
$op: The operation to be performed on the licensed file. Possible values are:
- "view"
- "download"
$file: The file entity.
$account: The account to check for. If not given, the current user is used instead.
Return value
The license if found, FALSE otherwise.
2 calls to commerce_file_get_license()
- commerce_file_access in ./
commerce_file.module - Determines if a user may perform the given operation on the licensed file.
- commerce_file_exit in ./
commerce_file.module - Implements hook_exit().
File
- ./
commerce_file.module, line 316 - Extends Commerce License with the ability to sell access to files.
Code
function commerce_file_get_license($op, $file, $account = NULL) {
if (!$account) {
$account = $GLOBALS['user'];
}
// Checkout complete account override.
if (!empty($_SESSION['commerce_license_uid']) && empty($account->uid)) {
$account = user_load($_SESSION['commerce_license_uid']);
}
// Ignore anonymous users, they can't have licenses.
if (empty($account->uid)) {
return FALSE;
}
$licenses =& drupal_static(__FUNCTION__, array());
if (!isset($licenses[$file->fid])) {
$licenses[$file->fid] = FALSE;
// Get all products that offer the provided file.
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'commerce_product')
->fieldCondition('commerce_file', 'fid', $file->fid);
$result = $query
->execute();
if (!empty($result['commerce_product'])) {
$product_ids = array_keys($result['commerce_product']);
// Get the user's licenses for any of the found product ids.
// The oldest active licenses are used first.
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'commerce_license')
->entityCondition('bundle', 'file')
->propertyCondition('status', COMMERCE_LICENSE_ACTIVE)
->propertyCondition('product_id', $product_ids)
->propertyCondition('uid', $account->uid)
->entityOrderby('entity_id', 'ASC');
$result = $query
->execute();
if (!empty($result['commerce_license'])) {
$license_ids = array_keys($result['commerce_license']);
$loaded_licenses = entity_load('commerce_license', $license_ids);
// Go through all loaded licenses and check their eligibility.
foreach ($loaded_licenses as $license) {
$op_view = $op == 'view';
$op_download = $op == 'download' && commerce_file_can_download($license, $file, $account);
if ($op_view || $op_download) {
// License found, stop the search.
$licenses[$file->fid] = $license;
break;
}
}
}
}
}
return $licenses[$file->fid];
}