You are here

function lexicon_block in Lexicon 6

Implementation of hook_block().

File

./lexicon.module, line 61
Lexicon is used to create lists of terms and definitions to use on a website and optionally mark them in the content with an indicator.

Code

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

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

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

            // Get the current selected tid.
            $tid = variable_get("lexicon_block_{$delta}_tid", 0);
            $vid = variable_get("lexicon_block_{$delta}_vid", 0);
          }
          $term = taxonomy_get_term($tid);
          $blocks['content'] = theme('lexicon_block_term', $vid, $term, variable_get("lexicon_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 = variable_get('lexicon_vids', array());
          foreach ($vid_list as $vid) {
            if ($vid != 0) {
              $voc = taxonomy_vocabulary_load($vid);
              $vids[$vid] = check_plain($voc->name);
            }
          }
          $form['vids'] = array(
            '#type' => 'checkboxes',
            '#title' => t('Choose from'),
            '#description' => t('Select the vocabularies to choose a term from.'),
            '#options' => $vids,
            '#default_value' => variable_get("lexicon_block_{$delta}_vids", array()),
            '#prefix' => '<div class="lexicon_checkboxes">',
            '#suffix' => '</div>',
          );
          $form['interval'] = array(
            '#type' => 'textfield',
            '#size' => 4,
            '#maxlength' => 3,
            '#default_value' => variable_get("lexicon_block_{$delta}_interval", 0),
            '#field_prefix' => '<strong>' . t('Update every') . '</strong>&nbsp;',
            '#prefix' => '<div class="container-inline lexicon-interval">',
          );
          $form['step'] = array(
            '#type' => 'select',
            '#default_value' => variable_get("lexicon_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("lexicon_block_{$delta}_link", TRUE),
            '#description' => t('If selected, this option causes the term name to be made a link to the lexicon entry.'),
          );
          return $form;
      }
      return $form;
    case 'save':
      switch ($delta) {
        case 0:

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

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