You are here

function uc_product_kit_form in Ubercart 6.2

Same name and namespace in other branches
  1. 5 uc_product_kit/uc_product_kit.module \uc_product_kit_form()
  2. 7.3 uc_product_kit/uc_product_kit.module \uc_product_kit_form()

Implements hook_form().

File

uc_product_kit/uc_product_kit.module, line 453
The product kit module for Ubercart.

Code

function uc_product_kit_form(&$node) {
  $form = array();
  $sign_flag = variable_get('uc_sign_after_amount', FALSE);
  $currency_sign = variable_get('uc_currency_sign', '$');
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Name'),
    '#required' => TRUE,
    '#weight' => -5,
    '#default_value' => $node->title,
    '#description' => t('Name of the product kit'),
  );
  $form['body_filter']['body'] = array(
    '#type' => 'textarea',
    '#title' => t('Description'),
    '#required' => FALSE,
    '#default_value' => $node->body,
    '#rows' => 20,
    '#description' => t('Explain these whatchamacallits.'),
  );
  $form['body_filter']['format'] = filter_form($node->format);
  $form['body_filter']['#weight'] = -4;

  // Create an array of products on the site for use in the product selector.
  $product_types = uc_product_types();
  $products = array();

  // Disregard other product kits.
  unset($product_types[array_search('product_kit', $product_types)]);

  // Query the database and loop through the results.
  $result = db_query("SELECT nid, title FROM {node} WHERE type IN ('" . implode("','", $product_types) . "') ORDER BY title, nid");
  while ($product = db_fetch_object($result)) {

    // Add each product to the options array.
    $products[$product->nid] = $product->title;
  }
  $form['base'] = array(
    '#type' => 'fieldset',
    '#title' => t('Product information'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#weight' => -1,
    '#attributes' => array(
      'class' => 'product-field',
    ),
  );
  $form['base']['mutable'] = array(
    '#type' => 'radios',
    '#title' => t('How is this product kit handled by the cart?'),
    '#options' => array(
      UC_PRODUCT_KIT_UNMUTABLE_NO_LIST => t('As a unit. Customers may only change how many kits they are buying. Do not list component products.'),
      UC_PRODUCT_KIT_UNMUTABLE_WITH_LIST => t('As a unit. Customers may only change how many kits they are buying. List component products.'),
      UC_PRODUCT_KIT_MUTABLE => t('As individual products. Customers may add or remove kit components at will. Discounts entered below are not applied to the kit price'),
    ),
    '#default_value' => isset($node->mutable) ? $node->mutable : variable_get('uc_product_kit_mutable', UC_PRODUCT_KIT_UNMUTABLE_WITH_LIST),
  );
  $form['base']['products'] = array(
    '#type' => 'select',
    '#multiple' => TRUE,
    '#required' => TRUE,
    '#title' => t('Products'),
    '#options' => $products,
    '#default_value' => isset($node->products) ? array_keys($node->products) : array(),
  );
  $synchronized = FALSE;
  $total = 0;
  $base_total = 0;
  $form['base']['items'] = array(
    '#tree' => TRUE,
    '#weight' => 1,
  );
  $context = array(
    'revision' => 'formatted',
    'type' => 'product',
  );
  if (isset($node->products)) {
    foreach ($node->products as $i => $product) {
      $form['base']['items'][$i] = array(
        '#type' => 'fieldset',
      );
      $form['base']['items'][$i]['link'] = array(
        '#type' => 'item',
        '#value' => l($product->title, 'node/' . $i),
      );
      $form['base']['items'][$i]['qty'] = array(
        '#type' => 'uc_quantity',
        '#title' => t('Quantity'),
        '#default_value' => $product->qty,
      );
      $form['base']['items'][$i]['ordering'] = array(
        '#type' => 'weight',
        '#title' => t('List position'),
        '#default_value' => isset($product->ordering) ? $product->ordering : 0,
      );
      $context['field'] = 'sell_price';
      $context['subject'] = array(
        'node' => $product,
      );
      $form['base']['items'][$i]['discount'] = array(
        '#type' => 'textfield',
        '#title' => t('Discount'),
        '#description' => t('Enter a positive or negative value to raise or lower the item price by that amount. This change is applied to each %product in the kit.', array(
          '%product' => $product->title,
        )),
        '#field_prefix' => t('@price + ', array(
          '@price' => uc_price($product->sell_price, $context),
        )),
        '#default_value' => isset($product->discount) ? number_format($product->discount, 3, '.', '') : 0,
        '#size' => 5,
      );
      $total += $product->sell_price * $product->qty;
      $base_total += $product->sell_price * $product->qty;
      if (isset($product->discount)) {
        $total += $product->discount * $product->qty;
      }
    }
    if (!$node->synchronized && $node->sell_price != $total) {

      // Component products have changed their prices. Recalculate discounts
      // to keep the same total.
      $total = $base_total;
      foreach ($node->products as $i => $product) {
        $discount = ($node->sell_price - $base_total) * $product->sell_price / $base_total;
        $total += $discount * $product->qty;
        $form['base']['items'][$i]['discount']['#default_value'] = number_format($discount, 3, '.', '');
      }
    }
    $context = array(
      'revision' => 'formatted-original',
      'type' => 'amount',
    );
    $form['base']['kit_total'] = array(
      '#type' => 'textfield',
      '#title' => t('Total price'),
      '#default_value' => $node->synchronized ? '' : $total,
      '#description' => t('If this field is set, the discounts of the individual products will be recalculated to equal this value. Currently, the total sell price is %price.', array(
        '%price' => uc_price($total, $context),
      )),
      '#weight' => 0,
      '#size' => 20,
      '#maxlength' => 35,
      '#field_prefix' => $sign_flag ? '' : $currency_sign,
      '#field_suffix' => $sign_flag ? $currency_sign : '',
    );
  }
  $form['base']['default_qty'] = array(
    '#type' => 'uc_quantity',
    '#title' => t('Default quantity to add to cart'),
    '#default_value' => isset($node->default_qty) ? $node->default_qty : 1,
    '#description' => t('Use 0 to disable the quantity field next to the add to cart button.'),
    '#weight' => 27,
    '#allow_zero' => TRUE,
  );
  $form['base']['ordering'] = array(
    '#type' => 'weight',
    '#title' => t('List position'),
    '#delta' => 25,
    '#default_value' => isset($node->ordering) ? $node->ordering : 0,
    '#weight' => 30,
  );

  // Disable all shipping related functionality.
  $form['shipping']['#access'] = FALSE;
  return $form;
}