You are here

function commerce_file_license_issue_by_commerce_line_item in Commerce File 7

Issue licenses for files in a line item

Parameters

$line_item: A line item object

$license_status: The status for new and updated licenses.

$product_refresh: Enabling this refresh will update the as-purchased snapshot of the files on the line items with the current product files and access limits.

$order: An order object [optional]

Return value

The count of licenses updated and created

1 call to commerce_file_license_issue_by_commerce_line_item()
commerce_file_license_issue_by_commerce_order in includes/commerce_file.entities.inc
Issue licenses for files in an order

File

includes/commerce_file.entities.inc, line 918
Handles file licenses and file license logs

Code

function commerce_file_license_issue_by_commerce_line_item($line_item, $updated_license_status = 'pending', $product_refresh = FALSE, $order = NULL) {
  $processed =& drupal_static(__FUNCTION__, array());
  $updated_count = 0;
  $updated_license_status = !empty($updated_license_status) ? $updated_license_status : 'pending';
  $line_item_id = $line_item->line_item_id;

  // exit if we issued this already
  if (empty($line_item->line_item_id) || isset($processed[$line_item_id])) {
    return $updated_count;
  }

  // get commerce file info
  $line_item_field_name = _commerce_file_get_field_names('line_item_files');
  $license_entity_type = COMMERCE_FILE_LICENSE_ENTITY_NAME;
  $license_info = _commerce_file_collate_license_info();

  // wrap the line
  $line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
  $line_item_wrapper = _commerce_file_clean_line_item_wrapper($line_item_wrapper);

  // resolve order needed to determine order owner
  if (empty($order)) {

    // attempt to load order off of line item
    $order = commerce_order_load($line_item->order_id);
    if (empty($order)) {
      return $updated_count;
    }
  }

  // wrap order
  $order_wrapper = entity_metadata_wrapper('commerce_order', $order);

  // exit if owner does not exists
  if (empty($order_wrapper->owner)) {
    return $updated_count;
  }

  // get owner account
  $account = $order_wrapper->owner
    ->value();
  if (empty($account)) {
    return $updated_count;
  }

  // refresh from product
  if (!empty($product_refresh)) {
    $line_item_wrapper = commerce_file_refresh_line_item($line_item_wrapper, $order_wrapper);
  }

  // exit if no files on this line item
  if (empty($line_item_wrapper->{$line_item_field_name}) || !$line_item_wrapper->{$line_item_field_name}
    ->value()) {
    return $updated_count;
  }

  // mark as processed
  $processed[$line_item_id] = TRUE;

  // get existing licenses for this line item and account
  $existing_licenses = commerce_file_license_load_by_property(array(), array(
    $line_item_id,
  ), $account);

  // set base values for new licenses
  $new_license_base_values = array(
    'uid' => $account->uid,
    'status' => $updated_license_status,
  );

  // set base values for new files attached to licenses
  // - set to empty value since limits propogate from line item ref
  $new_file_base_values = array(
    'data' => array(),
  );
  foreach ($license_info as $k => $info) {
    if (isset($info['property info']['zero_value'])) {
      $new_file_base_values['data'][$k] = $info['property info']['zero_value'];
    }
  }

  // create licenses based on line item file field items
  foreach ($line_item_wrapper->{$line_item_field_name} as $field_item) {
    $field_value = $field_item
      ->value();
    $fid = $field_value['fid'];

    // Find any existing license
    $found_existing = FALSE;
    if (!empty($existing_licenses)) {
      foreach ($existing_licenses as $existing_license) {
        if (!empty($existing_license->file) && $existing_license->file['fid'] == $fid) {

          // only update first found
          if ($existing_license->status != $updated_license_status) {
            $existing_license->status = $updated_license_status;

            // override state if account cannot view
            if (!$existing_license
              ->access('view', $account)) {
              $existing_license
                ->deny();
            }

            // save the updated license
            $existing_license
              ->save();
            $updated_count++;
          }

          // break out of $existing_licenses loop
          $found_existing = TRUE;
          break;
        }
      }
    }

    // continue if existing found
    if ($found_existing) {
      continue;
    }

    // if no existing license, create a new license ...
    $license_new = commerce_file_license_create($new_license_base_values);

    // merge data for new file
    $field_value_new = $field_value;
    $field_value_new['data'] = $new_file_base_values['data'];
    if (isset($field_value['data'])) {
      $field_value_new['data'] = $field_value['data'] + $field_value_new['data'];
    }

    // set new file for license
    $license_new->file = $field_value_new;

    // override state if account cannot view
    if (!$license_new
      ->access('view', $account)) {
      $license_new
        ->deny();
    }

    // attempt to link the line item
    $license_new
      ->link_line_item($line_item_id);

    // save the license
    $license_new
      ->save();
    $updated_count++;

    // update existing licenses
    $existing_licenses[$license_new
      ->internalIdentifier()] = $license_new;
  }

  // /line item field items
  return $updated_count;
}