You are here

function similar_block in Similar Entries 5

Same name and namespace in other branches
  1. 6 similar.module \similar_block()

Implementation of hook_block().

This hook both declares to Drupal what blocks are provided by the module, and generates the contents of the blocks themselves.

Parameters

string $op:

integer $delta:

array $edit:

File

./similar.module, line 156
Module that shows a block listing similar entries. NOTE: Uses MySQL's FULLTEXT indexing for MyISAM tables.

Code

function similar_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {
    case 'list':
      $blocks[0]['info'] = t('Similar entries');
      return $blocks;
    case 'configure':
      $form = array();
      if ($delta == 0) {
        $form['similar_teaser_enabled'] = array(
          '#type' => 'radios',
          '#title' => t('Include teaser text'),
          '#default_value' => variable_get('similar_teaser_enabled', 0),
          '#options' => array(
            t('No'),
            t('Yes'),
          ),
        );
        $form['similar_rel_nofollow'] = array(
          '#type' => 'radios',
          '#title' => t('Block search engines'),
          '#description' => t('Adds rel="nofollow" to the HTML source of similar links so search engines won\'t count similar links in their ranking calculations.'),
          '#default_value' => variable_get('similar_rel_nofollow', 0),
          '#options' => array(
            t('No'),
            t('Yes'),
          ),
        );
        for ($i = 1, $options = array(); $i < 101; $options[$i] = $i, $i += 1) {
        }
        $form['similar_num_display'] = array(
          '#type' => 'select',
          '#title' => t('Number of similar entries to find'),
          '#default_value' => variable_get('similar_num_display', 5),
          '#options' => $options,
        );
        $types = _similar_published_node_types();
        $form['similar_node_types'] = array(
          '#type' => 'checkboxes',
          '#multiple' => true,
          '#title' => t('Node types to display'),
          '#default_value' => variable_get('similar_node_types', $types),
          '#options' => $types,
        );
        if (module_exists('taxonomy')) {
          $names = _similar_taxonomy_names();
          $form['similar_taxonomy'] = array(
            '#type' => 'fieldset',
            '#title' => t('Taxonomy category filter'),
            '#collapsible' => TRUE,
            '#collapsed' => TRUE,
          );
          $form['similar_taxonomy']['similar_taxonomy_filter'] = array(
            '#type' => 'radios',
            '#title' => t('Filter by taxonomy categories'),
            '#default_value' => variable_get('similar_taxonomy_filter', 0),
            '#options' => array(
              t('No category filtering'),
              t('Only show the similar nodes in the same category as the original node'),
              t('Use global category filtering'),
            ),
            '#description' => t('By selecting global filtering, only nodes assigned to the following selected categories will display as similar nodes, regardless of the categories the original node is or is not assigned to.'),
          );
          $form['similar_taxonomy']['similar_taxonomy_select'] = array(
            '#type' => 'fieldset',
            '#title' => t('Taxonomy categories to display'),
            '#collapsible' => TRUE,
            '#collapsed' => TRUE,
          );
          $form['similar_taxonomy']['similar_taxonomy_select']['similar_taxonomy_tids'] = array(
            '#type' => 'select',
            '#default_value' => variable_get('similar_taxonomy_tids', array_keys($names)),
            '#description' => t('Hold the CTRL key to (de)select multiple options.'),
            '#options' => $names,
            '#multiple' => TRUE,
          );
          $vocs = _similar_taxonomy_get_vocabularies();
          $form['similar_taxonomy']['similar_taxonomy_exclude'] = array(
            '#type' => 'select',
            '#multiple' => TRUE,
            '#title' => t('Exclude vocabularies'),
            '#default_value' => variable_get('similar_taxonomy_exclude', ''),
            '#options' => $vocs,
          );
        }
      }
      return $form;
    case 'save':
      if ($delta == 0) {
        variable_set('similar_teaser_enabled', $edit['similar_teaser_enabled']);
        variable_set('similar_rel_nofollow', $edit['similar_rel_nofollow']);
        variable_set('similar_num_display', $edit['similar_num_display']);
        variable_set('similar_node_types', $edit['similar_node_types']);
        if (module_exists('taxonomy')) {
          variable_set('similar_taxonomy_filter', $edit['similar_taxonomy_filter']);
          variable_set('similar_taxonomy_tids', $edit['similar_taxonomy_tids']);
          variable_set('similar_taxonomy_exclude', $edit['similar_taxonomy_exclude']);
        }
      }
      return;
    case 'view':
    default:
      if (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) != 'edit') {
        $node = node_load(array(
          'nid' => arg(1),
        ));
      }
      else {
        return;
      }
      $similar_node_types = variable_get('similar_node_types', _similar_published_node_types());
      if ($node->nid > 0 && !empty($similar_node_types[$node->type])) {
        unset($similar_node_types);
        switch ($delta) {
          case 0:

            // The subject is displayed at the top of the block. Note that it should
            // be passed through t() for translation.
            $block['subject'] = t('Similar entries');
            $cache_blocks = variable_get('similar_cache', SIMILAR_CACHE_DISABLED);
            if ($cache_blocks) {
              $cached_content = cache_get("similar_block_0:{$node->nid}", 'cache');
              if ($cached_content == 0) {

                // cache_get() only returns 0 when data is not returned for the key
                $block['content'] = theme('similar_content', $node);
                $lifetime = variable_get('similar_cache_lifetime', SIMILAR_CACHE_LIFETIME);
                cache_set("similar_block_0:{$node->nid}", 'cache', $block['content'], $lifetime ? time() + $lifetime : CACHE_TEMPORARY);
              }
              else {
                $block['content'] = $cached_content->data;
              }
            }
            else {
              $block['content'] = theme('similar_content', $node);
            }
        }
      }
      return empty($block['content']) ? '' : $block;
      break;
  }
}