You are here

function uc_cart_block in Ubercart 5

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

Implementation of hook_block().

File

uc_cart/uc_cart.module, line 213

Code

function uc_cart_block($op = 'list', $delta = 0, $edit = array()) {
  if ($op == 'list') {
    $blocks[0]['info'] = t('Shopping cart');
    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) ? 'true' : 'false';
      uc_add_js('var collapsed_block = ' . $val . ';', 'inline');
      uc_add_js("var uc_cart_path = '" . $uc_cart_path . "';", 'inline');
      uc_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.<br />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']));
  }
}