You are here

function _commerce_file_license_build_field_query in Commerce File 7

Builds an EntityFieldQuery object for given inputs

Parameters

$fids: An array of file fids

$line_item_ids: An array of line_item_ids

$account: A user object of the license owner

Return value

An EntityFieldQuery object

1 call to _commerce_file_license_build_field_query()
_commerce_file_license_field_query in includes/commerce_file.entities.inc
Return all license ids for specified field values

File

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

Code

function _commerce_file_license_build_field_query($fids = array(), $line_item_ids = array(), $account = NULL, $allow_access_check = FALSE) {
  $entity_type = COMMERCE_FILE_LICENSE_ENTITY_NAME;

  // initialize query
  $query = new EntityFieldQuery();
  $query->_commerce_file_is_filtered = FALSE;
  $query
    ->entityCondition('entity_type', $entity_type, '=');

  // add account condition
  if (isset($account)) {

    // kick anonymous or bad object
    if (empty($account->uid)) {
      return NULL;
    }
    $query
      ->propertyCondition('uid', $account->uid, '=');
    $query->_commerce_file_is_filtered = TRUE;
  }

  // add file fid condition
  if (!empty($fids)) {

    // determine if there are any file field types on licenses entities
    $file_fields = commerce_info_fields(COMMERCE_FILE_FIELD_TYPE, $entity_type);
    if (!empty($file_fields)) {
      foreach ($file_fields as $field_name => $field_info) {
        $query
          ->fieldCondition($field_name, 'fid', $fids, 'IN');
      }
      $query->_commerce_file_is_filtered = TRUE;
    }
  }

  // add line item id condition
  if (!empty($line_item_ids)) {

    // determine if there are any file field types on licenses entities
    $line_fields = commerce_info_fields('commerce_line_item_reference', $entity_type);
    if (!empty($line_fields)) {
      foreach ($line_fields as $field_name => $field_info) {
        $query
          ->fieldCondition($field_name, 'line_item_id', $line_item_ids, 'IN');
      }
      $query->_commerce_file_is_filtered = TRUE;
    }
  }

  // set tag to ensure that we get raw results not altered by the node access system
  // @see http://drupal.org/node/1597378 - special tag added in Drupal 7.15
  // @see http://drupal.org/node/997394#comment-5096664 - work-around being used below to support all versions
  if (empty($allow_access_check)) {
    $query
      ->addMetaData('account', user_load(1));
  }
  return $query;
}