1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\CMS\Search; |
4
|
|
|
|
5
|
|
|
use Controller; |
6
|
|
|
use SilverStripe\ORM\DB; |
7
|
|
|
use SilverStripe\ORM\SS_List; |
8
|
|
|
use Form; |
9
|
|
|
use FieldList; |
10
|
|
|
use TextField; |
11
|
|
|
use HiddenField; |
12
|
|
|
use Translatable; |
13
|
|
|
use FormAction; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Standard basic search form which conducts a fulltext search on all {@link SiteTree} |
17
|
|
|
* objects. |
18
|
|
|
* |
19
|
|
|
* If multilingual content is enabled through the {@link Translatable} extension, |
20
|
|
|
* only pages the currently set language on the holder for this searchform are found. |
21
|
|
|
* The language is set through a hidden field in the form, which is prepoluated |
22
|
|
|
* with {@link Translatable::get_current_locale()} when then form is constructed. |
23
|
|
|
* |
24
|
|
|
* @see Use ModelController and SearchContext for a more generic search implementation based around DataObject |
25
|
|
|
* @package cms |
26
|
|
|
* @subpackage search |
27
|
|
|
*/ |
28
|
|
|
class SearchForm extends Form { |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var int $pageLength How many results are shown per page. |
32
|
|
|
* Relies on pagination being implemented in the search results template. |
33
|
|
|
*/ |
34
|
|
|
protected $pageLength = 10; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Classes to search |
38
|
|
|
*/ |
39
|
|
|
protected $classesToSearch = array( |
40
|
|
|
"SilverStripe\\CMS\\Model\\SiteTree", "File" |
41
|
|
|
); |
42
|
|
|
|
43
|
|
|
private static $casting = array( |
|
|
|
|
44
|
|
|
'SearchQuery' => 'Text' |
45
|
|
|
); |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* |
49
|
|
|
* @param Controller $controller |
50
|
|
|
* @param string $name The name of the form (used in URL addressing) |
51
|
|
|
* @param FieldList $fields Optional, defaults to a single field named "Search". Search logic needs to be customized |
52
|
|
|
* if fields are added to the form. |
53
|
|
|
* @param FieldList $actions Optional, defaults to a single field named "Go". |
54
|
|
|
*/ |
55
|
|
|
public function __construct($controller, $name, $fields = null, $actions = null) { |
56
|
|
|
if(!$fields) { |
57
|
|
|
$fields = new FieldList( |
58
|
|
|
new TextField('Search', _t('SearchForm.SEARCH', 'Search') |
59
|
|
|
)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if(class_exists('Translatable') && singleton('SilverStripe\\CMS\\Model\\SiteTree')->hasExtension('Translatable')) { |
63
|
|
|
$fields->push(new HiddenField('searchlocale', 'searchlocale', Translatable::get_current_locale())); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if(!$actions) { |
67
|
|
|
$actions = new FieldList( |
68
|
|
|
new FormAction("getResults", _t('SearchForm.GO', 'Go')) |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
parent::__construct($controller, $name, $fields, $actions); |
73
|
|
|
|
74
|
|
|
$this->setFormMethod('get'); |
75
|
|
|
|
76
|
|
|
$this->disableSecurityToken(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Return a rendered version of this form. |
82
|
|
|
* |
83
|
|
|
* This is returned when you access a form as $FormObject rather |
84
|
|
|
* than <% with FormObject %> |
85
|
|
|
*/ |
86
|
|
|
public function forTemplate() { |
87
|
|
|
$return = $this->renderWith(array_merge( |
88
|
|
|
(array)$this->getTemplate(), |
89
|
|
|
array('SilverStripe\\CMS\\Search\\SearchForm', 'Form') |
90
|
|
|
)); |
91
|
|
|
|
92
|
|
|
// Now that we're rendered, clear message |
93
|
|
|
$this->clearMessage(); |
94
|
|
|
|
95
|
|
|
return $return; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Set the classes to search. |
100
|
|
|
* Currently you can only choose from "SiteTree" and "File", but a future version might improve this. |
101
|
|
|
*/ |
102
|
|
|
public function classesToSearch($classes) { |
103
|
|
|
$illegalClasses = array_diff($classes, array('SilverStripe\\CMS\\Model\\SiteTree', 'File')); |
104
|
|
|
if($illegalClasses) { |
|
|
|
|
105
|
|
|
user_error("SearchForm::classesToSearch() passed illegal classes '" . implode("', '", $illegalClasses) . "'. At this stage, only File and SiteTree are allowed", E_USER_WARNING); |
106
|
|
|
} |
107
|
|
|
$legalClasses = array_intersect($classes, array('SilverStripe\\CMS\\Model\\SiteTree', 'File')); |
108
|
|
|
$this->classesToSearch = $legalClasses; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get the classes to search |
113
|
|
|
* |
114
|
|
|
* @return array |
115
|
|
|
*/ |
116
|
|
|
public function getClassesToSearch() { |
117
|
|
|
return $this->classesToSearch; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Return dataObjectSet of the results using $_REQUEST to get info from form. |
122
|
|
|
* Wraps around {@link searchEngine()}. |
123
|
|
|
* |
124
|
|
|
* @param int $pageLength DEPRECATED 2.3 Use SearchForm->pageLength |
125
|
|
|
* @param array $data Request data as an associative array. Should contain at least a key 'Search' with all searched keywords. |
126
|
|
|
* @return SS_List |
127
|
|
|
*/ |
128
|
|
|
public function getResults($pageLength = null, $data = null){ |
129
|
|
|
// legacy usage: $data was defaulting to $_REQUEST, parameter not passed in doc.silverstripe.org tutorials |
130
|
|
|
if(!isset($data) || !is_array($data)) $data = $_REQUEST; |
131
|
|
|
|
132
|
|
|
// set language (if present) |
133
|
|
View Code Duplication |
if(class_exists('Translatable')) { |
|
|
|
|
134
|
|
|
if(singleton('SilverStripe\\CMS\\Model\\SiteTree')->hasExtension('Translatable') && isset($data['searchlocale'])) { |
135
|
|
|
if($data['searchlocale'] == "ALL") { |
136
|
|
|
Translatable::disable_locale_filter(); |
137
|
|
|
} else { |
138
|
|
|
$origLocale = Translatable::get_current_locale(); |
139
|
|
|
|
140
|
|
|
Translatable::set_current_locale($data['searchlocale']); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$keywords = $data['Search']; |
146
|
|
|
|
147
|
|
|
$andProcessor = create_function('$matches',' |
148
|
|
|
return " +" . $matches[2] . " +" . $matches[4] . " "; |
149
|
|
|
'); |
150
|
|
|
$notProcessor = create_function('$matches', ' |
151
|
|
|
return " -" . $matches[3]; |
152
|
|
|
'); |
153
|
|
|
|
154
|
|
|
$keywords = preg_replace_callback('/()("[^()"]+")( and )("[^"()]+")()/i', $andProcessor, $keywords); |
155
|
|
|
$keywords = preg_replace_callback('/(^| )([^() ]+)( and )([^ ()]+)( |$)/i', $andProcessor, $keywords); |
156
|
|
|
$keywords = preg_replace_callback('/(^| )(not )("[^"()]+")/i', $notProcessor, $keywords); |
157
|
|
|
$keywords = preg_replace_callback('/(^| )(not )([^() ]+)( |$)/i', $notProcessor, $keywords); |
158
|
|
|
|
159
|
|
|
$keywords = $this->addStarsToKeywords($keywords); |
160
|
|
|
|
161
|
|
|
if(!$pageLength) $pageLength = $this->pageLength; |
|
|
|
|
162
|
|
|
$start = isset($_GET['start']) ? (int)$_GET['start'] : 0; |
163
|
|
|
|
164
|
|
|
if(strpos($keywords, '"') !== false || strpos($keywords, '+') !== false || strpos($keywords, '-') !== false || strpos($keywords, '*') !== false) { |
165
|
|
|
$results = DB::get_conn()->searchEngine($this->classesToSearch, $keywords, $start, $pageLength, "\"Relevance\" DESC", "", true); |
166
|
|
|
} else { |
167
|
|
|
$results = DB::get_conn()->searchEngine($this->classesToSearch, $keywords, $start, $pageLength); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
// filter by permission |
171
|
|
|
if($results) foreach($results as $result) { |
172
|
|
|
if(!$result->canView()) $results->remove($result); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
// reset locale |
176
|
|
View Code Duplication |
if(class_exists('Translatable')) { |
|
|
|
|
177
|
|
|
if(singleton('SilverStripe\\CMS\\Model\\SiteTree')->hasExtension('Translatable') && isset($data['searchlocale'])) { |
178
|
|
|
if($data['searchlocale'] == "ALL") { |
179
|
|
|
Translatable::enable_locale_filter(); |
180
|
|
|
} else { |
181
|
|
|
Translatable::set_current_locale($origLocale); |
|
|
|
|
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
return $results; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
protected function addStarsToKeywords($keywords) { |
190
|
|
|
if(!trim($keywords)) return ""; |
191
|
|
|
// Add * to each keyword |
192
|
|
|
$splitWords = preg_split("/ +/" , trim($keywords)); |
193
|
|
|
while(list($i,$word) = each($splitWords)) { |
|
|
|
|
194
|
|
|
if($word[0] == '"') { |
195
|
|
|
while(list($i,$subword) = each($splitWords)) { |
|
|
|
|
196
|
|
|
$word .= ' ' . $subword; |
197
|
|
|
if(substr($subword,-1) == '"') break; |
198
|
|
|
} |
199
|
|
|
} else { |
200
|
|
|
$word .= '*'; |
201
|
|
|
} |
202
|
|
|
$newWords[] = $word; |
|
|
|
|
203
|
|
|
} |
204
|
|
|
return implode(" ", $newWords); |
|
|
|
|
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Get the search query for display in a "You searched for ..." sentence. |
209
|
|
|
* |
210
|
|
|
* @param array $data |
211
|
|
|
* @return string |
212
|
|
|
*/ |
213
|
|
|
public function getSearchQuery($data = null) { |
214
|
|
|
// legacy usage: $data was defaulting to $_REQUEST, parameter not passed in doc.silverstripe.org tutorials |
215
|
|
|
if(!isset($data)) $data = $_REQUEST; |
216
|
|
|
|
217
|
|
|
// The form could be rendered without the search being done, so check for that. |
218
|
|
|
if (isset($data['Search'])) return $data['Search']; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Set the maximum number of records shown on each page. |
223
|
|
|
* |
224
|
|
|
* @param int $length |
225
|
|
|
*/ |
226
|
|
|
public function setPageLength($length) { |
227
|
|
|
$this->pageLength = $length; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @return int |
232
|
|
|
*/ |
233
|
|
|
public function getPageLength() { |
234
|
|
|
return $this->pageLength; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
|
240
|
|
|
|