You are here

function taxonomy_image_block in Taxonomy Image 5

Same name and namespace in other branches
  1. 6 contributed/taxonomy_image_blocks/taxonomy_image_blocks.module \taxonomy_image_block()

Implementation of hook_block().

File

contributed/taxonomy_image_blocks/taxonomy_image_blocks.module, line 10
Add a block to show the images attached to the terms of a node.

Code

function taxonomy_image_block($op = 'list', $delta = 0, $edit = array()) {
  global $user;
  drupal_add_css(drupal_get_path('module', 'taxonomy_image') . '/taxonomy_image.css');
  $multi_id = isset($edit['multiblock_delta']) ? '_' . $edit['multiblock_delta']['#value'] : NULL;
  switch ($op) {
    case 'list':
      $blocks[0]['info'] = t('Taxonomy Image: Node Images');
      return $blocks;
    case 'mb_enabled':
      return 'mb_enabled';
    case 'view':
      switch ($delta) {
        case 0:

          // Node Images.
          if (arg(0) != 'node' || !is_numeric(arg(1))) {
            return array();
          }
          $nid = (int) arg(1);
          $rows = array();
          $count = 0;
          $max = variable_get('taxonomy_image_block_max_images' . $multi_id, 3);
          $suppress = variable_get('taxonomy_image_block_suppress' . $multi_id, FALSE);
          $size = variable_get('taxonomy_image_block_max_size' . $multi_id, 32);
          $imagecache_preset = variable_get('taxonomy_image_block_imagecache_preset' . $multi_id, 'ORIGINAL');
          if ($node = node_load($nid)) {
            foreach ($node->taxonomy as $term) {
              if ($imagecache_preset != 'ORIGINAL') {
                $img = taxonomy_image_display($term->tid, array(), $imagecache_preset);
              }
              else {
                if ($size == 'ORIGINAL') {
                  $img = taxonomy_image_display($term->tid, array(), 'ORIGINAL', array(
                    'resize' => 0,
                  ));
                }
                else {
                  $img = taxonomy_image_display($term->tid, array(), 'ORIGINAL', array(
                    'resize' => 1,
                    'width' => $size,
                    'height' => $size,
                  ));
                }
              }
              if ($img || !$suppress) {
                $term_path = drupal_get_path_alias(taxonomy_term_path($term));
                $name_link = '<div class="taxonomy-image-block-name">' . l($term->name, $term_path) . '</div>';
                if (user_access('administer taxonomy')) {
                  $name_link .= '<div class="taxonomy-image-block-edit">' . l('edit term', 'admin/content/taxonomy/edit/term/' . $term->tid, array(
                    'class' => 'links inline',
                  )) . '</div>';
                }
                $rows[] = array(
                  l($img, $term_path, array(), NULL, NULL, FALSE, TRUE) . $name_link . $edit_link,
                );
                ++$count;
              }

              // Have we hit the limit?
              if ($count == $max) {
                break;
              }
            }
          }
          if ($rows) {
            $subs = array(
              '@title' => check_plain($node->title),
              '!nid' => $node->nid,
              '!vid' => $node->vid,
              '!type' => $node->type,
            );
            $block = array(
              'subject' => strtr(variable_get('taxonomy_image_block_title', 'Term Images for "@title"'), $subs),
              'content' => theme('table', array(), $rows, array(
                'id' => 'taxonomy_image_terms',
              )),
            );
          }
          break;
      }
      return $block;
    case 'configure':
      switch ($delta) {
        case 0:
          $form['ti'] = array(
            '#type' => 'fieldset',
            '#title' => t('Taxonomy Image '),
            '#collapsible' => TRUE,
          );
          $form['ti']['taxonomy_image_block_max_images'] = array(
            '#type' => 'textfield',
            '#size' => 6,
            '#title' => t('Number of images to show'),
            '#description' => t('This controls the number of images that appear in the "Node Images" block.'),
            '#default_value' => variable_get('taxonomy_image_block_max_images' . $multi_id, 3),
          );
          $form['ti']['taxonomy_image_block_suppress'] = array(
            '#type' => 'checkbox',
            '#title' => t('Suppress term if no image'),
            '#description' => t('Do not show the term if it has no image.'),
            '#default_value' => variable_get('taxonomy_image_block_suppress' . $multi_id, FALSE),
          );
          if (module_exists('imagecache')) {
            $form['ti']['taxonomy_image_block_imagecache_preset'] = array(
              '#type' => 'radios',
              '#title' => t('Imagecache Preset'),
              '#description' => t('The Imagecache module is available. If you select one of these presets, or provide one in your call, then Taxonomy Image will use Imagecache to process the image and ignore the following settings. "ORIGINAL" effectively disables Imagecache and uses the following settings.'),
              '#options' => drupal_map_assoc(_taxonomy_image_presets()),
              '#default_value' => variable_get('taxonomy_image_block_imagecache_preset' . $multi_id, NULL),
              '#prefix' => '<div class="taxonomy_image_radios">',
              '#suffix' => '</div>',
            );
          }
          else {
            $form['ti']['taxonomy_image_block_imagecache_preset'] = array(
              '#type' => 'value',
              '#value' => 'ORIGINAL',
            );
          }
          $form['ti']['taxonomy_image_block_max_size'] = array(
            '#type' => 'radios',
            '#options' => drupal_map_assoc(array(
              'ORIGINAL',
              16,
              24,
              32,
              48,
              64,
              80,
              96,
            )),
            '#title' => t('Maximum size'),
            '#description' => t('This limits the size of images that appear in the "Node Images" block. This is the size, in pixels, of the longer side of the image. Selecting "ORIGINAL" will keep the image at it\'s original size.'),
            '#default_value' => variable_get('taxonomy_image_block_max_size' . $multi_id, 32),
            '#prefix' => '<div class="taxonomy_image_radios">',
            '#suffix' => '</div>',
          );
          break;
      }
      return $form;
    case 'save':
      switch ($delta) {
        case 0:

          // Node Images.
          variable_set('taxonomy_image_block_max_images' . $multi_id, $edit['taxonomy_image_block_max_images']);
          variable_set('taxonomy_image_block_suppress' . $multi_id, $edit['taxonomy_image_block_suppress']);
          variable_set('taxonomy_image_block_max_size' . $multi_id, $edit['taxonomy_image_block_max_size']);
          variable_set('taxonomy_image_block_imagecache_preset' . $multi_id, $edit['taxonomy_image_block_imagecache_preset']);
          variable_set('taxonomy_image_block_title' . $multi_id, $edit['title']);
          if (!$multi_id) {

            // Don't let blocks.module have the real title.
            db_query("UPDATE {blocks} SET title='' WHERE module='taxonomy_image' AND delta=0");
          }
          break;
      }
      return;
  }

  // end switch($op)
}