'',
'form_key' => NULL,
'email' => 1,
'pid' => 0,
'weight' => 0,
'value' => '',
'mandatory' => 0,
'extra' => array(
'width' => '',
'unique' => 0,
'disabled' => 0,
'title_display' => 0,
'description' => '',
'attributes' => array(),
'private' => FALSE,
'products' => NULL,
'multiple' => NULL,
'order' => NULL,
),
);
}
/**
* Implements _webform_theme_component().
*/
function _webform_theme_product_list() {
return array(
'uc_webform_display_product_list' => array(
'render element' => 'element',
'file' => 'components/product_list.inc',
'path' => drupal_get_path('module', 'uc_webform'),
),
'uc_webform_render_product_list' => array(
'render element' => 'element',
'file' => 'components/product_list.inc',
'path' => drupal_get_path('module', 'uc_webform'),
),
);
}
/**
* Theme function for when a product is displayed in a webform submission.
*/
function theme_uc_webform_display_product_list($variables) {
$element = $variables['element'];
$output = '';
// TODO: Should this theme uc_webform_display_product_list be declared in
// hook_theme()?
if (!empty($element['#value'])) {
$output = (count($element['#value']) > 1) ? theme('item_list', array('items' => $element['#value'])) : $element['#value'][0];
}
return $output;
}
/**
* Theme function for a product when it is rendered as a part of a list.
*/
function theme_uc_webform_render_product_list($products) {
// TODO: Should this theme uc_webform_render_product_list be declared in
// hook_theme()?
$output = array();
foreach ($products as $key => $product) {
if (!empty($product)) {
$output[$key] = '' . $product['title'] . ', ' . t('Price:') . ' ' . $product['price'] . '';
}
}
return $output;
}
/**
* Generate the form for editing a component.
*/
function _webform_edit_product_list($component) {
$products = array();
$form = array();
$product_types = uc_product_types();
// Limit selection to only those products that do *not* contain attributes.
$attrib_query = db_select('uc_product_attributes', 'pa')
->fields('pa', array('nid'));
$query = db_select('node', 'n')
->fields('n', array('nid', 'title'))
->condition('n.nid', $attrib_query, 'NOT IN');
// JOIN node with product.
$query->join('uc_products', 'p', 'n.nid = p.nid');
$query->addField('p', 'model');
$query->orderBy('n.title');
foreach ($query->execute() as $product) {
$products[$product->nid . '_' . $product->model] = check_plain($product->title);
}
// Most options are stored in the "extra" array, which stores any settings
// unique to a particular component type.
$form['extra']['products'] = array(
'#type' => 'select',
'#title' => t('Products'),
'#default_value' => $component['extra']['products'],
'#multiple' => TRUE,
'#description' => t('Please select your products. Only products without attributes are displayed.'),
'#weight' => -3,
'#size' => 20,
'#required' => TRUE,
'#options' => $products,
);
$form['extra']['multiple'] = array(
'#type' => 'checkbox',
'#title' => t('Multiple'),
'#default_value' => $component['extra']['multiple'],
'#description' => t('Check this option if the user is allowed to select multiple products.'),
'#weight' => -2,
);
$form['extra']['order'] = array(
'#type' => 'radios',
'#title' => t('Product Order'),
'#default_value' => $component['extra']['order'],
'#multiple' => TRUE,
'#description' => t('Please select the order in which the products will be displayed.'),
'#weight' => -3,
'#size' => 20,
'#required' => TRUE,
'#options' => array(
'order_by_price_asc' => t('Price (ascending)'),
'order_by_price_desc' => t('Price (descending)'),
'order_by_title_asc' => t('Title (ascending)'),
'order_by_title_desc' => t('Title (descending)'),
),
);
return $form;
}
/**
* Implements _webform_render_component().
*/
function _webform_render_product_list($component, $value = NULL) {
$stock_description = "";
foreach ($component['extra']['products'] as $val) {
$product_info = explode('_', $val, 2);
$node = node_load($product_info[0]);
if (module_exists('uc_stock')) {
$stock_level = uc_stock_level($product_info[1]);
}
else {
$stock_level = FALSE;
}
// Check stock levels. The product is only selectable if it is in stock.
if (($stock_level === FALSE) or (intval($stock_level) > 0)) {
$product['title'] = check_plain($node->title);
$money = round($node->sell_price, 2);
// Keep trailing zeros.
$product['price'] = sprintf("%01.2f", $money);
$products[$val] = $product;
}
else {
$stock_description .= check_plain($node->title) . ' ' . t('is out of stock.') . '
';
}
}
// Users may choose to sort the products based on price or title.
switch ($component['extra']['order']) {
case 'order_by_price_asc':
// Obtain a list of columns.
foreach ($products as $key => $row) {
$price[$key] = $row['price'];
}
// Sort the products with price ascending.
array_multisort($price, SORT_ASC, $products);
break;
case 'order_by_price_desc':
// Obtain a list of columns.
foreach ($products as $key => $row) {
$price[$key] = $row['price'];
}
// Sort the products with price descending.
array_multisort($price, SORT_DESC, $products);
break;
case 'order_by_title_asc':
// Obtain a list of columns.
foreach ($products as $key => $row) {
$title[$key] = $row['title'];
}
// Sort the products with title ascending.
array_multisort($title, SORT_ASC, $products);
break;
case 'order_by_title_desc':
// Obtain a list of columns.
foreach ($products as $key => $row) {
$title[$key] = $row['title'];
}
// Sort the products with title descending.
array_multisort($title, SORT_DESC, $products);
break;
}
// Add the currency sign back to the price.
$currency_sign = variable_get('uc_currency_sign', '$');
foreach ($products as $prod_key => $prod_val) {
$products[$prod_key]['price'] = $currency_sign . $products[$prod_key]['price'];
}
$form_item = array();
if ($component['extra']['multiple'] == 0) {
$form_item = array(
'#type' => 'radios',
'#title' => $component['name'],
'#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
'#weight' => $component['weight'],
'#description' => $stock_description . _webform_filter_descriptions($component['extra']['description']),
'#prefix' => '