3 /* Reminder: always indent with 4 spaces (no tabs). */
4 // +---------------------------------------------------------------------------+
6 // +---------------------------------------------------------------------------+
7 // | searchcriteria.class.php |
9 // | This class acts as a container which allows data to be passed between the |
10 // | search engine and plugins. |
11 // +---------------------------------------------------------------------------+
12 // | Copyright (C) 2000-2009 by the following authors: |
14 // | Authors: Sami Barakat, s.m.barakat AT gmail DOT com |
15 // +---------------------------------------------------------------------------+
17 // | This program is free software; you can redistribute it and/or |
18 // | modify it under the terms of the GNU General Public License |
19 // | as published by the Free Software Foundation; either version 2 |
20 // | of the License, or (at your option) any later version. |
22 // | This program is distributed in the hope that it will be useful, |
23 // | but WITHOUT ANY WARRANTY; without even the implied warranty of |
24 // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
25 // | GNU General Public License for more details. |
27 // | You should have received a copy of the GNU General Public License |
28 // | along with this program; if not, write to the Free Software Foundation, |
29 // | Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
31 // +---------------------------------------------------------------------------+
33 class SearchCriteria {
43 var $_results = array();
44 var $_callback_func = '';
45 var $_total_results = 0;
48 function SearchCriteria( $pluginName, $pluginLabel )
50 $this->_pluginName = $pluginName;
51 $this->_pluginLabel = $pluginLabel;
53 $this->_url_rewrite = false;
54 $this->_append_query = true;
55 $this->_total_results = 0;
61 return $this->_pluginName;
66 return $this->_pluginLabel;
69 function setRank( $rank )
74 function setURLRewrite( $url_rewrite )
76 $this->_url_rewrite = $url_rewrite;
79 function setAppendQuery( $append_query )
81 $this->_append_query = $append_query;
84 function setResults( $result_arr )
86 $this->_results = $result_arr;
94 function UrlRewriteEnable()
96 return $this->_url_rewrite;
99 function AppendQueryEnable()
101 return $this->_append_query;
104 function getResults()
106 return $this->_results;
110 function setCallback( $func )
112 $this->_callback_func = $func;
115 function getCallback()
117 return $this->_callback_func;
120 function setTotal( $total_results )
122 $this->_total_results = $total_results;
127 return $this->_total_results;
131 function setSQL( $sql )
136 function setFTSQL( $ftsql )
138 $this->_ftsql = $ftsql;
150 // When only one SQL statment is set we assume it is for MySQL
151 if ($this->_ftsql != '' && (is_string($this->_ftsql) && $_DB_dbms == 'mysql') || is_array($this->_ftsql)) {
152 return $this->_ftsql;
158 function buildSearchSQL( $keyType, $query, $columns, $sql = '' )
160 if ($keyType == 'all')
162 // must contain ALL of the keywords
163 $words = explode(' ', $query);
166 $ftwords['mysql'] = '+' . str_replace(' ', ' +', $query);
167 $ftwords['mssql'] = '"' . str_replace(' ', '" AND "', $query) . '"';
169 else if ($keyType == 'any')
171 // must contain ANY of the keywords
172 $words = explode(' ', $query);
174 $ftwords['mysql'] = $query;
175 $ftwords['mssql'] = '"' . str_replace(' ', '" OR "', $query) . '"';
179 // do an exact phrase search (default)
180 $words = array($query);
183 // Puttings quotes around a single word in mysql really slows things down
184 if (strpos($query, ' ') !== false) {
185 $ftwords['mysql'] = '"' . $query . '"';
187 $ftwords['mysql'] = $query;
189 $ftwords['mssql'] = '"' . $query . '"';
192 $titles = (isset($_GET['title']) && isset($columns['title'])) ? true : false;
195 $strcol = $columns['title'];
197 $strcol = implode(',', $columns);
200 $ftsql['mysql'] = $sql . "AND MATCH($strcol) AGAINST ('{$ftwords['mysql']}' IN BOOLEAN MODE)";
201 $ftsql['mssql'] = $sql . "AND CONTAINS(($strcol), '{$ftwords['mssql']}')";
204 foreach ($words AS $word)
210 $tmp .= $columns['title'] . " LIKE '%$word%' OR ";
212 foreach ($columns AS $col) {
213 $tmp .= "$col LIKE '%$word%' OR ";
216 $tmp = substr($tmp, 0, -4) . ") $sep ";
218 $sql .= substr($tmp, 0, -5) . ') ';
220 return array($sql,$ftsql);
223 function getDateRangeSQL( $type = 'WHERE', $column, $datestart, $dateend )
225 if (!empty($datestart) && !empty($dateend))
227 $delim = substr($datestart, 4, 1);
230 $DS = explode($delim, $datestart);
231 $DE = explode($delim, $dateend);
232 $startdate = mktime(0,0,0,$DS[1],$DS[2],$DS[0]);
233 $enddate = mktime(23,59,59,$DE[1],$DE[2],$DE[0]);
234 return " $type (UNIX_TIMESTAMP($column) BETWEEN '$startdate' AND '$enddate') ";