You are here

public function similar_handler_argument_nid::query in Similar Entries 6.2

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

Builds the query.

File

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

Class

similar_handler_argument_nid
Defines the similar entries View node ID argument.

Code

public function query() {
  $boolean = !empty($this->options['similar']['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);

    // 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 && isset($this->options['similar']['boolean_options']['title_operator'])) {
      $title = $this
        ->alter_node_title($title);
    }

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

  // Perform replacements to add custom operators to node body and title words.
  if ($boolean && !empty($this->options['similar']['boolean_options']['enable_custom_operators']) && !empty($this->options['similar']['boolean_options']['custom_operators'])) {
    $replacements = array_map('trim', explode('|', $this->options['similar']['boolean_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);
  $this->query
    ->add_field('', $query, 'score');

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