You are here

function search_config_query_node_access_alter in Search configuration 7

Same name and namespace in other branches
  1. 8 search_config.module \search_config_query_node_access_alter()

Implements of hook_query_node_access_alter().

File

./search_config.module, line 150
Main functionality for the Search Configuration module.

Code

function search_config_query_node_access_alter(QueryAlterableInterface $query) {
  global $user;

  // Provides a tag option to bypass any changes.
  if ($query
    ->hasTag('bypass_search_configuration')) {
    return;
  }
  $search = FALSE;
  $node = FALSE;
  foreach ($query
    ->getTables() as $alias => $table) {
    if ($table['table'] == 'search_index') {
      $search = $alias;
    }
    elseif ($table['table'] == 'node') {
      $node = $alias;
    }
  }
  if ($node && $search) {

    // Only conditionally restrict the query if not admin (user.uid = 1).
    if ($user->uid != 1) {
      if (!user_access('search all content')) {
        $excluded_content_types = array();
        foreach (search_config_content_types() as $type => $label) {
          if (!user_access("search {$type} content")) {
            $excluded_content_types[] = $type;
          }
        }

        // A core bug results in a DB error if we use the following: Ref: #1210072
        // $query->condition($node . '.type', array($excluded_content_types), 'NOT IN');
        if (!empty($excluded_content_types)) {
          $db_and = db_and();
          foreach ($excluded_content_types as $type) {
            $db_and
              ->condition($node . '.type', $type, '!=');
          }
          $query
            ->condition($db_and);
        }
      }
      if (!user_access('search all excluded entities')) {

        // Join into the {} table to see if it is excluded.
        $query
          ->leftJoin('search_config_exclude', 'sc', $node . '.nid = sc.entity_id AND sc.entity_type = :type', array(
          ':type' => 'node',
        ));
        $query
          ->isNull('sc.entity_id');
      }
    }
    if (get_class($query) == 'PagerDefault' || is_subclass_of($query, 'PagerDefault')) {
      $settings = search_config_node_settings();
      if (!empty($settings['results']['limit'])) {
        $query
          ->limit($settings['results']['limit']);
      }
    }
  }
}