You are here

function quotes_block in Quotes 5

Same name and namespace in other branches
  1. 6 quotes.module \quotes_block()

Implementation of hook_block().

File

./quotes.module, line 556

Code

function quotes_block($op = 'list', $delta = 0, $edit = array()) {
  global $_quotes_subs;

  // This is done here for "configure." The actual values are set below.
  $_quotes_subs = array(
    '%' . t('interval') => NULL,
    '%' . t('bid') => NULL,
    '%' . t('title') => NULL,
    '%' . t('nid') => NULL,
    '%' . t('user') => NULL,
  );
  switch ($op) {
    case 'mb_blocked':
      return 'quotes';
    case 'list':
      $blocks = array();
      $result = db_query('SELECT qb.bid, qb.name FROM {quotes_blocks} qb');
      while ($block = db_fetch_object($result)) {
        $blocks[$block->bid] = array(
          'info' => t('Quotes') . ': ' . $block->name,
        );
      }
      return $blocks;
    case 'view':
      $block = db_fetch_array(db_query('SELECT qb.* FROM {quotes_blocks} qb WHERE qb.bid=%d', $delta));
      if (!$block) {
        return NULL;
      }
      if ($block['cron_interval'] > 0) {
        if (!$block['vid'] || $block['cron_last'] + $block['cron_interval'] * $block['cron_step'] < time()) {
          $block['vid'] = quotes_get_quote($block, TRUE, 1);
          db_query('UPDATE {quotes_blocks} SET vid=%d, cron_last=%d WHERE bid=%d', $block['vid'][0], time(), $delta);
          cache_clear_all();
        }
        else {
          $block['vid'] = array(
            $block['vid'],
          );
        }
      }
      else {
        $block['vid'] = quotes_get_quote($block, TRUE, $block['count']);
      }
      if (!$block['vid']) {
        return NULL;
      }
      $view_text = $block['view_text'];
      $link_weight = $block['view_weight'];
      $more_text = $block['more_text'];
      $rand_freq = $block['rand_freq'];
      $output = NULL;
      unset($blk_title);
      foreach ($block['vid'] as $nid) {
        $node = node_load($nid);

        // Save first node title for possible block title below
        if (!isset($blk_title)) {
          $blk_title = filter_xss($node->title);
          $blk_nid = $node->nid;
          $blk_uid = $node->uid;
        }

        // See if we don't want the citation shown.
        if (!$block['show_citation']) {
          unset($node->quotes_citation);
        }

        // We don't want bios in a block.
        unset($node->quotes_bio);

        // Quotes_view builds $node->content['body'].
        $quote = quotes_view($node, FALSE, FALSE, FALSE, $block['max_length']);

        // Do we want titles?
        switch ($block['show_titles']) {
          case 1:

            // Link to node.
            $quote->content['title'] = array(
              '#value' => '<h3>' . drupal_get_path_alias(l($node->title, 'node/' . $nid)) . '</h3>',
              '#weight' => -100,
            );
            break;
          case 2:

            // Plain text.
            $quote->content['title'] = array(
              '#value' => '<h3>' . check_plain($node->title) . '</h3>',
              '#weight' => -100,
            );
            break;
        }
        if ($node->comment && !empty($view_text)) {
          $quote->content['view_link'] = array(
            '#value' => '<div class="quotes-view-link">' . drupal_get_path_alias(l($view_text, 'node/' . $nid)) . '</div>',
            '#weight' => $link_weight,
          );
        }
        if ($more_text) {
          $quote->content['more_link'] = array(
            '#value' => '<div class="quotes-more-link">' . drupal_get_path_alias(l($more_text, 'quotes')) . '</div>',
            '#weight' => 2,
          );
        }
        $output .= drupal_render($quote->content);
      }
      $_quotes_subs = array(
        '%' . t('interval') => format_interval($block['cron_interval'] * $block['cron_step'], 1),
        '%' . t('bid') => $block['bid'],
        '%' . t('title') => $blk_title,
        '%' . t('nid') => $blk_nid,
        '%' . t('user') => theme('username', user_load(array(
          'uid' => $blk_uid,
        ))),
      );
      $subject = strtr($block['name'], $_quotes_subs);
      return array(
        'subject' => $subject,
        'content' => $output,
      );
    case 'configure':
      return _quotes_block_configure($delta, $edit);
    case 'save':
      _quotes_block_configure_save($delta, $edit);
  }
}