uc_order.tokens.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <?php
  2. /**
  3. * @file
  4. * Token hooks for the uc_order module.
  5. */
  6. /**
  7. * Implements hook_token_info().
  8. */
  9. function uc_order_token_info() {
  10. $types = array(
  11. 'name' => t('Orders'),
  12. 'description' => t('Tokens related to Ubercart orders.'),
  13. 'needs-data' => 'uc_order',
  14. );
  15. $tokens = array();
  16. $tokens['new-username'] = array(
  17. 'name' => t('New username'),
  18. 'description' => t('New username associated with an order if applicable.'),
  19. );
  20. $tokens['new-password'] = array(
  21. 'name' => t('New password'),
  22. 'description' => t('New password associated with an order if applicable.'),
  23. );
  24. $tokens['order-number'] = array(
  25. 'name' => t('Order number'),
  26. 'description' => t('The unique identifier of the order.'),
  27. );
  28. $tokens['url'] = array(
  29. 'name' => t('URL'),
  30. 'description' => t('The URL to the order'),
  31. 'type' => 'url',
  32. );
  33. $tokens['link'] = array(
  34. 'name' => t('Link'),
  35. 'description' => t('A link to the order using the order ID.'),
  36. );
  37. $tokens['admin-url'] = array(
  38. 'name' => t('Admin URL'),
  39. 'description' => t('The URL to the admin view page using the order ID.'),
  40. 'type' => 'url',
  41. );
  42. $tokens['admin-link'] = array(
  43. 'name' => t('Admin link'),
  44. 'description' => t('A link to the order admin view page using the order ID.'),
  45. );
  46. $tokens['subtotal'] = array(
  47. 'name' => t('Subtotal'),
  48. 'description' => t('The subtotal of products on an order.'),
  49. );
  50. $tokens['total'] = array(
  51. 'name' => t('Total'),
  52. 'description' => t('The order total.'),
  53. );
  54. $tokens['email'] = array(
  55. 'name' => t('Email'),
  56. 'description' => t('The primary e-mail address of the order.'),
  57. );
  58. // Duplicate [uc_order:email] as [uc_order:mail] as VBO validates tokens ending in :mail.
  59. $tokens['mail'] = $tokens['email'];
  60. $tokens['shipping-method'] = array(
  61. 'name' => t('Shipping method'),
  62. 'description' => t('The title of the first shipping line item.'),
  63. );
  64. $tokens['shipping-address'] = array(
  65. 'name' => t('Shipping address'),
  66. 'description' => t('The order shipping address.'),
  67. );
  68. $tokens['shipping-phone'] = array(
  69. 'name' => t('Shipping phone number'),
  70. 'description' => t('The phone number for the shipping address.'),
  71. );
  72. $tokens['billing-address'] = array(
  73. 'name' => t('Billing address'),
  74. 'description' => t('The order billing address.'),
  75. );
  76. $tokens['billing-phone'] = array(
  77. 'name' => t('Billing phone number'),
  78. 'description' => t('The phone number for the billing address.'),
  79. );
  80. $tokens['first-name'] = array(
  81. 'name' => t("Customer's first name"),
  82. 'description' => t('The first name associated with the order.'),
  83. );
  84. $tokens['last-name'] = array(
  85. 'name' => t("Customer's last name"),
  86. 'description' => t('The last name associated with the order.'),
  87. );
  88. $tokens['comments'] = array(
  89. 'name' => t('Comments'),
  90. 'description' => t('Comments left by the customer.'),
  91. );
  92. $tokens['last-comment'] = array(
  93. 'name' => t('Last comment'),
  94. 'description' => t('Last order comment left by an administrator (not counting the order admin comments).'),
  95. );
  96. $tokens['order-status'] = array(
  97. 'name' => t('Order status'),
  98. 'description' => t('The current order status.'),
  99. );
  100. $tokens['customer'] = array(
  101. 'name' => t('Customer'),
  102. 'description' => t('The user associated with the order.'),
  103. 'type' => 'user',
  104. );
  105. $tokens['created'] = array(
  106. 'name' => t('Created'),
  107. 'description' => t('The date and time when the order was created.'),
  108. 'type' => 'date',
  109. );
  110. $tokens['modified'] = array(
  111. 'name' => t('Modified'),
  112. 'description' => t('The date and time when the order was last modified.'),
  113. 'type' => 'date',
  114. );
  115. $tokens['products'] = array(
  116. 'name' => t('Products'),
  117. 'description' => t('A list of products in the order.'),
  118. );
  119. return array(
  120. 'types' => array('uc_order' => $types),
  121. 'tokens' => array('uc_order' => $tokens),
  122. );
  123. }
  124. /**
  125. * Implements hook_tokens().
  126. */
  127. function uc_order_tokens($type, $tokens, $data = array(), $options = array()) {
  128. $language_code = NULL;
  129. if (isset($options['language'])) {
  130. $language_code = $options['language']->language;
  131. }
  132. $sanitize = !empty($options['sanitize']);
  133. $replacements = array();
  134. if ($type == 'uc_order' && !empty($data['uc_order'])) {
  135. $order = $data['uc_order'];
  136. $path = 'user/' . $order->uid . '/orders/' . $order->order_id;
  137. $admin_path = 'admin/store/orders/' . $order->order_id;
  138. foreach ($tokens as $name => $original) {
  139. switch ($name) {
  140. case 'new-username':
  141. if (isset($order->data['new_user']['name'])) {
  142. $replacements[$original] = $sanitize ? check_plain($order->data['new_user']['name']) : $order->data['new_user']['name'];
  143. }
  144. break;
  145. case 'new-password':
  146. $replacements[$original] = isset($order->password) ? $order->password : t('Your password');
  147. break;
  148. case 'order-number':
  149. $replacements[$original] = $order->order_id;
  150. break;
  151. case 'url':
  152. $replacements[$original] = url($path, array('absolute' => TRUE));
  153. break;
  154. case 'link':
  155. $replacements[$original] = l($order->order_id, url($path, array('absolute' => TRUE)));
  156. break;
  157. case 'admin-url':
  158. $replacements[$original] = url($admin_path, array('absolute' => TRUE));
  159. break;
  160. case 'admin-link':
  161. $replacements[$original] = l($order->order_id, url($admin_path, array('absolute' => TRUE)));
  162. break;
  163. case 'subtotal':
  164. $subtotal = '';
  165. if (is_array($order->line_items)) {
  166. foreach ($order->line_items as $key => $value) {
  167. if ($value['type'] == 'subtotal') {
  168. $subtotal = uc_currency_format($order->line_items[$key]['amount']);
  169. }
  170. }
  171. }
  172. $replacements[$original] = $subtotal;
  173. break;
  174. case 'subtotal-value':
  175. if (is_array($order->line_items)) {
  176. foreach ($order->line_items as $key => $value) {
  177. if ($value['type'] == 'subtotal') {
  178. $subtotal = $order->line_items[$key]['amount'];
  179. }
  180. }
  181. }
  182. $replacements[$original] = $subtotal;
  183. break;
  184. case 'total':
  185. $replacements[$original] = uc_currency_format($order->order_total);
  186. break;
  187. case 'total-value':
  188. $replacements[$original] = $order->order_total;
  189. break;
  190. case 'email':
  191. case 'mail':
  192. $replacements[$original] = $sanitize ? check_plain($order->primary_email) : $order->primary_email;
  193. break;
  194. case 'shipping-method':
  195. if (is_array($order->line_items)) {
  196. foreach ($order->line_items as $key => $value) {
  197. if ($value['type'] == 'shipping' && !isset($ship_method)) {
  198. $ship_method = $value['title'];
  199. break;
  200. }
  201. }
  202. }
  203. $replacements[$original] = !isset($ship_method) ? t('Standard delivery') : $ship_method;
  204. break;
  205. case 'shipping-address':
  206. $address = uc_order_address($order, 'delivery');
  207. $address = str_replace(array('<br />', '<BR />'), "<br />\n", $address);
  208. $replacements[$original] = $address;
  209. break;
  210. case 'shipping-phone':
  211. $replacements[$original] = check_plain($order->delivery_phone);
  212. break;
  213. case 'billing-address':
  214. $address = uc_order_address($order, 'billing');
  215. $address = str_replace(array('<br />', '<BR />'), "<br />\n", $address);
  216. $replacements[$original] = $address;
  217. break;
  218. case 'billing-phone':
  219. $replacements[$original] = check_plain($order->billing_phone);
  220. break;
  221. case 'first-name':
  222. if (variable_get('uc_customer_list_address', 'billing') == 'shipping') {
  223. $replacements[$original] = $sanitize ? check_plain($order->delivery_first_name) : $order->delivery_first_name;
  224. }
  225. else {
  226. $replacements[$original] = $sanitize ? check_plain($order->billing_first_name) : $order->billing_first_name;
  227. }
  228. break;
  229. case 'last-name':
  230. if (variable_get('uc_customer_list_address', 'billing') == 'shipping') {
  231. $replacements[$original] = $sanitize ? check_plain($order->delivery_last_name) : $order->delivery_last_name;
  232. }
  233. else {
  234. $replacements[$original] = $sanitize ? check_plain($order->billing_last_name) : $order->billing_last_name;
  235. }
  236. break;
  237. case 'comments':
  238. $result = db_query_range("SELECT message FROM {uc_order_comments} WHERE order_id = :order_id AND uid = :uid ORDER BY created DESC", 0, 1, array(':order_id' => $order->order_id, ':uid' => 0))->fetchField();
  239. if ($sanitize) {
  240. $result = check_plain($result);
  241. }
  242. $replacements[$original] = empty($result) ? t('<i>No comments left.</i>') : $result;
  243. break;
  244. case 'last-comment':
  245. $result = db_query_range("SELECT message FROM {uc_order_comments} WHERE order_id = :order_id AND uid > :uid ORDER BY created DESC", 0, 1, array(':order_id' => $order->order_id, ':uid' => 0))->fetchField();
  246. if ($sanitize) {
  247. $result = check_plain($result);
  248. }
  249. $replacements[$original] = empty($result) ? t('<i>No comment found.</i>') : $result;
  250. break;
  251. case 'order-status':
  252. $replacements[$original] = uc_order_status_data($order->order_status, 'title');
  253. break;
  254. case 'customer':
  255. $replacements[$original] = $order->uid;
  256. break;
  257. case 'created':
  258. $replacements[$original] = format_date($order->created, 'short');
  259. break;
  260. case 'modified':
  261. $replacements[$original] = format_date($order->modified, 'short');
  262. break;
  263. case 'products':
  264. $products = array();
  265. foreach ($order->products as $product) {
  266. $products[] = theme('uc_qty', array('qty' => $product->qty)) . ' ' . $product->title;
  267. }
  268. if ($sanitize) {
  269. $products = array_map('check_plain', $products);
  270. }
  271. $replacements[$original] = implode("<br />\n", $products);
  272. break;
  273. }
  274. }
  275. // Handles chaining for tokens that have 'type' defined in hook_token_info()
  276. if ($link_tokens = token_find_with_prefix($tokens, 'url')) {
  277. $replacements += token_generate('url', $link_tokens, array('path' => $path), $options);
  278. }
  279. if ($link_tokens = token_find_with_prefix($tokens, 'admin-url')) {
  280. $replacements += token_generate('url', $link_tokens, array('path' => $admin_path), $options);
  281. }
  282. if ($customer_tokens = token_find_with_prefix($tokens, 'customer')) {
  283. $customer = user_load($order->uid);
  284. $replacements += token_generate('user', $customer_tokens, array('user' => $customer), $options);
  285. }
  286. if ($created_tokens = token_find_with_prefix($tokens, 'created')) {
  287. $replacements += token_generate('date', $created_tokens, array('date' => $order->created), $options);
  288. }
  289. if ($changed_tokens = token_find_with_prefix($tokens, 'modified')) {
  290. $replacements += token_generate('date', $changed_tokens, array('date' => $order->modified), $options);
  291. }
  292. }
  293. return $replacements;
  294. }