uid == $node->uid) { return user_access('edit own products'); } else { return user_access('edit products'); } } } function uc_product_kit_enable() { $node_type = node_get_types('type', 'product_kit'); if ($node_type->module == 'node') { $node_type->module = 'uc_product_kit'; $node_type->custom = 0; node_type_save($node_type); } } function uc_product_kit_disable() { $node_type = node_get_types('type', 'product_kit'); $node_type->module = 'node'; $node_type->custom = 1; node_type_save($node_type); } /** * Implementation of hook_node_info(). * * @return Node type information for product kits. */ function uc_product_kit_node_info() { return array( 'product_kit' => array( 'name' => t('Product kit'), 'module' => 'uc_product_kit', 'description' => t('This node represents two or more products that have been listed together. This presents a logical and convenient grouping of items to the customer.'), 'title_label' => t('Name'), 'body_label' => t('Description'), ), ); } /** * Implementation of hook_insert(). * * Add a row to {uc_products} to make a product. Extra information about the * component products are stored in {uc_product_kits}. * @param &$node The node object being saved. * @see uc_product_insert */ function uc_product_kit_insert(&$node) { $obj = new stdClass(); $obj->vid = $node->vid; $obj->nid = $node->nid; $obj->model = ''; $obj->list_price = 0; $obj->cost = 0; $obj->sell_price = 0; $obj->weight = 0; $obj->weight_units = variable_get('uc_weight_unit', 'lb'); $obj->ordering = $node->ordering; $obj->shippable = false; $values = array(); $placeholders = array(); foreach ($node->products as $product) { $product = node_load($product); $values[] = $node->vid; $values[] = $node->nid; $values[] = $product->nid; $values[] = $node->mutable; $values[] = 1; $values[] = $product->sell_price; $placeholders[] = '(%d, %d, %d, %d, %d, %f)'; $obj->model .= $product->model . ' / '; $obj->list_price += $product->list_price; $obj->cost += $product->cost; $obj->sell_price += $product->sell_price; $obj->weight += $product->weight * uc_weight_conversion($product->weight_units, $obj->weight_units); if ($product->shippable) { $obj->shippable = true; } } db_query("INSERT INTO {uc_product_kits} (vid, nid, product_id, mutable, qty, discount) VALUES ". implode(',', $placeholders), $values); $obj->model = rtrim($obj->model, ' / '); db_query("INSERT INTO {uc_products} (vid, nid, model, list_price, cost, sell_price, weight, weight_units, default_qty, unique_hash, ordering, shippable) VALUES (%d, %d, '%s', %f, %f, %f, %f, '%s', %d, '%s', %d, %d)", $obj->vid, $obj->nid, $obj->model, $obj->list_price, $obj->cost, $obj->sell_price, $obj->weight, $obj->weight_units, $obj->default_qty, md5($obj->vid . $obj->nid . $obj->model . $obj->list_price . $obj->cost . $obj->sell_price . $obj->weight . $obj->weight_units . $obj->default_qty . $obj->ordering . $obj->shippable . time()), $obj->ordering, $obj->shippable ); } /** * Implementation of hook_update(). * * Updates information in {uc_products} as well as {uc_product_kits}. Because * component products are known when the form is loaded, discount information * can be input and saved. * @param &$node The node to be updated. * @see uc_product_update */ function uc_product_kit_update(&$node) { $obj = new stdClass(); $obj->vid = $node->vid; $obj->nid = $node->nid; $obj->model = ''; $obj->list_price = 0; $obj->cost = 0; $obj->sell_price = 0; $obj->weight = 0; $obj->weight_units = variable_get('uc_weight_unit', 'lb'); $obj->default_qty = $node->default_qty; $obj->ordering = $node->ordering; $obj->shippable = false; $values = array(); $placeholders = array(); foreach ($node->products as $nid) { $values[] = $node->vid; $values[] = $node->nid; $values[] = $nid; $values[] = $node->mutable; $product = node_load($nid); if (is_null($node->items[$nid]['qty']) || $node->items[$nid]['qty'] === '') { $node->items[$nid]['qty'] = 1; } $values[] = $node->items[$nid]['qty']; if (is_null($node->items[$nid]['discount']) || $node->items[$nid]['discount'] === '') { $node->items[$nid]['discount'] = $product->sell_price; } $values[] = $node->items[$nid]['discount']; $values[] = $node->items[$nid]['ordering']; $placeholders[] = '(%d, %d, %d, %d, %d, %f, %d)'; $product->qty = $node->items[$nid]['qty']; if ($node->items[$nid]['discount'] < 0) { $product->sell_price += $node->items[$nid]['discount']; } else { $product->sell_price = $node->items[$nid]['discount']; } $obj->model .= $product->model . ' / '; $obj->list_price += $product->list_price * $product->qty; $obj->cost += $product->cost * $product->qty; $obj->sell_price += $product->sell_price * $product->qty; $obj->weight += $product->weight * $product->qty * uc_weight_conversion($product->weight_units, $obj->weight_units); if ($product->shippable) { $obj->shippable = true; } } $obj->model = rtrim($obj->model, ' / '); if (!$node->revision) { db_query("DELETE FROM {uc_product_kits} WHERE nid = %d", $node->nid); } db_query("INSERT INTO {uc_product_kits} (vid, nid, product_id, mutable, qty, discount, ordering) VALUES ". implode(',', $placeholders), $values); if ($node->revision) { db_query("INSERT INTO {uc_products} (vid, nid, model, list_price, cost, sell_price, weight, weight_units, default_qty, unique_hash, ordering, shippable) VALUES (%d, %d, '%s', %f, %f, %f, %f, '%s', %d, '%s', %d, %d)", $obj->vid, $obj->nid, $obj->model, $obj->list_price, $obj->cost, $obj->sell_price, $obj->weight, $obj->weight_units, $obj->default_qty, md5($obj->vid . $obj->nid . $obj->model . $obj->list_price . $obj->cost . $obj->sell_price . $obj->weight . $obj->weight_units . $obj->default_qty . $obj->ordering . time()), $obj->ordering, $obj->shippable ); } else { db_query("UPDATE {uc_products} SET model = '%s', list_price = %f, cost = %f, sell_price = %f, weight = %f, weight_units = '%s', default_qty = %d, ordering = %d, shippable = %d WHERE vid = %d", $obj->model, $obj->list_price, $obj->cost, $obj->sell_price, $obj->weight, $obj->weight_units, $obj->default_qty, $obj->ordering, $obj->shippable, $obj->vid); if (!db_affected_rows()) { db_query("INSERT INTO {uc_products} (vid, nid, model, list_price, cost, sell_price, weight, weight_units, default_qty, unique_hash, ordering, shippable) VALUES (%d, %d, '%s', %f, %f, %f, %f, '%s', %d, '%s', %d, %d)", $obj->vid, $obj->nid, $obj->model, $obj->list_price, $obj->cost, $obj->sell_price, $obj->weight, $obj->weight_units, $obj->default_qty, md5($obj->vid . $obj->nid . $obj->model . $obj->list_price . $obj->cost . $obj->sell_price . $obj->weight . $obj->weight_units . $obj->default_qty . $obj->ordering . $obj->shippable . time()), $obj->ordering, $boj->shippable ); } } } /** * Implementation of hook_delete(). */ function uc_product_kit_delete(&$node) { if (module_exists('uc_cart')) { db_query("DELETE FROM {uc_cart_products} WHERE data LIKE '%%s:6:\"kit_id\";s:%d:\"%s\";%%'", strlen($node->nid), $node->nid); } db_query("DELETE FROM {uc_product_kits} WHERE nid = %d", $node->nid); db_query("DELETE FROM {uc_products} WHERE nid = %d", $node->nid); } /** * Implementation of hook_load(). */ function uc_product_kit_load(&$node) { $obj = new stdClass(); $products = array(); $result = db_query("SELECT product_id, mutable, qty, discount, ordering FROM {uc_product_kits} WHERE vid = %d ORDER BY ordering", $node->vid); while ($prod = db_fetch_object($result)) { $obj->mutable = $prod->mutable; $product = node_load($prod->product_id); $product->qty = $prod->qty; $product->discount = $prod->discount; $product->ordering = $prod->ordering; $products[$prod->product_id] = $product; } uasort($products, '_uc_product_kit_sort_products'); $obj->products = $products; if ($extra = uc_product_load($node)) { foreach ($extra as $key => $value) { $obj->$key = $value; } } return $obj; } function uc_product_kit_nodeapi(&$node, $op, $arg3 = null, $arg4 = null) { switch ($op) { case 'update': $result = db_query("SELECT DISTINCT nid FROM {uc_product_kits} WHERE product_id = %d", $node->nid); while ($nid = db_fetch_object($result)) { $kit = node_load($nid, NULL, TRUE); $redirect = drupal_execute('product_kit_node_form', array('nid' => $kit->nid), $kit); } break; } } /** * Register an "Add to Cart" form for each product kit. * @see uc_product_kit_add_to_cart_form * @see uc_catalog_buy_it_now_form */ function uc_product_kit_forms($saved_args) { $forms = array(); if (substr($saved_args[0], 0, 31) == 'uc_product_kit_add_to_cart_form' || substr($saved_args[0], 0, 27) == 'uc_product_add_to_cart_form' || substr($saved_args[0], 0, 26) == 'uc_catalog_buy_it_now_form') { $product = $saved_args[1]; if ($product->type == 'product_kit') { $forms['uc_product_kit_add_to_cart_form_'. $product->nid] = array('callback' => 'uc_product_kit_add_to_cart_form'); $forms['uc_product_add_to_cart_form_'. $product->nid] = array('callback' => 'uc_product_kit_add_to_cart_form'); $forms['uc_catalog_buy_it_now_form_'. $product->nid] = array('callback' => 'uc_product_kit_buy_it_now_form'); } } return $forms; } /** * Add product kit settings to the product settings form. */ function uc_product_kit_form_alter($form_id, &$form) { if ($form_id == 'uc_product_settings_form') { $form['uc_product_kit_mutable'] = array('#type' => 'radios', '#title' => t('How are product kits handled by the cart?'), '#options' => array( UC_PRODUCT_KIT_UNMUTABLE_NO_LIST => t('As a unit. Customers may only change how many kits they are buying. Do not list component products.'), UC_PRODUCT_KIT_UNMUTABLE_WITH_LIST => t('As a unit. Customers may only change how many kits they are buying. List component products.'), UC_PRODUCT_KIT_MUTABLE => t('As individual products. Customers may add or remove kit components at will.'), ), '#default_value' => variable_get('uc_product_kit_mutable', 0), '#weight' => -5, ); } } /** * Implementation of hook_form(). * * @ingroup forms */ function uc_product_kit_form(&$node) { $form = array(); $form['title'] = array('#type' => 'textfield', '#title' => t('Name'), '#required' => TRUE, '#weight' => -5, '#default_value' => $node->title, '#description' => t('Name of the product kit') ); $form['body_filter']['body'] = array('#type' => 'textarea', '#title' => t('Description'), '#required' => FALSE, '#default_value' => $node->body, '#rows' => 20, '#description' => t('Explain these whatchamacallits.'), ); $form['body_filter']['format'] = filter_form($node->format); $form['body_filter']['#weight'] = -4; $product_types = module_invoke_all('product_types'); $products = array(); unset($product_types[array_search('product_kit', $product_types)]); $result = db_query("SELECT nid, title FROM {node} WHERE type IN ('". implode("','", $product_types) ."') ORDER BY title, nid"); while ($product = db_fetch_object($result)) { $products[$product->nid] = $product->title; } $form['base'] = array('#type' => 'fieldset', '#title' => t('Product Information'), '#collapsible' => true, '#collapsed' => false, '#weight' => -1, '#attributes' => array('class' => 'product-field'), ); $form['base']['mutable'] = array('#type' => 'radios', '#title' => t('How is this product kit handled by the cart?'), '#options' => array( UC_PRODUCT_KIT_UNMUTABLE_NO_LIST => t('As a unit. Customers may only change how many kits they are buying. Do not list component products.'), UC_PRODUCT_KIT_UNMUTABLE_WITH_LIST => t('As a unit. Customers may only change how many kits they are buying. List component products.'), UC_PRODUCT_KIT_MUTABLE => t('As individual products. Customers may add or remove kit components at will.'), ), '#default_value' => isset($node->mutable) ? $node->mutable : variable_get('uc_product_kit_mutable', UC_PRODUCT_KIT_UNMUTABLE_WITH_LIST), ); $form['base']['products'] = array('#type' => 'select', '#multiple' => true, '#required' => true, '#title' => t('Products'), '#options' => $products, '#default_value' => isset($node->products) ? array_keys($node->products) : array(), ); $form['base']['items'] = array('#tree' => true); if (isset($node->products)) { foreach ($node->products as $i => $product) { $form['base']['items'][$i] = array('#type' => 'fieldset', //'#title' => $product->title, ); $form['base']['items'][$i]['link'] = array('#type' => 'item', '#value' => l($product->title, 'node/'. $i), ); $form['base']['items'][$i]['qty'] = array('#type' => 'textfield', '#title' => t('Quantity'), '#default_value' => $product->qty, '#size' => 5, ); $form['base']['items'][$i]['ordering'] = array('#type' => 'weight', '#title' => t('Ordering'), '#default_value' => isset($product->ordering) ? $product->ordering : 0, ); $item = node_load($i); $form['base']['items'][$i]['discount'] = array('#type' => 'textfield', '#title' => t('Discount'), '#description' => t('Enter a negative value to lower the item price by that amount. Enter a postive value to set the item price to that amount. This discount is applied to each %product in the kit.', array('%product' => $product->title)), '#default_value' => (is_null($product->discount) || $product->discount === '' ? $item->sell_price : $product->discount), '#size' => 5, ); } } $form['base']['default_qty'] = array('#type' => 'textfield', '#title' => t('Default quantity to add to cart'), '#default_value' => !is_null($node->default_qty) ? $node->default_qty : 1, '#description' => t('Leave blank or zero to disable the quantity field in the add to cart form.'), '#weight' => 27, '#size' => 5, '#maxlength' => 6, ); $form['base']['ordering'] = array('#type' => 'weight', '#title' => t('List order'), '#delta' => 25, '#default_value' => $node->ordering, '#weight' => 30, ); return $form; } /** * Implementation of hook_view(). */ function uc_product_kit_view($node, $teaser = 0, $page = 0) { $node = node_prepare($node, $teaser); $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, )); $weight = variable_get('uc_product_field_weight', array( 'image' => -2, 'display_price' => -1, 'model' => 0, 'list_price' => 2, 'cost' => 3, 'sell_price' => 4, 'weight' => 5, 'dimensions' => 6, 'add_to_cart' => 10, )); if (isset($node->field_image_cache) && file_exists($node->field_image_cache[0]['filepath'])) { $node->content['image'] = array('#value' => theme('uc_product_image', $node->field_image_cache), '#access' => $enabled['image'] && module_exists('imagecache'), '#weight' => $weight['image'], ); } $node->content['display_price'] = array('#value' => theme('uc_product_display_price', $node->sell_price), '#access' => $enabled['display_price'], '#weight' => $weight['display_price'], ); if (!$teaser) { $node->content['model'] = array('#value' => theme('uc_product_model', $node->model), '#access' => $enabled['model'], '#weight' => $weight['model'], ); $node->content['body']['#weight'] = 1; $node->content['list_price'] = array('#value' => theme('uc_product_price', $node->list_price, 'list_price'), '#access' => $enabled['list_price'], '#weight' => $weight['list_price'], ); $node->content['cost'] = array('#value' => theme('uc_product_price', $node->cost, 'cost'), '#access' => $enabled['cost'] && user_access('administer products'), '#weight' => $weight['cost'], ); } else { $node->content['#attributes'] = array('style' => 'display: inline'); } $node->content['sell_price'] = array('#value' => theme('uc_product_sell_price', $node->sell_price, $teaser), '#access' => $enabled['sell_price'], '#weight' => $weight['sell_price'], ); if (!$teaser) { $node->content['weight'] = array('#value' => theme('uc_product_weight', $node->weight, $node->weight_units), '#access' => $enabled['weight'], '#weight' => $weight['weight'], ); if ($node->mutable != UC_PRODUCT_KIT_UNMUTABLE_NO_LIST) { $node->content['products'] = array('#weight' => 6); foreach ($node->products as $product) { if (node_access('view', $product)) { $title = l($product->title, 'node/'. $product->nid); } else { $title = check_plain($product->title); } $node->content['products'][$product->nid]['qty'] = array('#value' => '
'. print_r($kit, true) .''; if ($kit->mutable == UC_PRODUCT_KIT_MUTABLE) { return uc_product_cart_display($item); } else { if (!isset($products[$unique_id])) { // Initialize table row $element = array(); $element['nid'] = array('#type' => 'value', '#value' => $kit->nid); $element['module'] = array('#type' => 'value', '#value' => 'uc_product_kit'); $element['remove'] = array('#type' => 'checkbox'); if ($kit->mutable == UC_PRODUCT_KIT_UNMUTABLE_WITH_LIST) { $element['options'] = array('#value' => '