You are here

function uc_discounts_admin_discount_edit_form in Ubercart Discounts (Alternative) 7.2

Generates admin form to create or edit a discount.

See also

uc_discounts_admin_discount_edit_form_submit()

uc_discounts_admin_discount_edit_form_validate()

1 string reference to 'uc_discounts_admin_discount_edit_form'
uc_discounts_menu in uc_discounts/uc_discounts.module
Implements hook_menu().

File

uc_discounts/uc_discounts.admin.inc, line 198
Admin forms and functions for uc_discounts module.

Code

function uc_discounts_admin_discount_edit_form($form, &$form_state, $discount_id = 0) {
  $form = array();
  $form_submitted = $form_state['submitted'];
  $is_edit = $discount_id != 0;
  if (!$form_submitted && $is_edit) {
    $form_state['values'] = (array) uc_discounts_load($discount_id);
  }
  $form['discount_id'] = array(
    '#type' => 'hidden',
    '#value' => isset($form_state['values']['discount_id']) ? $form_state['values']['discount_id'] : '',
  );
  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Name'),
    '#description' => t('Shown in admin reports and logs.'),
    '#default_value' => isset($form_state['values']['name']) ? $form_state['values']['name'] : '',
    '#size' => 50,
    '#required' => TRUE,
  );
  $form['short_description'] = array(
    '#type' => 'textfield',
    '#title' => t('Short Description'),
    '#description' => t('Description displayed to user during checkout and in order review.'),
    '#default_value' => isset($form_state['values']['short_description']) ? $form_state['values']['short_description'] : '',
    '#size' => 75,
    '#required' => TRUE,
  );
  $form['description'] = array(
    '#type' => 'textfield',
    '#title' => t('Description'),
    '#description' => t('Internal description for discount administrators.'),
    '#default_value' => isset($form_state['values']['description']) ? $form_state['values']['description'] : '',
    '#size' => 75,
  );
  $is_active = isset($form_state['values']['is_active']) ? $form_state['values']['is_active'] : FALSE;
  $form['is_active'] = array(
    '#type' => 'checkbox',
    '#title' => t('Is active'),
    '#description' => t('Is this discount active for use on the site?'),
    '#default_value' => $is_active,
  );
  $form['can_be_combined_with_other_discounts'] = array(
    '#type' => 'checkbox',
    '#title' => t('Can be combined with other discounts'),
    '#default_value' => isset($form_state['values']['can_be_combined_with_other_discounts']) ? $form_state['values']['can_be_combined_with_other_discounts'] : FALSE,
  );
  $has_activation = isset($form_state['values']['has_activation']) ? $form_state['values']['has_activation'] : FALSE;
  $form['has_activation'] = array(
    '#type' => 'checkbox',
    '#title' => t('Discount activates'),
    '#description' => t('Whether or not this discount rule will only become active after a given date.'),
    '#default_value' => $has_activation,
  );
  $activates_on = isset($form_state['values']['activates_on']) ? $form_state['values']['activates_on'] : REQUEST_TIME;
  $form['activates_on'] = array(
    '#type' => 'date_popup',
    '#date_type' => 'DATE_UNIX',
    '#title' => t('Discount activation date'),
    '#description' => t('Date and time when the discount will become active.  Note that if the %is_active field is not checked, this will be ignored.', array(
      '%is_active' => t('Is active'),
    )),
    '#default_value' => date('Y-m-d H:i:s', $activates_on),
    '#states' => array(
      'visible' => array(
        'input[name="has_activation"]' => array(
          'checked' => TRUE,
        ),
      ),
      'required' => array(
        'input[name="has_activation"]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );
  $has_expiration = isset($form_state['values']['has_expiration']) ? $form_state['values']['has_expiration'] : FALSE;
  $form['has_expiration'] = array(
    '#type' => 'checkbox',
    '#title' => t('Discount expires'),
    '#description' => t('Whether or not this discount rule will expire on a given date.'),
    '#default_value' => $has_expiration,
  );
  $expiration = isset($form_state['values']['expiration']) ? $form_state['values']['expiration'] : REQUEST_TIME;
  $form['expiration'] = array(
    '#type' => 'date_popup',
    '#date_type' => 'DATE_UNIX',
    '#title' => t('Discount expiration'),
    '#description' => t('Date and time when the discount expires.'),
    '#default_value' => date('Y-m-d H:i:s', $expiration),
    '#states' => array(
      'visible' => array(
        'input[name="has_expiration"]' => array(
          'checked' => TRUE,
        ),
      ),
      'required' => array(
        'input[name="has_expiration"]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );

  // Add custom weight range [-50, 50].
  $options = array();
  for ($i = -50; $i <= 50; $i++) {
    $options[$i] = $i;
  }
  $form['weight'] = array(
    '#type' => 'select',
    '#title' => t('Weight'),
    '#description' => t('Lighter discounts are applied to an order first. This value is unimportant if there are no discounts on discount line items.'),
    '#options' => $options,
    '#default_value' => isset($form_state['values']['weight']) ? $form_state['values']['weight'] : 0,
  );
  $form['qualifications'] = array(
    '#type' => 'fieldset',
    '#title' => t('Conditions of Qualification'),
    '#collapsible' => TRUE,
    '#description' => t('Carts qualify for a discount based upon these conditions.'),
  );
  $form['qualifications']['qualifying_type'] = array(
    '#type' => 'select',
    '#title' => t('Qualification type'),
    '#description' => t('The type of qualification used to determine if a cart qualifies for this discount.'),
    '#options' => uc_discounts_qualifying_type_options(),
    '#default_value' => isset($form_state['values']['qualifying_type']) ? $form_state['values']['qualifying_type'] : '',
  );
  $min_qual_amount_title = t('Minimum qualification amount');
  $form['qualifications']['qualifying_amount'] = array(
    '#type' => 'textfield',
    '#title' => $min_qual_amount_title,
    '#description' => t('The amount of qualification type required. E.g. 50 (for $50), 5 (for 5 items). Remember for a discount like "buy 4 get 1 free" the qualifying amount is "5". For a discount like "spend at least $25, get $5 off" the qualifying amount is "30".'),
    '#default_value' => isset($form_state['values']['qualifying_amount']) ? $form_state['values']['qualifying_amount'] : '',
    '#size' => 15,
    '#required' => TRUE,
  );
  $form['qualifications']['use_only_discounted_products_to_qualify'] = array(
    '#type' => 'checkbox',
    '#title' => t('Use only %discounted_products to determine if %min_qual_amount_title is met.', array(
      '%discounted_products' => t('Discounted Products'),
      '%min_qual_amount_title' => $min_qual_amount_title,
    )),
    '#description' => t('If checked, uses only the products included by the %discounted_products input.  Otherwise it will be based off %required_products or all products in the cart.', array(
      '%discounted_products' => t('Discounted Products'),
      '%required_products' => t('Required Products'),
    )),
    '#default_value' => isset($form_state['values']['use_only_discounted_products_to_qualify']) ? $form_state['values']['use_only_discounted_products_to_qualify'] : FALSE,
  );
  $has_qualifying_amount_max = isset($form_state['values']['has_qualifying_amount_max']) ? $form_state['values']['has_qualifying_amount_max'] : FALSE;
  $form['qualifications']['has_qualifying_amount_max'] = array(
    '#type' => 'checkbox',
    '#title' => t('Has max qualifying amount.'),
    '#description' => t('Whether or not discount has a maximum amount above which the discount will not be applied.'),
    '#default_value' => $has_qualifying_amount_max,
  );
  $form['qualifications']['qualifying_amount_max'] = array(
    '#type' => 'textfield',
    '#title' => t('Maximum qualifying amount'),
    '#description' => t('Maximum amount to NOT exceed to qualify for the discount.  E.g. 50 (for $50), 5 (for 5 items).'),
    '#default_value' => isset($form_state['values']['qualifying_amount_max']) ? $form_state['values']['qualifying_amount_max'] : '',
    '#size' => 15,
    '#states' => array(
      'visible' => array(
        'input[name="has_qualifying_amount_max"]' => array(
          'checked' => TRUE,
        ),
      ),
      'required' => array(
        'input[name="has_qualifying_amount_max"]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );
  $requires_code = isset($form_state['values']['requires_code']) ? $form_state['values']['requires_code'] : FALSE;
  $form['qualifications']['requires_code'] = array(
    '#type' => 'checkbox',
    '#title' => t('Require code to activate discount.'),
    '#default_value' => $requires_code,
  );

  // Determine codes default value.
  if (isset($form_state['values']['codes'])) {
    $codes_string = $form_state['values']['codes'];
  }
  else {
    $codes = array();
    if ($is_edit) {
      $codes = uc_discounts_get_codes_for_discount_id($discount_id);
    }
    $codes_string = uc_discounts_codes_to_str($codes);
  }
  $form['qualifications']['codes'] = array(
    '#type' => 'textarea',
    '#title' => t('Discount codes'),
    '#description' => t('Enter discount codes in box above, one code per line.  Spaces are permitted but may confuse consumers.  You may use the !link if you need to create a lot of unique codes.', array(
      '!link' => l(t('discount code generator'), 'admin/store/uc_discounts/generate_codes/' . $discount_id),
    )),
    '#default_value' => $codes_string,
    '#rows' => 5,
    '#states' => array(
      'visible' => array(
        'input[name="requires_code"]' => array(
          'checked' => TRUE,
        ),
      ),
      'required' => array(
        'input[name="requires_code"]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );
  $has_role_filter = isset($form_state['values']['has_role_filter']) ? $form_state['values']['has_role_filter'] : FALSE;
  $form['qualifications']['has_role_filter'] = array(
    '#type' => 'checkbox',
    '#title' => t('Qualification by Role(s).'),
    '#default_value' => $has_role_filter,
    '#attributes' => array(
      'onchange' => "jQuery('#discount-role-ids-wrapper').toggle();",
    ),
  );

  // Get current discount roles.
  $role_ids = null;
  if (isset($form_state['values']['role_ids'])) {
    $role_ids = $form_state['values']['role_ids'];
  }
  elseif ($is_edit) {
    $role_ids = uc_discounts_get_role_ids_for_discount_id($discount_id, FALSE);
  }

  // Create roles form element.
  $options = array();
  $result = db_query('SELECT rid, name
                     FROM {role}
                     ORDER BY rid');
  $options[UC_DISCOUNTS_OPTION_ALL_ROLES] = t('<All Roles>');
  foreach ($result as $row) {
    $options[$row->rid] = $row->name;
  }
  $form['qualifications']['role_ids'] = array(
    '#type' => 'select',
    '#title' => t('Roles'),
    '#description' => t('Select all roles that this discount applies to or &lt;All Roles&gt; to apply to all roles.'),
    '#options' => $options,
    '#default_value' => $role_ids,
    '#multiple' => TRUE,
    '#states' => array(
      'visible' => array(
        'input[name="has_role_filter"]' => array(
          'checked' => TRUE,
        ),
      ),
      'required' => array(
        'input[name="has_role_filter"]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );
  $form['qualifications']['requires_single_product_to_qualify'] = array(
    '#type' => 'checkbox',
    '#title' => t('Require single product SKU to qualify.'),
    '#description' => t('Requires products with unique SKUs to meet the qualifying requirements. Otherwise, qualifying requirements can be met by a combination of different products.'),
    '#default_value' => isset($form_state['values']['requires_single_product_to_qualify']) ? $form_state['values']['requires_single_product_to_qualify'] : FALSE,
  );
  _uc_discounts_product_filter_form($form, $form_state, UC_DISCOUNTS_GROUPING_QUALIFICATION, $discount_id);
  $form['application'] = array(
    '#type' => 'fieldset',
    '#title' => t('Discount Application'),
    '#description' => t('Details of the discount to apply'),
    '#collapsible' => TRUE,
  );
  $form['application']['discount_type'] = array(
    '#type' => 'select',
    '#title' => t('Discount type'),
    '#description' => t('Type of discount to apply.'),
    '#options' => uc_discounts_discount_type_options(),
    '#default_value' => isset($form_state['values']['discount_type']) ? $form_state['values']['discount_type'] : '',
  );
  $form['application']['discount_amount'] = array(
    '#type' => 'textfield',
    '#title' => t('Discount amount'),
    '#description' => t('The amount of discount.  E.g. 50 (for $50), 5 (for 5 items), or 0.05 (for 5%)'),
    '#default_value' => isset($form_state['values']['discount_amount']) ? $form_state['values']['discount_amount'] : '',
    '#size' => 15,
    '#required' => TRUE,
  );
  _uc_discounts_product_filter_form($form, $form_state, UC_DISCOUNTS_GROUPING_APPLICATION, $discount_id);
  $form['application']['add_to_cart'] = array(
    '#type' => 'checkbox',
    '#title' => t('Add "Discounted products" to cart.'),
    '#description' => t('Ensure that the selected items are added to the cart if not already present. If already present, update the quantity appropriately. You must set at least one "Required product" above and a "Discount type" of "Free items" in order for this to be effective. Does not work with discounts that require a Code.'),
    '#default_value' => isset($form_state['values']['add_to_cart']) ? $form_state['values']['add_to_cart'] : FALSE,
  );
  $form['application']['max_times_applied'] = array(
    '#type' => 'textfield',
    '#title' => t('Maximum applications'),
    '#description' => t('How many times can this discount be applied in a single cart (0 for unlimited)?  Does not apply to "Percentage off" or "Fixed amount off" discounts.'),
    '#default_value' => isset($form_state['values']['max_times_applied']) ? $form_state['values']['max_times_applied'] : 0,
    '#size' => 7,
    '#required' => TRUE,
  );
  $form['application']['limit_max_times_applied'] = array(
    '#type' => 'checkbox',
    '#title' => t('Further limit maximum applications to number of Required Products in cart.'),
    '#description' => t('Allows for a discount like "Get half off X for each Y that is purchased."'),
    '#default_value' => isset($form_state['values']['limit_max_times_applied']) ? $form_state['values']['limit_max_times_applied'] : FALSE,
    '#size' => 7,
  );
  $form['application']['max_uses'] = array(
    '#type' => 'textfield',
    '#title' => t('Max uses'),
    '#description' => t('Number of times this discount can be applied (0 for unlimited).'),
    '#default_value' => isset($form_state['values']['max_uses']) ? $form_state['values']['max_uses'] : 0,
    '#size' => 7,
    '#required' => TRUE,
  );
  $form['application']['max_uses_per_user'] = array(
    '#type' => 'textfield',
    '#title' => t('Max uses per user'),
    '#description' => t('Number of times this discount can be applied to a particular user (0 for unlimited).'),
    '#default_value' => isset($form_state['values']['max_uses_per_user']) ? $form_state['values']['max_uses_per_user'] : 0,
    '#size' => 7,
    '#required' => TRUE,
  );
  $form['application']['max_uses_per_code'] = array(
    '#type' => 'textfield',
    '#title' => t('Max uses per code'),
    '#description' => t('Number of times this discount can be applied to a particular code (0 for unlimited).  Note: "Max uses" (if set) still applies as overall maximum number of uses for this discount.'),
    '#default_value' => isset($form_state['values']['max_uses_per_code']) ? $form_state['values']['max_uses_per_code'] : 0,
    '#size' => 7,
    '#required' => TRUE,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );
  return $form;
}