You are here

function featured_content_get_filtered_nids in Featured Content 7

Same name and namespace in other branches
  1. 6.2 featured_content.module \featured_content_get_filtered_nids()
  2. 6 featured_content.module \featured_content_get_filtered_nids()
  3. 7.2 featured_content.module \featured_content_get_filtered_nids()

Get filtered node nids. Filter base on content types, users (authors) and taxonomy terms.

1 call to featured_content_get_filtered_nids()
featured_content_block_view in ./featured_content.module
Implements hook_block_view().

File

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

Code

function featured_content_get_filtered_nids($data, $show_num) {
  $node = _featured_content_load_node();
  $query = db_select('node', 'n');
  $query
    ->fields('n', array(
    'nid',
  ));
  $query
    ->condition('n.status', 0, '<>');

  // Unless configured, exclude current node page.
  if (isset($node->nid) && !$data['include_node']) {
    $query
      ->condition('n.nid', $node->nid, '!=');
  }

  // Filter based on language type.
  if (module_exists('locale')) {
    $languages = array();
    $all_languages = featured_content_get_languages();
    if (count($all_languages) > 1) {
      foreach ($all_languages as $language_code => $language_label) {
        if (!empty($data['languages'][$language_code])) {
          if ($language_code == 'CURRENT' && isset($node->nid)) {
            $languages[$node->language] = $node->language;
          }
          else {
            if ($language_code == 'INTERFACE') {
              global $language;
              $languages[] = $language->language;
            }
            else {
              $languages[$language_code] = $language_code;
            }
          }
        }
      }
      if (!empty($languages)) {
        $query
          ->condition('n.language', array_values($languages));
      }
    }
  }

  // Filter based on content type.
  $content_types = array();
  $all_content_types = featured_content_get_content_types();
  foreach ($all_content_types as $content_type_name => $content_type_label) {
    if (!empty($data['content_types']['type'][$content_type_name])) {
      if ($content_type_name == 'CURRENT' && isset($node->nid)) {
        $content_types[$node->type] = $node->type;
      }
      else {
        $content_types[$content_type_name] = $content_type_name;
      }
    }
  }
  if (!empty($content_types)) {
    $query
      ->condition('n.type', array_values($content_types));
  }

  // Filter based on user.
  $uids = array();
  $all_users = featured_content_get_users();
  foreach ($all_users as $user_uid => $user_label) {
    if (!empty($data['users']['user'][$user_uid])) {
      if ($user_uid == 'CURRENT' && isset($node->uid)) {
        $uids[] = $node->uid;
      }
      else {
        $uids[] = $user_uid;
      }
    }
  }
  if (!empty($uids)) {
    $query
      ->condition('n.uid', $uids);
  }

  // Filter based on taxonomy terms.
  $using_terms = FALSE;
  $tids = array();
  $vocabularies = taxonomy_get_vocabularies();
  foreach ($vocabularies as $vocabulary) {
    $vid = $vocabulary->vid;
    $all_terms = featured_content_get_vocabulary_terms($vocabulary);
    foreach ($all_terms as $term_tid => $term_name) {
      if (!empty($data['vocab'][$vid]['term'][$term_tid])) {
        $using_terms = TRUE;
        if ($term_tid == 'CURRENT-' . $vid) {
          if (isset($node->nid)) {
            $node_terms = featured_content_get_node_terms_by_vocabulary($node, $vid);
            if (!empty($node_terms)) {
              foreach ($node_terms as $node_term) {
                $tids[$node_term->tid] = $node_term->tid;
              }
            }
          }
          elseif (arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric(arg(2))) {

            // Check if taxonomy page.
            $tids[arg(2)] = arg(2);
          }
        }
        else {
          $tids[$term_tid] = $term_tid;
        }
      }
    }
  }
  if (!empty($tids)) {
    $query
      ->join('taxonomy_index', 'tn', 'n.nid = tn.nid');
    $query
      ->condition('tn.tid', $tids);
  }
  elseif ($using_terms) {

    // The term filter is being used but this page has no terms so do not
    // include any results.
    return array();
  }

  // Filter by views.
  if (module_exists('views')) {
    if (function_exists('views_get_all_views') && function_exists('views_get_view_result')) {
      $views_nids = array();
      $views = views_get_all_views();
      if (!empty($views)) {
        foreach ($views as $view_name => $view_data) {
          if (!empty($view_data->display)) {
            foreach ($view_data->display as $display_name => $display_data) {
              if (!empty($data['views']['view'][$view_name][$display_name])) {
                $views_results = views_get_view_result($view_name, $display_name);
                if (!empty($views_results)) {
                  foreach ($views_results as $views_result) {
                    if (isset($views_result->nid)) {
                      $views_nids[] = $views_result->nid;
                    }
                  }
                }
              }
            }
          }
        }
      }
      if (!empty($views_nids)) {
        $query
          ->condition('n.nid', $views_nids);
      }
    }
  }

  // Filter by node title keyword search.
  if (module_exists('search')) {

    // Check to make sure this option has been enabled.
    if (isset($data['keyword']['filter_by_keyword']) && $data['keyword']['filter_by_keyword'] === 1) {

      // If we haven't already loaded the node from earlier on, grab it now.
      // If we're not able to, don't do any keyword searching.
      $node = empty($node) ? _featured_content_load_node() : $node;
      if ($node) {
        $include_node = (bool) $data['include_node'];
        $num_words = (int) $data['keyword']['num_words_in_title_keyword'];
        $keyword_search_string = featured_content_get_search_string($node, $num_words);
        $restrict_type = (bool) $data['search_restrict_type'];

        // Use drupal search for actual search.
        $results = featured_content_do_search($keyword_search_string, $show_num, $include_node, $restrict_type);
        $keyword_match_nids = array();
        if (!empty($results)) {
          foreach ($results as $result) {
            if (isset($result['node'])) {
              $keyword_match_nids[] = $result['node']->nid;
            }
          }
        }
        if (!empty($keyword_match_nids)) {
          $query
            ->condition('n.nid', $keyword_match_nids);
        }
        else {

          // No matches.
          $query
            ->condition('n.nid', array(
            0,
          ));
        }
      }
    }
  }

  // Filter by primary term.
  if (isset($data['primary_term']['node']) && is_numeric($node->primaryterm) && $node->primaryterm > 0) {
    $query
      ->join('primary_term', 'pt', 'n.vid = pt.vid');
    $query
      ->condition('pt.tid', $node->primaryterm);
  }

  // Filter based on path.
  $path_alias = '';
  $filter_path = '';
  $match = array();
  if (isset($data['paths']['match'])) {
    $match = $data['paths']['match'];
  }
  if (!empty($match[0]) || !empty($match[1]) || !empty($match[2]) || !empty($match[3])) {
    if (isset($_GET['nid'])) {
      $path_alias = drupal_get_path_alias('node/' . $_GET['nid']);
    }
    elseif (isset($_GET['path'])) {
      $path_alias = drupal_get_path_alias($_GET['path']);
    }
    else {
      $path_alias = drupal_get_path_alias($_GET['q']);
    }
    if (!empty($path_alias)) {
      $tmp = array();
      $path = explode('/', $path_alias);
      if (!empty($path)) {
        $filter_path = featured_content_get_path_pattern($match, $path, 0);
        $filter_path .= featured_content_get_path_pattern($match, $path, 1);
        $filter_path .= featured_content_get_path_pattern($match, $path, 2);
        $filter_path .= featured_content_get_path_pattern($match, $path, 3);
      }
    }
  }
  $nids = array();
  $results = $query
    ->execute();
  foreach ($results as $row) {
    $add = TRUE;
    if (!empty($filter_path)) {
      $node_alias = drupal_get_path_alias('node/' . $row->nid) . '/';
      if (!preg_match('/^' . $filter_path . '/', $node_alias, $matches)) {
        $add = FALSE;
      }
    }
    if ($add) {
      $nids[] = $row->nid;
    }
  }
  return $nids;
}