Fixed theme Blogger
[vanilla-miry.git] / library / Vanilla / Vanilla.Class.Search.php
blob0497d04ef3f64182abc91709992f0d918d182e64
1 <?php
2 /**
3 * Search class (represents a saved search).
5 * Copyright 2003 Mark O'Sullivan
6 * This file is part of Lussumo's Software Library.
7 * Lussumo's Software Library is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
8 * Lussumo's Software Library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
9 * You should have received a copy of the GNU General Public License along with Vanilla; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
10 * The latest source code is available at www.lussumo.com
11 * Contact Mark O'Sullivan at mark [at] lussumo [dot] com
13 * @author Mark O'Sullivan
14 * @copyright 2003 Mark O'Sullivan
15 * @license http://lussumo.com/community/gpl.txt GPL 2
16 * @package Vanilla
17 * @version 1.1.10
21 /**
22 * Search class (represents a saved search).
23 * @package Vanilla
25 class Search {
26 var $SearchID; // The unique identifier assigned to this search by the system
27 var $Label; // The label assigned to this search by the user
28 var $Type; // The type of search to perform
29 var $Keywords; // The keywords defined by the user
30 var $Query; // The actual string to be searched on in the sql query
31 var $Categories; // The category names to search in (comment & discussion search)
32 var $AuthUsername; // The author's username to filter to (comment & discussion search)
33 var $WhisperFilter; // Should the search be limited to whispers
34 var $Roles; // The roles to filter to (user search)
35 var $UserOrder; // The order to sort results in (user search)
36 var $HighlightWords; // Breaks the query into words to be highlighted in search results
38 // Clears all properties
39 function Clear() {
40 $this->SearchID = 0;
41 $this->Label = '';
42 $this->Type = 'Topics';
43 $this->Keywords = '';
44 $this->Query = '';
45 $this->Categories = 0;
46 $this->AuthUsername = '';
47 $this->WhisperFilter = 0;
48 $this->Roles = 0;
49 $this->UserOrder = '';
50 $this->HighlightWords = array();
53 function DefineType($InValue) {
54 if ($InValue != 'Users' && $InValue != 'Comments') $InValue = 'Topics';
55 return $InValue;
58 function GetPropertiesFromDataSet($DataSet, $ParseKeywords = '0') {
59 $ParseKeywords = ForceBool($ParseKeywords, 0);
61 $this->SearchID = ForceInt(@$DataSet['SearchID'], 0);
62 $this->Label = ForceString(@$DataSet['Label'], '');
63 $this->Type = $this->DefineType(ForceString(@$DataSet['Type'], ''));
64 $this->Keywords = urldecode(ForceString(@$DataSet['Keywords'], ''));
65 if ($ParseKeywords) $this->ParseKeywords($this->Type, $this->Keywords);
68 function GetPropertiesFromForm() {
69 $this->SearchID = ForceIncomingInt('SearchID', 0);
70 $this->Label = ForceIncomingString('Label', '');
71 $this->Type = $this->DefineType(ForceIncomingString('Type', ''));
72 $this->Keywords = urldecode(ForceIncomingString('Keywords', ''));
74 // Parse out the keywords differently based on the type of search
75 $Advanced = ForceIncomingBool('Advanced', 0);
76 if ($Advanced) {
77 // Load all of the search variables from the form
78 $this->Categories = ForceIncomingString('Categories', '');
79 $this->AuthUsername = ForceIncomingString('AuthUsername', '');
80 $this->Roles = ForceIncomingString('Roles', '');
81 $this->UserOrder = ForceIncomingString('UserOrder', '');
82 $this->Query = $this->Keywords;
84 // Build the keyword definition
85 $KeyDef = '';
86 if ($this->Type == 'Users') {
87 if ($this->Roles != '') $KeyDef = 'roles:'.$this->Roles.';';
88 if ($this->UserOrder != '') $KeyDef .= 'sort:'.$this->UserOrder.';';
89 $this->Keywords = $KeyDef.$this->Keywords;
90 } else {
91 if ($this->Categories != '') $KeyDef = 'cats:'.$this->Categories.';';
92 if ($this->AuthUsername != '') $KeyDef .= $this->AuthUsername.':';
93 $this->Keywords = $KeyDef.$this->Keywords;
95 } else {
96 // Load all of the search variables from the keyword definition
97 $this->ParseKeywords($this->Type, $this->Keywords);
101 function ParseKeywords($Type, $Keywords) {
102 if ($Type == 'Users') {
103 // Parse twice to hit both of the potential keyword assignment operators (roles or sort)
104 $this->Query = $this->ParseUserKeywords($Keywords);
105 $this->Query = $this->ParseUserKeywords($this->Query);
106 } else {
107 // Check for category assignments
108 $this->Query = $Keywords;
109 $CatPos = strpos($this->Query, 'cats:');
110 if ($CatPos !== false && $CatPos == 0) {
111 $this->Query = $this->ParsePropertyAssignment('Categories', 5, $this->Query);
114 // Check for whisper filtering
115 $WhisperPos = strpos($this->Query, 'whisper;');
116 if ($WhisperPos !== false && $WhisperPos == 0) {
117 $this->WhisperFilter = 1;
118 $this->Query = substr($this->Query, 8);
121 // Check for username assignment
122 $ColonPos = strpos($this->Query, ':');
123 if ($ColonPos !== false && $ColonPos != 0) {
124 // If a colon was found, check to see that it didn't occur before any quotes
125 $QuotePos = strpos($this->Query, '"');
127 if ($QuotePos === false || $QuotePos > $ColonPos) {
128 $this->AuthUsername = substr($this->Query, 0, $ColonPos);
129 $this->Query = substr($this->Query, $ColonPos+1);
133 $Highlight = $this->Query;
134 if (!empty($Highlight)) {
135 $patterns = array(
136 '/\'/',
137 '/ and /',
138 '/ or /',
140 $replacements = array(
141 '//',
142 '//',
143 '//',
145 $Highlight = preg_replace($patterns, $replacements, $Highlight);
146 $this->HighlightWords = explode(' ', $Highlight);
150 function ParsePropertyAssignment($Property, $PropertyLength, $Keywords) {
151 $sReturn = $Keywords;
152 $DelimiterPos = false;
153 $sReturn = substr($sReturn, $PropertyLength);
154 $DelimiterPos = strpos($sReturn, ';');
155 if ($DelimiterPos !== false) {
156 $this->$Property = substr($sReturn, 0, $DelimiterPos);
157 } else {
158 $this->$Property = substr($sReturn, 0);
160 return substr($sReturn, $DelimiterPos+1);
163 function ParseUserKeywords($Keywords) {
164 $sReturn = $Keywords;
165 // Check for roles or sort definition
166 $RolePos = strpos($sReturn, 'roles:');
167 $SortPos = strpos($sReturn, 'sort:');
168 if ($RolePos !== false && $RolePos == 0) {
169 $sReturn = $this->ParsePropertyAssignment('Roles', 6, $sReturn);
170 } elseif ($SortPos !== false && $SortPos == 0) {
171 $sReturn = $this->ParsePropertyAssignment('UserOrder', 5, $sReturn);
173 return $sReturn;
176 function FormatPropertiesForDatabaseInput() {
177 $this->Label = FormatStringForDatabaseInput($this->Label);
178 $this->Keywords = FormatStringForDatabaseInput($this->Keywords);
179 $this->Query = FormatStringForDatabaseInput($this->Query);
180 $this->AuthUsername = FormatStringForDatabaseInput($this->AuthUsername);
181 $this->Categories = FormatStringForDatabaseInput($this->Categories);
182 $this->Roles = FormatStringForDatabaseInput($this->Roles);
185 function FormatPropertiesForDisplay() {
186 $this->Label = FormatStringForDisplay($this->Label);
187 $this->Keywords = FormatStringForDisplay($this->Keywords);
188 $this->AuthUsername = FormatStringForDisplay($this->AuthUsername);
189 $this->Query = FormatStringForDisplay($this->Query);