1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13: 14: 15: 16:
17: class SearchResultModule {
18:
19: 20: 21: 22:
23: protected $_countValues;
24:
25: 26: 27: 28:
29: protected $_searchResultCount = 0;
30:
31: 32: 33: 34:
35: protected $_label;
36:
37: 38: 39: 40:
41: protected $_itemsPerPage;
42:
43: 44: 45: 46:
47: protected $_maxTeaserTextLen;
48:
49: 50: 51: 52:
53: protected $_searchTerm;
54:
55: 56: 57: 58:
59: protected $_dispSearchTerm = NULL;
60:
61: 62: 63: 64:
65: protected $_prepSearchTerm = NULL;
66:
67: 68: 69: 70:
71: protected $_searchResultData = NULL;
72:
73: 74: 75: 76:
77: protected $_searchResults = NULL;
78:
79: 80: 81: 82:
83: protected $_numberOfPages = NULL;
84:
85: 86: 87: 88:
89: public function __construct(array $options = NULL) {
90:
91:
92: if (NULL !== $options) {
93: foreach ($options as $name => $value) {
94: $name = '_' . $name;
95: $this->$name = $value;
96: }
97: }
98:
99:
100: $this->_cfg = cRegistry::getConfig();
101: $this->_db = cRegistry::getDb();
102: $this->_client = cRegistry::getClientId();
103: $this->_lang = cRegistry::getLanguageId();
104: $this->_idcat = cRegistry::getCategoryId();
105: $this->_idart = cRegistry::getArticleId();
106: $this->_sess = cRegistry::getSession();
107:
108:
109: global $sArtSpecs;
110: $this->_artSpecs = $sArtSpecs;
111:
112:
113: $searchTerm = $this->_searchTerm;
114: $searchTerm = stripslashes($searchTerm);
115: $searchTerm = strip_tags($searchTerm);
116: $searchTerm = conHtmlentities($searchTerm);
117:
118: $searchTerm = urldecode($searchTerm);
119: $searchTerm = str_replace(' + ', ' AND ', $searchTerm);
120: $searchTerm = str_replace(' - ', ' NOT ', $searchTerm);
121:
122:
123: $this->_dispSearchTerm = $searchTerm;
124:
125:
126: if (strlen(trim($searchTerm)) > 0) {
127: $searchTerm = conHtmlEntityDecode($searchTerm);
128: if (false === stristr($searchTerm, ' or ')) {
129: $this->_combine = 'and';
130: } else {
131: $this->_combine = 'or';
132: }
133:
134: $searchTerm = htmlentities($searchTerm, ENT_COMPAT, 'UTF-8');
135: $searchTerm = (trim(strtolower($searchTerm)));
136: $searchTerm = html_entity_decode($searchTerm, ENT_COMPAT, 'UTF-8');
137:
138: $searchTerm = str_replace(' and ', ' ', $searchTerm);
139: $searchTerm = str_replace(' or ', ' ', $searchTerm);
140: }
141:
142:
143: $this->_prepSearchTerm = $searchTerm;
144:
145:
146: $this->_performSearch();
147: }
148:
149: 150:
151: public function render() {
152: $tpl = cSmartyFrontend::getInstance();
153:
154: $tpl->assign('label', $this->_label);
155: $tpl->assign('searchTerm', $this->_dispSearchTerm);
156: $tpl->assign('currentPage', $this->_page);
157:
158: $tpl->assign('results', $this->_getResults());
159: $tpl->assign('msgResult', $this->_msgResult);
160: $tpl->assign('msgRange', $this->_msgRange);
161: $tpl->assign('prev', $this->_getPreviousLink());
162: $tpl->assign('next', $this->_getNextLink());
163: $tpl->assign('pages', $this->_getPageLinks());
164:
165:
166:
167: if (class_exists('ModRewrite') && ModRewrite::isEnabled()) {
168: $tpl->assign('action', cUri::getInstance()->build(array(
169: 'idart' => cRegistry::getArticleLanguageId(),
170: 'lang' => cRegistry::getLanguageId()
171: )));
172: } else {
173: $tpl->assign('action', 'front_content.php');
174: $tpl->assign('idart', cRegistry::getArticleLanguageId());
175: $tpl->assign('idlang', cRegistry::getLanguageId());
176: }
177:
178: $tpl->display($this->_templateName);
179: }
180:
181: 182:
183: protected function _performSearch() {
184:
185:
186:
187: $search = new cSearch(array(
188:
189: 'db' => 'regexp',
190:
191: 'combine' => $this->_combine,
192:
193:
194: 'exclude' => false,
195:
196: 'cat_tree' => $this->_getSearchableIdcats(),
197:
198:
199: 'artspecs' => $this->_getArticleSpecs(),
200:
201:
202: 'protected' => true
203: ));
204: $search->setCmsOptions(array(
205: 'head',
206: 'html',
207: 'htmlhead',
208: 'htmltext',
209: 'text'
210: ));
211: if (strlen($this->_prepSearchTerm) > 1) {
212: $searchResultArray = $search->searchIndex($this->_prepSearchTerm, '');
213:
214: $searchResultCount = 0;
215: if (false !== $searchResultArray) {
216:
217: $this->_searchResultCount = count($searchResultArray);
218:
219:
220: $this->_searchResults = new cSearchResult($searchResultArray, $this->_itemsPerPage);
221:
222:
223: $this->_numberOfPages = $this->_searchResults->getNumberOfPages();
224:
225:
226: $this->_searchResults->setReplacement('<strong>', '</strong>');
227: }
228:
229:
230: if (0 === $this->_searchResultCount) {
231: $this->_msgResult = sprintf($this->_label['msgNoResultsFound'], $this->_dispSearchTerm);
232: } else {
233: $this->_msgResult = sprintf($this->_label['msgResultsFound'], $this->_dispSearchTerm, $this->_searchResultCount);
234: }
235: }
236: }
237:
238: 239: 240: 241:
242: protected function _getMsgResult() {
243: return $this->_msgResult;
244: }
245:
246: 247: 248: 249: 250:
251: protected function _setMsgResult($count, $countIdarts) {
252: $this->_countValues = $count;
253:
254:
255: if (0 === $this->_searchResultCount) {
256: $this->_msgResult = sprintf($this->_label['msgNoResultsFound'], $this->_dispSearchTerm);
257: } else {
258: $this->_msgResult = sprintf($this->_label['msgResultsFound'], $this->_dispSearchTerm, $this->_searchResultCount);
259: }
260: }
261:
262: 263: 264: 265: 266: 267:
268: protected function _getSearchableIdcats() {
269: $searchableIdcats = getEffectiveSetting('searchable', 'idcats', 1);
270: $searchableIdcats = explode(',', $searchableIdcats);
271:
272: return $searchableIdcats;
273: }
274:
275: 276: 277: 278: 279: 280: 281:
282: protected function _getArticleSpecs() {
283: $sql = "-- getArticleSpecs()
284: SELECT
285: idartspec
286: , artspec
287: FROM
288: " . $this->_cfg['tab']['art_spec'] . "
289: WHERE
290: client = $this->_client
291: AND lang = $this->_lang
292: AND online = 1
293: ;";
294:
295: $this->_db->query($sql);
296:
297: $aArtSpecs = array();
298: while ($this->_db->next_record()) {
299: $aArtSpecs[] = $this->_db->f('idartspec');
300: }
301: $aArtSpecs[] = 0;
302:
303: return $aArtSpecs;
304: }
305:
306: 307: 308: 309:
310: protected function _getResults() {
311: if (NULL === $this->_searchResults) {
312: return array();
313: }
314:
315:
316: $searchResultPage = $this->_searchResults->getSearchResultPage($this->_page);
317:
318:
319: if (0 == count($searchResultPage) > 0) {
320: return array();
321: }
322:
323:
324: $entries = array();
325: $i = 0;
326: foreach (array_keys($searchResultPage) as $idart) {
327:
328: $i++;
329:
330:
331: $number = $this->_itemsPerPage * ($this->_page - 1) + $i;
332:
333:
334: $headlines = $this->_searchResults->getSearchContent($idart, 'HTMLHEAD', 1);
335: $headline = cApiStrTrimAfterWord($headlines[0], $this->_maxTeaserTextLen);
336:
337:
338: $subheadlines = $this->_searchResults->getSearchContent($idart, 'HTMLHEAD', 2);
339: $subheadline = cApiStrTrimAfterWord($subheadlines[0], $this->_maxTeaserTextLen);
340:
341:
342: $paragraphs = $this->_searchResults->getSearchContent($idart, 'HTML', 1);
343: $paragraph = cApiStrTrimAfterWord($paragraphs[0], $this->_maxTeaserTextLen);
344:
345:
346: $similarity = $this->_searchResults->getSimilarity($idart);
347: $similarity = sprintf("%.0f", $similarity);
348:
349:
350: $href = cUri::getInstance()->build(array(
351: 'lang' => cRegistry::getLanguageId(),
352: 'idcat' => $this->_searchResults->getArtCat($idart),
353: 'idart' => $idart
354: ));
355:
356:
357: $entries[] = array(
358: 'number' => $number,
359: 'headline' => $headline,
360: 'subheadline' => $subheadline,
361: 'paragraph' => $paragraph,
362: 'similarity' => $similarity,
363: 'href' => $href
364: );
365: }
366:
367: $lower = ($this->_page - 1) * $this->_itemsPerPage + 1;
368: $upper = $lower + count($entries) - 1;
369: $total = $this->_searchResults->getNumberOfResults();
370:
371: $this->_msgRange = sprintf($this->_label['msgRange'], $lower, $upper, $total);
372:
373: return $entries;
374: }
375:
376: 377: 378: 379:
380: protected function _getPreviousLink() {
381:
382:
383: if (1 >= $this->_page) {
384: return '';
385: }
386:
387:
388: $url = $this->_getPageLink($this->_dispSearchTerm, $this->_page - 1);
389:
390: return $url;
391: }
392:
393: 394: 395: 396:
397: protected function _getNextLink() {
398:
399:
400: if ($this->_page >= $this->_numberOfPages) {
401: return '';
402: }
403:
404:
405: $url = $this->_getPageLink($this->_dispSearchTerm, $this->_page + 1);
406:
407: return $url;
408: }
409:
410: 411: 412: 413: 414:
415: protected function _getPageLinks() {
416: $pageLinks = array();
417: for ($i = 1; $i <= $this->_numberOfPages; $i++) {
418: $pageLinks[$i] = $this->_getPageLink($this->_dispSearchTerm, $i);
419: }
420:
421: return $pageLinks;
422: }
423:
424: 425: 426: 427: 428: 429: 430: 431:
432: protected function _getPageLink($searchTerm = NULL, $page = NULL) {
433:
434:
435: $params = array(
436: 'lang' => $this->_lang,
437: 'idcat' => $this->_idcat,
438: 'idart' => $this->_idart
439: );
440:
441: if (NULL !== $searchTerm) {
442: $params['search_term'] = $searchTerm;
443: }
444: if (NULL !== $page) {
445: $params['page'] = $page . $this->_artSpecs;
446: }
447:
448: $defaultParams = $params;
449:
450:
451:
452:
453:
454: $url_builder = array(
455: 'front_content',
456: 'MR'
457: );
458: if (false === in_array($this->_cfg['url_builder']['name'], $url_builder)) {
459: $params = array(
460: 'search' => $params,
461: 'lang' => $this->_lang,
462: 'idcat' => $this->_idcat,
463: 'level' => 1
464: );
465: }
466:
467: try {
468: $url = cUri::getInstance()->build($params);
469: } catch (cInvalidArgumentException $e) {
470: $url = $this->_sess->url(implode('?', array(
471: 'front_content.php',
472: implode('&', $defaultParams)
473: )));
474: }
475:
476: return $url;
477: }
478:
479: }
480:
481: ?>