You are here

function uc_coupon_add_form in Ubercart Discount Coupons 5

Same name and namespace in other branches
  1. 6 uc_coupon.admin.inc \uc_coupon_add_form()
  2. 7.3 uc_coupon.admin.inc \uc_coupon_add_form()
  3. 7.2 uc_coupon.admin.inc \uc_coupon_add_form()

Form builder for product attributes.

Parameters

$action string: Form action, edit or add. 'edit' loads default values.

$cid int: Coupon ID, used to load defaults when $action = 'edit'

1 string reference to 'uc_coupon_add_form'
uc_coupon_menu in ./uc_coupon.module
Implementation of hook_menu().

File

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

Code

function uc_coupon_add_form($action, $cid = NULL) {
  _uc_coupon_paypal_check();
  if ($action == 'edit') {
    $value = uc_coupon_load($cid);
    $used = db_result(db_query("SELECT COUNT(*) FROM {uc_coupons_orders} WHERE cid = %d", $cid));
    $form['cid'] = array(
      '#type' => 'value',
      '#value' => $value->cid,
    );
    $form['original_code'] = array(
      '#type' => 'value',
      '#value' => $value->code,
    );
    $form['original_bulk_number'] = array(
      '#type' => 'value',
      '#value' => $value->data['bulk_number'],
    );
    $form['used'] = array(
      '#type' => 'value',
      '#value' => $used,
    );
  }
  else {
    $value->valid_from = time();
    $value->valid_until = time();
    $value->minimum_order = 0;
    $value->max_uses = 0;
    $used = 0;
  }
  $value->valid_from = array(
    'year' => format_date($value->valid_from, 'custom', 'Y'),
    'month' => format_date($value->valid_from, 'custom', 'n'),
    'day' => format_date($value->valid_from, 'custom', 'j'),
  );
  $value->valid_until = array(
    'year' => format_date($value->valid_until, 'custom', 'Y'),
    'month' => format_date($value->valid_until, 'custom', 'n'),
    'day' => format_date($value->valid_until, 'custom', 'j'),
  );
  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Coupon name'),
    '#default_value' => $value->name,
    '#required' => TRUE,
  );
  $form['code'] = array(
    '#type' => 'textfield',
    '#title' => t('Coupon code'),
    '#description' => t('Coupon codes cannot be changed once they have been used in an order.'),
    '#default_value' => $value->code,
    '#size' => 25,
    '#required' => !$used,
    '#maxlength' => 14,
    '#disabled' => $used,
  );
  $form['bulk'] = array(
    '#type' => 'fieldset',
    '#title' => t('Bulk coupon codes'),
    '#description' => t('The coupon code entered above will be used to prefix each generated code.'),
    '#collapsible' => TRUE,
    '#collapsed' => !$value->bulk,
  );
  if (!$used) {
    $form['bulk']['bulk_generate'] = array(
      '#type' => 'checkbox',
      '#title' => t('Enable bulk generation of coupon codes.'),
      '#default_value' => $value->bulk,
      '#disabled' => $used,
    );
  }
  else {
    $form['bulk']['bulk_generate'] = array(
      '#type' => 'value',
      '#default_value' => $value->bulk,
    );
  }
  $form['bulk']['bulk_number'] = array(
    '#type' => 'textfield',
    '#title' => t('Number of codes to generate'),
    '#default_value' => $value->data['bulk_number'],
    '#size' => 10,
    '#maxlength' => 10,
    '#disabled' => $used,
  );
  $form['bulk']['bulk_length'] = array(
    '#type' => 'select',
    '#title' => t('Code length'),
    '#description' => t('The number of characters selected here will be appended to the coupon code entered above..'),
    '#default_value' => $value->data['bulk_length'],
    '#options' => drupal_map_assoc(range(8, 30)),
    '#disabled' => $used,
  );
  $form['valid_from'] = array(
    '#type' => 'date',
    '#title' => t('Start date'),
    '#default_value' => $value->valid_from,
    '#required' => TRUE,
    '#after_build' => array(
      '_uc_coupon_date_range',
    ),
  );
  $form['valid_until'] = array(
    '#type' => 'date',
    '#title' => t('Expiry date'),
    '#default_value' => $value->valid_until,
    '#required' => TRUE,
    '#after_build' => array(
      '_uc_coupon_date_range',
    ),
  );
  $form['status'] = array(
    '#type' => 'checkbox',
    '#title' => t('Active'),
    '#description' => t('Check to enable the coupon, uncheck to disable the coupon.'),
    '#default_value' => $value->status,
  );
  $form['type'] = array(
    '#type' => 'select',
    '#title' => t('Discount type'),
    '#default_value' => $value->type,
    '#options' => array(
      'percentage' => 'Percentage',
      'price' => 'Price',
    ),
  );
  $form['value'] = array(
    '#type' => 'textfield',
    '#title' => t('Discount value'),
    '#default_value' => $value->value,
    '#size' => 10,
    '#description' => t('Enter values without symbols, for 15%, enter "15" and choose Percentage as the discount type.'),
    '#required' => TRUE,
  );
  $form['minimum_order'] = array(
    '#type' => 'textfield',
    '#title' => t('Minimum order total'),
    '#default_value' => $value->minimum_order,
    '#size' => 10,
    '#description' => t('A minimum order total that applies to the coupon, or 0 for no minimum order limit.'),
    '#required' => TRUE,
    '#field_prefix' => variable_get('uc_sign_after_amount', FALSE) ? '' : variable_get('uc_currency_sign', '$'),
    '#field_suffix' => variable_get('uc_sign_after_amount', FALSE) ? variable_get('uc_currency_sign', '$') : '',
  );
  $form['max_uses'] = array(
    '#type' => 'textfield',
    '#title' => t('Maximum number of redemptions (per code)'),
    '#default_value' => $value->max_uses,
    '#description' => t('Enter the maximum number of times each code for this coupon can be used, or 0 for unlimited.'),
    '#size' => 5,
    '#required' => TRUE,
  );
  $form['max_uses_per_user'] = array(
    '#type' => 'textfield',
    '#title' => t('Maximum number of redemptions (per user)'),
    '#default_value' => isset($value->data['max_uses_per_user']) ? $value->data['max_uses_per_user'] : 0,
    '#description' => t('Enter the maximum number of times this coupon can be used by a single user, or 0 for unlimited.'),
    '#size' => 5,
    '#required' => TRUE,
  );
  $options = array(
    '' => '(none)',
  );
  foreach (module_invoke_all('product_types') as $type) {
    $options[$type] = $type;
  }
  $form['product_types'] = array(
    '#type' => 'select',
    '#title' => t('Product classes'),
    '#description' => t('Selecting one or more product classes will restrict this coupon to matching products only. Discounts will then apply to each matching product.'),
    '#options' => $options,
    '#default_value' => $value->data['product_types'],
    '#multiple' => TRUE,
  );
  $form['products'] = array(
    '#type' => 'fieldset',
    '#title' => t('Applicable products'),
    '#description' => t('Enter one or more products below to restrict this coupon to a set of products, regardless of any product attributes. Discounts will apply to each matching product.'),
    '#tree' => TRUE,
    '#collapsible' => TRUE,
    '#collapsed' => !isset($value->data['products']),
  );
  $form['products']['negate_products'] = array(
    '#type' => 'radios',
    '#default_value' => isset($value->data['negate_products']) ? 1 : 0,
    '#options' => array(
      0 => t('Apply coupon to products listed below.'),
      1 => t('Apply coupon to all products except those listed below.'),
    ),
    '#tree' => FALSE,
  );
  if (isset($value->data['products'])) {
    foreach ($value->data['products'] as $nid) {
      $title = db_result(db_query('SELECT title FROM {node} WHERE nid = %d', $nid));
      $form['products'][] = array(
        '#type' => 'textfield',
        '#default_value' => $title . ' [nid:' . $nid . ']',
        '#autocomplete_path' => 'uc_coupon/autocomplete/node',
      );
    }
  }
  for ($i = 0; $i < 3; $i++) {
    $form['products'][] = array(
      '#type' => 'textfield',
      '#autocomplete_path' => 'uc_coupon/autocomplete/node',
    );
  }
  $form['skus'] = array(
    '#type' => 'fieldset',
    '#title' => t('Applicable SKUs'),
    '#description' => t('Enter one or more SKUs below to restrict this coupon to a set of SKUs, allowing coupons to apply to specific products or attribute options. Discounts will apply to matching SKU.'),
    '#tree' => TRUE,
    '#collapsible' => TRUE,
    '#collapsed' => !isset($value->data['skus']),
  );
  if (isset($value->data['skus'])) {
    foreach ($value->data['skus'] as $sku) {
      $form['skus'][] = array(
        '#type' => 'textfield',
        '#default_value' => $sku,
      );
    }
  }
  for ($i = 0; $i < 3; $i++) {
    $form['skus'][] = array(
      '#type' => 'textfield',
    );
  }
  $form['terms'] = array(
    '#type' => 'fieldset',
    '#title' => t('Applicable taxonomy terms'),
    '#description' => t('Enter one or more taxonomy terms (categories) below to restrict this coupon to a set of products. Discounts will apply to all matching products with these terms.'),
    '#tree' => TRUE,
    '#collapsible' => TRUE,
    '#collapsed' => !isset($value->data['terms']),
  );
  $form['terms']['negate_terms'] = array(
    '#type' => 'radios',
    '#default_value' => isset($value->data['negate_terms']) ? 1 : 0,
    '#options' => array(
      0 => t('Apply coupon to products with terms listed below.'),
      1 => t('Apply coupon to all products except those with terms listed below.'),
    ),
    '#tree' => FALSE,
  );
  if (isset($value->data['terms'])) {
    foreach ($value->data['terms'] as $tid) {
      $name = db_result(db_query('SELECT name FROM {term_data} WHERE tid = %d', $tid));
      $form['terms'][] = array(
        '#type' => 'textfield',
        '#default_value' => $name . ' [tid:' . $tid . ']',
        '#autocomplete_path' => 'uc_coupon/autocomplete/term',
      );
    }
  }
  for ($i = 0; $i < 3; $i++) {
    $form['terms'][] = array(
      '#type' => 'textfield',
      '#autocomplete_path' => 'uc_coupon/autocomplete/term',
    );
  }
  $form['users'] = array(
    '#type' => 'fieldset',
    '#title' => t('User restrictions'),
    '#description' => t('Enter one or more user names and/or "anonymous users" below to make this coupon valid only for those users.'),
    '#tree' => TRUE,
    '#collapsible' => TRUE,
    '#collapsed' => !isset($value->data['users']),
  );
  if (isset($value->data['users'])) {
    foreach ($value->data['users'] as $uid) {
      $username = $uid ? db_result(db_query('SELECT name FROM {users} WHERE uid = %d', $uid)) : t('anonymous users');
      $form['users'][] = array(
        '#type' => 'textfield',
        '#default_value' => $username . ' [uid:' . $uid . ']',
        '#autocomplete_path' => 'uc_coupon/autocomplete/user',
      );
    }
  }
  for ($i = 0; $i < 3; $i++) {
    $form['users'][] = array(
      '#type' => 'textfield',
      '#autocomplete_path' => 'uc_coupon/autocomplete/user',
    );
  }
  $form['roles'] = array(
    '#type' => 'fieldset',
    '#title' => t('Role restrictions'),
    '#description' => t('Enter one or more role names below to make this coupon valid only for users with those roles.'),
    '#tree' => TRUE,
    '#collapsible' => TRUE,
    '#collapsed' => !isset($value->data['roles']),
  );
  if (isset($value->data['roles'])) {
    foreach ($value->data['roles'] as $role) {
      $form['roles'][] = array(
        '#type' => 'textfield',
        '#default_value' => $role,
        '#autocomplete_path' => 'uc_coupon/autocomplete/role',
      );
    }
  }
  for ($i = 0; $i < 3; $i++) {
    $form['roles'][] = array(
      '#type' => 'textfield',
      '#autocomplete_path' => 'uc_coupon/autocomplete/role',
    );
  }
  $form['wholesale'] = array(
    '#type' => 'radios',
    '#title' => 'Wholesale permissions',
    '#description' => t('Select the groups who are able to use this coupon. This option is deprecated, it is recommended that you leave this option as "Both wholesale and retail" use the role selection above instead.'),
    '#default_value' => isset($value->data['wholesale']) ? $value->data['wholesale'] : 1,
    '#options' => array(
      '1' => 'Both wholesale and retail',
      '2' => 'Wholesale buyers only',
      '3' => 'Retail buyers only',
    ),
    '#required' => TRUE,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );
  return $form;
}