1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @description: Allows user to specifically search products |
5
|
|
|
**/ |
6
|
|
|
class ProductSearchForm extends Form |
|
|
|
|
7
|
|
|
{ |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* |
11
|
|
|
* @return string |
12
|
|
|
*/ |
13
|
|
|
public static function get_last_search_phrase() |
14
|
|
|
{ |
15
|
|
|
$string = ''; |
16
|
|
|
$oldData = Session::get(Config::inst()->get('ProductSearchForm', 'form_data_session_variable')); |
17
|
|
|
if ($oldData && (is_array($oldData) || is_object($oldData))) { |
18
|
|
|
if(isset($oldData['ShortKeyword'])) { |
19
|
|
|
$string = $oldData['ShortKeyword']; |
20
|
|
|
} elseif(isset($oldData['Keyword'])) { |
21
|
|
|
$string = $oldData['Keyword']; |
22
|
|
|
} |
23
|
|
|
} |
24
|
|
|
return trim($string); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* |
29
|
|
|
* @param string $phrase |
30
|
|
|
*/ |
31
|
|
|
public static function set_last_search_phrase($phrase) |
32
|
|
|
{ |
33
|
|
|
$oldData = Session::get(Config::inst()->get('ProductSearchForm', 'form_data_session_variable')); |
34
|
|
|
if ($oldData && (is_array($oldData) || is_object($oldData))) { |
35
|
|
|
$oldData['ShortKeyword'] = $phrase; |
36
|
|
|
$oldData['Keyword'] = $phrase; |
37
|
|
|
} |
38
|
|
|
Session::set(Config::inst()->get('ProductSearchForm', 'form_data_session_variable'), $phrase); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* set to TRUE to show the search logic. |
43
|
|
|
* |
44
|
|
|
* @var bool |
45
|
|
|
*/ |
46
|
|
|
protected $debug = false; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* list of additional fields to add to search. |
50
|
|
|
* |
51
|
|
|
* Additional fields array is formatted as follows: |
52
|
|
|
* array( |
53
|
|
|
* "FormField" => Field, |
54
|
|
|
* "DBField" => Acts On / Searches, |
55
|
|
|
* "FilterUsed" => SearchFilter |
56
|
|
|
* ); |
57
|
|
|
* e.g. |
58
|
|
|
* array( |
59
|
|
|
* [1] => array( |
60
|
|
|
* "FormField" => TextField::create("MyDatabaseField", "Keyword"), |
61
|
|
|
* "DBField" => "MyDatabaseField", |
62
|
|
|
* "FilterUsed" => "PartialMatchFilter" |
63
|
|
|
* ) |
64
|
|
|
* ); |
65
|
|
|
* |
66
|
|
|
* @var array |
67
|
|
|
*/ |
68
|
|
|
protected $additionalFields = array(); |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* list of products that need to be searched. |
72
|
|
|
* |
73
|
|
|
* @var null | Array | Datalist |
74
|
|
|
*/ |
75
|
|
|
protected $productsToSearch = null; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* class name of the buyables to search |
79
|
|
|
* at this stage, you can only search one type of buyable at any one time |
80
|
|
|
* e.g. only products or only mydataobject. |
81
|
|
|
* |
82
|
|
|
* @var string |
83
|
|
|
*/ |
84
|
|
|
protected $baseClassNameForBuyables = ''; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* this is mysql specific, see: https://dev.mysql.com/doc/refman/5.0/en/fulltext-boolean.html. |
88
|
|
|
* |
89
|
|
|
* @var bool |
90
|
|
|
*/ |
91
|
|
|
protected $useBooleanSearch = true; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* get parameters added to the link |
95
|
|
|
* you dont need to start them with & or ? |
96
|
|
|
* e.g. |
97
|
|
|
* a=23&b=234. |
98
|
|
|
* |
99
|
|
|
* @var string |
100
|
|
|
*/ |
101
|
|
|
protected $additionalGetParameters = ''; |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* List of additional fields that should be searched full text. |
105
|
|
|
* We are matching this against the buyable class name. |
106
|
|
|
* |
107
|
|
|
* @var array |
108
|
|
|
*/ |
109
|
|
|
protected $extraBuyableFieldsToSearchFullText = array( |
110
|
|
|
'Product' => array('Title', 'MenuTitle', 'Content', 'MetaDescription'), |
111
|
|
|
'ProductVariation' => array('FullTitle', 'Description'), |
112
|
|
|
); |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Maximum number of results to return |
116
|
|
|
* we limit this because otherwise the system will choke |
117
|
|
|
* the assumption is that no user is really interested in looking at |
118
|
|
|
* tons of results. |
119
|
|
|
* It defaults to: EcommerceConfig::get("ProductGroup", "maximum_number_of_products_to_list"). |
120
|
|
|
* |
121
|
|
|
* @var int |
122
|
|
|
*/ |
123
|
|
|
protected $maximumNumberOfResults = 0; |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* The method on the parent controller that can display the results of the |
127
|
|
|
* search results. |
128
|
|
|
* |
129
|
|
|
* @var string |
130
|
|
|
*/ |
131
|
|
|
protected $controllerSearchResultDisplayMethod = 'searchresults'; |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* array of IDs of the results found so far. |
135
|
|
|
* |
136
|
|
|
* @var array |
137
|
|
|
*/ |
138
|
|
|
protected $resultArray = array(); |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* product groups found. |
142
|
|
|
* |
143
|
|
|
* @var array |
144
|
|
|
*/ |
145
|
|
|
protected $productGroupIDs = array(); |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Number of results found so far. |
149
|
|
|
* |
150
|
|
|
* @var int |
151
|
|
|
*/ |
152
|
|
|
protected $resultArrayPos = 0; |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Is the extended or the short form? |
156
|
|
|
* |
157
|
|
|
* @var bool |
158
|
|
|
*/ |
159
|
|
|
protected $isShortForm = 0; |
160
|
|
|
|
161
|
|
|
public function setControllerSearchResultDisplayMethod($s) |
162
|
|
|
{ |
163
|
|
|
$this->controllerSearchResultDisplayMethod = $s; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function setExtraBuyableFieldsToSearchFullText($a) |
167
|
|
|
{ |
168
|
|
|
$this->extraBuyableFieldsToSearchFullText = $a; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function setBaseClassNameForBuyables($s) |
172
|
|
|
{ |
173
|
|
|
$this->baseClassNameForBuyables = $s; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function setUseBooleanSearch($b) |
177
|
|
|
{ |
178
|
|
|
$this->useBooleanSearch = $b; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function setMaximumNumberOfResults($i) |
182
|
|
|
{ |
183
|
|
|
$this->maximumNumberOfResults = $i; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public function setAdditionalGetParameters($s) |
187
|
|
|
{ |
188
|
|
|
$this->additionalGetParameters = $s; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function addAdditionalField($formField, $dbField, $filterUsed) |
192
|
|
|
{ |
193
|
|
|
$this->additionalFields[$dbField] = array( |
194
|
|
|
'FormField' => $formField, |
195
|
|
|
'DBField' => $dbField, |
196
|
|
|
'FilterUsed' => $filterUsed, |
197
|
|
|
); |
198
|
|
|
$this->fields->push($formField); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* ProductsToSearch can be left blank to search all products. |
203
|
|
|
* |
204
|
|
|
* @param Controller $controller - associated controller |
205
|
|
|
* @param string $name - name of form |
206
|
|
|
* @param string $nameOfProductsBeingSearched - name of the products being search (also see productsToSearch below) |
207
|
|
|
* @param DataList | Array | Null $productsToSearch (see comments above) |
208
|
|
|
*/ |
209
|
|
|
public function __construct($controller, $name, $nameOfProductsBeingSearched = '', $productsToSearch = null) |
210
|
|
|
{ |
211
|
|
|
|
212
|
|
|
//turn of security to allow caching of the form: |
213
|
|
|
$this->disableSecurityToken(); |
214
|
|
|
|
215
|
|
|
//set basics |
216
|
|
|
$productsToSearchCount = 0; |
217
|
|
|
if ($productsToSearch) { |
218
|
|
|
if (is_array($productsToSearch)) { |
219
|
|
|
$productsToSearchCount = count($productsToSearch); |
220
|
|
|
} elseif ($productsToSearch instanceof DataList) { |
221
|
|
|
$productsToSearchCount = $productsToSearch->count(); |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
$this->productsToSearch = $productsToSearch; |
225
|
|
|
if ($this->isShortForm) { |
226
|
|
|
$fields = FieldList::create( |
227
|
|
|
$shortKeywordField = TextField::create('ShortKeyword', '') |
228
|
|
|
); |
229
|
|
|
$actions = FieldList::create( |
230
|
|
|
FormAction::create('doProductSearchForm', 'Go') |
231
|
|
|
); |
232
|
|
|
$shortKeywordField->setAttribute('placeholder', _t('ProductSearchForm.SHORT_KEYWORD_PLACEHOLDER', 'search products ...')); |
233
|
|
|
} else { |
234
|
|
|
if (Config::inst()->get('ProductSearchForm', 'include_price_filters')) { |
235
|
|
|
$fields = FieldList::create( |
236
|
|
|
$keywordField = TextField::create('Keyword', _t('ProductSearchForm.KEYWORDS', 'Keywords')), |
237
|
|
|
NumericField::create('MinimumPrice', _t('ProductSearchForm.MINIMUM_PRICE', 'Minimum Price')), |
238
|
|
|
NumericField::create('MaximumPrice', _t('ProductSearchForm.MAXIMUM_PRICE', 'Maximum Price')) |
239
|
|
|
); |
240
|
|
|
} else { |
241
|
|
|
$fields = FieldList::create( |
242
|
|
|
$keywordField = TextField::create('Keyword', _t('ProductSearchForm.KEYWORDS', 'Keywords')) |
243
|
|
|
); |
244
|
|
|
} |
245
|
|
|
$actions = FieldList::create( |
246
|
|
|
FormAction::create('doProductSearchForm', 'Search') |
247
|
|
|
); |
248
|
|
|
if ($productsToSearchCount) { |
249
|
|
|
$fields->push( |
250
|
|
|
CheckboxField::create('SearchOnlyFieldsInThisSection', _t('ProductSearchForm.ONLY_SHOW', 'Only search in').' <i>'.$nameOfProductsBeingSearched.'</i> ', true) |
251
|
|
|
); |
252
|
|
|
} |
253
|
|
|
if (Director::isDev() || Permission::check('ADMIN')) { |
254
|
|
|
$fields->push(CheckboxField::create('DebugSearch', 'Debug Search')); |
255
|
|
|
} |
256
|
|
|
$keywordField->setAttribute('placeholder', _t('ProductSearchForm.KEYWORD_PLACEHOLDER', 'search products ...')); |
257
|
|
|
} |
258
|
|
|
$requiredFields = array(); |
259
|
|
|
$validator = ProductSearchForm_Validator::create($requiredFields); |
260
|
|
|
parent::__construct($controller, $name, $fields, $actions, $validator); |
261
|
|
|
$this->setFormMethod('get'); |
262
|
|
|
//extensions need to be set after __construct |
263
|
|
|
//extension point |
264
|
|
|
$this->extend('updateFields', $fields); |
265
|
|
|
$this->setFields($fields); |
266
|
|
|
$this->extend('updateActions', $actions); |
267
|
|
|
$this->setActions($actions); |
268
|
|
|
$this->extend('updateValidator', $validator); |
269
|
|
|
$this->setValidator($validator); |
270
|
|
|
|
271
|
|
|
$oldData = Session::get($this->Config()->get('form_data_session_variable')); |
272
|
|
|
if ($oldData && (is_array($oldData) || is_object($oldData))) { |
273
|
|
|
$this->loadDataFrom($oldData); |
|
|
|
|
274
|
|
|
} |
275
|
|
|
$this->extend('updateProductSearchForm', $this); |
276
|
|
|
|
277
|
|
|
return $this; |
|
|
|
|
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
public function doProductSearchForm($data, $form) |
|
|
|
|
281
|
|
|
{ |
282
|
|
|
$searchHistoryObject = null; |
283
|
|
|
$immediateRedirectLink = ''; |
284
|
|
|
if (!$this->maximumNumberOfResults) { |
285
|
|
|
$this->maximumNumberOfResults = EcommerceConfig::get('ProductGroup', 'maximum_number_of_products_to_list'); |
286
|
|
|
} |
287
|
|
|
if (isset($data['DebugSearch'])) { |
288
|
|
|
$this->debug = $data['DebugSearch'] ? true : false; |
289
|
|
|
} |
290
|
|
|
if ($this->debug) { |
291
|
|
|
$this->debugOutput('<hr /><hr /><hr /><h2>Debugging Search Results</h2>'); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
//what is the baseclass? |
295
|
|
|
$baseClassName = $this->baseClassForBuyables; |
|
|
|
|
296
|
|
|
if (!$baseClassName) { |
297
|
|
|
$baseClassName = EcommerceConfig::get('ProductGroup', 'base_buyable_class'); |
298
|
|
|
} |
299
|
|
|
if (!$baseClassName) { |
300
|
|
|
user_error("Can not find $baseClassName (baseClassName)"); |
301
|
|
|
} |
302
|
|
|
//basic get |
303
|
|
|
$searchableFields = ($baseClassName::create()->stat('searchable_fields')); |
|
|
|
|
304
|
|
|
$baseList = $baseClassName::get()->filter(array('ShowInSearch' => 1)); |
305
|
|
|
$ecomConfig = EcommerceDBConfig::current_ecommerce_db_config(); |
306
|
|
|
if ($ecomConfig->OnlyShowProductsThatCanBePurchased) { |
|
|
|
|
307
|
|
|
$baseList->filter(array('AllowPurchase' => 1)); |
308
|
|
|
} |
309
|
|
|
$limitToCurrentSection = false; |
310
|
|
|
if (isset($data['SearchOnlyFieldsInThisSection']) && $data['SearchOnlyFieldsInThisSection']) { |
311
|
|
|
$limitToCurrentSection = true; |
312
|
|
|
if(! $this->productsToSearch) { |
313
|
|
|
$controller = Controller::curr(); |
314
|
|
|
if($controller) { |
315
|
|
|
$this->productsToSearch = $controller->Products(); |
316
|
|
|
} |
317
|
|
|
} |
318
|
|
|
if ($this->productsToSearch instanceof DataList) { |
319
|
|
|
$this->productsToSearch = $this->productsToSearch->map('ID', 'ID')->toArray(); |
|
|
|
|
320
|
|
|
} |
321
|
|
|
//last resort |
322
|
|
|
if($this->productsToSearch) { |
323
|
|
|
$baseList = $baseList->filter(array('ID' => $this->productsToSearch)); |
324
|
|
|
} |
325
|
|
|
} |
326
|
|
|
if (isset($data['MinimumPrice']) && $data['MinimumPrice']) { |
327
|
|
|
$baseList = $baseList->filter(array('Price:GreaterThanOrEqual' => floatval($data['MinimumPrice']))); |
328
|
|
|
} |
329
|
|
|
if (isset($data['MaximumPrice']) && $data['MaximumPrice']) { |
330
|
|
|
$baseList = $baseList->filter(array('Price:LessThanOrEqual' => floatval($data['MaximumPrice']))); |
331
|
|
|
} |
332
|
|
|
//defining some variables |
333
|
|
|
$isKeywordSearch = false; |
334
|
|
|
if ($this->debug) { |
335
|
|
|
if($this->productsToSearch) { |
336
|
|
|
$this->debugOutput('<hr /><h3>PRODUCTS TO SEARCH</h3><pre>'.str_replace($this->sqlWords, array_flip($this->sqlWords), $this->productsToSearch->sql()).'</pre>'); |
337
|
|
|
} |
338
|
|
|
$this->debugOutput('<hr /><h3>BASE LIST</h3><pre>'.str_replace($this->sqlWords, array_flip($this->sqlWords), $baseList->sql()).'</pre>'); |
339
|
|
|
} |
340
|
|
|
//KEYWORD SEARCH - only bother if we have any keywords and results at all ... |
341
|
|
|
if (isset($data['ShortKeyword']) && !isset($data['Keyword'])) { |
342
|
|
|
$data['Keyword'] = $data['ShortKeyword']; |
343
|
|
|
} |
344
|
|
|
if (isset($data['Keyword']) && $keywordPhrase = $data['Keyword']) { |
345
|
|
|
echo 'AAAAAAAAAAA'; |
346
|
|
|
if ($baseList->count()) { |
347
|
|
|
if (strlen($keywordPhrase) > 1) { |
348
|
|
|
$isKeywordSearch = true; |
349
|
|
|
$immediateRedirectLink = ''; |
350
|
|
|
$this->resultArrayPos = 0; |
351
|
|
|
$this->resultArray = array(); |
352
|
|
|
|
353
|
|
|
$this->debugOutput('<hr /><h3>Raw Keyword '.$keywordPhrase.'</h3><pre>'); |
354
|
|
|
$keywordPhrase = Convert::raw2sql($keywordPhrase); |
355
|
|
|
$keywordPhrase = strtolower($keywordPhrase); |
356
|
|
|
|
357
|
|
|
$searchHistoryObjectID = SearchHistory::add_entry($keywordPhrase); |
358
|
|
|
if ($searchHistoryObjectID) { |
359
|
|
|
$searchHistoryObject = SearchHistory::get()->byID($searchHistoryObjectID); |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
// 1) Exact search by code |
363
|
|
|
$count = 0; |
|
|
|
|
364
|
|
|
if ($this->debug) { |
365
|
|
|
$this->debugOutput('<hr /><h2>SEARCH BY CODE</h2>'); |
366
|
|
|
} |
367
|
|
|
$list1 = $baseList->filter(array('InternalItemID' => $keywordPhrase)); |
368
|
|
|
$count = $list1->count(); |
369
|
|
|
if ($count == 1) { |
370
|
|
|
$immediateRedirectLink = $list1->First()->Link(); |
371
|
|
|
$this->controller->redirect($immediateRedirectLink); |
372
|
|
|
$this->debugOutput('<p style="color: red">Found one answer for potential immediate redirect: '.$immediateRedirectLink.'</p>'); |
373
|
|
|
} |
374
|
|
|
if ($count > 0) { |
375
|
|
|
if ($this->addToResults($list1)) { |
|
|
|
|
376
|
|
|
//break; |
377
|
|
|
} |
378
|
|
|
} |
379
|
|
|
if ($this->debug) { |
380
|
|
|
$this->debugOutput("<h3>SEARCH BY CODE RESULT: $count</h3>"); |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
// 2) Search of the entire keyword phrase and its replacements |
384
|
|
|
$count = 0; |
385
|
|
|
if ($this->debug) { |
386
|
|
|
$this->debugOutput('<hr /><h3>FULL KEYWORD SEARCH</h3>'); |
387
|
|
|
} |
388
|
|
|
if ($this->resultArrayPos <= $this->maximumNumberOfResults) { |
389
|
|
|
$keywordPhrase = $this->replaceSearchPhraseOrWord($keywordPhrase); |
390
|
|
|
//now we are going to look for synonyms |
391
|
|
|
$words = explode(' ', trim(preg_replace('!\s+!', ' ', $keywordPhrase))); |
392
|
|
|
foreach ($words as $wordKey => $word) { |
393
|
|
|
$keywordPhrase = $this->replaceSearchPhraseOrWord($keywordPhrase); |
394
|
|
|
} |
395
|
|
|
if ($this->debug) { |
396
|
|
|
$this->debugOutput('<pre>WORD ARRAY: '.print_r($keywordPhrase, 1).'</pre>'); |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
//work out searches |
400
|
|
|
$singleton = $baseClassName::create(); |
401
|
|
|
foreach ($this->extraBuyableFieldsToSearchFullText as $tempClassName => $fieldArrayTemp) { |
402
|
|
|
if ($singleton instanceof $tempClassName) { |
403
|
|
|
$fieldArray = $fieldArrayTemp; |
404
|
|
|
break; |
405
|
|
|
} |
406
|
|
|
} |
407
|
|
|
if ($this->debug) { |
408
|
|
|
$this->debugOutput('<pre>FIELD ARRAY: '.print_r($fieldArray, 1).'</pre>'); |
|
|
|
|
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
$searches = $this->getSearchArrays($keywordPhrase, $fieldArray); |
412
|
|
|
//if($this->debug) { $this->debugOutput("<pre>SEARCH ARRAY: ".print_r($searches, 1)."</pre>");} |
|
|
|
|
413
|
|
|
|
414
|
|
|
//we search exact matches first then other matches ... |
415
|
|
|
foreach ($searches as $search) { |
416
|
|
|
$list2 = $baseList->where($search); |
417
|
|
|
$count = $list2->count(); |
418
|
|
|
if ($this->debug) { |
419
|
|
|
$this->debugOutput("<p>$search: $count</p>"); |
420
|
|
|
} |
421
|
|
|
if ($count > 0) { |
422
|
|
|
if ($this->addToResults($list2)) { |
423
|
|
|
break; |
424
|
|
|
} |
425
|
|
|
} |
426
|
|
|
if ($this->resultArrayPos > $this->maximumNumberOfResults) { |
427
|
|
|
break; |
428
|
|
|
} |
429
|
|
|
} |
430
|
|
|
} |
431
|
|
|
if ($this->debug) { |
432
|
|
|
$this->debugOutput("<h3>FULL KEYWORD SEARCH: $count</h3>"); |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
if ($this->debug) { |
436
|
|
|
$this->debugOutput('<hr /><h3>PRODUCT GROUP SEARCH</h3>'); |
437
|
|
|
} |
438
|
|
|
// 3) Do the same search for Product Group names |
439
|
|
|
$count = 0; |
440
|
|
|
if ($limitToCurrentSection) { |
|
|
|
|
441
|
|
|
//cant search other sections in this case... |
442
|
|
|
} else { |
443
|
|
|
$searches = $this->getSearchArrays($keywordPhrase); |
444
|
|
|
if ($this->debug) { |
445
|
|
|
$this->debugOutput('<pre>SEARCH ARRAY: '.print_r($searches, 1).'</pre>'); |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
foreach ($searches as $search) { |
449
|
|
|
$productGroups = ProductGroup::get()->where($search)->filter(array('ShowInSearch' => 1)); |
450
|
|
|
$count = $productGroups->count(); |
451
|
|
|
//redirect if we find exactly one match and we have no matches so far... |
452
|
|
|
if ($count == 1 && !$this->resultArrayPos && !$limitToCurrentSection) { |
453
|
|
|
$immediateRedirectLink = $productGroups->First()->Link(); |
454
|
|
|
$this->debugOutput('<p style="color: red">Found one answer for potential immediate redirect: '.$immediateRedirectLink.'</p>'); |
455
|
|
|
} |
456
|
|
|
if ($count > 0) { |
457
|
|
|
foreach ($productGroups as $productGroup) { |
458
|
|
|
//we add them like this because we like to keep them in order! |
459
|
|
|
if (!in_array($productGroup->ID, $this->productGroupIDs)) { |
460
|
|
|
$this->productGroupIDs[] = $productGroup->ID; |
461
|
|
|
} |
462
|
|
|
} |
463
|
|
|
} |
464
|
|
|
} |
465
|
|
|
if ($this->debug) { |
466
|
|
|
$this->debugOutput("<h3>PRODUCT GROUP SEARCH: $count</h3>"); |
467
|
|
|
} |
468
|
|
|
} |
469
|
|
|
} |
470
|
|
|
} |
471
|
|
|
} |
472
|
|
|
if (!$isKeywordSearch) { |
473
|
|
|
$this->addToResults($baseList); |
474
|
|
|
} |
475
|
|
|
$redirectToPage = null; |
476
|
|
|
//if no specific section is being searched then we redirect to search page: |
477
|
|
|
if (!$limitToCurrentSection) { |
478
|
|
|
$redirectToPage = DataObject::get_one('ProductGroupSearchPage'); |
479
|
|
|
} |
480
|
|
|
if (!$redirectToPage) { |
481
|
|
|
// for section specific search, |
482
|
|
|
// redirect to the specific section (basically where we came from) |
483
|
|
|
$redirectToPage = $this->controller->dataRecord; |
484
|
|
|
} |
485
|
|
|
|
486
|
|
|
$sessionNameProducts = $redirectToPage->SearchResultsSessionVariable(false); |
487
|
|
|
$sessionNameGroups = $redirectToPage->SearchResultsSessionVariable(true); |
488
|
|
|
|
489
|
|
|
if ($this->debug) { |
490
|
|
|
$this->debugOutput('<hr />'. |
491
|
|
|
'<h3>Previous Search Products: '.$sessionNameProducts.'</h3><p>'.print_r(Session::get($sessionNameProducts), 1).'</p>'. |
492
|
|
|
'<h3>Previous Search Groups: '.$sessionNameGroups.'</h3><p>'.print_r(Session::get($sessionNameGroups), 1).'</p>' |
493
|
|
|
); |
494
|
|
|
} |
495
|
|
|
Session::set($sessionNameProducts, implode(',', $this->resultArray)); |
496
|
|
|
Session::set($sessionNameGroups, implode(',', $this->productGroupIDs)); |
497
|
|
|
Session::save(); |
498
|
|
|
if ($searchHistoryObject) { |
499
|
|
|
$searchHistoryObject->ProductCount = count($this->resultArray); |
500
|
|
|
$searchHistoryObject->GroupCount = count($this->productGroupIDs); |
501
|
|
|
$searchHistoryObject->write(); |
502
|
|
|
} |
503
|
|
|
if ($this->debug) { |
504
|
|
|
$this->debugOutput('<hr />'. |
505
|
|
|
'<h3>SAVING Products to session: '.$sessionNameProducts.'</h3><p>'.print_r(explode(',', Session::get($sessionNameProducts)), 1).'</p>'. |
506
|
|
|
'<h3>SAVING Groups to session: '.$sessionNameGroups.'</h3><p>'.print_r(explode(',', Session::get($sessionNameGroups)), 1).'</p>' |
507
|
|
|
); |
508
|
|
|
} |
509
|
|
|
if ($immediateRedirectLink) { |
510
|
|
|
$link = $immediateRedirectLink; |
511
|
|
|
} else { |
512
|
|
|
$link = $redirectToPage->Link($this->controllerSearchResultDisplayMethod); |
513
|
|
|
} |
514
|
|
|
if ($this->additionalGetParameters) { |
515
|
|
|
$link .= '?'.$this->additionalGetParameters; |
516
|
|
|
} |
517
|
|
|
if ($this->debug) { |
518
|
|
|
die($link); |
|
|
|
|
519
|
|
|
} |
520
|
|
|
$this->controller->redirect($link); |
521
|
|
|
} |
522
|
|
|
|
523
|
|
|
/** |
524
|
|
|
* creates three levels of searches that |
525
|
|
|
* can be executed one after the other, each |
526
|
|
|
* being less specific than the last... |
527
|
|
|
* |
528
|
|
|
* @return array |
|
|
|
|
529
|
|
|
*/ |
530
|
|
|
protected function addToResults($listToAdd) |
531
|
|
|
{ |
532
|
|
|
$listToAdd = $listToAdd->limit($this->maximumNumberOfResults - $this->resultArrayPos); |
533
|
|
|
foreach ($listToAdd as $page) { |
534
|
|
|
if (!in_array($page->ID, $this->resultArray)) { |
535
|
|
|
++$this->resultArrayPos; |
536
|
|
|
$this->resultArray[$this->resultArrayPos] = $page->ID; |
537
|
|
|
if ($this->resultArrayPos > $this->maximumNumberOfResults) { |
538
|
|
|
return true; |
539
|
|
|
} |
540
|
|
|
} |
541
|
|
|
} |
542
|
|
|
|
543
|
|
|
return false; |
544
|
|
|
} |
545
|
|
|
|
546
|
|
|
/** |
547
|
|
|
* creates three levels of searches that |
548
|
|
|
* can be executed one after the other, each |
549
|
|
|
* being less specific than the last... |
550
|
|
|
* |
551
|
|
|
* @param array $words - words being search |
|
|
|
|
552
|
|
|
* @param array $fields - fields being searched |
553
|
|
|
* |
554
|
|
|
* @return array |
555
|
|
|
*/ |
556
|
|
|
protected function getSearchArrays($keywordPhrase, $fields = array('Title', 'MenuTitle')) |
557
|
|
|
{ |
558
|
|
|
//make three levels of search |
559
|
|
|
$searches = array(); |
560
|
|
|
$wordsAsString = preg_replace('!\s+!', ' ', $keywordPhrase); |
561
|
|
|
$wordAsArray = explode(' ', $wordsAsString); |
562
|
|
|
$wordsAsLikeString = trim(implode('%', $wordAsArray)); |
563
|
|
|
if (in_array('Title', $fields)) { |
564
|
|
|
$searches[0][] = "LOWER(\"Title\") = '$wordsAsString'"; // a) Exact match |
565
|
|
|
$searches[1][] = "LOWER(\"Title\") LIKE '%$wordsAsString%'"; // b) Full match within a bigger string |
566
|
|
|
$searches[2][] = "LOWER(\"Title\") LIKE '%$wordsAsLikeString%'"; // c) Words matched individually |
567
|
|
|
} |
568
|
|
|
foreach ($fields as $field) { |
569
|
|
|
$searches[3][] = "LOWER(\"$field\") = '$wordsAsString'"; // a) Exact match |
570
|
|
|
$searches[4][] = "LOWER(\"$field\") LIKE '%$wordsAsString%'"; // b) Full match within a bigger string |
571
|
|
|
$searches[5][] = "LOWER(\"$field\") LIKE '%$wordsAsLikeString%'"; // c) Words matched individually |
572
|
|
|
/* |
573
|
|
|
* OR WORD SEARCH |
574
|
|
|
* OFTEN leads to too many results, so we keep it simple... |
575
|
|
|
foreach($wordArray as $word) { |
576
|
|
|
$searches[6][] = "LOWER(\"$field\") LIKE '%$word%'"; // d) One word match within a bigger string |
577
|
|
|
} |
578
|
|
|
*/ |
579
|
|
|
} |
580
|
|
|
//$searches[3][] = DB::getconn()->fullTextSearchSQL($fields, $wordsAsString, true); |
|
|
|
|
581
|
|
|
$returnArray = array(); |
582
|
|
|
foreach ($searches as $key => $search) { |
583
|
|
|
$returnArray[$key] = implode(' OR ', $search); |
584
|
|
|
} |
585
|
|
|
|
586
|
|
|
return $returnArray; |
587
|
|
|
} |
588
|
|
|
|
589
|
|
|
/** |
590
|
|
|
* saves the form into session. |
591
|
|
|
* |
592
|
|
|
* @param array $data - data from form (OPTIONAL) |
|
|
|
|
593
|
|
|
*/ |
594
|
|
|
public function saveDataToSession($data = null) |
595
|
|
|
{ |
596
|
|
|
if(! is_array($data)) { |
597
|
|
|
$data = $this->getData(); |
598
|
|
|
} |
599
|
|
|
if (isset($data['MinimumPrice']) && !$data['MinimumPrice']) { |
600
|
|
|
unset($data['MinimumPrice']); |
601
|
|
|
} |
602
|
|
|
if (isset($data['MaximumPrice']) && !$data['MaximumPrice']) { |
603
|
|
|
unset($data['MaximumPrice']); |
604
|
|
|
} |
605
|
|
|
if (isset($data['ShortKeyword']) && $data['ShortKeyword']) { |
606
|
|
|
$data['Keyword'] = $data['ShortKeyword']; |
607
|
|
|
} |
608
|
|
|
if (isset($data['Keyword']) && $data['Keyword']) { |
609
|
|
|
$data['ShortKeyword'] = $data['Keyword']; |
610
|
|
|
} |
611
|
|
|
Session::set($this->Config()->get('form_data_session_variable'), $data); |
|
|
|
|
612
|
|
|
} |
613
|
|
|
|
614
|
|
|
private function debugOutput($string) |
615
|
|
|
{ |
616
|
|
|
echo "<br />$string"; |
617
|
|
|
} |
618
|
|
|
|
619
|
|
|
/** |
620
|
|
|
* @var array |
621
|
|
|
* List of words to be replaced. |
622
|
|
|
*/ |
623
|
|
|
protected $sqlWords = array( |
624
|
|
|
"\r\n SELECT" => 'SELECT', |
625
|
|
|
"\r\n FROM" => 'FROM', |
626
|
|
|
"\r\n WHERE" => 'WHERE', |
627
|
|
|
"\r\n HAVING" => 'HAVING', |
628
|
|
|
"\r\n GROUP" => 'GROUP', |
629
|
|
|
"\r\n ORDER BY" => 'ORDER BY', |
630
|
|
|
"\r\n INNER JOIN" => 'INNER JOIN', |
631
|
|
|
"\r\n LEFT JOIN" => 'LEFT JOIN', |
632
|
|
|
); |
633
|
|
|
|
634
|
|
|
/** |
635
|
|
|
* |
636
|
|
|
* @param string $keywordPhrase |
637
|
|
|
* @param string $word (optional word within keywordPhrase) |
638
|
|
|
* |
639
|
|
|
* @return string (updated Keyword Phrase) |
640
|
|
|
*/ |
641
|
|
|
protected function replaceSearchPhraseOrWord($keywordPhrase, $word = '') |
642
|
|
|
{ |
643
|
|
|
if(! $word) { |
644
|
|
|
$word = $keywordPhrase; |
645
|
|
|
} |
646
|
|
|
$replacements = SearchReplacement::get() |
647
|
|
|
->where(" |
648
|
|
|
LOWER(\"Search\") = '$word' OR |
649
|
|
|
LOWER(\"Search\") LIKE '%,$word' OR |
650
|
|
|
LOWER(\"Search\") LIKE '$word,%' OR |
651
|
|
|
LOWER(\"Search\") LIKE '%,$word,%'" |
652
|
|
|
); |
653
|
|
|
//if it is a word replacement then we do not want replace whole phrase ones ... |
654
|
|
|
if($keywordPhrase != $word) { |
655
|
|
|
$replacements = $replacements->exclude(array('ReplaceWholePhrase' => 1)); |
656
|
|
|
} |
657
|
|
|
if ($replacements->count()) { |
658
|
|
|
$replacementsArray = $replacements->map('ID', 'Replace')->toArray(); |
659
|
|
|
if ($this->debug) { |
660
|
|
|
$this->debugOutput("found alias for $word"); |
661
|
|
|
} |
662
|
|
|
foreach ($replacementsArray as $replacementWord) { |
663
|
|
|
$keywordPhrase = str_replace($word, $replacementWord, $keywordPhrase); |
664
|
|
|
} |
665
|
|
|
} |
666
|
|
|
|
667
|
|
|
return $keywordPhrase; |
668
|
|
|
} |
669
|
|
|
|
670
|
|
|
} |
671
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.