You are here

function commerce_coupon_ui_coupon_type_form_submit in Commerce Coupon 7

Form API submit callback for the type form.

1 string reference to 'commerce_coupon_ui_coupon_type_form_submit'
commerce_coupon_ui_type_form in includes/commerce_coupon_ui.forms.inc
Generates the commerce coupon type editing form.

File

includes/commerce_coupon_ui.forms.inc, line 251
Commerce Coupon editing UI.

Code

function commerce_coupon_ui_coupon_type_form_submit(&$form, &$form_state) {
  $coupon_type = $form_state['coupon_type'];
  $updated = !empty($coupon_type->type);

  // If a type is set, we should still check to see if a row for the type exists
  // in the database; this is done to accomodate types defined by Features.
  if ($updated) {
    $updated = db_query('SELECT 1 FROM {commerce_coupon_type} WHERE type = :type', array(
      ':type' => $coupon_type->type,
    ))
      ->fetchField();
  }
  if (isset($form_state['values']['reset']) && $form_state['values']['reset']) {
    $reset = TRUE;
  }
  else {
    $reset = FALSE;
  }
  foreach ($form_state['values']['coupon_type'] as $key => $value) {
    $coupon_type->{$key} = $value;
  }
  $coupon_type = commerce_coupon_type_save($coupon_type, $reset);

  // Redirect based on the button clicked.
  drupal_set_message(t('Coupon type saved.'));
  if ($form_state['triggering_element']['#parents'][0] == 'save_continue') {
    $form_state['redirect'] = 'admin/commerce/coupons/types/' . strtr($coupon_type->type, '_', '-') . '/fields';
  }
  else {
    $form_state['redirect'] = 'admin/commerce/coupons/types';
  }
}