You are here

function uc_cart_block in Ubercart 6.2

Same name and namespace in other branches
  1. 5 uc_cart/uc_cart.module \uc_cart_block()

Implements hook_block().

File

uc_cart/uc_cart.module, line 344

Code

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) {

        // Pressflow has a function to determine cacheability.
        if (function_exists('drupal_page_is_cacheable')) {
          $cachable = drupal_page_is_cacheable();
        }
        else {
          $cachable = !$user->uid && variable_get('cache', CACHE_DISABLED) != CACHE_DISABLED;
        }
        $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 (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');
        }

        // Hack in some CSS to hide the block if necessary. drupal_add_css() is in Drupal 7 for this...
        if (variable_get('uc_cart_block_collapsed', TRUE)) {
          drupal_set_html_head("<style type='text/css'>#cart-block-contents { display: none; }</style>");
        }

        // Build the block content for display based on cache settings.
        $block['subject'] = t('Shopping cart');
        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['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(
                  'nid' => $display_item['nid']['#value'],
                  'qty' => t('@qty&times;', 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,
                );
                $total += $display_item['#total'];
              }
              $item_count += $item->qty;
            }
          }

          // Build the item count text and cart links.
          $item_text = format_plural($item_count, '<span class="num-items">@count</span> Item', '<span class="num-items">@count</span> Items');
          $summary_links = array(
            'cart-block-view-cart' => array(
              'title' => t('View cart'),
              'href' => 'cart',
              'attributes' => array(
                'rel' => 'nofollow',
              ),
            ),
          );

          // Only add the checkout link if checkout is enabled.
          if (variable_get('uc_checkout_enabled', TRUE)) {
            $summary_links['cart-block-checkout'] = array(
              'title' => t('Checkout'),
              'href' => 'cart/checkout',
              'attributes' => array(
                'rel' => 'nofollow',
              ),
            );
          }
          $block['content'] = theme('uc_cart_block_content', $help_text, $items, $item_count, $item_text, $total, $summary_links);
        }
        return $block;
      }
      break;
  }
}