You are here

function uc_coupon_add_form_submit in Ubercart Discount Coupons 7.3

Same name and namespace in other branches
  1. 5 uc_coupon.module \uc_coupon_add_form_submit()
  2. 6 uc_coupon.admin.inc \uc_coupon_add_form_submit()
  3. 7.2 uc_coupon.admin.inc \uc_coupon_add_form_submit()

Coupon form submit handler.

1 string reference to 'uc_coupon_add_form_submit'
uc_coupon_add_form in ./uc_coupon.admin.inc
Coupon add/edit form.

File

./uc_coupon.admin.inc, line 906
Discount Coupons administration pages.

Code

function uc_coupon_add_form_submit($form, &$form_state) {

  // If the coupon was previously used, reset disabled textfields to their original values.
  if (isset($form['#uc_coupon_used']) && $form['#uc_coupon_used'] != 0) {
    $form_state['values']['code'] = $form['#uc_coupon']->code;
  }

  // Ensure all fields of original coupon object propagate to presave hook.
  $coupon = isset($form['#uc_coupon']) ? $form['#uc_coupon'] : new stdClass();
  if (isset($form['#uc_coupon_cid'])) {
    $coupon->cid = $form['#uc_coupon_cid'];
  }

  // Set basic coupon information.
  $coupon->name = $form_state['values']['name'];
  $coupon->code = strtoupper($form_state['values']['code']);
  $coupon->bulk = $form_state['values']['bulk_generate'];
  $coupon->data['bulk_number'] = $form_state['values']['bulk_generate'] ? $form_state['values']['bulk_number'] : 0;
  $coupon->data['bulk_length'] = $form_state['values']['bulk_length'];
  if ($form_state['values']['use_validity']) {
    $coupon->valid_from = _uc_coupon_mktime($form_state['values']['valid_from']['month'], $form_state['values']['valid_from']['day'], $form_state['values']['valid_from']['year']);
    $coupon->valid_until = _uc_coupon_mktime($form_state['values']['valid_until']['month'], $form_state['values']['valid_until']['day'], $form_state['values']['valid_until']['year']);
  }
  else {
    $coupon->valid_from = $coupon->valid_until = 0;
  }
  $coupon->status = $form_state['values']['status'];
  if (substr(trim($form_state['values']['discount']), -1) == '%') {
    $coupon->type = 'percentage';
  }
  elseif (substr(trim($form_state['values']['discount']), 0, 1) == '=') {
    $coupon->type = 'set_price';
  }
  elseif (!empty($form_state['values']['store_credit'])) {
    $coupon->type = 'credit';
  }
  else {
    $coupon->type = 'price';
  }
  $coupon->value = str_replace(array(
    '%',
    '=',
  ), '', $form_state['values']['discount']);
  $coupon->minimum_order = $form_state['values']['minimum_order'] ? $form_state['values']['minimum_order'] : 0;
  $coupon->data['minimum_qty'] = $form_state['values']['minimum_qty'];
  $coupon->data['minimum_qty_restrict'] = $form_state['values']['minimum_qty_restrict'];
  $coupon->data['require_match_all'] = $form_state['values']['require_match_all'];
  $coupon->max_uses = $form_state['values']['max_uses'] ? $form_state['values']['max_uses'] : 0;
  $coupon->data['max_uses_per_user'] = $form_state['values']['max_uses_per_user'];
  $coupon->data['apply_to'] = $form_state['values']['apply_to'];
  if ($form_state['values']['apply_to'] == 'cheapest' || $form_state['values']['apply_to'] == 'expensive') {
    $coupon->data['apply_count'] = $form_state['values']['apply_count'];
  }

  // Map restrictions back to their IDs.
  array_walk($form_state['values']['products'], '_uc_coupon_map_restriction', 'nid');
  array_walk($form_state['values']['terms'], '_uc_coupon_map_restriction', 'tid');
  array_walk($form_state['values']['users'], '_uc_coupon_map_restriction', 'uid');

  // Set coupon restrictions.
  $coupon->data['line_items'] = drupal_map_assoc(array_filter($form_state['values']['line_items']));
  $coupon->data['product_types'] = drupal_map_assoc(array_filter($form_state['values']['product_types']));
  $coupon->data['negate_products'] = $form_state['values']['negate_products'];
  $coupon->data['products'] = drupal_map_assoc(array_filter($form_state['values']['products']));
  $coupon->data['skus'] = drupal_map_assoc(array_filter($form_state['values']['skus']));
  $coupon->data['negate_terms'] = $form_state['values']['negate_terms'];
  $coupon->data['terms'] = drupal_map_assoc(array_filter($form_state['values']['terms']));
  $coupon->data['negate_users'] = $form_state['values']['negate_users'];
  $coupon->data['users'] = drupal_map_assoc(array_filter($form_state['values']['users'], 'is_numeric'));
  $coupon->data['negate_roles'] = $form_state['values']['negate_roles'];
  $coupon->data['roles'] = drupal_map_assoc(array_filter($form_state['values']['roles']));
  $coupon->data['negate_combinations'] = $form_state['values']['negate_combinations'];
  $coupon->data['combinations'] = drupal_map_assoc(array_filter($form_state['values']['combinations']));

  // Set coupon display info
  $coupon->data['apply_message'] = trim($form_state['values']['apply_message']);
  $coupon->data['line_item_format'] = trim($form_state['values']['line_item_format']);

  // Remove zero values and empty arrays.
  $coupon->data = array_filter($coupon->data);

  // Handle AJAX submissions.
  if (!empty($form_state['triggering_element']) && substr($elem = $form_state['triggering_element']['#name'], 0, 3) === 'op_') {
    $form_state['ajax_submission'] = substr($elem, 3);
    $form_state['coupon_data'] = $coupon->data;
    $form_state['rebuild'] = TRUE;
  }
  else {
    field_attach_submit('uc_coupon', $coupon, $form, $form_state);

    // Invoke all specified builders for copying form values to entity properties.
    // !TODO - Make full use of the EntityUIController class for uc_coupon
    if (isset($form['#entity_builders'])) {
      foreach ($form['#entity_builders'] as $function) {
        $function('uc_coupon', $coupon, $form, $form_state);
      }
    }
    uc_coupon_save($coupon, $form_state['values']);
    drupal_set_message(t('Coupon %name has been saved.', array(
      '%name' => $coupon->name,
    )));
    $form_state['redirect'] = 'admin/store/coupons' . ($coupon->status ? '' : '/inactive');
  }
}