<?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 $order
 *   The order to valiate against.
 */
function uc_coupon_workflow_uc_coupon_revalidate($order) {
  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;
    }
  }
}

/**
 * Implements hook uc_checkout_complete().
 */
function uc_coupon_workflow_uc_checkout_complete($order, $account) {
  foreach (uc_coupon_get_order_coupons($order) as $coupon) {
    rules_invoke_event('uc_coupon_workflow_checkout', $coupon, $order);
  }
}


