1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Definition of views_handler_argument_search.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Argument that accepts query keys for search.
|
10
|
*
|
11
|
* @ingroup views_argument_handlers
|
12
|
*/
|
13
|
class views_handler_argument_search extends views_handler_argument {
|
14
|
|
15
|
/**
|
16
|
* Take sure that parseSearchExpression is runned and everything is set up.
|
17
|
*
|
18
|
* @param string $input
|
19
|
* The search phrase which was input by the user.
|
20
|
*/
|
21
|
public function query_parse_search_expression($input) {
|
22
|
if (!isset($this->search_query)) {
|
23
|
$this->search_query = db_select('search_index', 'i', array('target' => 'slave'))->extend('viewsSearchQuery');
|
24
|
$this->search_query->searchExpression($input, $this->view->base_table);
|
25
|
$this->search_query->publicParseSearchExpression();
|
26
|
}
|
27
|
}
|
28
|
|
29
|
/**
|
30
|
* Add this argument to the query.
|
31
|
*/
|
32
|
public function query($group_by = FALSE) {
|
33
|
$required = FALSE;
|
34
|
$this->query_parse_search_expression($this->argument);
|
35
|
if (!isset($this->search_query)) {
|
36
|
$required = TRUE;
|
37
|
}
|
38
|
else {
|
39
|
$words = $this->search_query->words();
|
40
|
if (empty($words)) {
|
41
|
$required = TRUE;
|
42
|
}
|
43
|
}
|
44
|
if ($required) {
|
45
|
if (isset($this->operator) && ($this->operator == 'required')) {
|
46
|
$this->query->add_where(0, 'FALSE');
|
47
|
}
|
48
|
}
|
49
|
else {
|
50
|
$search_index = $this->ensure_my_table();
|
51
|
|
52
|
$search_condition = db_and();
|
53
|
|
54
|
// Create a new join to relate the 'search_total' table to our current
|
55
|
// 'search_index' table.
|
56
|
$join = new views_join();
|
57
|
$join->construct('search_total', $search_index, 'word', 'word');
|
58
|
$search_total = $this->query->add_relationship('search_total', $join, $search_index);
|
59
|
|
60
|
$this->search_score = $this->query->add_field('', "$search_index.score * $search_total.count", 'score', array('aggregate' => TRUE, 'function' => 'sum'));
|
61
|
|
62
|
if (empty($this->query->relationships[$this->relationship])) {
|
63
|
$base_table = $this->query->base_table;
|
64
|
}
|
65
|
else {
|
66
|
$base_table = $this->query->relationships[$this->relationship]['base'];
|
67
|
}
|
68
|
$search_condition->condition("$search_index.type", $base_table);
|
69
|
|
70
|
if (!$this->search_query->simple()) {
|
71
|
$search_dataset = $this->query->add_table('search_dataset');
|
72
|
$conditions = $this->search_query->conditions();
|
73
|
$condition_conditions =& $conditions->conditions();
|
74
|
foreach ($condition_conditions as $key => &$condition) {
|
75
|
// Take sure we just look at real conditions.
|
76
|
if (is_numeric($key)) {
|
77
|
// Replace the conditions with the table alias of views.
|
78
|
$this->search_query->condition_replace_string('d.', "$search_dataset.", $condition);
|
79
|
}
|
80
|
}
|
81
|
$search_conditions =& $search_condition->conditions();
|
82
|
$search_conditions = array_merge($search_conditions, $condition_conditions);
|
83
|
}
|
84
|
else {
|
85
|
// Stores each condition, so and/or on the filter level will still work.
|
86
|
$or = db_or();
|
87
|
foreach ($words as $word) {
|
88
|
$or->condition("$search_index.word", $word);
|
89
|
}
|
90
|
|
91
|
$search_condition->condition($or);
|
92
|
}
|
93
|
|
94
|
$this->query->add_where(0, $search_condition);
|
95
|
$this->query->add_groupby("$search_index.sid");
|
96
|
$matches = $this->search_query->matches();
|
97
|
$placeholder = $this->placeholder();
|
98
|
$this->query->add_having_expression(0, "COUNT(*) >= $placeholder", array($placeholder => $matches));
|
99
|
}
|
100
|
}
|
101
|
|
102
|
}
|