You are here

public function similar_handler_argument_nid::query in Similar Entries 7.2

Same name and namespace in other branches
  1. 6.2 views/similar_handler_argument_nid.inc \similar_handler_argument_nid::query()

Builds the query.

Overrides views_handler_argument_numeric::query

File

views/similar_handler_argument_nid.inc, line 285
Defines the Similar entries node ID argument.

Class

similar_handler_argument_nid
Defines the similar entries View node ID argument.

Code

public function query($group_by = FALSE) {
  $boolean = !empty($this->options['boolean_mode']);
  $text = '';

  // Since the view could have multiple nid arguments, load each node
  // and populate the $text variable with node titles and bodies.
  foreach ($this->value as $nid) {
    $node = node_load($nid);
    if ($this->options['rendered_content']) {
      $node_view = node_view($node, $this->options['rendered_view_mode'], $node->language);
      $node->body[$node->language][0]['value'] = render($node_view);
    }
    if (isset($node->title)) {

      // Remove punctuation from the title.
      $title = preg_replace('/[^a-z0-9 _-]+/i', '', $node->title);

      // Alter the relevancy of words in the node title if option is selected.
      if ($boolean && !empty($this->options['source_relevance']) && isset($this->options['title_operator'])) {
        $title = $this
          ->alter_node_title($title);
      }
      $text .= " {$title}";
    }
    if (isset($node->body) && isset($node->body[$node->language])) {

      // Strip tags and add slashes only to the body before adding the title.
      $body = trim(addslashes(strip_tags($node->body[$node->language][0]['safe_value'])));
      $text .= " {$body}";
    }
  }

  // Perform replacements to add custom operators to node body and title words.
  if ($boolean && !empty($this->options['enable_custom_operators']) && !empty($this->options['custom_operators'])) {
    $replacements = array_map('trim', explode('|', $this->options['custom_operators']));
    $words = array_map('similar_handler_argument_nid::get_search_word', $replacements);
    $text = preg_replace($words, $replacements, $text);
  }
  $text = trim($text);
  $this
    ->ensure_my_table();
  $query = $boolean ? $this
    ->similar_build_query($text, 'IN BOOLEAN MODE') : $this
    ->similar_build_query($text);

  // An empty string is passed for the table to bypass the table alias.
  $this->query
    ->add_field('', $query, 'score');

  // Exclude the current node(s).
  if (count($this->value) > 1) {
    $this->query
      ->add_where(0, 'node.nid', $this->value, 'NOT IN');
  }
  else {
    $this->query
      ->add_where(0, 'node.nid', $this->value[0], '<>');
  }

  // Add a node_access tag to prevent conflict with node_access module.
  $this->query
    ->add_tag('node_access');

  // Add a Similar Entries tag to indicate that filters and sort handlers are valid.
  $this->query
    ->add_tag('similar_entries');
}