uc_coupon_workflow.rules.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <?php
  2. /**
  3. * @file
  4. * Rules integration for uc_coupon_workflow
  5. */
  6. /**
  7. * Implements hook_rules_condition_info().
  8. */
  9. /**
  10. * Implements hook_rules_event_info().
  11. *
  12. */
  13. function uc_coupon_workflow_rules_event_info() {
  14. // Event invoked when a valid coupon is applied.
  15. $events['uc_coupon_workflow_applied'] = array(
  16. 'label' => t('A valid discount coupon is applied'),
  17. 'group' => t('Coupon'),
  18. 'variables' => array(
  19. 'coupon' => array(
  20. 'type' => 'uc_coupon',
  21. 'label' => t('Coupon'),
  22. ),
  23. ),
  24. );
  25. // Event invoked when a valid coupon is applied.
  26. $events['uc_coupon_workflow_removed'] = array(
  27. 'label' => t('A valid discount coupon is removed'),
  28. 'group' => t('Coupon'),
  29. 'variables' => array(
  30. 'coupon' => array(
  31. 'type' => 'uc_coupon',
  32. 'label' => t('Coupon'),
  33. ),
  34. ),
  35. );
  36. // Event invoked when checking for automatic coupons.
  37. $events['uc_coupon_workflow_automatic'] = array(
  38. 'label' => t('Check for automatic discounts.'),
  39. 'group' => t('Coupon'),
  40. 'variables' => array(
  41. 'current_order' => array(
  42. 'type' => 'uc_order',
  43. 'label' => t('Current Order'),
  44. ),
  45. ),
  46. );
  47. // Event invoked when a coupon is submitted with an order.
  48. // !TODO This functionality would be better implemented by some sort of conditional
  49. // loop processing. See http://drupal.org/node/821986
  50. $events['uc_coupon_workflow_checkout'] = array(
  51. 'label' => t('A customer checks out with a valid discount coupon'),
  52. 'group' => t('Coupon'),
  53. 'variables' => array(
  54. 'coupon' => array(
  55. 'type' => 'uc_coupon',
  56. 'label' => t('Coupon'),
  57. ),
  58. 'order' => array(
  59. 'type' => 'uc_order',
  60. 'label' => t('Order'),
  61. ),
  62. ),
  63. );
  64. return $events;
  65. }
  66. /**
  67. * Implements hook_rules_condition_info().
  68. */
  69. function uc_coupon_workflow_rules_condition_info() {
  70. $conditions['uc_coupon_workflow_suspended'] = array(
  71. 'label' => t('Check if an administrator has suspended automatic coupon workflow for this request.'),
  72. 'group' => t('Coupon'),
  73. );
  74. return $conditions;
  75. }
  76. /**
  77. * Implements hook_rules_action_info().
  78. */
  79. function uc_coupon_workflow_rules_action_info() {
  80. $actions = array();
  81. // Assign a coupon to a user.
  82. $actions['uc_coupon_workflow_assign'] = array(
  83. 'label' => t('Authorize a user to use a coupon.'),
  84. 'group' => t('Coupon'),
  85. 'provides' => array(
  86. 'coupon' => array(
  87. 'type' => 'uc_coupon',
  88. 'label' => t('Coupon'),
  89. ),
  90. ),
  91. 'parameter' => array(
  92. 'account' => array(
  93. 'type' => 'user',
  94. 'label' => t('Account'),
  95. 'description' => t('Select the user account you wish to authorize.'),
  96. ),
  97. 'cid' => array(
  98. 'type' => 'integer',
  99. 'label' => t('Coupon'),
  100. 'description' => t('Select the base coupon to be assigned.
  101. The new user will be added to the list of authorized users for the selected coupon.'),
  102. 'options list' => 'uc_coupon_workflow_cid_options',
  103. 'restriction' => 'input',
  104. ),
  105. ),
  106. );
  107. // Send an e-mail regarding a coupon.
  108. $actions['uc_coupon_workflow_email'] = array(
  109. 'label' => t('Send an email regarding a coupon.'),
  110. 'group' => t('Coupon'),
  111. 'parameter' => array(
  112. 'coupon' => array(
  113. 'type' => 'uc_coupon',
  114. 'label' => t('Coupon'),
  115. ),
  116. 'from' => array(
  117. 'type' => 'text',
  118. 'label' => t('Sender'),
  119. 'restriction' => 'input',
  120. ),
  121. 'addresses' => array(
  122. 'type' => 'text',
  123. 'label' => t('Recipients'),
  124. 'description' => t('Enter the email addresses to receive the notifications, one per line.'),
  125. 'restriction' => 'input',
  126. ),
  127. 'subject' => array(
  128. 'type' => 'text',
  129. 'label' => t('Subject'),
  130. 'description' => t('Enter the subject line for the notifications.'),
  131. 'restriction' => 'input',
  132. ),
  133. 'message' => array(
  134. 'type' => 'text',
  135. 'label' => t('Message'),
  136. 'description' => t('Enter the message body for the notifications.'),
  137. 'restriction' => 'input',
  138. ),
  139. 'format' => array(
  140. 'type' => 'text',
  141. 'label' => t('Message format'),
  142. 'options list' => 'uc_coupon_workflow_message_formats',
  143. ),
  144. ),
  145. );
  146. // Apply an automatic coupon.
  147. $actions['uc_coupon_workflow_apply'] = array(
  148. 'label' => t('Apply a coupon to the current order.'),
  149. 'group' => t('Coupon'),
  150. 'provides' => array(
  151. 'coupon' => array(
  152. 'type' => 'uc_coupon',
  153. 'label' => t('Coupon'),
  154. ),
  155. ),
  156. 'parameter' => array(
  157. 'cid' => array(
  158. 'type' => 'integer',
  159. 'label' => t('Coupon'),
  160. 'description' => t('Select the coupon to apply. The coupon will only be
  161. applied if and when all of its restrictions are satisfied.'),
  162. 'options list' => 'uc_coupon_workflow_apply_options',
  163. 'restriction' => 'input',
  164. ),
  165. 'mode' => array(
  166. 'type' => 'text',
  167. 'label' => t('Mode'),
  168. 'description' => t('Choose the way this coupon should be applied.'),
  169. 'options list' => 'uc_coupon_workflow_apply_modes',
  170. 'default value' => 'retain',
  171. 'restriction' => 'input',
  172. ),
  173. ),
  174. );
  175. return $actions;
  176. }
  177. /**
  178. * Options list callback for list of coupons to assign.
  179. */
  180. function uc_coupon_workflow_cid_options() {
  181. return db_query('SELECT cid, name FROM {uc_coupons} WHERE status = 1')->fetchAllKeyed();
  182. }
  183. /**
  184. * Options list callback for message formats.
  185. */
  186. function uc_coupon_workflow_message_formats() {
  187. global $user;
  188. $options = array();
  189. $formats = filter_formats($user);
  190. foreach ($formats as $format) {
  191. $options[$format->format] = $format->name;
  192. }
  193. return $options;
  194. }
  195. /**
  196. * For certain events (e.g. new user creation), administrators are provided
  197. * with an interface which allows them to prevent execution of coupon actions
  198. * based on those events.
  199. */
  200. function uc_coupon_workflow_suspended($settings) {
  201. $auth = drupal_static('uc_coupon_workflow_suspended');
  202. return !empty($auth);
  203. }
  204. /**
  205. * Action callback to assign a coupon to a user.
  206. */
  207. function uc_coupon_workflow_assign($account, $cid) {
  208. $coupon = uc_coupon_load($cid);
  209. if ($coupon->cid) {
  210. // Grant access to the coupon.
  211. $coupon->data['users'][$account->uid] = $account->uid;
  212. uc_coupon_save($coupon);
  213. return array('coupon' => $coupon);
  214. }
  215. else {
  216. return array('coupon' => NULL);
  217. }
  218. }
  219. /**
  220. * Action callback to send an e-mail regarding a coupon.
  221. */
  222. function uc_coupon_workflow_email($coupon, $from, $addresses, $subject, $message, $format) {
  223. // Build the e-mail parameters.
  224. $params = array(
  225. 'from' => $from,
  226. 'subject' => $subject,
  227. 'message' => $message,
  228. 'format' => $format,
  229. 'replacements' => array(), // The replacements are already handled by Rules.
  230. );
  231. // Split up our recipient e-mail addresses.
  232. $recipients = array();
  233. foreach (explode("\n", $addresses) as $address) {
  234. $recipients[] = trim($address);
  235. }
  236. // Send the e-mails.
  237. foreach ($recipients as $email) {
  238. $sent = drupal_mail('uc_order', 'action-mail', $email, uc_store_mail_recipient_language($email), $params, $from);
  239. if (!$sent['result']) {
  240. watchdog('uc_coupon', 'Attempt to e-mail @email concerning coupon @cid failed.', array('@email' => $email, '@cid' => $coupon->cid), WATCHDOG_ERROR);
  241. }
  242. }
  243. }
  244. /**
  245. * Action callback to apply a coupon.
  246. * This simply adds the coupon to the session; it is validated later.
  247. */
  248. function uc_coupon_workflow_apply($cid, $mode) {
  249. $coupon = uc_coupon_load($cid);
  250. if ($coupon->cid) {
  251. uc_coupon_session_add($coupon->code, $mode);
  252. return array('coupon' => $coupon);
  253. }
  254. else {
  255. return array('coupon' => NULL);
  256. }
  257. }
  258. function uc_coupon_workflow_apply_help() {
  259. return '<p>' .
  260. t('This action can be used in two ways: to apply a coupon normally in response to a discrete event
  261. (for example, when a particular user logs in), or to apply an automatic discount in response to the
  262. "%event" event, which occurs every time the current cart contents are updated (for example, to
  263. apply a discount if a particular combination of products is ordered).',
  264. array('%event' => t('Check for automatic discounts'))) .
  265. '</p><p>' .
  266. t('Automatic discounts are treated differently than normal coupons: they are listed in separate panes
  267. on the checkout and cart pages, and they may not be removed by the customer. Unlike normal coupons,
  268. they are automatically removed if the rule\'s conditions are not met.') .
  269. '</p><p>' .
  270. t('Note that a coupon specified in this action will only apply to an order if its restrictions
  271. (as specified on the coupon add/edit page) are met. That is, both the conditions of this rule
  272. and the coupon\'s restrictions must be satisfied. Note also that normal coupons always take
  273. precedence over automatic discounts, so if a customer enters a code for a coupon which has not
  274. been configured combine with a particular automatic discount, that automatic discount will
  275. be pre-empted.') .
  276. '</p>';
  277. }
  278. /**
  279. * Generate list of coupon options for auto apply action.
  280. */
  281. function uc_coupon_workflow_apply_options() {
  282. // Only non-bulk coupons may be applied automatically to avoid ambiguity regarding which code to apply.
  283. $rows = db_query('SELECT cid, name, code FROM {uc_coupons} WHERE status = :status AND bulk = :bulk',
  284. array(':status' => 1, ':bulk' => 0, ));
  285. $ops = array();
  286. foreach ($rows as $row) {
  287. $ops[$row->cid] = t("@name (Code: @code)", array('@name' => $row->name, '@code' => $row->code));
  288. }
  289. return $ops;
  290. }
  291. function uc_coupon_workflow_apply_modes() {
  292. return array(
  293. 'retain' => t('Apply this coupon normally. It will added to the session for the current customer, and when its
  294. restrictions are satisfied, it will be applied exactly as if the code had been submitted manually.'),
  295. 'auto' => t('Apply this coupon as an automatic discount.') . ' <strong>' . t('Only select this option if this rule
  296. reacts to the "%event" event.', array('%event' => t('Check for automatic discounts'))) . '</strong>. ' .
  297. t('Then, this rule will be evaluated whenever the cart contents are updated, and the coupon applied
  298. only if all conditions and restrictions are satisfied.'),
  299. );
  300. }
  301. /**
  302. * Implementatino of 'form_alter' callback for the apply action.
  303. * Change the select list to radio buttons.
  304. */
  305. function uc_coupon_workflow_apply_form_alter(&$form, $form_state) {
  306. $form['parameter']['mode']['settings']['mode']['#type'] = 'radios';
  307. }