You are here

function featured_content_view in Featured Content 6

Same name and namespace in other branches
  1. 6.2 featured_content.module \featured_content_view()

Provides 'view' info for hook_block().

Parameters

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

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

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

File

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

Code

function featured_content_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;
      }
    }
  }
  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 ($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;
      }
      foreach ($vocabularies as $vocabulary) {
        $vid = $vocabulary->vid;
        if (!empty($featured_content['visibility']['vocab'][$vid]['selected'])) {
          $tid_intersect = array_intersect($featured_content['visibility']['vocab'][$vid]['selected'], array_keys($this_node->taxonomy));
          if (empty($this_node->taxonomy) || empty($tid_intersect)) {
            $show = FALSE;
          }
        }
      }
    }
  }
  if (!$show || !$type && arg(0) == 'featured-content' && arg(1) == 'more' && arg(2) == $delta) {

    // don't show featured 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']);
      break;
    case 'search':
      $nids = featured_content_get_search_nids($featured_content['search']);
      break;
  }

  // if using modr8, only show nodes with moderate = 0
  if (module_exists('modr8')) {
    if (db_column_exists('node', 'moderate')) {
      $results = db_query('SELECT n.nid FROM {node} AS n WHERE n.nid in (' . db_placeholders($nids, 'int') . ') AND n.moderate = 0', $nids);
      $tmp = array();
      while ($row = db_fetch_object($results)) {
        $tmp[$row->nid] = $row->nid;
      }
      $nids = $tmp;
    }
  }
  $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 (isset($this_node) && $this_node->nid) {
    $query_string = array(
      'query' => 'nid=' . $this_node->nid,
    );
  }
  else {
    $query_string = array(
      'query' => 'path=' . $_GET['q'],
    );
  }

  // add rss link
  if ($featured_content['rss']['display']) {
    $settings['rss-link'] = theme('feed_icon', url('featured-content/feed/' . $delta, $query_string), 'RSS Feed');
  }

  // sort nodes
  if (empty($nodes)) {
    if (trim($featured_content['empty'])) {
      $data = array();
      $data['content'] = theme('featured_content_empty', $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 ($nids) {
          foreach ($nids as $nid) {
            $tmp[] = $nodes[$nid];
          }
          $nodes = $tmp;
        }
        break;
    }
  }
  if ($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();
    $teasers = array();
    $full_nodes = array();
    $rss_nids = array();
    foreach ($nodes as $node) {
      if ($node->title && is_numeric($node->nid)) {
        $links[] = l($node->title, 'node/' . $node->nid);
        if ($type == 'rss') {
          $rss_nids[] = $node->nid;
        }
        elseif ($featured_content['display'] == 'teasers' || $type == 'more' && $featured_content['more']['display'] == 'teasers') {
          $teasers[] = node_view(node_load($node->nid), TRUE);
        }
        elseif ($featured_content['display'] == 'full_nodes' || $type == 'more' && $featured_content['more']['display'] == 'full_nodes') {
          $full_nodes[] = node_view(node_load($node->nid));
        }
      }
    }
    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['teasers'] = $teasers;
    $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', $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', $settings);
    return $data;
  }
}