You are here

function featured_content_block_view in Featured Content 7.2

Same name and namespace in other branches
  1. 7 featured_content.module \featured_content_block_view()

Implements hook_block_view().

Parameters

$delta string The name of the block to render.:

$type string If set, should be 'rss' or 'more'.:

1 call to featured_content_block_view()
featured_content_get_content in ./featured_content.pages.inc
Get the content for rss or more page.

File

./featured_content.module, line 169
Featured Content module for created related & featured content blocks.

Code

function featured_content_block_view($delta, $type = NULL) {
  $featured_content = featured_content_get_block_data($delta);
  $vocabularies = taxonomy_get_vocabularies();

  // Check visibility settings to see if block should be shown.
  $show = TRUE;

  // If there are some visibility settings, default to FALSE.
  if (!empty($featured_content['visibility']['content_types']['selected']) || !empty($featured_content['visibility']['users']['selected'])) {
    $show = FALSE;
  }
  else {
    foreach ($vocabularies as $vocabulary) {
      $vid = $vocabulary->vid;
      if (!empty($featured_content['visibility']['vocab'][$vid]['selected'])) {
        $show = FALSE;
        break;
      }
    }
  }
  $this_node = NULL;
  if ($type == NULL && arg(0) == 'node' && is_numeric(arg(1)) && !arg(2)) {
    $show = TRUE;

    // Only check on node pages for now.
    $this_node = node_load(arg(1));
    if (isset($this_node->nid)) {
      if (!empty($featured_content['visibility']['content_types']['selected']) && !in_array($this_node->type, $featured_content['visibility']['content_types']['selected'])) {
        $show = FALSE;
      }
      if (!empty($featured_content['visibility']['users']['selected']) && !in_array($this_node->uid, $featured_content['visibility']['users']['selected'])) {
        $show = FALSE;
      }
      if ($show && !empty($featured_content['visibility']['vocab'])) {

        // Check if at least one term has been selected.
        $has_setting = FALSE;
        foreach ($featured_content['visibility']['vocab'] as $vocab_setting) {
          if ($vocab_setting['selected']) {
            $has_setting = TRUE;
            break;
          }
        }

        // Check if there is a term match.
        if ($has_setting) {
          $has_term = FALSE;
          foreach ($vocabularies as $vocabulary) {
            $vid = $vocabulary->vid;
            if (!empty($featured_content['visibility']['vocab'][$vid]['selected'])) {
              $terms = featured_content_taxonomy_node_get_terms($this_node);
              $tid_intersect = array_intersect($featured_content['visibility']['vocab'][$vid]['selected'], array_keys($terms));
              if (!empty($tid_intersect)) {
                $has_term = TRUE;
              }
            }
            if ($has_term) {
              break;
            }
          }
          $show = $has_term;

          // Must have at least one term.
        }
      }
    }
  }
  if (!$show || !$type && arg(0) == 'featured-content' && arg(1) == 'more' && arg(2) == $delta) {

    // Don't show Featured Content block on more page for that block.
    return;
  }

  // Get nodes to show.
  switch ($featured_content['type']) {
    case 'manual':
      $nids = $featured_content['manual']['nids'];
      break;
    case 'cck':
      $nids = featured_content_get_cck_nids($featured_content['cck']);
      break;
    case 'filter':
      $nids = featured_content_get_filtered_nids($featured_content['filter'], $featured_content['num_show']);
      break;
    case 'search':
      $nids = featured_content_get_search_nids($featured_content['search'], $featured_content['num_show']);
      break;
  }
  $nodes = featured_content_get_nodes($nids, $featured_content['sort']);

  // Create settings for theming.
  // All block settings.
  $settings = $featured_content[$featured_content['type']];

  // Keep some general settings.
  $settings['delta'] = $delta;
  $settings['type'] = $featured_content['type'];
  $settings['header'] = check_markup($featured_content['header']);
  $settings['footer'] = check_markup($featured_content['footer']);
  $settings['empty'] = check_markup($featured_content['empty']);
  $settings['style'] = $featured_content['style'];

  // Keep track of nid in URL in for context purposes.
  $query_string = array();
  if (!empty($this_node) && $this_node->nid) {
    $query_string = array(
      'query' => array(
        'nid' => $this_node->nid,
      ),
    );
  }
  else {
    $query_string = array(
      'query' => array(
        'path' => $_GET['q'],
      ),
    );
  }

  // Add RSS link.
  if (!empty($featured_content['rss']['display'])) {
    $query_string = array_merge($query_string, array(
      'absolute' => TRUE,
    ));
    $settings['rss-link'] = theme('feed_icon', array(
      'url' => url('featured-content/feed/' . $delta, $query_string),
      'title' => 'RSS Feed',
    ));
  }

  // Sort nodes.
  if (empty($nodes)) {
    if (trim($featured_content['empty'])) {
      $data = array();
      $data['content'] = theme('featured_content_empty', array(
        'block_settings' => $settings,
      ));
      return $data;
    }
  }
  else {
    switch ($featured_content['sort']) {
      case 'random':
        if (is_array($nodes)) {
          shuffle($nodes);
        }
        break;
      case 'popular_asc':
        $nodes = featured_content_sort_nodes($nodes, 'totalcount', TRUE, 'asc');
        break;
      case 'popular_desc':
        $nodes = featured_content_sort_nodes($nodes, 'totalcount');
        break;
      case 'date_asc':
        $nodes = featured_content_sort_nodes($nodes, 'created', TRUE, 'asc');
        break;
      case 'date_desc':
        $nodes = featured_content_sort_nodes($nodes, 'created');
        break;
      case 'alpha_asc':
        $nodes = featured_content_sort_nodes($nodes, 'title', FALSE, 'asc');
        break;
      case 'alpha_desc':
        $nodes = featured_content_sort_nodes($nodes, 'title', FALSE);
        break;
      case 'terms_asc':
        $nodes = featured_content_sort_nodes_by_terms($nodes, 'asc');
        break;
      case 'terms_desc':
        $nodes = featured_content_sort_nodes_by_terms($nodes);
        break;
      case 'vocab_asc':
        $nodes = featured_content_sort_nodes_by_vocab($nodes, 'asc');
        break;
      case 'vocab_desc':
        $nodes = featured_content_sort_nodes_by_vocab($nodes);
        break;
      case 'none':
        if (!empty($nids)) {
          foreach ($nids as $nid) {
            $tmp[] = $nodes[$nid];
          }
          $nodes = $tmp;
        }
        break;
    }
  }
  if (!empty($nodes)) {

    // Limit to num_show and create links.
    $show_num = $featured_content['num_show'];
    if ($type == 'more' && !in_array($featured_content['more']['display'], array(
      'none',
      'custom',
    ))) {
      $show_num = $featured_content['more']['num'];
    }
    $nodes = array_splice($nodes, 0, $show_num);
    $links = array();
    $full_nodes = array();
    $rss_nids = array();
    foreach ($nodes as $node) {
      if (!empty($node->title) && is_numeric($node->nid)) {
        $links[] = l($node->title, 'node/' . $node->nid);
        if ($type == 'rss') {
          $rss_nids[] = $node->nid;
        }
        else {

          // Get the view mode for the block or page.
          $view_mode = '';
          if ($type == 'more') {
            $view_mode = isset($featured_content['more']['display']) ? $featured_content['more']['display'] : '';
          }
          else {
            $view_mode = isset($featured_content['display']) ? $featured_content['display'] : '';
          }
          if ($view_mode != 'links') {
            if ($featured_content['type'] == 'search') {

              // Have to reset the search result nodes due to caching issues.
              $full_node = node_load($node->nid, NULL, TRUE);
            }
            else {
              $full_node = node_load($node->nid);
            }

            // Display selected view mode.
            $node_view = node_view($full_node, $view_mode);
            $full_nodes[] = drupal_render($node_view);
          }
        }
      }
    }

    // For RSS page, only need the node ids.
    if ($type == 'rss') {
      featured_content_set_page_title($delta, $featured_content['rss']['title']);
      return $rss_nids;
    }

    // Add content and more settings.
    // Content to show.
    $settings['links'] = $links;
    $settings['full_nodes'] = $full_nodes;
    if ($type == 'more') {

      // Return more page.
      $settings['style'] = $featured_content['more']['style'];
      $settings['title'] = $featured_content['more']['title'];
      $settings['header'] = $featured_content['more']['header'];
      $settings['footer'] = $featured_content['more']['footer'];
      featured_content_set_page_title($delta, $featured_content['more']['title']);
      return theme('featured_content_more', array(
        'block_settings' => $settings,
      ));
    }
    elseif ($featured_content['more']['display'] != 'none' && trim($featured_content['more']['text'])) {

      // Add more link.
      if ($featured_content['more']['display'] == 'custom' && trim($featured_content['more']['url'])) {
        $settings['more-link'] = l($featured_content['more']['text'], $featured_content['more']['url']);
      }
      else {
        $settings['more-link'] = l($featured_content['more']['text'], 'featured-content/more/' . $delta, $query_string);
      }
    }

    // Return block content.
    $data = array();
    $data['content'] = theme('featured_content_block', array(
      'block_settings' => $settings,
    ));
    return $data;
  }
}