uc_coupon_workflow.module in Ubercart Discount Coupons 7.2
Same filename and directory in other branches
Discount coupon workflow.
Provides rules-based workflow enhancements to uc_coupon.
File
uc_coupon_workflow/uc_coupon_workflow.moduleView source
<?php
/**
* @file
* Discount coupon workflow.
*
* Provides rules-based workflow enhancements to uc_coupon.
*/
/**
* Implements hook_permission().
*/
function uc_coupon_workflow_permission() {
$perms = array(
'suspend coupon workflow' => array(
'title' => t('suspend coupon workflow'),
'description' => t('Prevent configured coupon workflow rules from being executed.'),
),
);
return $perms;
}
/**
* Implements hook_uc_coupon_revalidate().
* @param $products
* The list of products to valiate against (usually the current cart contents).
*/
function uc_coupon_workflow_uc_coupon_revalidate($products) {
if (isset($_SESSION['cart_order']) && ($order = uc_order_load($_SESSION['cart_order']))) {
// If checkout is in progress, use the cart_order to provide additional information.
$order = clone $order;
}
else {
// Otherwise create an empty order
$order = new UcOrder();
}
$order->products = $products;
rules_invoke_event('uc_coupon_workflow_automatic', $order);
}
/**
* Implements hook_uc_coupon_apply().
*/
function uc_coupon_workflow_uc_coupon_apply($coupon) {
rules_invoke_event('uc_coupon_workflow_applied', $coupon);
}
/**
* Implements hook_uc_coupon_remove().
*/
function uc_coupon_workflow_uc_coupon_remove($coupon) {
rules_invoke_event('uc_coupon_workflow_removed', $coupon);
}
/**
* Implements hook_form_user_register_form_alter().
*
* Gives administrators the option of preventing any coupon workflow rules from being executed
* when a new user is created.
*/
function uc_coupon_workflow_form_user_register_form_alter(&$form, &$form_state) {
$element = uc_coupon_workflow_suspend_element('user_insert');
if (isset($element)) {
// Decide which part of the form to modify.
if ($form['account']) {
$use_form =& $form['account'];
}
else {
$use_form =& $form;
}
$use_form['uc_coupon_workflow'] = $element;
}
}
/**
* Implements hook_user_insert().
*
* If the creator of this user has suspended coupon workflow, then set the static variable accordingly.
*/
function uc_coupon_workflow_user_insert(&$edit, $account, $category) {
if (user_access('suspend coupon workflow') && isset($edit['uc_coupon_workflow_suspended']) && $edit['uc_coupon_workflow_suspended'] == TRUE) {
$var =& drupal_static('uc_coupon_workflow_suspended');
$var = TRUE;
}
else {
drupal_static_reset('uc_coupon_workflow_suspended');
}
}
/**
* Builds a form element allowing an administrator to suspend coupon workflow.
* @param $events
* An array of event names. If specified, the listed rules will be limited to these events.
*/
function uc_coupon_workflow_suspend_element($events = FALSE) {
if ($events && !is_array($events)) {
$events = array(
$events,
);
}
if (user_access('suspend coupon workflow')) {
// Examine all rules to see if there are any which depend on our condition,
// so we can list them for the administrator.
$found = array();
$rules = rules_config_load_multiple(FALSE);
foreach ($rules as $name => $rule) {
if ($rule instanceof RulesReactionRule && $rule->active) {
foreach ($rule
->conditions() as $condition) {
if (method_exists($condition, 'getElementName') && $condition
->getElementName() == 'uc_coupon_workflow_suspended' && (!$events || count(array_intersect($rule
->events(), $events)))) {
$found[] = $rule->label;
}
}
}
}
// If we've found some applicable rules, then list them and offer to suspend.
if (count($found)) {
$element = array(
'#type' => 'fieldset',
'#title' => t('Discount coupon workflow'),
'#description' => t('Some automatic discount coupon actions are configured to be executed when this form is submitted.
You can suspend these actions by checking the box below.'),
);
$element['actions'] = array(
'#type' => 'item',
'#title' => t('The following actions are enabled:'),
'list' => array(
'#theme' => 'item_list',
'#items' => $found,
'#type' => 'ul',
),
);
$element['uc_coupon_workflow_suspended'] = array(
'#type' => 'checkbox',
'#title' => t('Prevent these actions from being executed this time.'),
);
return $element;
}
}
}
Functions
Name | Description |
---|---|
uc_coupon_workflow_form_user_register_form_alter | Implements hook_form_user_register_form_alter(). |
uc_coupon_workflow_permission | Implements hook_permission(). |
uc_coupon_workflow_suspend_element | Builds a form element allowing an administrator to suspend coupon workflow. |
uc_coupon_workflow_uc_coupon_apply | Implements hook_uc_coupon_apply(). |
uc_coupon_workflow_uc_coupon_remove | Implements hook_uc_coupon_remove(). |
uc_coupon_workflow_uc_coupon_revalidate | Implements hook_uc_coupon_revalidate(). |
uc_coupon_workflow_user_insert | Implements hook_user_insert(). |