00001 <?php
00002
00008 class views_handler_argument_search extends views_handler_argument {
00009
00016 function query_parse_search_expression($input) {
00017 if (!isset($this->search_query)) {
00018 $this->search_query = db_select('search_index', 'i', array('target' => 'slave'))->extend('viewsSearchQuery');
00019 $this->search_query->searchExpression($input, $this->view->base_table);
00020 $this->search_query->publicParseSearchExpression();
00021 }
00022 }
00023
00027 function query($group_by = FALSE) {
00028 $required = FALSE;
00029 $this->query_parse_search_expression($this->argument);
00030 if (!isset($this->search_query)) {
00031 $required = TRUE;
00032 }
00033 else {
00034 $words = $this->search_query->words();
00035 if (empty($words)) {
00036 $required = TRUE;
00037 }
00038 }
00039 if ($required) {
00040 if ($this->operator == 'required') {
00041 $this->query->add_where(0, 'FALSE');
00042 }
00043 }
00044 else {
00045 $search_index = $this->ensure_my_table();
00046
00047 $search_condition = db_and();
00048
00049
00050 $join = new views_join;
00051 $join->construct('search_total', $search_index, 'word', 'word');
00052 $search_total = $this->query->add_relationship('search_total', $join, $search_index);
00053
00054 $this->search_score = $this->query->add_field('', "SUM($search_index.score * $search_total.count)", 'score', array('aggregate' => TRUE));
00055
00056 if (empty($this->query->relationships[$this->relationship])) {
00057 $base_table = $this->query->base_table;
00058 }
00059 else {
00060 $base_table = $this->query->relationships[$this->relationship]['base'];
00061 }
00062 $search_condition->condition("$search_index.type", $base_table);
00063
00064 if (!$this->search_query->simple()) {
00065 $search_dataset = $this->query->add_table('search_dataset');
00066 $conditions = $this->search_query->conditions();
00067 $condition_conditions =& $conditions->conditions();
00068 foreach ($condition_conditions as $key => &$condition) {
00069
00070 if (is_numeric($key)) {
00071
00072 $this->search_query->condition_replace_string('d.', "$search_dataset.", $condition);
00073 }
00074 }
00075 $search_conditions =& $search_condition->conditions();
00076 $search_conditions = array_merge($search_conditions, $condition_conditions);
00077 }
00078 else {
00079
00080 $or = db_or();
00081 foreach ($words as $word) {
00082 $or->condition("$search_index.word", $word);
00083 }
00084
00085 $search_condition->condition($or);
00086 }
00087
00088 $this->query->add_where(0, $search_condition);
00089 $this->query->add_groupby("$search_index.sid");
00090 $matches = $this->search_query->matches();
00091 $placeholder = $this->placeholder();
00092 $this->query->add_having_expression(0, "COUNT(*) >= $placeholder", array($placeholder => $matches));
00093 }
00094 }
00095 }