=== modified file 'uc_cart/uc_cart.module' --- uc_cart/uc_cart.module 2009-01-13 16:27:26 +0000 +++ uc_cart/uc_cart.module 2009-01-30 15:58:11 +0000 @@ -174,7 +174,11 @@ return $items; } +/** + * Implementation of hook_enable(). + */ function uc_cart_enable() { + // Create an Imagecache preset for the cart view form. if (module_exists('imagecache')) { $preset_name = 'cart'; $preset = imagecache_preset_by_name($preset_name); @@ -207,11 +211,23 @@ function uc_cart_theme() { return array( 'uc_cart_block_title' => array( - 'arguments' => array('cart_image' => NULL, 'arrow_up_image' => NULL), + 'arguments' => array('title' => NULL, 'title_class' => NULL, 'cart_image' => FALSE, 'arrow_up_image' => FALSE), + ), + 'uc_cart_block_title_icon' => array( + 'arguments' => array(), + ), + 'uc_cart_block_content_cachable' => array( + 'arguments' => array(), ), 'uc_cart_block_content' => array( 'arguments' => array(), ), + 'uc_cart_block_items' => array( + 'arguments' => array(), + ), + 'uc_cart_block_summary' => array( + 'arguments' => array(), + ), 'uc_empty_cart' => array( 'arguments' => array(), ), @@ -252,187 +268,6 @@ } /** - * Implementation of hook_block(). - */ -function uc_cart_block($op = 'list', $delta = 0, $edit = array()) { - if ($op == 'list') { - $blocks[0]['info'] = t('Shopping cart'); - $blocks[0]['cache'] = BLOCK_NO_CACHE; - return $blocks; - } - elseif ($op == 'view') { - $uc_cart_path = base_path() . drupal_get_path('module', 'uc_cart'); - - if (variable_get('uc_cart_block_collapsible', TRUE)) { - $val = variable_get('uc_cart_block_collapsed', TRUE); - drupal_add_js(array('ucCollapsedBlock' => $val), 'setting'); - drupal_add_js(drupal_get_path('module', 'uc_cart') .'/uc_cart_block.js'); - } - - drupal_add_css(drupal_get_path('module', 'uc_cart') .'/uc_cart_block.css'); - - $item_count = count(uc_cart_get_contents()); - if ($item_count == 0 && variable_get('uc_cart_block_empty_hide', FALSE)) { - return; - } - - $cart_image = $uc_cart_path; - $cart_image .= ($item_count) ? '/images/cart_full.gif' : '/images/cart_empty.gif'; - $arrow_down_image = $uc_cart_path .'/images/bullet-arrow-down.gif'; - $arrow_up_image = $uc_cart_path .'/images/bullet-arrow-up.gif'; - - $block['subject'] = theme('uc_cart_block_title', $cart_image, $arrow_up_image); - $block['content'] = theme('uc_cart_block_content'); - - return $block; - } - elseif ($op == 'configure') { - $form['uc_cart_block_empty_hide'] = array( - '#type' => 'checkbox', - '#title' => t('Hide block if cart is empty.'), - '#default_value' => variable_get('uc_cart_block_empty_hide', FALSE), - ); - $form['uc_cart_block_image'] = array( - '#type' => 'checkbox', - '#title' => t('Display the shopping cart icon in the block title.'), - '#default_value' => variable_get('uc_cart_block_image', TRUE), - ); - $form['uc_cart_block_title'] = array( - '#type' => 'textfield', - '#title' => t('Cart name'), - '#description' => t('This name will be displayed when using the default block title.
Leaving this blank defaults to the translatable name "Shopping Cart."'), - '#default_value' => variable_get('uc_cart_block_title', ''), - ); - $form['uc_cart_block_collapsible'] = array( - '#type' => 'checkbox', - '#title' => t('Make the shopping cart block collapsible by clicking the name or arrow.'), - '#default_value' => variable_get('uc_cart_block_collapsible', TRUE), - ); - $form['uc_cart_block_collapsed'] = array( - '#type' => 'checkbox', - '#title' => t('Display the shopping cart block collapsed by default.'), - '#default_value' => variable_get('uc_cart_block_collapsed', TRUE), - ); - $form['uc_cart_show_help_text'] = array( - '#type' => 'checkbox', - '#title' => t('Display small help text in the shopping cart block.'), - '#default_value' => variable_get('uc_cart_show_help_text', FALSE), - ); - $form['uc_cart_help_text'] = array( - '#type' => 'textfield', - '#title' => t('Cart help text'), - '#description' => t('Displayed if the above box is checked.'), - '#size' => 32, - '#default_value' => variable_get('uc_cart_help_text', t('Click title to display cart contents.')), - ); - - return $form; - } - elseif ($op == 'save' && isset($edit['uc_cart_block_empty_hide'])) { - variable_set('uc_cart_block_empty_hide', $edit['uc_cart_block_empty_hide']); - variable_set('uc_cart_block_image', $edit['uc_cart_block_image']); - variable_set('uc_cart_block_title', $edit['uc_cart_block_title']); - variable_set('uc_cart_block_collapsible', $edit['uc_cart_block_collapsible']); - variable_set('uc_cart_block_collapsed', $edit['uc_cart_block_collapsed']); - variable_set('uc_cart_show_help_text', $edit['uc_cart_show_help_text']); - variable_set('uc_cart_help_text', check_plain($edit['uc_cart_help_text'])); - } -} - -/** - * Theme the shopping cart block title - */ -function theme_uc_cart_block_title($cart_image, $arrow_up_image) { - if (variable_get('uc_cart_block_image', TRUE)) { - $output = l('', 'cart', array('html' => TRUE)); - } - - $title = trim(variable_get('uc_cart_block_title', '')); - if (empty($title)) { - $title = t('Shopping cart'); - } - - if (variable_get('uc_cart_block_collapsible', TRUE)) { - $class = ' cart-block-toggle'; - } - - $output .= '' - .''. check_plain($title) .''; - - if (variable_get('uc_cart_block_collapsible', TRUE)) { - $output .= '' - .'[]'; - } - - return $output; -} - -/** - * Theme the shopping cart block content. - */ -function theme_uc_cart_block_content() { - global $user; - - // Disabled until we figure out if this is actually screwing up caching. -RS - //if (!$user->uid && variable_get('cache', 0) !== 0) { - // return t('View your shopping cart.', array('!url' => url('cart'))); - //} - - if (variable_get('uc_cart_show_help_text', FALSE)) { - $output = '' - . variable_get('uc_cart_help_text', t('Click title to display cart contents.')) - .''; - } - - $output .= '
'; - - $items = uc_cart_get_contents(); - - $item_count = 0; - if (!empty($items)) { - $output .= '' - .''; - foreach ($items as $item) { - $display_item = module_invoke($item->module, 'cart_display', $item); - if (!empty($display_item)) { - $output .= '' - .'' - .''; - - if (isset($display_item['description']['#value'])) { - $output .= ''; - } - } - $total += ($item->price) * $item->qty; - $item_count += $item->qty; - } - - $output .= '
'. $display_item['qty']['#default_value'] .'x'. $display_item['title']['#value'] .''. uc_currency_format($display_item['#total']) .'
'. $display_item['description']['#value'] .'
'; - } - else { - $output .= '

'. t('There are no products in your shopping cart.') .'

'; - } - - $output .= '
'; - - $item_text = format_plural($item_count, '@count Item', '@count Items'); - $view = '('. l(t('View cart'), 'cart', array('attributes' => array('rel' => 'nofollow'))) .')'; - if (variable_get('uc_checkout_enabled', TRUE)) { - $checkout = ' ('. l(t('Checkout'), 'cart/checkout', array('attributes' => array('rel' => 'nofollow'))) .')'; - } - $output .= '' - .''; - if ($item_count > 0) { - $output .= ''; - } - $output .= '
' - . $item_text .'' - .''. t('Total:') .' '. uc_currency_format($total) .'
'. $view . $checkout .'
'; - - return $output; -} - -/** * Implementation of hook_exit(). * * Code from CacheExclude - http://drupal.org/project/cacheexclude @@ -505,6 +340,387 @@ } } +/** + * Implementation of hook_block(). + */ +function uc_cart_block($op = 'list', $delta = 0, $edit = array()) { + global $user; + + switch ($op) { + case 'list': + $blocks = array(); + + // TODO: Add sensible default settings for the cart block based on the + // docs at http://api.drupal.org/api/function/hook_block/6. + $blocks[0] = array( + 'info' => t('Shopping cart'), + 'cache' => BLOCK_NO_CACHE, + ); + + return $blocks; + + case 'configure': + // 0 = Default shopping cart block. + if ($delta == 0) { + return uc_cart_block_settings_form(); + } + break; + + case 'save': + // 0 = Default shopping cart block. + if ($delta == 0) { + uc_cart_block_settings_form_submit($edit); + } + break; + + case 'view': + // 0 = Default shopping cart block. + if ($delta == 0) { + $uc_cart_path = drupal_get_path('module', 'uc_cart'); + $cachable = !$user->uid && variable_get('cache', 0); + $product_count = count(uc_cart_get_contents()); + + // Display nothing if the block is set to hide on empty and there are no + // items in the cart. + if (!$cachable && variable_get('uc_cart_block_empty_hide', FALSE) && !$product_count) { + return; + } + + // Add the cart block CSS. + drupal_add_css(drupal_get_path('module', 'uc_cart') .'/uc_cart_block.css'); + + // If the block is collapsible, add the appropriate JS. + if (!$cachable && variable_get('uc_cart_block_collapsible', TRUE)) { + drupal_add_js(array('ucCollapsedBlock' => variable_get('uc_cart_block_collapsed', TRUE)), 'setting'); + drupal_add_js(drupal_get_path('module', 'uc_cart') .'/uc_cart_block.js'); + $arrow_up_image = $uc_cart_path .'/images/bullet-arrow-up.gif'; + $title_class = 'block-cart-title-bar cart-block-toggle'; + } + else { + $arrow_up_image = FALSE; + $title_class = 'block-cart-title-bar'; + } + + // Build the cart image if enabled. + if (variable_get('uc_cart_block_image', TRUE)) { + // If the cart is empty or we need a cachable cart block... + if ($cachable || !$product_count) { + // Use the "empty" cart icon. + $cart_image = $uc_cart_path .'/images/cart_empty.gif'; + } + else { + // Otherwise use the "full" cart icon. + $cart_image = $uc_cart_path .'/images/cart_full.gif'; + } + } + else { + $cart_image = FALSE; + } + + // Set the text of the block title. + $title = check_plain(trim(variable_get('uc_cart_block_title', ''))); + + if (empty($title)) { + $title = t('Shopping cart'); + } + + // Build the block array for display based on cache settings. + if ($cachable) { + // Caching is turned on and the user is not logged in, so we should + // deliver a block that is safe for caching. + $block = array( + 'subject' => theme('uc_cart_block_title', $title, $title_class, $cart_image), + 'content' => theme('uc_cart_block_content_cachable'), + ); + } + else { + // Otherwise build the whole shebang. + + // First build the help text. + $help_text = FALSE; + + if (variable_get('uc_cart_show_help_text', FALSE) && ($text = variable_get('uc_cart_help_text', t('Click title to display cart contents.')))) { + $help_text = check_plain($text); + } + + $items = FALSE; + $item_count = 0; + $total = 0; + + if ($product_count) { + foreach (uc_cart_get_contents() as $item) { + $display_item = module_invoke($item->module, 'cart_display', $item); + + if (!empty($display_item)) { + $items[] = array( + 'qty' => t('@qtyx', array('@qty' => $display_item['qty']['#default_value'])), + 'title' => $display_item['title']['#value'], + 'price' => $display_item['#total'], + 'desc' => isset($display_item['description']['#value']) ? $display_item['description']['#value'] : FALSE, + ); + } + + $item_count += $item->qty; + $total += $item->price * $item->qty; + } + } + + // Build the item count text and cart links. + $item_text = format_plural($item_count, '@count Item', '@count Items'); + + $view_link = l(t('View cart'), 'cart', array('attributes' => array('rel' => 'nofollow'))); + + if (variable_get('uc_checkout_enabled', TRUE)) { + $checkout_link = l(t('Checkout'), 'cart/checkout', array('attributes' => array('rel' => 'nofollow'))); + } + else { + $checkout_link = FALSE; + } + + $block = array( + 'subject' => theme('uc_cart_block_title', $title, $title_class, $cart_image, $arrow_up_image), + 'content' => theme('uc_cart_block_content', $help_text, $items, $item_count, $item_text, $total, $view_link, $checkout_link), + ); + } + + return $block; + } + break; + } +} + +// Build the settings form used by the shopping cart block. +function uc_cart_block_settings_form() { + $form = array(); + + $form['uc_cart_block_empty_hide'] = array( + '#type' => 'checkbox', + '#title' => t('Hide block if cart is empty.'), + '#default_value' => variable_get('uc_cart_block_empty_hide', FALSE), + ); + $form['uc_cart_block_image'] = array( + '#type' => 'checkbox', + '#title' => t('Display the shopping cart icon in the block title.'), + '#default_value' => variable_get('uc_cart_block_image', TRUE), + ); + $form['uc_cart_block_title'] = array( + '#type' => 'textfield', + '#title' => t('Cart name'), + '#description' => t('This name will be displayed when using the default block title.
Leaving this blank defaults to the translatable name "Shopping Cart."'), + '#default_value' => variable_get('uc_cart_block_title', ''), + ); + $form['uc_cart_block_collapsible'] = array( + '#type' => 'checkbox', + '#title' => t('Make the shopping cart block collapsible by clicking the name or arrow.'), + '#default_value' => variable_get('uc_cart_block_collapsible', TRUE), + ); + $form['uc_cart_block_collapsed'] = array( + '#type' => 'checkbox', + '#title' => t('Display the shopping cart block collapsed by default.'), + '#default_value' => variable_get('uc_cart_block_collapsed', TRUE), + ); + $form['uc_cart_show_help_text'] = array( + '#type' => 'checkbox', + '#title' => t('Display small help text in the shopping cart block.'), + '#default_value' => variable_get('uc_cart_show_help_text', FALSE), + ); + $form['uc_cart_help_text'] = array( + '#type' => 'textfield', + '#title' => t('Cart help text'), + '#description' => t('Displayed if the above box is checked.'), + '#default_value' => variable_get('uc_cart_help_text', t('Click title to display cart contents.')), + '#size' => 32, + ); + + return $form; +} + +// Save the shopping cart block settings. +function uc_cart_block_settings_form_submit($edit = array()) { + variable_set('uc_cart_block_empty_hide', $edit['uc_cart_block_empty_hide']); + variable_set('uc_cart_block_image', $edit['uc_cart_block_image']); + variable_set('uc_cart_block_title', $edit['uc_cart_block_title']); + variable_set('uc_cart_block_collapsible', $edit['uc_cart_block_collapsible']); + variable_set('uc_cart_block_collapsed', $edit['uc_cart_block_collapsed']); + variable_set('uc_cart_show_help_text', $edit['uc_cart_show_help_text']); + variable_set('uc_cart_help_text', $edit['uc_cart_help_text']); +} + +/** + * Theme the shopping cart block title + * + * @param $title + * The text to use for the title of the block. + * @param $title_class + * The class to assign to the title's span based on collapsibility settings. + * @param $cart_image + * Filepath to the shopping cart icon to use or FALSE if the icon is disabled. + * @param $arrow_up_image + * Filepath to the arrow up image or FALSE if collapsibility is disabled. + * @ingroup themeable + */ +function theme_uc_cart_block_title($title, $title_class, $cart_image = FALSE, $arrow_up_image = FALSE) { + $output = ''; + + // Add in the cart image if specified. + if ($cart_image) { + $output .= theme('uc_cart_block_title_icon', $cart_image); + } + + // Add the main title span and text. + $output .= '' + .''. $title .''; + + + // Add the arrow image if specified. + if ($arrow_up_image) { + $output .= '' + . theme('image', $arrow_up_image, '[]', t('Expand the cart block.'), array('id' => 'block-cart-title-arrow')) + .''; + } + + return $output; +} + +/** + * Theme the shopping cart icon. + * + * @param $cart_image + * Filepath to the shopping cart icon to use. + * @ingroup themeable + */ +function theme_uc_cart_block_title_icon($cart_image) { + return l(theme('image', $cart_image, '', t('Shopping cart'), array('id' => 'block-cart-title-image')), 'cart', array('html' => TRUE)); +} + + /** + * Theme the cachable shopping cart block content. + * + * @ingroup themeable + */ +function theme_uc_cart_block_content_cachable() { + return t('View your shopping cart.', array('!url' => url('cart'))); +} + +/** + * Theme the shopping cart block content. + * + * @param $help_text + * Text to place in the small help text area beneath the cart block title or + * FALSE if disabled. + * @param $items + * An associative array of item information containing the keys 'qty', + * 'title', 'price', and 'desc'. + * @param $item_count + * The number of items in the shopping cart. + * @param $item_text + * A textual representation of the number of items in the shopping cart. + * @param $total + * The unformatted total of all the products in the shopping cart. + * @param $view_link + * The HTML for a link to the cart view page or FALSE if the cart is empty. + * @param $checkout_link + * The HTML for a link to the checkout page or FALSE if inaccessible. + * @ingroup themeable + */ +function theme_uc_cart_block_content($help_text, $items, $item_count, $item_text, $total, $view_link, $checkout_link) { + $output = ''; + + // Add the help text if enabled. + if ($help_text) { + $output .= ''. $help_text .''; + } + + // Add a wrapper div for use when collapsing the block. + $output .= '
'; + + // Add a table of items in the cart or the empty message. + $output .= theme('uc_cart_block_items', $items); + + $output .= '
'; + + // Add the summary section beneath the items table. + $output .= theme('uc_cart_block_summary', $item_count, $item_text, $total, $view_link, $checkout_link); + + return $output; +} + +/** + * Theme the table listing the items in the shopping cart block. + * + * @param $items + * An associative array of item information containing the keys 'qty', + * 'title', 'price', and 'desc'. + * @ingroup themeable + */ +function theme_uc_cart_block_items($items) { + // If there are items in the shopping cart... + if ($items) { + $output = ''; + + // Loop through each item. + foreach ($items as $item) { + // Add the basic row with quantity, title, and price. + $output .= '' + .'' + .''; + + // Add a row of description if necessary. + if ($item['desc']) { + $output .= ''; + } + } + + $output .= '
'. $item['qty'] .''. $item['title'] .''. uc_currency_format($item['price']) .'
'. $item['desc'] .'
'; + } + else { + // Otherwise display an empty message. + $output = '

'. t('There are no products in your shopping cart.') .'

'; + } + + return $output; +} + +/** + * Theme the summary table at the bottom of the default shopping cart block. + * + * @param $item_count + * The number of items in the shopping cart. + * @param $item_text + * A textual representation of the number of items in the shopping cart. + * @param $total + * The unformatted total of all the products in the shopping cart. + * @param $view_link + * The HTML for a link to the cart view page or FALSE if the cart is empty. + * @param $checkout_link + * The HTML for a link to the checkout page or FALSE if inaccessible. + * @ingroup themeable + */ +function theme_uc_cart_block_summary($item_count, $item_text, $total, $view_link, $checkout_link) { + // Build the basic table with the number of items in the cart and total. + $output = '' + .'' + .''; + + // If there are products in the cart... + if ($item_count > 0) { + // Add a view cart link. + $output .= ''; + } + + $output .= '
'. $item_text .'' + . t('Total:') .' '. uc_currency_format($total) .'
'; + + return $output; +} + /******************************************************************************* * Hook Functions (Ubercart) @@ -719,9 +935,6 @@ unset($_SESSION['cart_order']); } - // TODO: Review use of cache_clear_all(). Empties temporary cached pages. - cache_clear_all(); - // Update the items in the shopping cart based on the form values. uc_cart_update_item_object((object)$form_state['values']); @@ -1126,8 +1339,6 @@ module_invoke($data['module'], 'update_cart_item', $node->nid, $data, min($qty, 999999), $cid); } - cache_clear_all(); - if ($check_redirect) { if (isset($_GET['destination'])) { drupal_goto(); @@ -1154,7 +1365,6 @@ $cart_id = !(is_null($cid) || empty($cid)) ? $cid : uc_cart_get_id(); db_query("DELETE FROM {uc_cart_products} WHERE cart_id = '%s' AND nid = %d AND data = '%s'", $cart_id, $nid, serialize($data)); - cache_clear_all(); } /** === modified file 'uc_cart/uc_cart.pages.inc' --- uc_cart/uc_cart.pages.inc 2009-01-13 16:27:26 +0000 +++ uc_cart/uc_cart.pages.inc 2009-01-30 15:29:56 +0000 @@ -394,7 +394,6 @@ } function uc_cart_checkout_review_form_back($form, &$form_state) { - cache_clear_all(); unset($_SESSION['do_review']); $form_state['redirect'] = 'cart/checkout'; } === modified file 'uc_cart/uc_cart_block.css' --- uc_cart/uc_cart_block.css 2009-01-21 14:48:10 +0000 +++ uc_cart/uc_cart_block.css 2009-01-30 04:36:42 +0000 @@ -30,17 +30,16 @@ top: -5px; right: -23px; } -.cart-block-table { +.cart-block-items { border-bottom: 2px; padding: 2px; } -.cart-block-tbody { +.cart-block-items tbody { border-top: 0px; } -.cart-block-item { +.cart-block-items tr { vertical-align: top; } -.cart-block-item-qty {} .cart-block-item-title { width: 100%; } @@ -48,13 +47,13 @@ text-align: right; white-space: nowrap; } -.cart-block-summary-table { +.cart-block-summary { padding: 2px; } -.cart-block-summary-tbody { +.cart-block-summary tbody { border-top: 0px; } -.cart-block-summary-tr { +.cart-block-summary tr { vertical-align: top; } .cart-block-summary-items { @@ -64,7 +63,10 @@ width: 100%; text-align: right; } -.cart-block-summary-checkout { +.cart-block-summary-total-label { + font-weight: bold; +} +.cart-block-summary-links { text-align: right; } @@ -79,7 +81,3 @@ font-size: .8em; } - -.cart-block-tbody .product-description li { - -} === modified file 'uc_product/uc_product.module' --- uc_product/uc_product.module 2009-01-20 20:32:43 +0000 +++ uc_product/uc_product.module 2009-01-30 15:29:58 +0000 @@ -1116,7 +1116,6 @@ } else { db_query("UPDATE {uc_cart_products} SET qty = %d, changed = %d WHERE nid = %d AND cart_id = '%s' AND data = '%s'", $qty, time(), $nid, $cid, serialize($data)); - cache_clear_all(); } // Rebuild the items hash