You are here

function similarterms_block in Similar By Terms 5

Same name and namespace in other branches
  1. 6 similarterms.module \similarterms_block()

Implementation of hook_block().

File

./similarterms.module, line 13
Similar By Terms module displays a block with similar content based on taxonomy terms.

Code

function similarterms_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {
    case 'list':
      $blocks[]['info'] = t('Similar entries from ANY vocabulary.');
      if (variable_get('similarterms_vocabularies', 'multi_select_and_tags') == 'all') {
        foreach (taxonomy_get_vocabularies() as $v) {
          $blocks[$v->vid]['info'] = t('Similar entries from the @vocab vocabulary.', array(
            '@vocab' => $v->name,
          ));
        }
      }
      else {
        foreach (taxonomy_get_vocabularies() as $v) {

          // this only makes sense for multi-select vocabularies and free tagging
          if ($v->multiple || $v->tags) {
            $blocks[$v->vid]['info'] = t('Similar entries from the @vocab vocabulary.', array(
              '@vocab' => $v->name,
            ));
          }
        }
      }
      return $blocks;
    case 'configure':
      $form['count'] = array(
        '#type' => 'textfield',
        '#title' => t('Item count'),
        '#default_value' => variable_get('simterms_count_' . $delta, 5),
        '#size' => 3,
        '#description' => t('The maximum number of similar items to display'),
      );

      //petertj addition to configuration to permit display of current node in list
      $form['showcurrentnode'] = array(
        '#type' => 'checkbox',
        '#title' => t('Show current node as active in the list'),
        '#default_value' => variable_get('simterms_showcurrentnode_' . $delta, FALSE),
        '#required' => FALSE,
      );

      //mimo addition to configuration to limit to same page type
      $types = array(
        '0' => '<none>',
        '1' => t('same as current'),
      );
      $arr_types_obj = node_get_types();
      foreach ($arr_types_obj as $type => $obj) {
        $types[$type] = $obj->name;
      }
      $form['sametype'] = array(
        '#type' => 'select',
        '#title' => t('Content type limit'),
        '#default_value' => variable_get('simterms_sametype_' . $delta, FALSE),
        '#options' => $types,
        '#description' => t('Limit to pages of this or these content type(s)'),
        '#multiple' => TRUE,
      );
      if ($delta > 0) {
        $terms = array();
        $tree = taxonomy_get_tree($delta);
        foreach ($tree as $term) {
          $terms[$term->tid] = $term->name;
        }
        $form['ignoreterms'] = array(
          '#type' => 'select',
          '#title' => t('Terms to be ignored'),
          '#default_value' => variable_get('simterms_ignoreterms_' . $delta, array()),
          '#options' => $terms,
          '#description' => t('Ignore selected terms here from being used to search for similar entries'),
          '#multiple' => TRUE,
        );
      }
      return $form;
    case 'save':
      variable_set('simterms_count_' . $delta, $edit['count']);
      variable_set('simterms_sametype_' . $delta, $edit['sametype']);
      variable_set('simterms_ignoreterms_' . $delta, $edit['ignoreterms']);
      variable_set('similarterms_showcurrentnode_' . $delta, $edit['showcurrentnode']);
      return;
    case 'view':
      if ($delta == 0) {
        $block['subject'] = t('Similar');
        $block['content'] = theme('similarterms', variable_get('similarterms_display_options', 'title_only'), similarterms_list());
      }
      else {
        $block['subject'] = t('Similar');

        // $delta = $vocid - integer - vocabulary id, leave out to use ALL terms for this node
        $block['content'] = theme('similarterms', variable_get('similarterms_display_options', 'title_only'), similarterms_list($delta));
      }
      return $block;
  }
}