1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Adds SearchForm and results methods to the controller. |
4
|
|
|
* |
5
|
|
|
* @author Mark Guinn <[email protected]> |
6
|
|
|
* @date 08.29.2013 |
7
|
|
|
* @package shop_search |
8
|
|
|
*/ |
9
|
|
|
class ShopSearchControllerExtension extends Extension |
|
|
|
|
10
|
|
|
{ |
11
|
|
|
private static $allowed_actions = array('SearchForm', 'results', 'search_suggest'); |
|
|
|
|
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @return ShopSearchForm |
15
|
|
|
*/ |
16
|
|
|
public function SearchForm() |
17
|
|
|
{ |
18
|
|
|
return ShopSearchForm::create($this->owner, 'SearchForm', $this->owner->Link() . 'search-suggest'); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param SS_HTTPRequest $req |
24
|
|
|
* @return string |
25
|
|
|
*/ |
26
|
|
|
public function search_suggest(SS_HTTPRequest $req) |
27
|
|
|
{ |
28
|
|
|
/** @var SS_HTTPResponse $response */ |
29
|
|
|
$response = $this->owner->getResponse(); |
30
|
|
|
$callback = $req->requestVar('callback'); |
31
|
|
|
|
32
|
|
|
// convert the search results into usable json for search-as-you-type |
33
|
|
|
if (ShopSearch::config()->search_as_you_type_enabled) { |
34
|
|
|
$searchVars = $req->requestVars(); |
35
|
|
|
$searchVars[ ShopSearch::config()->qs_query ] = $searchVars['term']; |
36
|
|
|
unset($searchVars['term']); |
37
|
|
|
$results = ShopSearch::inst()->suggestWithResults($searchVars); |
38
|
|
|
} else { |
39
|
|
|
$results = array( |
40
|
|
|
'suggestions' => ShopSearch::inst()->suggest($req->requestVar('term')), |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if ($callback) { |
45
|
|
|
$response->addHeader('Content-type', 'application/javascript'); |
46
|
|
|
$response->setBody($callback . '(' . json_encode($results) . ');'); |
47
|
|
|
} else { |
48
|
|
|
$response->addHeader('Content-type', 'application/json'); |
49
|
|
|
$response->setBody(json_encode($results)); |
50
|
|
|
} |
51
|
|
|
return $response; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* If there is a search encoded in the link, go ahead and log it. |
57
|
|
|
* This happens when you click through on a search suggestion |
58
|
|
|
*/ |
59
|
|
|
public function onAfterInit() |
60
|
|
|
{ |
61
|
|
|
$req = $this->owner->getRequest(); |
62
|
|
|
$src = $req->requestVar(Config::inst()->get('ShopSearch', 'qs_source')); |
63
|
|
|
if ($src) { |
64
|
|
|
$qs_q = Config::inst()->get('ShopSearch', 'qs_query'); |
65
|
|
|
$qs_f = Config::inst()->get('ShopSearch', 'qs_filters'); |
66
|
|
|
$vars = json_decode(base64_decode($src), true); |
67
|
|
|
|
68
|
|
|
// log the search |
69
|
|
|
$log = SearchLog::create(array( |
70
|
|
|
'Query' => strtolower($vars[$qs_q]), |
71
|
|
|
'Link' => $req->getURL(false), // These searches will never have child searches, but this will allow us to know what they clicked |
72
|
|
|
'NumResults' => $vars['total'], |
73
|
|
|
'MemberID' => Member::currentUserID(), |
74
|
|
|
'Filters' => !empty($vars[$qs_f]) ? json_encode($vars[$qs_f]) : null, |
75
|
|
|
)); |
76
|
|
|
$log->write(); |
77
|
|
|
|
78
|
|
|
// redirect to the clean page |
79
|
|
|
$this->owner->redirect($req->getURL(false)); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param ArrayData $results |
86
|
|
|
* @param array $data |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
|
|
protected function generateLongTitle(ArrayData $results, array $data) |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
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.