You are here

function lexicon_block_view in Lexicon 7

Implements hook_block_view().

File

./lexicon.module, line 220
The Lexicon module is used to create lists of terms and definitions to use on a website and optionally mark those terms in the content of the website.

Code

function lexicon_block_view($delta) {
  $block = array();
  global $language;
  $lang = $language->language;
  switch ($delta) {
    case 'lexicon_random_term':
      $interval = variable_get('lexicon_block_lexicon_random_term_interval', 0) * variable_get('lexicon_block_lexicon_random_term_step', 0);
      $last = variable_get('lexicon_block_lexicon_random_term_last_{$lang}', 0);

      // Check if the configured interval time has expired.
      if ($last + $interval < REQUEST_TIME) {

        // Time to load a new term.
        $vids = variable_get('lexicon_block_lexicon_random_term_vids', array());

        // Get rid off any array items with value '0'.
        $vids = array_filter($vids);

        // Check if the block is configured properly.
        if (is_null($vids) || empty($vids)) {
          $block['content'] = t('The Lexicon random term block is not properly configured.');
          return $block;
        }

        // Retrieve a random term from one of the Lexicon vocabularies.
        $query = db_select('taxonomy_term_data', 'ttd')
          ->fields('ttd', array(
          'tid',
        ))
          ->condition('ttd.vid', $vids, 'IN');

        // If 118n_taxonomy is enabled also add a language condition.
        $i18n_enabled = module_exists('i18n_taxonomy');
        if ($i18n_enabled) {
          $query
            ->condition('ttd.language', array(
            $lang,
            "und",
          ), 'IN');
        }
        $query
          ->range(0, 1);
        $result = $query
          ->orderRandom()
          ->execute()
          ->fetch();
        $tid = $result->tid;

        // Set "now" as the last selection time and save that tid.
        variable_set('lexicon_block_lexicon_random_term_last_{$lang}', REQUEST_TIME);
        variable_set('lexicon_block_lexicon_random_term_tid_{$lang}', $tid);
      }
      else {

        // Get the currently selected tid.
        $tid = variable_get('lexicon_block_lexicon_random_term_tid_{$lang}', 0);
      }
      $term = taxonomy_term_load($tid);

      // If the term is localizable, localize it.
      if (function_exists('i18n_taxonomy_localize_terms')) {
        $term = i18n_taxonomy_localize_terms($term);
      }
      $term = _lexicon_term_add_info($term);

      // If the block is configured to trim the description adjust the
      // description.
      $trim = variable_get('lexicon_block_lexicon_random_term_trim', FALSE);
      if ($trim) {
        $trim_length = variable_get('lexicon_block_lexicon_random_term_trim_length', 100);
        $trim_ellipsis = variable_get('lexicon_block_lexicon_random_term_trim_elipsis', TRUE);
        $term->description = truncate_utf8($term->description, $trim_length, TRUE, $trim_ellipsis);
        $term->safe_description = truncate_utf8($term->safe_description, $trim_length, TRUE, $trim_ellipsis);
      }

      // If the block is configured to add a read more link to the description
      // add it to the description.
      $read_more_link = variable_get('lexicon_block_lexicon_random_term_read_more_link', TRUE);
      $read_more_link_text = variable_get('lexicon_block_lexicon_random_term_read_more_link_text', 'read more');
      if ($read_more_link) {
        $term->description = $term->description . ' ' . l(t($read_more_link_text), $term->link['path'], array(
          'fragment' => $term->link['fragment'],
        ));
        $term->safe_description = $term->safe_description . ' ' . l(t($read_more_link_text), $term->link['path'], array(
          'fragment' => $term->link['fragment'],
        ));
      }

      // If the block is configured not to link terms remove the link.
      $link = variable_get('lexicon_block_lexicon_random_term_link', TRUE);
      if (!$link) {
        unset($term->link);
      }

      // Theme the output.
      $block['content'] = theme('lexicon_block_term', array(
        'term' => $term,
      ));
      break;
  }
  return $block;
}