/
CwpSearchResult.php
194 lines (173 loc) · 3.69 KB
/
CwpSearchResult.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
/**
* Container for a set of search results
*/
class CwpSearchResult extends ViewableData {
private static $casting = array(
'Original' => 'Text',
'OriginalLink' => 'Text',
'Suggestion' => 'Text',
'SuggestionLink' => 'Text',
'Query' => 'Text',
'SearchLink' => 'Text',
'RSSLink' => 'Text',
'AtomLink' => 'Text',
);
/**
* List of results
*
* @var PaginatedList
*/
protected $matches;
/**
* Search terms for these results
*
* @var string
*/
protected $query;
/**
* Suggested search keywords
* Used when this search has suggested terms, but following suggestions isn't enabled
*
* @var string
*/
protected $suggestion;
/**
* Original terms superceded by these result.
* Used when a prior search had suggested terms, and follow suggestions is enabled.
*
* @var type
*/
protected $original;
/**
* Create a new CwpSearchResult
*
* @param string $terms
* @param ArrayData $results Result from SolrIndex
*/
public function __construct($terms = '', ArrayData $results = null) {
$this->query = $terms;
if($results) {
// Clean up the results.
$matches = $results->Matches;
foreach($matches as $result) {
if(!$result->canView()) {
$matches->remove($result);
}
}
$this->matches = $matches;
$this->suggestion = $results->SuggestionNice;
}
}
/**
* Get search results
*
* @return PaginatedList
*/
public function getResults() {
return $this->matches;
}
/**
* Check if there are found results
*
* @return bool
*/
public function hasResults() {
return $this->matches && $this->matches->exists();
}
/**
* Get search keywords matching these results
*
* @return string
*/
public function getQuery() {
return $this->query;
}
/**
* Get suggested search keywords
*
* @return string
*/
public function getSuggestion() {
return $this->suggestion;
}
/**
* Get original search keywords superceded by these results
*
* @return string
*/
public function getOriginal() {
return $this->original;
}
/**
* Set original keywords
*
* @param string $original
*/
public function setOriginal($original) {
$this->original = $original;
}
/**
* Get the link to the suggested search
*
* @param string $format Optional output format
* @return string
*/
public function getSuggestionLink($format = null) {
return $this->getLink($this->suggestion, $format);
}
/**
* Gets the link to the given search
*
* @param string $format Optional output format
* @return string
*/
public function getSearchLink($format = null) {
return $this->getLink($this->query, $format);
}
/**
* Gets the link to the original search, with suggestions disabled
*
* @param string $format Optional output format
* @return string
*/
public function getOriginalLink($format = null) {
return $this->getLink($this->original, $format) . "&suggestions=0";
}
/**
* Get link to these results in RSS format
*
* @return string
*/
public function getRSSLink() {
return $this->getLink($this->query, 'rss');
}
/**
* Get link to these results in atom format
*
* @return string
*/
public function getAtomLink() {
return $this->getLink($this->query, 'atom');
}
/**
* Get a search link for given terms
*
* @param string $terms
* @return string|null
*/
protected function getLink($terms, $format = null) {
if(!$terms) {
return null;
}
$link = 'search/SearchForm?Search='.rawurlencode($terms);
if($format) {
$link .= '&format='.rawurlencode($format);
}
return $link;
}
public function hasField($field) {
// Fix customise not detecting custom field getters
return array_key_exists($field, $this->config()->casting);
}
}