You are here

function commerce_gc_query_commerce_gc_transaction_access_alter in Commerce GC 7

Implements hook_query_TAG_alter().

Derived from commerce_payment_query_commerce_payment_transaction_access_alter().

File

./commerce_gc.module, line 326
Provides Giftcard coupon bundle, Giftcard Transaction entity and basic user interface elements.

Code

function commerce_gc_query_commerce_gc_transaction_access_alter(QueryAlterableInterface $query) {

  // Read the meta-data from the query.
  if (!($account = $query
    ->getMetaData('account'))) {
    global $user;
    $account = $user;
  }

  // If the user is allowed to administrate giftcards, stop here.
  if (user_access('administer giftcard transactions')) {
    return;
  }

  // If the user is not administrative-level, he/she must own the coupon related
  // to a given transaction record.
  if (user_access('view own giftcard transactions')) {
    $tables =& $query
      ->getTables();

    // Look for an existing commerce_coupon table as well as the transaction
    // table.
    foreach ($tables as $table) {
      if ($table['table'] == 'commerce_coupon') {
        $coupon_alias = $table['alias'];
      }
      else {
        if ($table['table'] == 'commerce_gc_transaction') {
          $transaction_alias = $table['alias'];
        }
      }
    }

    // If not found, join to the coupon table and check access on the coupon.
    // Otherwise, we know that Commerce Coupon has already added its access
    // checks.
    if (!isset($coupon_alias) && isset($transaction_alias)) {
      $coupon_alias = $query
        ->innerJoin('commerce_coupon', 'cc', '%alias.coupon_id = ' . $transaction_alias . '.coupon_id');

      // Look up access on the coupon.
      commerce_coupon_apply_access_query_substitute($query, $coupon_alias, $account);
    }
  }
  else {

    // The user may not view the results of this query.
    $query
      ->condition('1=0');
  }
}