You are here

function _commerce_webform_productfield_expand in Commerce Webform 7.2

Same name and namespace in other branches
  1. 8 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 227

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 && !$required) {
    $options[''] = ' - ' . 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() : '';
  $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 (empty($details)) {
        continue;
      }
      if (!empty($details->order_id)) {
        $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 ($disabled) {

    // The product has been paid for so should not be able to change it.
    $element[] = array(
      '#type' => 'value',
      '#value' => $value,
    );
    $markup = array(
      '#markup' => '<p>' . t('It is no longer possible to edit the products in this submission.') . '</p>',
    );
    $markup['#markup'] .= theme_webform_display_productfield(array(
      'element' => $element,
    ));
    $element[] = $markup;
    $element['#title_display'] = 'before';
  }
  elseif (!$multiple || !$choose_quantity) {

    // Single quantity options.
    $new_element = array(
      '#type' => $aslist ? 'select' : ($multiple ? 'checkboxes' : 'radios'),
      '#multiple' => $multiple,
      '#size' => $aslist && $multiple ? 4 : 0,
      '#title' => $name,
      '#title_display' => 'none',
      '#required' => $required,
      '#description' => $description,
      '#translatable' => array(
        'title',
        'description',
        'options',
      ),
      '#options' => $options,
      '#suffix' => $hidden_elements_html,
      '#pre_render' => array(),
      '#validated' => 'TRUE',
      '#element_validate' => array(
        '_webform_productfield_selection_validate',
      ),
    );
    if (!empty($default_product_ids)) {
      $new_element['#default_value'] = $default_product_ids;
    }
    $element[] = $new_element;
    if (!empty($choose_quantity) && !empty($element['#webform_component']['extra']['choose_quantity_max'])) {
      $quantity_options = array();
      foreach (range($element['#webform_component']['extra']['choose_quantity_min'], $element['#webform_component']['extra']['choose_quantity_max']) as $quantity_option) {
        $quantity_options[$quantity_option] = $quantity_option;
      }
      $element[] = array(
        '#type' => 'select',
        '#options' => $quantity_options,
        '#title' => t('@product quantity', array(
          '@product' => $name,
        )),
        '#title_display' => 'after',
        '#default_value' => is_array($default_quantities) ? $element['#webform_component']['extra']['choose_quantity_min'] : $default_quantities,
        '#element_validate' => array(
          '_webform_productfield_quantity_validate',
        ),
        '#weight' => 1,
        '#attributes' => array(
          'class' => array(
            'productfield-quantity',
          ),
        ),
      );
    }
    else {
      $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;
    $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) {
      if (empty($element['#webform_component']['extra']['choose_quantity_max'])) {
        $element['multiple_product_quantities'][$product_id] = array(
          '#type' => 'textfield',
          '#title' => $product_name,
          '#title_display' => 'after',
          '#size' => 6,
          '#default_value' => isset($default_quantities[$product_id]) ? $default_quantities[$product_id] : 0,
          '#element_validate' => array(
            '_webform_productfield_quantity_validate',
          ),
          '#weight' => $i++,
        );
      }
      else {
        $quantity_options = array();
        foreach (range($element['#webform_component']['extra']['choose_quantity_min'], $element['#webform_component']['extra']['choose_quantity_max']) as $quantity_option) {
          $quantity_options[$quantity_option] = $quantity_option;
        }
        $element['multiple_product_quantities'][$product_id] = array(
          '#type' => 'select',
          '#options' => $quantity_options,
          '#title' => $product_name,
          '#title_display' => 'after',
          '#default_value' => isset($default_quantities[$product_id]) ? $default_quantities[$product_id] : 0,
          '#element_validate' => array(
            '_webform_productfield_quantity_validate',
          ),
          '#weight' => $i++,
          '#attributes' => array(
            'class' => array(
              'productfield-quantity',
            ),
          ),
        );
      }
    }
  }
  return $element;
}