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