You are here

function uc_coupon_form_submit in Ubercart Discount Coupons 7.2

Same name and namespace in other branches
  1. 7.3 uc_coupon.module \uc_coupon_form_submit()

Submit handler for the uc_coupon form.

1 call to uc_coupon_form_submit()
uc_checkout_pane_coupon in ./uc_coupon.module
Checkout Pane callback function.

File

./uc_coupon.module, line 1509

Code

function uc_coupon_form_submit($form, $form_state) {
  $trigger = $form_state['triggering_element']['#name'];

  // Determine where the values are (they will be in the pane subarray if called from checkout page).
  $values = isset($form_state['values']['panes']['coupon']) ? $form_state['values']['panes']['coupon'] : $form_state['values'];

  // If this was the result of a 'remove' submission.
  if (substr($trigger, 0, 16) == 'uc-coupon-remove') {

    // See if there was an individual remove button clicked.
    $code = substr($trigger, 17);
    if (!empty($code)) {
      uc_coupon_session_clear($code);
      drupal_set_message(t('Coupon "%code" has been removed from your order', array(
        '%code' => $code,
      )));
      module_invoke_all('uc_coupon_remove', uc_coupon_find($code));
    }
    elseif (isset($values['coupons'])) {
      $removed = array();
      foreach ($values['coupons'] as $code => $selected) {
        if (!$selected) {
          $removed[] = $code;
          uc_coupon_session_clear($code);
          module_invoke_all('uc_coupon_remove', uc_coupon_find($code));
        }
      }
      $n = count($removed);
      if ($n > 1) {
        $last = $removed[$n - 1];
        $rest = implode(', ', array_slice($removed, 0, $n - 1));
        drupal_set_message(t('Coupons %rest and %last have been removed from your order.', array(
          '%rest' => $rest,
          '%last' => $last,
        )));
      }
      elseif (!empty($removed)) {
        drupal_set_message(t('Coupon %code has been removed from your order', array(
          '%code' => $removed[0],
        )));
      }
    }
  }
  else {
    $code = strtoupper(trim($values['code']));
    $removed = FALSE;

    // If multiple codes are not enabled, then remove any codes currently applied.
    if (!variable_get('uc_coupon_allow_multiple', FALSE) && count($session = uc_coupon_session_get()) > 0) {
      foreach (array_keys($session) as $remove_code) {
        uc_coupon_session_clear($remove_code);
        drupal_set_message(t('Coupon "%code" has been removed from your order', array(
          '%code' => $remove_code,
        )));
        module_invoke_all('uc_coupon_remove', uc_coupon_find($remove_code));
      }
      $removed = TRUE;
    }
    if (!empty($code)) {
      uc_coupon_session_add($code, 'submit');
    }
    elseif (!$removed) {
      drupal_set_message(t("You must enter a valid coupon code."), 'error');
    }
  }
}