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