1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Helper class for phpMyFAQ search. |
5
|
|
|
* |
6
|
|
|
* PHP Version 5.5 |
7
|
|
|
* |
8
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public License, |
9
|
|
|
* v. 2.0. If a copy of the MPL was not distributed with this file, You can |
10
|
|
|
* obtain one at http://mozilla.org/MPL/2.0/. |
11
|
|
|
* |
12
|
|
|
* @category phpMyFAQ |
13
|
|
|
* @author Thorsten Rinne <[email protected]> |
14
|
|
|
* @copyright 2009-2017 phpMyFAQ Team |
15
|
|
|
* @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 |
16
|
|
|
* @link http://www.phpmyfaq.de |
17
|
|
|
* @since 2009-09-07 |
18
|
|
|
*/ |
19
|
|
|
if (!defined('IS_VALID_PHPMYFAQ')) { |
20
|
|
|
exit(); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Helper. |
25
|
|
|
* |
26
|
|
|
* @category phpMyFAQ |
27
|
|
|
* @author Thorsten Rinne <[email protected]> |
28
|
|
|
* @copyright 2009-2017 phpMyFAQ Team |
29
|
|
|
* @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 |
30
|
|
|
* @link http://www.phpmyfaq.de |
31
|
|
|
* @since 2009-09-07 |
32
|
|
|
*/ |
33
|
|
|
class PMF_Helper_Search extends PMF_Helper |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* Language. |
37
|
|
|
* |
38
|
|
|
* @var PMF_Language |
39
|
|
|
*/ |
40
|
|
|
private $language = null; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* PMF_Pagination object. |
44
|
|
|
* |
45
|
|
|
* @var PMF_Pagination |
46
|
|
|
*/ |
47
|
|
|
private $pagination = null; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Search term. |
51
|
|
|
* |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
private $searchterm = ''; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Constructor. |
58
|
|
|
* |
59
|
|
|
* @param PMF_Configuration $config |
60
|
|
|
* |
61
|
|
|
* @return PMF_Helper_Search |
62
|
|
|
*/ |
63
|
|
|
public function __construct(PMF_Configuration $config) |
64
|
|
|
{ |
65
|
|
|
$this->_config = $config; |
66
|
|
|
$this->pmfLang = $this->getTranslations(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Language setter. |
71
|
|
|
* |
72
|
|
|
* @param PMF_Language $language Language |
73
|
|
|
*/ |
74
|
|
|
public function setLanguage(PMF_Language $language) |
75
|
|
|
{ |
76
|
|
|
$this->language = $language; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* PMF_Pagination setter. |
81
|
|
|
* |
82
|
|
|
* @param PMF_Pagination $pagination PMF_Pagination |
83
|
|
|
*/ |
84
|
|
|
public function setPagination(PMF_Pagination $pagination) |
85
|
|
|
{ |
86
|
|
|
$this->pagination = $pagination; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Searchterm setter. |
91
|
|
|
* |
92
|
|
|
* @param string $searchterm Searchterm |
93
|
|
|
*/ |
94
|
|
|
public function setSearchterm($searchterm) |
95
|
|
|
{ |
96
|
|
|
$this->searchterm = $searchterm; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Renders the results for Typehead. |
101
|
|
|
* |
102
|
|
|
* @param PMF_Search_Resultset $resultSet PMF_Search_Resultset object |
103
|
|
|
* |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
|
|
public function renderInstantResponseResult(PMF_Search_Resultset $resultSet) |
107
|
|
|
{ |
108
|
|
|
$results = []; |
109
|
|
|
$maxResults = $this->_config->get('records.numberOfRecordsPerPage'); |
110
|
|
|
$numOfResults = $resultSet->getNumberOfResults(); |
111
|
|
|
|
112
|
|
|
if (0 < $numOfResults) { |
113
|
|
|
$i = 0; |
114
|
|
|
foreach ($resultSet->getResultset() as $result) { |
115
|
|
|
if ($i > $maxResults) { |
116
|
|
|
continue; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
// Build the link to the faq record |
120
|
|
|
$currentUrl = sprintf('%s?%saction=artikel&cat=%d&id=%d&artlang=%s&highlight=%s', |
121
|
|
|
PMF_Link::getSystemRelativeUri('ajaxresponse.php').'index.php', |
122
|
|
|
$this->sessionId, |
123
|
|
|
$result->category_id, |
124
|
|
|
$result->id, |
125
|
|
|
$result->lang, |
126
|
|
|
urlencode($this->searchterm) |
127
|
|
|
); |
128
|
|
|
|
129
|
|
|
$link = new PMF_Link($currentUrl, $this->_config); |
130
|
|
|
$link->itemTitle = $result->question; |
131
|
|
|
$faq = new stdClass(); |
132
|
|
|
$faq->categoryName = $this->Category->getPath($result->category_id); |
133
|
|
|
$faq->faqQuestion = PMF_Utils::chopString($result->question, 15); |
134
|
|
|
$faq->faqLink = $link->toString(); |
135
|
|
|
|
136
|
|
|
$results['results'][] = $faq; |
137
|
|
|
} |
138
|
|
|
} else { |
139
|
|
|
$results[] = $this->translation['err_noArticles']; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return json_encode($results); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Renders the result page for Instant Response. |
147
|
|
|
* |
148
|
|
|
* @param PMF_Search_Resultset $resultSet PMF_Search_Resultset object |
149
|
|
|
* |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
|
|
public function renderAdminSuggestionResult(PMF_Search_Resultset $resultSet) |
153
|
|
|
{ |
154
|
|
|
$html = ''; |
155
|
|
|
$confPerPage = $this->_config->get('records.numberOfRecordsPerPage'); |
156
|
|
|
$numOfResults = $resultSet->getNumberOfResults(); |
157
|
|
|
|
158
|
|
|
if (0 < $numOfResults) { |
159
|
|
|
$i = 0; |
160
|
|
|
foreach ($resultSet->getResultset() as $result) { |
161
|
|
|
if ($i > $confPerPage) { |
162
|
|
|
continue; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
if (!isset($result->solution_id)) { |
166
|
|
|
$faq = new PMF_Faq($this->_config); |
167
|
|
|
$solutionId = $faq->getSolutionIdFromId($result->id, $result->lang); |
168
|
|
|
} else { |
169
|
|
|
$solutionId = $result->solution_id; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
// Build the link to the faq record |
173
|
|
|
$currentUrl = sprintf('index.php?solution_id=%d', $solutionId); |
174
|
|
|
|
175
|
|
|
$html .= sprintf( |
176
|
|
|
'<label for="%d"><input id="%d" type="radio" name="faqURL" value="%s"> %s</label><br>', |
177
|
|
|
$result->id, |
178
|
|
|
$result->id, |
179
|
|
|
$currentUrl, |
180
|
|
|
$result->question |
181
|
|
|
); |
182
|
|
|
++$i; |
183
|
|
|
} |
184
|
|
|
} else { |
185
|
|
|
$html = $this->translation['err_noArticles']; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
return $html; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Renders the result page for the main search page. |
193
|
|
|
* |
194
|
|
|
* @param PMF_Search_Resultset $resultSet PMF_Search_Resultset object |
195
|
|
|
* @param int $currentPage Current page number |
196
|
|
|
* |
197
|
|
|
* @return string |
198
|
|
|
*/ |
199
|
|
|
public function renderSearchResult(PMF_Search_Resultset $resultSet, $currentPage) |
200
|
|
|
{ |
201
|
|
|
$html = ''; |
202
|
|
|
$confPerPage = $this->_config->get('records.numberOfRecordsPerPage'); |
203
|
|
|
$numOfResults = $resultSet->getNumberOfResults(); |
204
|
|
|
|
205
|
|
|
$totalPages = ceil($numOfResults / $confPerPage); |
206
|
|
|
$lastPage = $currentPage * $confPerPage; |
207
|
|
|
$firstPage = $lastPage - $confPerPage; |
208
|
|
|
if ($lastPage > $numOfResults) { |
209
|
|
|
$lastPage = $numOfResults; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
if (0 < $numOfResults) { |
213
|
|
|
$html .= sprintf( |
214
|
|
|
"<p role=\"heading\" aria-level=\"1\">%s</p>\n", |
215
|
|
|
$this->plurals->GetMsg('plmsgSearchAmount', $numOfResults) |
216
|
|
|
); |
217
|
|
|
|
218
|
|
|
if (1 < $totalPages) { |
219
|
|
|
$html .= sprintf( |
220
|
|
|
"<p><strong>%s%d %s %s</strong></p>\n", |
221
|
|
|
$this->translation['msgPage'], |
222
|
|
|
$currentPage, |
223
|
|
|
$this->translation['msgVoteFrom'], |
224
|
|
|
$this->plurals->GetMsg('plmsgPagesTotal', $totalPages) |
225
|
|
|
); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
$html .= "<ul class=\"phpmyfaq-search-results list-unstyled\">\n"; |
229
|
|
|
|
230
|
|
|
$counter = $displayedCounter = 0; |
231
|
|
|
$faqHelper = new PMF_Helper_Faq($this->_config); |
232
|
|
|
foreach ($resultSet->getResultset() as $result) { |
233
|
|
|
if ($displayedCounter >= $confPerPage) { |
234
|
|
|
break; |
235
|
|
|
} |
236
|
|
|
++$counter; |
237
|
|
|
if ($counter <= $firstPage) { |
238
|
|
|
continue; |
239
|
|
|
} |
240
|
|
|
++$displayedCounter; |
241
|
|
|
|
242
|
|
|
// Set language for current category to fetch the correct category name |
243
|
|
|
$this->Category->setLanguage($result->lang); |
244
|
|
|
|
245
|
|
|
$categoryInfo = $this->Category->getCategoriesFromArticle($result->id); |
246
|
|
|
$categoryInfo = array_values($categoryInfo); //Reset the array keys |
247
|
|
|
$question = PMF_Utils::chopString($result->question, 15); |
248
|
|
|
$answerPreview = $faqHelper->renderAnswerPreview($result->answer, 25); |
249
|
|
|
|
250
|
|
|
$searchterm = str_replace( |
251
|
|
|
['^', '.', '?', '*', '+', '{', '}', '(', ')', '[', ']', '"'], |
252
|
|
|
'', |
253
|
|
|
$this->searchterm |
254
|
|
|
); |
255
|
|
|
$searchterm = preg_quote($searchterm, '/'); |
256
|
|
|
$searchItems = explode(' ', $searchterm); |
257
|
|
|
|
258
|
|
|
if ($this->_config->get('search.enableHighlighting') && PMF_String::strlen($searchItems[0]) > 1) { |
259
|
|
View Code Duplication |
foreach ($searchItems as $item) { |
260
|
|
|
if (PMF_String::strlen($item) > 2) { |
261
|
|
|
$question = PMF_Utils::setHighlightedString($question, $item); |
|
|
|
|
262
|
|
|
$answerPreview = PMF_Utils::setHighlightedString($answerPreview, $item); |
|
|
|
|
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
// Build the link to the faq record |
268
|
|
|
$currentUrl = sprintf( |
269
|
|
|
'%s?%saction=artikel&cat=%d&id=%d&artlang=%s&highlight=%s', |
270
|
|
|
PMF_Link::getSystemRelativeUri(), |
271
|
|
|
$this->sessionId, |
272
|
|
|
$result->category_id, |
273
|
|
|
$result->id, |
274
|
|
|
$result->lang, |
275
|
|
|
urlencode($searchterm) |
276
|
|
|
); |
277
|
|
|
|
278
|
|
|
$oLink = new PMF_Link($currentUrl, $this->_config); |
279
|
|
|
$oLink->text = $question; |
280
|
|
|
$oLink->itemTitle = $oLink->tooltip = $result->question; |
281
|
|
|
|
282
|
|
|
$html .= '<li>'; |
283
|
|
|
$html .= $this->renderScore($result->score * 33); |
284
|
|
|
$html .= sprintf('<strong>%s</strong>: %s<br />', |
285
|
|
|
$categoryInfo[0]['name'], |
286
|
|
|
$oLink->toHtmlAnchor() |
287
|
|
|
); |
288
|
|
|
$html .= sprintf( |
289
|
|
|
"<small class=\"searchpreview\"><strong>%s</strong> %s...</small>\n", |
290
|
|
|
$this->translation['msgSearchContent'], |
291
|
|
|
$answerPreview |
292
|
|
|
); |
293
|
|
|
$html .= '</li>'; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
$html .= "</ul>\n"; |
297
|
|
|
|
298
|
|
|
if (1 < $totalPages) { |
299
|
|
|
$html .= $this->pagination->render(); |
300
|
|
|
} |
301
|
|
|
} else { |
302
|
|
|
$html = $this->translation['err_noArticles']; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
return $html; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* @param PMF_Search_Resultset $resultSet |
310
|
|
|
* @param int $recordId |
311
|
|
|
* |
312
|
|
|
* @return string |
313
|
|
|
*/ |
314
|
|
|
public function renderRelatedFaqs(PMF_Search_Resultset $resultSet, $recordId) |
315
|
|
|
{ |
316
|
|
|
$html = ''; |
317
|
|
|
$numOfResults = $resultSet->getNumberOfResults(); |
318
|
|
|
|
319
|
|
|
if ($numOfResults > 0) { |
320
|
|
|
$html .= '<ul>'; |
321
|
|
|
$counter = 0; |
322
|
|
|
foreach ($resultSet->getResultset() as $result) { |
323
|
|
|
if ($counter >= 5) { |
324
|
|
|
continue; |
325
|
|
|
} |
326
|
|
|
if ($recordId == $result->id) { |
327
|
|
|
continue; |
328
|
|
|
} |
329
|
|
|
++$counter; |
330
|
|
|
|
331
|
|
|
$url = sprintf( |
332
|
|
|
'%s?action=artikel&cat=%d&id=%d&artlang=%s', |
333
|
|
|
PMF_Link::getSystemRelativeUri(), |
334
|
|
|
$result->category_id, |
335
|
|
|
$result->id, |
336
|
|
|
$result->lang |
337
|
|
|
); |
338
|
|
|
$oLink = new PMF_Link($url, $this->_config); |
339
|
|
|
$oLink->itemTitle = $result->question; |
340
|
|
|
$oLink->text = $result->question; |
341
|
|
|
$oLink->tooltip = $result->question; |
342
|
|
|
$html .= '<li>'.$oLink->toHtmlAnchor().'</li>'; |
343
|
|
|
} |
344
|
|
|
$html .= '</ul>'; |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
return $html; |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* Renders the list of the most popular search terms. |
352
|
|
|
* |
353
|
|
|
* @param array $mostPopularSearches Array with popular search terms |
354
|
|
|
* |
355
|
|
|
* @return string |
356
|
|
|
*/ |
357
|
|
|
public function renderMostPopularSearches(Array $mostPopularSearches) |
358
|
|
|
{ |
359
|
|
|
$html = ''; |
360
|
|
|
|
361
|
|
|
foreach ($mostPopularSearches as $searchItem) { |
362
|
|
|
if (PMF_String::strlen($searchItem['searchterm']) > 0) { |
363
|
|
|
|
364
|
|
|
$html .= sprintf( |
365
|
|
|
'<li><a class="pmf tag" href="?search=%s&submit=Search&action=search">%s <span class="badge">%dx</span> </a></li>', |
366
|
|
|
urlencode($searchItem['searchterm']), |
367
|
|
|
$searchItem['searchterm'], |
368
|
|
|
$searchItem['number'] |
369
|
|
|
); |
370
|
|
|
} |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
return $html; |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* @param int $relevance |
378
|
|
|
* |
379
|
|
|
* @return string |
380
|
|
|
*/ |
381
|
|
|
private function renderScore($relevance = 0) |
382
|
|
|
{ |
383
|
|
|
$html = sprintf('<span title="%01.2f%%">', $relevance); |
384
|
|
|
|
385
|
|
|
if (0 === (int)$relevance) { |
386
|
|
|
$html .= '<i aria-hidden="true" class="fa fa-star-o"></i><i aria-hidden="true" class="fa fa-star-o"></i><i aria-hidden="true" class="fa fa-star-o"></i>'; |
387
|
|
|
} elseif ($relevance < 33) { |
388
|
|
|
$html .= '<i aria-hidden="true" class="fa fa-star"></i><i aria-hidden="true" class="fa fa-star-o"></i><i aria-hidden="true" class="fa fa-star-o"></i>'; |
389
|
|
|
} elseif ($relevance < 66) { |
390
|
|
|
$html .= '<i aria-hidden="true" class="fa fa-star"></i><i aria-hidden="true" class="fa fa-star"></i><i aria-hidden="true" class="fa fa-star-o"></i>'; |
391
|
|
|
} else { |
392
|
|
|
$html .= '<i aria-hidden="true" class="fa fa-star"></i><i aria-hidden="true" class="fa fa-star"></i><i aria-hidden="true" class="fa fa-star"></i>'; |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
return $html.'</span>'; |
396
|
|
|
} |
397
|
|
|
} |
398
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.