You are here

function uc_coupon_form in Ubercart Discount Coupons 7.3

Same name and namespace in other branches
  1. 7.2 uc_coupon.module \uc_coupon_form()

Form builder for the uc_coupon form.

Parameters

$context: Where the form is to appear: 'cart', 'block' or 'checkout'

$submit: An array of additional options to attach to the form's submit elements (e.g. #ajax, #submit)

2 calls to uc_coupon_form()
uc_checkout_pane_coupon in ./uc_coupon.module
Checkout Pane callback function.
uc_order_pane_coupon in ./uc_coupon.module
Coupon order pane callback.
4 string references to 'uc_coupon_form'
uc_checkout_pane_coupon in ./uc_coupon.module
Checkout Pane callback function.
uc_coupon_block_view in ./uc_coupon.module
Implements hook_block_view().
uc_coupon_uc_cart_pane in ./uc_coupon.module
Implements hook_uc_cart_pane().
uc_order_pane_coupon in ./uc_coupon.module
Coupon order pane callback.

File

./uc_coupon.module, line 1324
Provides discount codes and gift certificates for Ubercart.

Code

function uc_coupon_form($form, $form_state, $context = 'block', $submit = FALSE) {

  //dpm($form_state['order'], 'order build');
  $coupons = $context == 'order' ? uc_coupon_get_order_coupons($form_state['order']) : uc_coupon_session_validate();

  //dpm($coupons, 'coupons build');
  $components = variable_get('uc_coupon_form_components', drupal_map_assoc(variable_get('uc_coupon_allow_multiple', FALSE) ? array(
    'entry',
  ) : array(
    'entry',
    'list',
  )));

  // Show the coupon code entry component
  if (!empty($components['entry'])) {
    $form['code'] = array(
      '#type' => 'textfield',
      '#size' => 25,
      '#title' => t('Coupon Code'),
      '#description' => t('Enter a coupon code and click "Apply to order" below.'),
    );
    $form['apply'] = array(
      '#type' => 'submit',
      '#value' => t('Apply to order'),
      '#name' => 'uc-coupon-apply',
    );
    if (!variable_get('uc_coupon_allow_multiple', FALSE) && count(uc_coupon_session_get()) > 0) {
      $form['code']['#description'] .= ' ' . t('Apply a blank code to remove the currently applied coupon.');
    }
    drupal_add_css('#uc-coupon-active-coupons, #uc-coupon-other-discounts { clear: left; }', array(
      'type' => 'inline',
      'group' => CSS_DEFAULT,
    ));
    if ($submit) {
      $form['apply'] += $submit;
    }
  }

  // Add active coupons components (table and/or list).
  $options = _uc_coupon_options_list($coupons, $context != 'order');
  if (!empty($options)) {
    if (!empty($components['table'])) {
      $form['coupons_table'] = tapir_get_table('uc_coupon_table', $options, $submit);
    }
    if (!empty($components['list'])) {
      $form['coupons'] = array(
        '#prefix' => '<div id="uc-coupon-active-coupons">',
        '#suffix' => '</div>',
        '#type' => 'checkboxes',
        '#title' => t('Active Coupons'),
        '#options' => $options,
        '#default_value' => array_keys($options),
        '#description' => t('These coupons have been applied to your order. To remove one, uncheck the box and click "Remove coupons" below.'),
      );
      $form['coupons']['#description'] = t('These coupons have been applied to your order. To remove one, uncheck the box next to the coupon name and click "Update order" below.');
      $form['remove'] = array(
        '#type' => 'submit',
        '#value' => t('Update order'),
        '#name' => 'uc-coupon-remove',
      );
      if ($submit) {
        $form['remove'] += $submit;
      }
    }
  }

  // Add context to help out themers.
  $form['#uc_coupon_form_context'] = $context;
  return $form;
}