You are here

function similar_block in Similar Entries 6

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

Implements hook_block().

File

./similar.module, line 28
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');
      $blocks[0]['cache'] = BLOCK_CACHE_PER_PAGE | BLOCK_CACHE_PER_ROLE;
      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,
          );
        }
      }
      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']);
        }
      }
      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');
            $block['content'] = theme('similar_content', $node);
        }
      }
      return empty($block['content']) ? '' : $block;
      break;
  }
}