array(
'arguments' => array('products' => NULL),
),
'uc_multibuy_grid' => array(
'arguments' => array('products' => NULL),
),
);
}
/**
* Implementation of hook_catalog_display
*
* @return
* An array specifying catalog display types
*/
function uc_multibuy_catalog_display($op) {
switch ($op) {
case 'types':
$return = array();
$return['multibuy_table'] = array(
'name' => 'Multi-buy list table',
'callback' => 'theme',
'callback arguments' => 'uc_multibuy_table',
);
$return['multibuy_grid'] = array(
'name' => 'Multi-buy grid',
'callback' => 'theme',
'callback arguments' => 'uc_multibuy_grid'
);
break;
}
return $return;
}
function theme_uc_multibuy_table($products) {
return '
'. drupal_get_form('uc_multibuy_table_form', $products). '
';
}
function theme_uc_multibuy_grid($products) {
return ''. drupal_get_form('uc_multibuy_grid_form', $products). '
';
}
/**
* Display products in a TAPIr table in the multibuy format.
*
* Display image, name, price, and add to cart form with qty and optionally attributes.
*/
function uc_multibuy_table_form(&$form_state, $products) {
$enabled = variable_get('uc_product_field_enabled', array(
'image' => 1,
'display_price' => 1,
'model' => 1,
'list_price' => 0,
'cost' => 0,
'sell_price' => 1,
'weight' => 0,
'dimensions' => 0,
'add_to_cart' => 1,
));
$table = array(
'#type' => 'tapir_table',
'#attributes' => array(
'class' => 'category-products-multibuy',
),
'#columns' => uc_product_table_header(),
'#rows' => array(),
);
foreach ($products as $nid) {
$data = array();
$node = node_load($nid);
if ($enabled['image']) {
if (module_exists('imagecache')) {
if (($field = variable_get('uc_image_'. $node->type, '')) && isset($node->$field) && file_exists($node->{$field}[0]['filepath'])) {
$image = $node->{$field}[0];
$data['image'] = array('#value' => l(theme('imagecache', 'product_list', $image['filepath'], $image['data']['alt'], $image['data']['title']), 'node/'. $node->nid, array('html' => TRUE)));
}
else {
$data['image'] = array('#value' => t('n/a'));
}
}
}
$data['name'] = array(
'#value' => l($node->title, 'node/'. $node->nid),
'#cell_attributes' => array('width' => '100%'),
);
if ($enabled['list_price']) {
$data['list_price'] = array('#value' => uc_currency_format($node->list_price), '#cell_attriubtes' => array('nowrap' => 'nowrap'));
}
if ($enabled['sell_price']) {
$context = array(
'location' => 'product-view',
'class' => array(
'product',
),
'subject' => array(
'node' => $node,
),
);
$context['class'][1] = 'display';
$context['subject']['field'] = 'sell_price';
$data['price'] = array('#value' => theme('uc_product_price', $node->sell_price, $context), '#cell_attriubtes' => array('nowrap' => 'nowrap'));
}
if (module_exists('uc_cart') && arg(0) != 'admin' && $enabled['add_to_cart']) {
$data['add_to_cart'] = _uc_multibuy_add_form($node);
}
$table[] = $data;
}
if (!count(element_children($table))) {
$table[] = array(
'name' => array(
'#value' => t('No products available.'),
'#cell_attributes' => array(
'colspan' => 'full',
),
),
);
}
$form = array();
$form['table'] = $table;
$form['submit'] = array(
'#type' => 'submit',
'#value' => variable_get('uc_multibuy_add_all_to_cart_text', t('Add all to cart')),
'#id' => 'edit-submit',
'#prefix' => '',
'#suffix' => '
'
);
$form['#submit'][] = 'uc_multibuy_form_submit';
$form['#validate'][] = 'uc_multibuy_form_validate';
return $form;
}
/**
* theme_uc_catalog_product_grid from uc_catalog.module modified to produce a Forms API form instead of raw HTML
*
*/
function uc_multibuy_grid_form(&$form_state, $products) {
$columns = array();
$uc_catalog_grid_display_width = variable_get('uc_catalog_grid_display_width', 3);
$uc_catalog_grid_display_width = min($uc_catalog_grid_display_width, count($products));
for ($i = 0; $i < $uc_catalog_grid_display_width; $i++) {
$columns[$i] = array(
'weight' => $i,
);
}
$table = array(
'#type' => 'tapir_table',
'#attributes' => array(
'class' => 'category-grid-products',
),
'#columns' => $columns,
'#rows' => array(),
);
$column = $uc_catalog_grid_display_width; //trigger row initialisation code in loop
$row_even = false;
foreach ($products as $nid) {
$product = node_load($nid);
if ($column == $uc_catalog_grid_display_width) {
$column = 0;
$column_even = true;
if (is_array($row))
$table[] = $row;
$row = array(
'#attributes' => array(
'class' => $row_even ? 'even' : 'odd',
),
);
$row_even = !$row_even;
}
$titlelink = l($product->title, "node/$nid", array('html' => TRUE));
if (module_exists('imagecache') && ($field = variable_get('uc_image_'. $product->type, '')) && isset($product->$field) && file_exists($product->{$field}[0]['filepath'])) {
$imagelink = l(theme('imagecache', 'product_list', $product->{$field}[0]['filepath'], $product->title, $product->title), "node/$nid", array('html' => TRUE));
}
else {
$imagelink = '';
}
$info = '';
if (variable_get('uc_catalog_grid_display_title', TRUE)) {
$info .= ''. $titlelink .'';
}
if (variable_get('uc_catalog_grid_display_model', TRUE)) {
$info .= ''. $product->model .'';
}
$info .= ''. $imagelink .'';
if (variable_get('uc_catalog_grid_display_sell_price', TRUE)) {
$info .= ''. uc_currency_format($product->sell_price) .'';
}
$data = array();
$data['info'] = array('#value' => $info);
if (module_exists('uc_cart') && arg(0) != 'admin' && variable_get('uc_catalog_grid_display_add_to_cart', TRUE)) {
$data['add_to_cart'] = _uc_multibuy_add_form($product);
}
$row[$column] = array(
'data' => $data,
'#cell_attributes' => array(
'class' => $column_even ? 'even' : 'odd',
),
);
$column++;
$column_even = !$column_even;
}
//add last row if necessary
if ($column <= $uc_catalog_grid_display_width) {
//fill remaining cells with blank data
for ($i = $column; $i < $uc_catalog_grid_display_width; $i++) {
$row[$i] = array(
'#value' => '',
'#cell_attributes' => array(
'class' => $column_even ? 'even' : 'odd',
),
);
$column_even = !$column_even;
}
$table[] = $row;
}
$form = array();
$form['table'] = $table;
$form['submit'] = array(
'#type' => 'submit',
'#value' => variable_get('uc_multibuy_add_all_to_cart_text', t('Add all to cart')),
'#id' => 'edit-submit',
'#prefix' => '',
'#suffix' => '
'
);
$form['#submit'][] = 'uc_multibuy_form_submit';
$form['#validate'][] = 'uc_multibuy_form_validate';
return $form;
}
function _uc_multibuy_add_form(&$product) {
$form_id = "uc_product_add_to_cart_form_". $product->nid;
// Copy of relevant bits of uc_product_add_to_cart_form in uc_product.module
$form = array();
$form['qty'] = array('#type' => 'textfield',
'#title' => t('Quantity'),
'#default_value' => 0, //ignore product default, and make this 0 so validation doesn't fail later.
'#size' => 5,
'#maxlength' => 6,
);
//include nid and node incase form altering functions need them, unset them later
$form['nid'] = array('#type' => 'value', '#value' => $node->nid);
$form['node'] = array(
'#type' => 'value',
'#value' => $product,
);
uc_form_alter($form, $form_state, $form_id); //perform uc form alterations, including adding attributes
if (!variable_get('uc_catalog_display_attributes', false)) {
unset($form['attributes']);
}
//apply known form alterations (but not any and all because they all need special handling)
if ($product->type == 'donation') {
uc_donation_form_alter(&$form, &$form_state, $form_id);
}
//drupal_set_message("" . print_r($form, 1) . "
");
//don't need these anymore
unset($form['nid']);
unset($form['node']);
//modify qty element for our purposes
$form['qty-'. $product->nid] = $form['qty'];
unset($form['qty']);
if ($form['attributes']) {
foreach ($form['attributes'] as $key => $value) {
if (is_array($form['attributes'][$key]) && $form['attributes'][$key]['#required'] == 1) {
$attribute = uc_attribute_load($key, $product->nid, 'product');
$default_option = $attribute->default_option;
if (!isset($attribute->options[$default_option]))
$default_option = array_shift(array_keys($attribute->options));
if ($form['attributes'][$key]['#type'] != 'checkboxes') {
$form['attributes'][$key]['#default_value'] = $default_option;
}
else { //if checkbox then default value has different format as more than one option can be set (though never would be here)
$form['attributes'][$key]['#default_value'][$default_option] = $default_option;
}
}
}
$form['attributes-'. $product->nid] = $form['attributes'];
unset($form['attributes']);
}
if ($product->type == 'donation') {
//$form['donate_amount']['#default_value'] = 0;
$form['donate_amount-'. $product->nid] = $form['donate_amount'];
unset($form['donate_amount']);
}
$form[] = array('#value' => '', '#weight' => -99);
$form[] = array('#value' => '
', '#weight' => 99);
return $form;
}
function uc_multibuy_form_validate($form, &$form_state) {
foreach($form_state['values'] as $key => $value) {
if (strpos($key, 'qty-') === 0) {
if (!is_numeric($value) || intval($value) < 0) {
form_set_error($key, t('You have entered an invalid quantity. All quantities must be an integer greater than zero.'));
}
$nid = substr($key, 4);
if (intval($value) > 0 && isset($form_state['values']['donate_amount-'. $nid])) {
$donate_amount = $form_state['values']['donate_amount-'. $nid];
if (!is_numeric($donate_amount) || floatval($donate_amount) < 0) {
form_set_error('donate_amount-'. $nid, t('You have entered an invalid amount.'));
}
}
}
}
}
function uc_multibuy_form_submit($form, &$form_state) {
foreach($form_state['values'] as $key => $value) {
if (strpos($key, 'qty-') === 0) {
//extract data from form submission
$nid = substr($key, 4);
$qty = (int) $value;
if ($qty > 0) {
//build a $form_state array as if for a single product
$single_fs = array();
$single_fs['nid'] = $nid;
$single_fs['qty'] = $qty;
if (isset($form_state['values']['attributes-'. $nid]))
$single_fs['attributes'] = $form_state['values']['attributes-'. $nid];
//handle uc_donation amount
$add_to_cart = true;
if (isset($form_state['values']['donate_amount-'. $nid])) {
if (floatval($form_state['values']['donate_amount-'. $nid]) == 0)
$add_to_cart = false;
$single_fs['donate_amount'] = $form_state['values']['donate_amount-'. $nid];
}
if ($add_to_cart) {
//add item to cart
uc_cart_add_item($nid, $qty, module_invoke_all('add_to_cart_data', $single_fs));
}
}
}
}
}
/**
* Implementation of hook_menu().
*/
function uc_multibuy_menu() {
$items = array();
$items['admin/store/settings/catalog/edit/multibuy'] = array(
'access arguments' => array('administer catalog'),
'title' => 'Multi-buy',
'page callback' => 'drupal_get_form',
'page arguments' => array('uc_multibuy_admin_form'),
'weight' => -5,
'type' => MENU_LOCAL_TASK,
);
return $items;
}
function uc_multibuy_admin_form() {
$form = array();
$form['multibuy'] = array('#type' => 'fieldset',
'#title' => t('Multi-buy display'),
'#collapsible' => true,
'#collapsed' => false,
'#weight' => -3,
'#attributes' => array('class' => 'multibuy'),
);
$form['multibuy']['uc_multibuy_add_all_to_cart_text'] = array(
'#type' => 'textfield',
'#title' => t('Text of the Add all to cart button.'),
'#description' => t('Text of the Add all to cart button at the bottom of Multi-buy catalog pages.'),
'#default_value' => variable_get('uc_multibuy_add_all_to_cart_text', t('Add all to cart')),
);
$form['multibuy']['uc_catalog_display_attributes'] = array(
'#type' => 'checkbox',
'#title' => t('Display attributes'),
'#default_value' => variable_get('uc_catalog_display_attributes', false),
);
$form['multibuy']['uc_catalog_grid_display_teaser'] = array(
'#type' => 'checkbox',
'#title' => t('Display product teaser (applies to multi-buy grid format only)'),
'#default_value' => variable_get('uc_catalog_grid_display_teaser', false),
);
return system_settings_form($form);
}