You are here

function uc_coupon_tokens in Ubercart Discount Coupons 7.3

Same name and namespace in other branches
  1. 7.2 uc_coupon.tokens.inc \uc_coupon_tokens()

Implements hook_tokens().

File

./uc_coupon.tokens.inc, line 59
Token support for uc_coupon.

Code

function uc_coupon_tokens($type, $tokens, $data = array(), $options = array()) {
  $sanitize = !empty($options['sanitize']);
  $values = array();
  switch ($type) {
    case 'uc_order':
      if (array_key_exists('coupon-code', $tokens) && !empty($data['uc_order']) && isset($data['uc_order']->data['coupon'])) {
        $values[$tokens['coupon-code']] = $sanitize ? check_plain($data['uc_order']->data['coupon']) : $data['uc_order']->data['coupon'];
      }
      break;
    case 'uc_coupon':
      if (!empty($data['uc_coupon'])) {
        $coupon = $data['uc_coupon'];
        if (array_key_exists('name', $tokens)) {
          $values[$tokens['name']] = $sanitize ? check_plain($coupon->name) : $coupon->name;
        }
        if (array_key_exists('code', $tokens)) {
          $values[$tokens['code']] = $sanitize ? check_plain($coupon->code) : $coupon->code;
        }
        if (array_key_exists('codes', $tokens)) {
          $codes = array();
          if ($coupon->bulk) {
            for ($id = 0; $id < $coupon->data['bulk_number']; $id++) {
              $codes[] = uc_coupon_get_bulk_code($coupon, $id);
            }
          }
          else {
            $codes[] = $coupon->code;
          }
          $values[$tokens['codes']] = implode("\n", $sanitize ? array_walk($codes, 'check_plain') : $codes);
        }
        if (array_key_exists('value', $tokens)) {
          $values[$tokens['value']] = isset($coupon->amount) ? theme('uc_price', array(
            'price' => $coupon->amount,
          )) : theme('uc_coupon_discount', array(
            'coupon' => $coupon,
          ));
        }
        if (array_key_exists('credit', $tokens)) {
          if ($coupon->type !== 'credit') {
            if ($coupon->bulk && !$coupon->valid) {
              $values[$tokens['credit']] = t('n/a');
            }
            else {
              $amt = $coupon->value;
              $usage = isset($coupon->usage) ? $coupon->usage : uc_coupon_count_usage($coupon->cid);
              if (isset($usage['value'][$coupon->code])) {
                $amt -= $usage['value'][$coupon->code];
              }
              if (isset($coupon->amount)) {
                $amt -= $coupon->amount;
              }
              $values[$tokens['credit']] = uc_currency_format($amt);
            }
          }
          else {
            $values[$tokens['credit']] = t('n/a');
          }
        }
      }
      break;
  }
  return $values;
}