You are here

function uc_coupon_add_form_submit in Ubercart Discount Coupons 5

Same name and namespace in other branches
  1. 6 uc_coupon.admin.inc \uc_coupon_add_form_submit()
  2. 7.3 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.

File

./uc_coupon.module, line 642
Provides discount coupons for Ubercart.

Code

function uc_coupon_add_form_submit($form_id, $form) {

  // If the coupon was previously used, reset disabled textfields to their original values.
  if ($form['used']) {
    $form['code'] = $form['original_code'];
    $form['bulk_number'] = $form['original_bulk_number'];
  }
  $code = strtoupper($form['code']);
  $valid_from = mktime(0, 0, 0, $form['valid_from']['month'], $form['valid_from']['day'], $form['valid_from']['year']);
  $valid_until = mktime(0, 0, 0, $form['valid_until']['month'], $form['valid_until']['day'], $form['valid_until']['year']);
  $data = array();
  if ($form['bulk_generate']) {
    $data['bulk_number'] = $form['bulk_number'];
    $data['bulk_length'] = $form['bulk_length'];
  }
  if ($form['max_uses_per_user']) {
    $data['max_uses_per_user'] = $form['max_uses_per_user'];
  }
  if ($form['negate_products']) {
    $data['negate_products'] = TRUE;
  }
  if ($form['negate_terms']) {
    $data['negate_terms'] = TRUE;
  }
  foreach ($form['product_types'] as $type) {
    if ($type) {
      $data['product_types'][] = $type;
    }
  }
  foreach ($form['products'] as $key => $product) {
    if ($product && preg_match('/\\[nid:(\\d+)\\]$/', $product, $matches)) {
      $data['products'][] = $matches[1];
    }
  }
  foreach ($form['skus'] as $sku) {
    if ($sku) {
      $data['skus'][] = $sku;
    }
  }
  foreach ($form['terms'] as $key => $term) {
    if ($term && preg_match('/\\[tid:(\\d+)\\]$/', $term, $matches)) {
      $data['terms'][] = $matches[1];
    }
  }
  foreach ($form['users'] as $key => $user) {
    if ($user && preg_match('/\\[uid:(\\d+)\\]$/', $user, $matches)) {
      $data['users'][] = $matches[1];
    }
  }
  foreach ($form['roles'] as $role) {
    if ($role) {
      $data['roles'][] = $role;
    }
  }
  $data['wholesale'] = $form['wholesale'];

  // If the forms coupon id is not set then we try to insert a new coupon
  if (!isset($form['cid'])) {

    // Only set bulk coupon seed once.
    db_query("INSERT INTO {uc_coupons} (name, code, value, type, status, valid_from, valid_until, max_uses, minimum_order, data, bulk, bulk_seed) VALUES ('%s', '%s', %f, '%s', %d, %d, %d, %d, %f, '%s', %d, '%s')", $form['name'], $code, $form['value'], $form['type'], $form['status'], $valid_from, $valid_until, $form['max_uses'], $form['minimum_order'], serialize($data), $form['bulk_generate'], md5(uniqid()));
    drupal_set_message(t('Coupon %name has been created.', array(
      '%name' => $form['name'],
    )));
  }
  else {
    db_query("UPDATE {uc_coupons} SET name = '%s', code = '%s', value = %f, type = '%s', status = %d, valid_from = %d, valid_until = %d, max_uses = %d, minimum_order = %f, data = '%s', bulk = %d WHERE cid = %d", $form['name'], $code, $form['value'], $form['type'], $form['status'], $valid_from, $valid_until, $form['max_uses'], $form['minimum_order'], serialize($data), $form['bulk_generate'], $form['cid']);
    drupal_set_message(t('Coupon %name has been updated.', array(
      '%name' => $form['name'],
    )));
  }
  drupal_goto('admin/store/customers/coupon' . ($form['status'] ? '' : '/inactive'));
}