You are here

function _commerce_webform_productfield_expand in Commerce Webform 8

Same name and namespace in other branches
  1. 7.2 productfield.inc \_commerce_webform_productfield_expand()

Form API #process function to expand a productfield.

1 call to _commerce_webform_productfield_expand()
commerce_webform_productfield_expand in ./commerce_webform.module
Form API #process function to expand a productfield element.

File

./productfield.inc, line 178

Code

function _commerce_webform_productfield_expand($element) {
  $element += element_info('webform_productfield');
  $product_ids = empty($element['#productids']) ? array() : $element['#productids'];
  $choose_quantity = !empty($element['#choose_quantity']);
  $required = !empty($element['#required']);
  $value = $element['#default_value'];
  $aslist = !empty($element['#aslist']);
  $multiple = !empty($element['#multiple']);
  $name = $element['#name'];
  $description = $element['#description'];
  $title_display = !empty($element['#title_display']) ? $element['#title_display'] : 'before';

  // Each product should have a hidden element which describes it which allows
  // js to identify the products for possible dynamic total field.
  // @TODO Replace with RDFa.
  $hidden_elements_html = '';
  $options = array();
  if (!$multiple) {
    $options[''] = ' - ' . ($required ? t('Select') : t('None')) . ' - ';
  }
  $products = commerce_product_load_multiple($product_ids);
  foreach ($products as $product) {

    // Check if the product is active.
    if ($product->status == 1) {
      $price = commerce_product_calculate_sell_price($product);
      $options[$product->product_id] = theme('commerce_webform_product_display', array(
        'product' => $product,
        'price' => $price,
        'element' => $element,
      ));
      $hidden_elements_html .= "<input type='hidden' name='commerce_webform_product[{$product->product_id}]' value='{$price['amount']}' />\n";
    }
  }
  $default_product_ids = $multiple ? array() : 0;
  $default_quantities = $multiple ? array() : 1;
  $disabled = FALSE;
  if (is_array($value)) {

    // Load a previously saved value.
    foreach ($value as $id => $encoded_details) {
      $details = json_decode($encoded_details);
      if ($details->paid && !user_access('alter product field on paid webform')) {
        $disabled = TRUE;
      }
      if ($multiple) {
        $default_product_ids[] = $details->product_id;
        if ($choose_quantity) {
          $default_quantities[$details->product_id] = $details->quantity;
        }
        else {
          $default_quantities = $details->quantity;
        }
      }
      else {
        $default_product_ids = $details->product_id;
        $default_quantities = $details->quantity;
      }
    }
  }
  elseif (!empty($value)) {
    $defaults = explode(',', $value);
    foreach ($defaults as $default) {
      $product = commerce_product_load_by_sku(trim($default));
      if (!empty($product) && array_key_exists($product->product_id, $options)) {
        if ($multiple) {
          $default_product_ids[] = $product->product_id;
          $default_quantities[$product->product_id] = 1;
        }
        else {
          $default_product_ids = $product->product_id;
        }
      }
    }
  }
  if (!$multiple || !$choose_quantity) {

    // Single quantity options.
    if ($disabled) {

      // The product has been paid for so should not be able to change it.
      $rows = array();
      $element[] = array(
        '#type' => 'value',
        '#value' => $default_product_ids,
      );
      $element[] = array(
        '#type' => 'value',
        '#value' => $default_quantities,
      );
      if (is_array($default_product_ids)) {
        foreach ($default_product_ids as $product_id) {
          $rows[] = array(
            $product_id,
            $options[$product_id],
            $default_quantities,
          );
        }
      }
      elseif ($default_product_ids > 0) {
        $rows[] = array(
          $default_product_ids,
          $options[$default_product_ids],
          $default_quantities,
        );
      }

      // @TODO - put this in a theme function.
      $element['#markup'] = '<strong>' . $name . '</strong>';
      $element['#markup'] .= theme('table', array(
        'header' => array(
          t('Product ID'),
          t('Product name'),
          t('Quantity'),
        ),
        'rows' => $rows,
      ));
      $element['#markup'] .= '<p>' . t('This order has been completed and products choices cannot be edited online.') . '</p>';
    }
    else {
      $element[] = array(
        '#type' => $aslist ? 'select' : ($multiple ? 'checkboxes' : 'radios'),
        '#multiple' => $multiple,
        '#size' => $aslist && $multiple ? 4 : 0,
        '#title' => $name,
        '#title_display' => $title_display,
        '#required' => $required,
        '#description' => $description,
        '#translatable' => array(
          'title',
          'description',
          'options',
        ),
        '#options' => $options,
        '#default_value' => $default_product_ids,
        '#suffix' => $hidden_elements_html,
        '#pre_render' => array(),
        '#element_validate' => array(
          '_webform_productfield_selection_validate',
        ),
      );
      $editable = empty($multiple) && !empty($choose_quantity);
      $element[] = array(
        '#type' => $editable ? 'textfield' : 'value',
        '#title' => t('@product quantity', array(
          '@product' => $name,
        )),
        '#default_value' => $choose_quantity ? is_array($default_quantities) ? 1 : $default_quantities : 1,
        '#element_validate' => array(
          '_webform_productfield_quantity_validate',
        ),
        '#required' => $required,
        '#weight' => 1,
        '#attributes' => array(
          'class' => array(
            'productfield-quantity',
          ),
        ),
      );
    }
  }
  else {

    // Product field is multiple and user can choose quantity.
    $i = 1;
    if ($disabled) {
      $rows = array();
      foreach ($default_quantities as $product_id => $quantity) {
        $element[$product_id] = array(
          '#type' => 'value',
          '#value' => $quantity,
        );
        $rows[] = array(
          $product_id,
          $options[$product_id],
          $quantity,
        );
      }

      // @TODO - put this in a theme function.
      $element['#markup'] = '<strong>' . $name . '</strong>';
      $element['#markup'] .= theme('table', array(
        'header' => array(
          t('Product ID'),
          t('Product name'),
          t('Quantity'),
        ),
        'rows' => $rows,
      ));
      $element['#markup'] .= '<p>' . t('This order has been completed and products choices cannot be edited online.') . '</p>';
    }
    else {
      $element['multiple_product_quantities'] = array(
        '#type' => 'fieldset',
        '#title' => $name,
        '#title_display' => $title_display,
        '#element_validate' => array(
          '_webform_productfield_required_multiple_quantities_validate',
        ),
        '#required' => $required,
        '#weight' => 0,
        // Hide title as fieldsets don't support #title_display.
        '#pre_render' => array(
          'webform_element_title_display',
        ),
      );
      foreach ($options as $product_id => $product_name) {
        $element['multiple_product_quantities'][$product_id] = array(
          '#type' => 'textfield',
          '#title' => $product_name,
          '#title_display' => 'after',
          '#size' => 2,
          '#default_value' => isset($default_quantities[$product_id]) ? $default_quantities[$product_id] : 0,
          '#element_validate' => array(
            '_webform_productfield_quantity_validate',
          ),
          '#weight' => $i++,
        );
      }
    }
  }
  return $element;
}