uc_coupon_workflow.module 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * @file
  4. * Discount coupon workflow.
  5. *
  6. * Provides rules-based workflow enhancements to uc_coupon.
  7. */
  8. /**
  9. * Implements hook_permission().
  10. */
  11. function uc_coupon_workflow_permission() {
  12. $perms = array(
  13. 'suspend coupon workflow' => array(
  14. 'title' => t('suspend coupon workflow'),
  15. 'description' => t('Prevent configured coupon workflow rules from being executed.'),
  16. ),
  17. );
  18. return $perms;
  19. }
  20. /**
  21. * Implements hook_uc_coupon_revalidate().
  22. *
  23. * @param $order
  24. * The order to valiate against.
  25. */
  26. function uc_coupon_workflow_uc_coupon_revalidate($order) {
  27. rules_invoke_event('uc_coupon_workflow_automatic', $order);
  28. }
  29. /**
  30. * Implements hook_uc_coupon_apply().
  31. */
  32. function uc_coupon_workflow_uc_coupon_apply($coupon) {
  33. rules_invoke_event('uc_coupon_workflow_applied', $coupon);
  34. }
  35. /**
  36. * Implements hook_uc_coupon_remove().
  37. */
  38. function uc_coupon_workflow_uc_coupon_remove($coupon) {
  39. rules_invoke_event('uc_coupon_workflow_removed', $coupon);
  40. }
  41. /**
  42. * Implements hook_form_user_register_form_alter().
  43. *
  44. * Gives administrators the option of preventing any coupon workflow rules from being executed
  45. * when a new user is created.
  46. */
  47. function uc_coupon_workflow_form_user_register_form_alter(&$form, &$form_state) {
  48. $element = uc_coupon_workflow_suspend_element('user_insert');
  49. if (isset($element)) {
  50. // Decide which part of the form to modify.
  51. if ($form['account']) {
  52. $use_form = &$form['account'];
  53. }
  54. else {
  55. $use_form = &$form;
  56. }
  57. $use_form['uc_coupon_workflow'] = $element;
  58. }
  59. }
  60. /**
  61. * Implements hook_user_insert().
  62. *
  63. * If the creator of this user has suspended coupon workflow, then set the static variable accordingly.
  64. */
  65. function uc_coupon_workflow_user_insert(&$edit, $account, $category) {
  66. if (user_access('suspend coupon workflow') && isset($edit['uc_coupon_workflow_suspended']) && $edit['uc_coupon_workflow_suspended'] == TRUE) {
  67. $var = &drupal_static('uc_coupon_workflow_suspended');
  68. $var = TRUE;
  69. }
  70. else {
  71. drupal_static_reset('uc_coupon_workflow_suspended');
  72. }
  73. }
  74. /**
  75. * Builds a form element allowing an administrator to suspend coupon workflow.
  76. * @param $events
  77. * An array of event names. If specified, the listed rules will be limited to these events.
  78. */
  79. function uc_coupon_workflow_suspend_element($events = FALSE) {
  80. if ($events && !is_array($events)) {
  81. $events = array($events);
  82. }
  83. if (user_access('suspend coupon workflow')) {
  84. // Examine all rules to see if there are any which depend on our condition,
  85. // so we can list them for the administrator.
  86. $found = array();
  87. $rules = rules_config_load_multiple(FALSE);
  88. foreach ($rules as $name => $rule) {
  89. if ($rule instanceof RulesReactionRule && $rule->active) {
  90. foreach ($rule->conditions() as $condition) {
  91. if (method_exists($condition, 'getElementName') && $condition->getElementName() == 'uc_coupon_workflow_suspended' && (!$events || count(array_intersect($rule->events(), $events)))) {
  92. $found[] = $rule->label;
  93. }
  94. }
  95. }
  96. }
  97. // If we've found some applicable rules, then list them and offer to suspend.
  98. if (count($found)) {
  99. $element = array(
  100. '#type' => 'fieldset',
  101. '#title' => t('Discount coupon workflow'),
  102. '#description' => t('Some automatic discount coupon actions are configured to be executed when this form is submitted.
  103. You can suspend these actions by checking the box below.'),
  104. );
  105. $element['actions'] = array(
  106. '#type' => 'item',
  107. '#title' => t('The following actions are enabled:'),
  108. 'list' => array(
  109. '#theme' => 'item_list',
  110. '#items' => $found,
  111. '#type' => 'ul',
  112. ),
  113. );
  114. $element['uc_coupon_workflow_suspended'] = array(
  115. '#type' => 'checkbox',
  116. '#title' => t('Prevent these actions from being executed this time.'),
  117. );
  118. return $element;
  119. }
  120. }
  121. }
  122. /**
  123. * Implements hook uc_checkout_complete().
  124. */
  125. function uc_coupon_workflow_uc_checkout_complete($order, $account) {
  126. foreach (uc_coupon_get_order_coupons($order) as $coupon) {
  127. rules_invoke_event('uc_coupon_workflow_checkout', $coupon, $order);
  128. }
  129. }