You are here

function glossary_block in Glossary 5.2

Same name and namespace in other branches
  1. 5 glossary.module \glossary_block()
  2. 6 glossary.module \glossary_block()

Implementation of hook_block().

File

./glossary.module, line 56
Glossary terms will be automatically marked with links to their descriptions.

Code

function glossary_block($op = 'list', $delta = 0, $edit = array()) {
  $blocks = array();
  switch ($op) {
    case 'list':
      $blocks[0]['info'] = t('Glossary: Search');
      $blocks[1]['info'] = t('Glossary: Random');
      return $blocks;
    case 'view':
      switch ($delta) {
        case 0:
          $blocks['subject'] = t('Search Glossary');
          $blocks['content'] = drupal_get_form('glossary_search_form');
          return $blocks;
        case 1:
          $interval = variable_get("glossary_block_{$delta}_interval", 0) * variable_get("glossary_block_{$delta}_step", 0);
          $last = variable_get("glossary_block_{$delta}_last", 0);
          if ($last + $interval < time()) {

            // Time to get a new selection.
            $saved_vids = variable_get("glossary_block_{$delta}_vids", NULL);
            if (is_NULL($saved_vids)) {
              $blocks['content'] = t('Glossary block !blk has not been configured.', array(
                '!blk' => $delta,
              ));
              return $blocks;
            }
            $vids = array_filter($saved_vids);
            if (count($vids) == 0) {
              $vids = _glossary_get_filter_vids();
            }
            $placeholders = implode(',', array_fill(0, count($vids), '%d'));
            $tid = db_result(db_query_range('SELECT tid FROM {term_data} WHERE vid in (' . $placeholders . ') ORDER BY RAND()', $vids, 0, 1));

            // Set now as the last selection and save that tid.
            variable_set("glossary_block_{$delta}_last", time());
            variable_set("glossary_block_{$delta}_tid", $tid);
          }
          else {

            // Get the current selected tid.
            $tid = variable_get("glossary_block_{$delta}_tid", 0);
          }
          $term = taxonomy_get_term($tid);
          $blocks['content'] = theme('glossary_block_term', $term, variable_get("glossary_block_{$delta}_link", TRUE));
          return $blocks;
      }
    case 'configure':
      $form = array();
      switch ($delta) {
        case 0:

          // Search block - no config.
          return $form;
        case 1:
          $vids = array();
          $vid_list = _glossary_get_filter_vids();
          foreach ($vid_list as $vid) {
            $voc = taxonomy_get_vocabulary($vid);
            $vids[$vid] = check_plain($voc->name);
          }
          $form['vids'] = array(
            '#type' => 'checkboxes',
            '#title' => t('Choose from'),
            '#description' => t('Select the vocabularies from which to choose a term.'),
            '#options' => $vids,
            '#default_value' => variable_get("glossary_block_{$delta}_vids", array()),
            '#prefix' => '<div class="glossary_checkboxes">',
            '#suffix' => '</div>',
          );
          $form['interval'] = array(
            '#type' => 'textfield',
            '#size' => 4,
            '#maxlength' => 3,
            '#default_value' => variable_get("glossary_block_{$delta}_interval", 0),
            '#field_prefix' => '<strong>' . t('Update every') . '</strong>&nbsp;',
            '#prefix' => '<div class="container-inline glossary-interval">',
          );
          $form['step'] = array(
            '#type' => 'select',
            '#default_value' => variable_get("glossary_block_{$delta}_step", 0),
            '#options' => array(
              1 => t('seconds'),
              60 => t('minutes'),
              3600 => t('hours'),
              86400 => t('days'),
            ),
            '#suffix' => '</div>',
            '#description' => t('How often do you want a new term? Leaving this blank or zero means every time.'),
          );
          $form['link'] = array(
            '#type' => 'checkbox',
            '#title' => t('Show term as link'),
            '#default_value' => variable_get("glossary_block_{$delta}_link", TRUE),
            '#suffix' => '</div>',
            '#description' => t('If selected, this option causes the term name to be made a link to the glossary entry.'),
          );
          return $form;
      }
      return $form;
    case 'save':
      switch ($delta) {
        case 0:

          // Search block - no config.
          break;
        case 1:
          variable_set("glossary_block_{$delta}_vids", $edit['vids']);
          if (!$edit['interval'] || !is_numeric($edit['interval'])) {

            // Make interval numeric;
            $edit['interval'] = 0;
          }
          variable_set("glossary_block_{$delta}_interval", $edit['interval']);
          variable_set("glossary_block_{$delta}_step", $edit['step']);
          variable_set("glossary_block_{$delta}_link", $edit['link']);
          break;
      }
  }
}