function _webform_render_productfield in Commerce Webform 7
Same name and namespace in other branches
- 8 productfield.inc \_webform_render_productfield()
- 7.2 productfield.inc \_webform_render_productfield()
Implements _webform_render_component().
File
- ./
productfield.inc, line 152
Code
function _webform_render_productfield($component, $value = NULL, $filter = TRUE) {
$node = isset($component['nid']) ? node_load($component['nid']) : NULL;
$items = isset($component['extra']['items']) ? $component['extra']['items'] : array();
$multiple = $component['extra']['multiple'];
$choose_quantity = !empty($component['extra']['choose_quantity']);
$product_type = $component['extra']['product_type'];
// 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 != '1') {
$options[''] = ' - ' . (empty($component['mandatory']) ? t('None') : t('Select')) . ' - ';
}
$product_ids = array();
if (empty($product_type)) {
// Build an array of product IDs from this field's values.
foreach ($items as $item) {
$product_ids[] = $item['product_id'];
}
}
else {
$query = new EntityFieldQuery();
$result = $query
->entityCondition('entity_type', 'commerce_product')
->entityCondition('bundle', $product_type)
->execute();
if (!empty($result['commerce_product'])) {
$product_ids = array_keys($result['commerce_product']);
}
}
$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);
// @TODO - needs a theme function here.
$options[$product->product_id] = theme('commerce_webform_product_display', array(
'product' => $product,
'price' => $price,
'component' => $component,
));
$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 (!empty($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($component['value'])) {
$defaults = explode(',', _webform_filter_values($component['value']));
foreach ($defaults as $default) {
$product = commerce_product_load_by_sku(trim($default));
if (!empty($product) && array_key_exists($product->product_id, $options)) {
if ($component['extra']['multiple']) {
$default_product_ids[] = $product->product_id;
if ($choose_quantity) {
$default_quantities[$product->product_id] = 1;
}
}
else {
$default_product_ids = $product->product_id;
}
}
}
}
// @TODO: Make default quantity configurable.
// @TODO: Add validation of the quantity value.
$element = array(
'#weight' => $component['weight'],
);
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>' . _webform_filter_xss($component['name']) . '</strong>';
$element['#markup'] .= theme('table', array(
'header' => array(
'Product ID',
'Product name',
'Quantity',
),
'rows' => $rows,
));
$element['#markup'] .= '<p>' . t('This order has been completed and products choices cannot be edited online.') . '</p>';
}
else {
$element[] = array(
'#type' => $component['extra']['aslist'] ? 'select' : ($component['extra']['multiple'] ? 'checkboxes' : 'radios'),
'#multiple' => $component['extra']['multiple'],
'#size' => $component['extra']['aslist'] && $component['extra']['multiple'] ? 4 : 0,
'#title' => $filter ? _webform_filter_xss($component['name']) : $component['name'],
'#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
'#required' => $component['mandatory'],
'#weight' => $component['weight'],
'#description' => $filter ? _webform_filter_descriptions($component['extra']['description'], $node) : $component['extra']['description'],
'#translatable' => array(
'title',
'description',
'options',
),
'#options' => $options,
'#default_value' => $default_product_ids,
'#prefix' => '<div class="commerce_webform_products">',
'#suffix' => $hidden_elements_html . '</div>',
// Needed to disable double-wrapping of radios and checkboxes.
'#pre_render' => array(),
'#theme_wrappers' => array(
'webform_element',
),
'#element_validate' => array(
'_webform_productfield_selection_validate',
),
);
$editable = empty($component['extra']['multiple']) && !empty($component['extra']['choose_quantity']);
$element[] = array(
'#type' => $editable ? 'textfield' : 'value',
'#title' => t('@product quantity', array(
'@product' => $filter ? _webform_filter_xss($component['name']) : $component['name'],
)),
'#default_value' => $choose_quantity ? is_array($default_quantities) ? 1 : $default_quantities : 1,
'#element_validate' => array(
'_webform_productfield_quantity_validate',
),
'#required' => $component['mandatory'],
'#weight' => $component['weight'] + 0.5,
);
}
}
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>' . _webform_filter_xss($component['name']) . '</strong>';
$element['#markup'] .= theme('table', array(
'header' => array(
'Product ID',
'Product name',
'Quantity',
),
'rows' => $rows,
));
$element['#markup'] .= '<p>' . t('This order has been completed and products choices cannot be edited online.') . '</p>';
}
else {
$element = array(
'#type' => 'fieldset',
'#title' => _webform_filter_xss($component['name']),
'#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
'#element_validate' => array(
'_webform_productfield_required_multiple_quantities_validate',
),
'#required' => $component['mandatory'],
'#weight' => $component['weight'],
// Hide title as fieldsets don't support #title_display.
'#pre_render' => array(
'webform_element_title_display',
),
);
foreach ($options as $product_id => $product_name) {
$element[$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' => $component['weight'] + ('0.' . $i),
);
$i++;
}
}
}
return $element;
}