You are here

function uc_cart_block_view in Ubercart 7.3

Implements hook_block_view().

File

uc_cart/uc_cart.module, line 303

Code

function uc_cart_block_view($delta = '') {
  global $user;
  if ($delta == 'cart') {
    $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) {

      // 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 (variable_get('uc_cart_block_collapsible', TRUE)) {
        drupal_add_js(drupal_get_path('module', 'uc_cart') . '/uc_cart_block.js');
      }

      // First build the help text.
      $help_text = FALSE;

      // uc_cart_help_text variable is only here for backward compatibility.
      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) {
        $display_items = entity_view('uc_cart_item', uc_cart_get_contents(), 'cart');
        foreach (element_children($display_items['uc_cart_item']) as $key) {
          $display_item = $display_items['uc_cart_item'][$key];
          if (count(element_children($display_item))) {
            $items[] = array(
              'nid' => $display_item['nid']['#value'],
              'qty' => theme('uc_qty', array(
                'qty' => $display_item['qty']['#default_value'],
              )),
              'title' => $display_item['title']['#markup'],
              'price' => $display_item['#total'],
              'desc' => isset($display_item['description']['#markup']) ? $display_item['description']['#markup'] : FALSE,
            );
            $total += $display_item['#total'];
            $item_count += $display_item['qty']['#default_value'];
          }
        }
      }

      // Build the item count text and cart links.
      $item_text = format_plural($item_count, '<span class="num-items">1</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['subject'] = t('Shopping cart');
      $block['content'] = theme('uc_cart_block_content', array(
        'help_text' => $help_text,
        'items' => $items,
        'item_count' => $item_count,
        'item_text' => $item_text,
        'total' => $total,
        'summary_links' => $summary_links,
        'collapsed' => variable_get('uc_cart_block_collapsed', TRUE),
      ));
      return $block;
    }
  }
}