uc_product.tokens.inc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. /**
  3. * @file
  4. * Token hooks for the uc_product module.
  5. */
  6. /**
  7. * Implements hook_token_info().
  8. */
  9. function uc_product_token_info() {
  10. $node['product'] = array(
  11. 'name' => t('Product'),
  12. 'description' => t('Ubercart product data of the node.'),
  13. 'type' => 'uc_product',
  14. );
  15. $types['uc_product'] = array(
  16. 'name' => t('Product'),
  17. 'description' => t('Tokens for Ubercart products.'),
  18. 'needs-data' => 'node',
  19. );
  20. $product['model'] = array(
  21. 'name' => t('SKU / Model'),
  22. 'description' => t("The product's SKU or model number."),
  23. );
  24. $product['list-price'] = array(
  25. 'name' => t('List price'),
  26. 'description' => t("The product's list price."),
  27. 'type' => 'uc_price',
  28. );
  29. $product['cost'] = array(
  30. 'name' => t('Cost'),
  31. 'description' => t("The product's cost."),
  32. 'type' => 'uc_price',
  33. );
  34. $product['sell-price'] = array(
  35. 'name' => t('Sell price'),
  36. 'description' => t("The product's sell price."),
  37. 'type' => 'uc_price',
  38. );
  39. $product['weight'] = array(
  40. 'name' => t('Weight'),
  41. 'description' => t("The product's weight."),
  42. 'type' => 'uc_weight',
  43. );
  44. $product['length'] = array(
  45. 'name' => t('Length'),
  46. 'description' => t("The product's length."),
  47. 'type' => 'uc_dimension',
  48. );
  49. $product['width'] = array(
  50. 'name' => t('Width'),
  51. 'description' => t("The product's width."),
  52. 'type' => 'uc_dimension',
  53. );
  54. $product['height'] = array(
  55. 'name' => t('Height'),
  56. 'description' => t("The product's height."),
  57. 'type' => 'uc_dimension',
  58. );
  59. $types['uc_price'] = array(
  60. 'name' => t('Price'),
  61. 'description' => 'Tokens for price formats.',
  62. 'needs-data' => 'uc_price',
  63. );
  64. $price['formatted'] = array(
  65. 'name' => t('Formatted'),
  66. 'description' => t('A formatted price.'),
  67. );
  68. $price['raw'] = array(
  69. 'name' => t('Raw value'),
  70. 'description' => t('A decimal price.'),
  71. );
  72. $types['uc_weight'] = array(
  73. 'name' => t('Weight'),
  74. 'description' => 'Tokens for weight formats.',
  75. 'needs-data' => 'uc_weight',
  76. );
  77. $weight['formatted'] = array(
  78. 'name' => t('Formatted'),
  79. 'description' => t('A formatted weight.'),
  80. );
  81. $weight['raw'] = array(
  82. 'name' => t('Raw value'),
  83. 'description' => t('A decimal weight.'),
  84. );
  85. $weight['units'] = array(
  86. 'name' => t('Units'),
  87. 'description' => t('The unit of measurement.'),
  88. );
  89. $types['uc_dimension'] = array(
  90. 'name' => t('Dimension'),
  91. 'description' => 'Tokens for length, width, and height.',
  92. 'needs-data' => 'uc_dimension',
  93. );
  94. $dimension['formatted'] = array(
  95. 'name' => t('Formatted'),
  96. 'description' => t('A formatted dimension.'),
  97. );
  98. $dimension['raw'] = array(
  99. 'name' => t('Raw value'),
  100. 'description' => t('A decimal dimension.'),
  101. );
  102. $dimension['units'] = array(
  103. 'name' => t('Units'),
  104. 'description' => t('The unit of measurement.'),
  105. );
  106. return array(
  107. 'types' => $types,
  108. 'tokens' => array(
  109. 'node' => $node,
  110. 'uc_product' => $product,
  111. 'uc_price' => $price,
  112. 'uc_weight' => $weight,
  113. 'uc_dimension' => $dimension,
  114. ),
  115. );
  116. }
  117. /**
  118. * Implements hook_token_info_alter().
  119. *
  120. * Remove unwanted entity tokens.
  121. */
  122. function uc_product_token_info_alter(&$info) {
  123. $tokens = array(
  124. 'model', 'list-price', 'cost', 'sell-price', 'weight', 'weight-units',
  125. 'length', 'width', 'height', 'length_units', 'pkg-qty', 'ordering', 'shippable',
  126. );
  127. foreach ($tokens as $token) {
  128. if (!empty($info['tokens']['node'][$token]['entity-token'])) {
  129. unset($info['tokens']['node'][$token]);
  130. }
  131. }
  132. }
  133. /**
  134. * Implements hook_tokens().
  135. */
  136. function uc_product_tokens($type, $tokens, $data = array(), $options = array()) {
  137. $sanitize = !empty($options['sanitize']);
  138. $replacements = array();
  139. if ($type == 'node' && !empty($data['node']) && uc_product_is_product($data['node'])) {
  140. $product = &$data['node'];
  141. foreach ($tokens as $name => $original) {
  142. switch ($name) {
  143. case 'product:model':
  144. $replacements[$original] = $sanitize ? check_plain($product->model) : $product->model;
  145. break;
  146. case 'product:list-price':
  147. case 'product:list-price:formatted':
  148. $replacements[$original] = uc_currency_format($product->list_price);
  149. break;
  150. case 'product:cost':
  151. case 'product:cost:formatted':
  152. $replacements[$original] = uc_currency_format($product->cost);
  153. break;
  154. case 'product:sell-price':
  155. case 'product:sell-price:formatted':
  156. $replacements[$original] = uc_currency_format($product->sell_price);
  157. break;
  158. case 'product:list-price:raw':
  159. $replacements[$original] = $product->list_price;
  160. break;
  161. case 'product:cost:raw':
  162. $replacements[$original] = $product->cost;
  163. break;
  164. case 'product:sell-price:raw':
  165. $replacements[$original] = $product->sell_price;
  166. break;
  167. case 'product:weight':
  168. case 'product:weight:formatted':
  169. $replacements[$original] = uc_weight_format($product->weight, $product->weight_units);
  170. break;
  171. case 'product:weight:raw':
  172. $replacements[$original] = $product->weight;
  173. break;
  174. case 'product:weight:units':
  175. $replacements[$original] = $product->weight_units;
  176. break;
  177. case 'product:length':
  178. case 'product:length:formatted':
  179. $replacements[$original] = uc_length_format($product->length, $product->length_units);
  180. break;
  181. case 'product:width':
  182. case 'product:width:formatted':
  183. $replacements[$original] = uc_length_format($product->width, $product->length_units);
  184. break;
  185. case 'product:height':
  186. case 'product:height:formatted':
  187. $replacements[$original] = uc_length_format($product->height, $product->length_units);
  188. break;
  189. case 'product:length:raw':
  190. $replacements[$original] = $product->length;
  191. break;
  192. case 'product:width:raw':
  193. $replacements[$original] = $product->width;
  194. break;
  195. case 'product:height:raw':
  196. $replacements[$original] = $product->height;
  197. break;
  198. case 'product:length:units':
  199. case 'product:width:units':
  200. case 'product:height:units':
  201. $replacements[$original] = $product->length_units;
  202. break;
  203. }
  204. }
  205. return $replacements;
  206. }
  207. }