uc_coupon.tokens.inc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * @file
  4. * Token support for uc_coupon.
  5. */
  6. /**
  7. * Implements hook_token_info().
  8. */
  9. function uc_coupon_token_info() {
  10. $types = array(
  11. 'uc_coupon' => array(
  12. 'name' => t('Coupon'),
  13. 'description' => t('Tokens related to Ubercart discount coupons.'),
  14. 'needs-data' => 'uc_coupon',
  15. ),
  16. );
  17. $tokens = array(
  18. 'uc_coupon' => array(
  19. 'name' => array(
  20. 'name' => t('Name'),
  21. 'description' => t('The name of the coupon.'),
  22. ),
  23. 'code' => array(
  24. 'name' => t('Code'),
  25. 'description' => t('The coupon code. For bulk coupons this will be the actual code applied, or the code
  26. prefix if the coupon has not yet been applied.'),
  27. ),
  28. 'codes' => array(
  29. 'name' => t('All Codes'),
  30. 'description' => t('The coupon codes. For bulk coupons, this will be a comma separated list of codes.'),
  31. ),
  32. 'value' => array(
  33. 'name' => t('Value'),
  34. 'description' => t('The value of the coupon. If the coupon has already been applied to an order, this will be
  35. the actual currency value of the coupon. Otherwise, it will be the nominal discount.'),
  36. ),
  37. 'credit' => array(
  38. 'name' => t('Remaining credit'),
  39. 'description' => t('For store credit/gift certificate type coupons which have been applied to an order, the unused credit remaining.'),
  40. ),
  41. ),
  42. 'uc_order' => array(
  43. 'coupon-code' => array(
  44. 'name' => t('Coupon Code'),
  45. 'description' => t('The coupon code used in the order.'),
  46. ),
  47. ),
  48. );
  49. return array('types' => $types, 'tokens' => $tokens);
  50. }
  51. /**
  52. * Implements hook_tokens().
  53. */
  54. function uc_coupon_tokens($type, $tokens, $data = array(), $options = array()) {
  55. $sanitize = !empty($options['sanitize']);
  56. $values = array();
  57. switch ($type) {
  58. case 'uc_order':
  59. if (array_key_exists('coupon-code', $tokens) && !empty($data['uc_order']) && isset($data['uc_order']->data['coupon'])) {
  60. $values[$tokens['coupon-code']] = $sanitize ? check_plain($data['uc_order']->data['coupon']) : $data['uc_order']->data['coupon'];
  61. }
  62. break;
  63. case 'uc_coupon':
  64. if (!empty($data['uc_coupon'])) {
  65. $coupon = $data['uc_coupon'];
  66. if (array_key_exists('name', $tokens)) {
  67. $values[$tokens['name']] = $sanitize ? check_plain($coupon->name) : $coupon->name;
  68. }
  69. if (array_key_exists('code', $tokens)) {
  70. $values[$tokens['code']] = $sanitize ? check_plain($coupon->code) : $coupon->code;
  71. }
  72. if (array_key_exists('codes', $tokens)) {
  73. $codes = array();
  74. if ($coupon->bulk) {
  75. for ($id = 0; $id < $coupon->data['bulk_number']; $id++) {
  76. $codes[] = uc_coupon_get_bulk_code($coupon, $id);
  77. }
  78. }
  79. else {
  80. $codes[] = $coupon->code;
  81. }
  82. $values[$tokens['codes']] = implode("\n", $sanitize ? array_walk($codes, 'check_plain') : $codes);
  83. }
  84. if (array_key_exists('value', $tokens)) {
  85. $values[$tokens['value']] = isset($coupon->amount) ?
  86. theme('uc_price', array('price' => $coupon->amount)) : theme('uc_coupon_discount', array('coupon' => $coupon));
  87. }
  88. if (array_key_exists('credit', $tokens)) {
  89. if ($coupon->type !== 'credit') {
  90. if ($coupon->bulk && !$coupon->valid) {
  91. $values[$tokens['credit']] = t('n/a');
  92. }
  93. else {
  94. $amt = $coupon->value;
  95. $usage = isset($coupon->usage) ? $coupon->usage : uc_coupon_count_usage($coupon->cid);
  96. if (isset($usage['value'][$coupon->code])) {
  97. $amt -= $usage['value'][$coupon->code];
  98. }
  99. if (isset($coupon->amount)) {
  100. $amt -= $coupon->amount;
  101. }
  102. $values[$tokens['credit']] = uc_currency_format($amt);
  103. }
  104. }
  105. else {
  106. $values[$tokens['credit']] = t('n/a');
  107. }
  108. }
  109. }
  110. break;
  111. }
  112. return $values;
  113. }