/** * Find published news in search index * @param array * @return array */ public static function findPublishedInSearchIndexByString($keywords, $limit = '', $offset = 0) { // $objSearch = \Search::searchFor($strKeywords, ($strQueryType == 'or'), $arrPages, 0, 0, $blnFuzzy); $t = static::$strTable; // $arrColumns = array("($t.id IN(" . implode(',', array_map('intval', $arrIds)) . ")"); if (!BE_USER_LOGGED_IN) { $time = time(); $arrColumns[] = "({$t}.start='' OR {$t}.start<{$time}) AND ({$t}.stop='' OR {$t}.stop>{$time}) AND {$t}.published=1"; } $objSearch = \Search::searchFor($keywords, 'or', '', $limit, $offset, true); $arrIds = static::getIdsOfNewsItemsFromSearchObject($objSearch); // $arrIds = array('vgt-03-15'); //return static::findBy('alias', $arrIds); $objArticles = static::findPublishedNewsByIds($arrIds); return $objArticles; }
public static function findCurrentByPidAndFilter($arrPids, $intStart, $intEnd, array $arrFilter = array(), array $arrFilterOptions = array(), $arrFilterConfig = array(), $arrOptions = array(), $count = false) { $t = static::$strTable; $intStart = intval($intStart); $intEnd = intval($intEnd); $arrPids = !is_array($arrPids) ? array($arrPids) : $arrPids; $arrColumns[] = "({$t}.pid IN (" . implode(',', $arrPids) . "))"; $arrColumnsOr = array(); foreach ($arrFilter as $key => $value) { $arrValueOptions = isset($arrFilterOptions[$key]) && is_array($arrFilterOptions[$key]) ? $arrFilterOptions[$key] : array(); switch ($key) { case 'startDate': if ($value && $value >= $intStart) { $intStart = $value; } break; case 'endDate': if ($value && $value <= $intEnd) { $intEnd = strtotime(date('d.m.Y', $value) . ' 23:59:59'); // until last second of the day } break; case 'dates': if ($value != '') { $strQuery = ''; $valueArray = trimsplit(',', $value); foreach ($valueArray as $key => $strDate) { $intDate = strtotime($strDate); if ($key > 0) { $strQuery .= " OR ((({$t}.endDate IS NULL OR {$t}.endDate = '') AND {$t}.startDate = {$intDate}) OR (({$t}.startDate = '' OR {$t}.startDate <= {$intDate}) AND {$t}.endDate >= {$intDate}))"; } else { $strQuery = "((({$t}.endDate IS NULL OR {$t}.endDate = '') AND {$t}.startDate = {$intDate}) OR (({$t}.startDate = '' OR {$t}.startDate <= {$intDate}) AND {$t}.endDate >= {$intDate}))"; } } $arrColumns[] = '(' . $strQuery . ')'; } break; case 'promoter': case 'areasoflaw': if ($value != '') { $valueArray = trimsplit(',', $value); // permit intersections only if (!empty($arrValueOptions)) { $valueArray = array_intersect($valueArray, $arrValueOptions); } if (is_array($valueArray) && !empty($valueArray)) { if ($arrFilterConfig['show_related']) { $arrColumnsOr[] = EventModelHelper::createMySQLRegexpForMultipleIds("{$t}.{$key}", $valueArray, EVENTMODEL_CONDITION_OR); } else { $arrColumns[] = EventModelHelper::createMySQLRegexpForMultipleIds("{$t}.{$key}", $valueArray); } } } else { if (!empty($arrValueOptions)) { $arrColumns[] = EventModelHelper::createMySQLRegexpForMultipleIds("{$t}.{$key}", $arrValueOptions); } } break; case strrpos($key, 'eventtypes', -strlen($key)) !== FALSE: if ($value != '') { $valueArray = trimsplit(',', $value); // permit intersections only if (!empty($arrValueOptions)) { $valueArray = array_intersect($valueArray, $arrValueOptions); } if (is_array($valueArray) && !empty($valueArray)) { if ($arrFilterConfig['show_related']) { $arrColumnsOr[] = EventModelHelper::createMySQLRegexpForMultipleIds("{$t}.eventtypes", $valueArray, EVENTMODEL_CONDITION_OR); } else { $arrColumns[] = EventModelHelper::createMySQLRegexpForMultipleIds("{$t}.eventtypes", $valueArray, EVENTMODEL_CONDITION_AND); } } } else { if (!empty($arrValueOptions)) { $arrColumns[] = EventModelHelper::createMySQLRegexpForMultipleIds("{$t}.eventtypes", $arrValueOptions); } } break; case 'docents': case 'hosts': if ($value != '') { $valueArray = trimsplit(',', $value); if (is_array($valueArray) && !empty($valueArray)) { $arrDocents = array(); $arrMembers = array(); $arrHosts = array(); foreach ($valueArray as $id) { // docent if (\HeimrichHannot\Haste\Util\StringUtil::startsWith($id, 'd')) { $arrDocents[] = substr($id, 1); } else { if (\HeimrichHannot\Haste\Util\StringUtil::startsWith($id, 'm')) { $arrMembers[] = substr($id, 1); } else { if (\HeimrichHannot\Haste\Util\StringUtil::startsWith($id, 'h')) { $arrHosts[] = substr($id, 1); } } } } if (!empty($arrDocents)) { if (is_array($valueArray) && !empty($valueArray)) { if ($arrFilterConfig['show_related']) { $arrColumnsOr[] = EventModelHelper::createMySQLRegexpForMultipleIds("{$t}.docents", $arrDocents, EVENTMODEL_CONDITION_OR); } else { $arrColumns[] = EventModelHelper::createMySQLRegexpForMultipleIds("{$t}.docents", $arrDocents); } } } if (!empty($arrMembers)) { if (is_array($valueArray) && !empty($valueArray)) { if ($arrFilterConfig['show_related'] || $arrFilterConfig['combine_docents']) { $arrColumnsOr[] = EventModelHelper::createMySQLRegexpForMultipleIds("{$t}.memberdocents", $arrMembers, EVENTMODEL_CONDITION_OR); if ($key = 'hosts' || $key == 'docents' && $arrFilterConfig['combine_docents']) { $arrColumnsOr[] = EventModelHelper::createMySQLRegexpForMultipleIds("{$t}.memberhosts", $arrMembers, EVENTMODEL_CONDITION_OR); } } else { $arrColumns[] = EventModelHelper::createMySQLRegexpForMultipleIds("{$t}.memberdocents", $arrMembers); if ($key = 'hosts' || $key == 'docents' && $arrFilterConfig['combine_docents']) { $arrColumns[] = EventModelHelper::createMySQLRegexpForMultipleIds("{$t}.memberhosts", $arrMembers); } } } } if (!empty($arrHosts) && ($key = 'hosts' || $key == 'docents' && $arrFilterConfig['cal_docent_combine'])) { if (is_array($valueArray) && !empty($valueArray)) { if ($arrFilterConfig['show_related']) { $arrColumnsOr[] = EventModelHelper::createMySQLRegexpForMultipleIds("{$t}.hosts", $arrHosts, EVENTMODEL_CONDITION_OR); } else { $arrColumns[] = EventModelHelper::createMySQLRegexpForMultipleIds("{$t}.hosts", $arrHosts); } } } } } break; case 'postal': if ($value != '') { $arrColumns[] = "LEFT({$t}.postal, 1) = ?"; $arrValues[] = $value; } break; case 'timeHours': if ($value != '') { $arrColumns[] = "({$t}.{$key} >= {$value} AND {$t}.{$key} < ({$value} + 1))"; } break; case 'q': if ($value != '' && is_array($arrFilterConfig['jumpTo'])) { try { $objSearch = \Search::searchFor($value, $arrFilterConfig['module']['queryType'] == 'or', $arrFilterConfig['jumpTo'], 0, 0, $arrFilterConfig['module']['fuzzy']); // return if keyword not found if ($objSearch->numRows < 1) { return null; } $arrUrls = $objSearch->fetchEach('url'); $strKeyWordColumns = ""; $n = 0; foreach ($arrUrls as $i => $strAlias) { $strKeyWordColumns .= ($n > 0 ? " OR " : "") . "{$t}.alias = ?"; $arrValues[] = basename($strAlias); $n++; } $arrColumns[] = "({$strKeyWordColumns})"; } catch (\Exception $e) { \System::log('Website search failed: ' . $e->getMessage(), __METHOD__, TL_ERROR); } } break; default: if ($value != '') { $arrColumns[] = "{$t}.{$key}=?"; $arrValues[] = $value; } } } $arrColumns[] = "(({$t}.startTime>={$intStart} AND {$t}.startTime<={$intEnd}) OR ({$t}.endTime>={$intStart} AND {$t}.endTime<={$intEnd}) OR ({$t}.startTime<={$intStart} AND {$t}.endTime>={$intEnd}) OR ({$t}.recurring=1 AND ({$t}.recurrences=0 OR {$t}.repeatEnd>={$intStart}) AND {$t}.startTime<={$intEnd}))"; if (!BE_USER_LOGGED_IN) { $time = time(); $arrColumns[] = "({$t}.start='' OR {$t}.start<{$time}) AND ({$t}.stop='' OR {$t}.stop>{$time}) AND {$t}.published=1"; } // for related search if (!empty($arrColumnsOr)) { $arrColumns[] = implode(' OR ', $arrColumnsOr); } if (!isset($arrOptions['order'])) { $arrOptions['order'] = "{$t}.startTime"; } if ($count) { return static::countBy($arrColumns, $arrValues, $arrOptions); } return static::findBy($arrColumns, $arrValues, $arrOptions); }
/** * Generate the module */ protected function compile() { /** @var \PageModel $objPage */ global $objPage; // Mark the x and y parameter as used (see #4277) if (isset($_GET['x'])) { \Input::get('x'); \Input::get('y'); } // Trigger the search module from a custom form if (!isset($_GET['keywords']) && \Input::post('FORM_SUBMIT') == 'tl_search') { $_GET['keywords'] = \Input::post('keywords'); $_GET['query_type'] = \Input::post('query_type'); $_GET['per_page'] = \Input::post('per_page'); } $blnFuzzy = $this->fuzzy; $strQueryType = \Input::get('query_type') ?: $this->queryType; $strKeywords = trim(\Input::get('keywords')); /** @var \FrontendTemplate|object $objFormTemplate */ $objFormTemplate = new \FrontendTemplate($this->searchType == 'advanced' ? 'mod_search_advanced' : 'mod_search_simple'); $objFormTemplate->uniqueId = $this->id; $objFormTemplate->queryType = $strQueryType; $objFormTemplate->keyword = specialchars($strKeywords); $objFormTemplate->keywordLabel = $GLOBALS['TL_LANG']['MSC']['keywords']; $objFormTemplate->optionsLabel = $GLOBALS['TL_LANG']['MSC']['options']; $objFormTemplate->search = specialchars($GLOBALS['TL_LANG']['MSC']['searchLabel']); $objFormTemplate->matchAll = specialchars($GLOBALS['TL_LANG']['MSC']['matchAll']); $objFormTemplate->matchAny = specialchars($GLOBALS['TL_LANG']['MSC']['matchAny']); $objFormTemplate->id = \Config::get('disableAlias') && \Input::get('id') ? \Input::get('id') : false; $objFormTemplate->action = ampersand(\Environment::get('indexFreeRequest')); // Redirect page if ($this->jumpTo && ($objTarget = $this->objModel->getRelated('jumpTo')) !== null) { $objFormTemplate->action = $this->generateFrontendUrl($objTarget->row()); } $this->Template->form = $objFormTemplate->parse(); $this->Template->pagination = ''; $this->Template->results = ''; // Execute the search if there are keywords if ($strKeywords != '' && $strKeywords != '*' && !$this->jumpTo) { // Reference page if ($this->rootPage > 0) { $intRootId = $this->rootPage; $arrPages = $this->Database->getChildRecords($this->rootPage, 'tl_page'); array_unshift($arrPages, $this->rootPage); } else { $intRootId = $objPage->rootId; $arrPages = $this->Database->getChildRecords($objPage->rootId, 'tl_page'); } // HOOK: add custom logic (see #5223) if (isset($GLOBALS['TL_HOOKS']['customizeSearch']) && is_array($GLOBALS['TL_HOOKS']['customizeSearch'])) { foreach ($GLOBALS['TL_HOOKS']['customizeSearch'] as $callback) { $this->import($callback[0]); $this->{$callback[0]}->{$callback[1]}($arrPages, $strKeywords, $strQueryType, $blnFuzzy); } } // Return if there are no pages if (!is_array($arrPages) || empty($arrPages)) { $this->log('No searchable pages found', __METHOD__, TL_ERROR); return; } $arrResult = null; $strChecksum = md5($strKeywords . $strQueryType . $intRootId . $blnFuzzy); $query_starttime = microtime(true); $strCacheFile = 'system/cache/search/' . $strChecksum . '.json'; // Load the cached result if (file_exists(TL_ROOT . '/' . $strCacheFile)) { $objFile = new \File($strCacheFile, true); if ($objFile->mtime > time() - 1800) { $arrResult = json_decode($objFile->getContent(), true); } else { $objFile->delete(); } } // Cache the result if ($arrResult === null) { try { $objSearch = \Search::searchFor($strKeywords, $strQueryType == 'or', $arrPages, 0, 0, $blnFuzzy); $arrResult = $objSearch->fetchAllAssoc(); } catch (\Exception $e) { $this->log('Website search failed: ' . $e->getMessage(), __METHOD__, TL_ERROR); $arrResult = array(); } \File::putContent($strCacheFile, json_encode($arrResult)); } $query_endtime = microtime(true); // Sort out protected pages if (\Config::get('indexProtected') && !BE_USER_LOGGED_IN) { $this->import('FrontendUser', 'User'); foreach ($arrResult as $k => $v) { if ($v['protected']) { if (!FE_USER_LOGGED_IN) { unset($arrResult[$k]); } else { $groups = deserialize($v['groups']); if (!is_array($groups) || empty($groups) || !count(array_intersect($groups, $this->User->groups))) { unset($arrResult[$k]); } } } } $arrResult = array_values($arrResult); } $count = count($arrResult); $this->Template->count = $count; $this->Template->page = null; $this->Template->keywords = $strKeywords; // No results if ($count < 1) { $this->Template->header = sprintf($GLOBALS['TL_LANG']['MSC']['sEmpty'], $strKeywords); $this->Template->duration = substr($query_endtime - $query_starttime, 0, 6) . ' ' . $GLOBALS['TL_LANG']['MSC']['seconds']; return; } $from = 1; $to = $count; // Pagination if ($this->perPage > 0) { $id = 'page_s' . $this->id; $page = \Input::get($id) !== null ? \Input::get($id) : 1; $per_page = \Input::get('per_page') ?: $this->perPage; // Do not index or cache the page if the page number is outside the range if ($page < 1 || $page > max(ceil($count / $per_page), 1)) { /** @var \PageError404 $objHandler */ $objHandler = new $GLOBALS['TL_PTY']['error_404'](); $objHandler->generate($objPage->id); } $from = ($page - 1) * $per_page + 1; $to = $from + $per_page > $count ? $count : $from + $per_page - 1; // Pagination menu if ($to < $count || $from > 1) { $objPagination = new \Pagination($count, $per_page, \Config::get('maxPaginationLinks'), $id); $this->Template->pagination = $objPagination->generate("\n "); } $this->Template->page = $page; } // Get the results for ($i = $from - 1; $i < $to && $i < $count; $i++) { /** @var \FrontendTemplate|object $objTemplate */ $objTemplate = new \FrontendTemplate($this->searchTpl ?: 'search_default'); $objTemplate->url = $arrResult[$i]['url']; $objTemplate->link = $arrResult[$i]['title']; $objTemplate->href = $arrResult[$i]['url']; $objTemplate->title = specialchars($arrResult[$i]['title']); $objTemplate->class = ($i == $from - 1 ? 'first ' : '') . ($i == $to - 1 || $i == $count - 1 ? 'last ' : '') . ($i % 2 == 0 ? 'even' : 'odd'); $objTemplate->relevance = sprintf($GLOBALS['TL_LANG']['MSC']['relevance'], number_format($arrResult[$i]['relevance'] / $arrResult[0]['relevance'] * 100, 2) . '%'); $objTemplate->filesize = $arrResult[$i]['filesize']; $objTemplate->matches = $arrResult[$i]['matches']; $arrContext = array(); $arrMatches = trimsplit(',', $arrResult[$i]['matches']); // Get the context foreach ($arrMatches as $strWord) { $arrChunks = array(); preg_match_all('/(^|\\b.{0,' . $this->contextLength . '}\\PL)' . str_replace('+', '\\+', $strWord) . '(\\PL.{0,' . $this->contextLength . '}\\b|$)/ui', $arrResult[$i]['text'], $arrChunks); foreach ($arrChunks[0] as $strContext) { $arrContext[] = ' ' . $strContext . ' '; } } // Shorten the context and highlight all keywords if (!empty($arrContext)) { $objTemplate->context = trim(\StringUtil::substrHtml(implode('…', $arrContext), $this->totalLength)); $objTemplate->context = preg_replace('/(\\PL)(' . implode('|', $arrMatches) . ')(\\PL)/ui', '$1<span class="highlight">$2</span>$3', $objTemplate->context); $objTemplate->hasContext = true; } $this->Template->results .= $objTemplate->parse(); } $this->Template->header = vsprintf($GLOBALS['TL_LANG']['MSC']['sResults'], array($from, $to, $count, $strKeywords)); $this->Template->duration = substr($query_endtime - $query_starttime, 0, 6) . ' ' . $GLOBALS['TL_LANG']['MSC']['seconds']; } }
/** * Generate the module */ protected function compile() { // Trigger the search module from a custom form if (!$_GET['keywords'] && \Input::post('FORM_SUBMIT') == 'tl_search') { $_GET['keywords'] = \Input::post('keywords'); $_GET['query_type'] = \Input::post('query_type'); $_GET['per_page'] = \Input::post('per_page'); } // Remove insert tags $strKeywords = trim(\Input::get('keywords')); $strKeywords = preg_replace('/\\{\\{[^\\}]*\\}\\}/', '', $strKeywords); // Overwrite the default query_type if (\Input::get('query_type')) { $this->queryType = \Input::get('query_type'); } $objFormTemplate = new \FrontendTemplate($this->searchType == 'advanced' ? 'mod_search_advanced' : 'mod_search_simple'); $objFormTemplate->uniqueId = $this->id; $objFormTemplate->queryType = $this->queryType; $objFormTemplate->keyword = specialchars($strKeywords); $objFormTemplate->keywordLabel = $GLOBALS['TL_LANG']['MSC']['keywords']; $objFormTemplate->optionsLabel = $GLOBALS['TL_LANG']['MSC']['options']; $objFormTemplate->search = specialchars($GLOBALS['TL_LANG']['MSC']['searchLabel']); $objFormTemplate->matchAll = specialchars($GLOBALS['TL_LANG']['MSC']['matchAll']); $objFormTemplate->matchAny = specialchars($GLOBALS['TL_LANG']['MSC']['matchAny']); $objFormTemplate->id = $GLOBALS['TL_CONFIG']['disableAlias'] && \Input::get('id') ? \Input::get('id') : false; $objFormTemplate->action = $this->getIndexFreeRequest(); // Redirect page if ($this->jumpTo && ($objTarget = $this->objModel->getRelated('jumpTo')) !== null) { $objFormTemplate->action = $this->generateFrontendUrl($objTarget->row()); } $this->Template->form = $objFormTemplate->parse(); $this->Template->pagination = ''; $this->Template->results = ''; // Execute the search if there are keywords if ($strKeywords != '' && $strKeywords != '*' && !$this->jumpTo) { // Reference page if ($this->rootPage > 0) { $intRootId = $this->rootPage; $arrPages = $this->Database->getChildRecords($this->rootPage, 'tl_page'); array_unshift($arrPages, $this->rootPage); } else { global $objPage; $intRootId = $objPage->rootId; $arrPages = $this->Database->getChildRecords($objPage->rootId, 'tl_page'); } // Return if there are no pages if (!is_array($arrPages) || empty($arrPages)) { $this->log('No searchable pages found', 'ModuleSearch compile()', TL_ERROR); return; } $arrResult = null; $strChecksum = md5($strKeywords . \Input::get('query_type') . $intRootId . $this->fuzzy); $query_starttime = microtime(true); $strCacheFile = 'system/cache/search/' . $strChecksum . '.json'; // Load the cached result if (file_exists(TL_ROOT . '/' . $strCacheFile)) { $objFile = new \File($strCacheFile); if ($objFile->mtime > time() - 1800) { $arrResult = json_decode($objFile->getContent(), true); } else { $objFile->delete(); } } // Cache the result if ($arrResult === null) { try { $objSearch = \Search::searchFor($strKeywords, \Input::get('query_type') == 'or', $arrPages, 0, 0, $this->fuzzy); $arrResult = $objSearch->fetchAllAssoc(); } catch (\Exception $e) { $this->log('Website search failed: ' . $e->getMessage(), 'ModuleSearch compile()', TL_ERROR); $arrResult = array(); } $objFile = new \File($strCacheFile); $objFile->write(json_encode($arrResult)); $objFile->close(); } $query_endtime = microtime(true); // Sort out protected pages if ($GLOBALS['TL_CONFIG']['indexProtected'] && !BE_USER_LOGGED_IN) { $this->import('FrontendUser', 'User'); foreach ($arrResult as $k => $v) { if ($v['protected']) { if (!FE_USER_LOGGED_IN) { unset($arrResult[$k]); } else { $groups = deserialize($v['groups']); if (!is_array($groups) || empty($groups) || !count(array_intersect($groups, $this->User->groups))) { unset($arrResult[$k]); } } } } $arrResult = array_values($arrResult); } $count = count($arrResult); // No results if ($count < 1) { $this->Template->header = sprintf($GLOBALS['TL_LANG']['MSC']['sEmpty'], $strKeywords); $this->Template->duration = substr($query_endtime - $query_starttime, 0, 6) . ' ' . $GLOBALS['TL_LANG']['MSC']['seconds']; return; } $from = 1; $to = $count; // Pagination if ($this->perPage > 0) { $id = 'page_s' . $this->id; $page = \Input::get($id) ?: 1; $per_page = \Input::get('per_page') ?: $this->perPage; // Do not index or cache the page if the page number is outside the range if ($page < 1 || $page > max(ceil($count / $per_page), 1)) { global $objPage; $objPage->noSearch = 1; $objPage->cache = 0; // Send a 404 header header('HTTP/1.1 404 Not Found'); return; } $from = ($page - 1) * $per_page + 1; $to = $from + $per_page > $count ? $count : $from + $per_page - 1; // Pagination menu if ($to < $count || $from > 1) { $objPagination = new \Pagination($count, $per_page, 7, $id); $this->Template->pagination = $objPagination->generate("\n "); } } // Get the results for ($i = $from - 1; $i < $to && $i < $count; $i++) { $objTemplate = new \FrontendTemplate($this->searchTpl ?: 'search_default'); $objTemplate->url = $arrResult[$i]['url']; $objTemplate->link = $arrResult[$i]['title']; $objTemplate->href = $arrResult[$i]['url']; $objTemplate->title = specialchars($arrResult[$i]['title']); $objTemplate->class = ($i == $from - 1 ? 'first ' : '') . ($i == $to - 1 || $i == $count - 1 ? 'last ' : '') . ($i % 2 == 0 ? 'even' : 'odd'); $objTemplate->relevance = sprintf($GLOBALS['TL_LANG']['MSC']['relevance'], number_format($arrResult[$i]['relevance'] / $arrResult[0]['relevance'] * 100, 2) . '%'); $objTemplate->filesize = $arrResult[$i]['filesize']; $objTemplate->matches = $arrResult[$i]['matches']; $arrContext = array(); $arrMatches = trimsplit(',', $arrResult[$i]['matches']); // Get the context foreach ($arrMatches as $strWord) { $arrChunks = array(); preg_match_all('/\\b.{0,' . $this->contextLength . '}\\PL' . $strWord . '\\PL.{0,' . $this->contextLength . '}\\b/ui', $arrResult[$i]['text'], $arrChunks); foreach ($arrChunks[0] as $strContext) { $arrContext[] = ' ' . $strContext . ' '; } } // Shorten the context and highlight all keywords if (!empty($arrContext)) { $objTemplate->context = trim(\String::substrHtml(implode('…', $arrContext), $this->totalLength)); $objTemplate->context = preg_replace('/(\\PL)(' . implode('|', $arrMatches) . ')(\\PL)/ui', '$1<span class="highlight">$2</span>$3', $objTemplate->context); $objTemplate->hasContext = true; } $this->Template->results .= $objTemplate->parse(); } $this->Template->header = vsprintf($GLOBALS['TL_LANG']['MSC']['sResults'], array($from, $to, $count, $strKeywords)); $this->Template->duration = substr($query_endtime - $query_starttime, 0, 6) . ' ' . $GLOBALS['TL_LANG']['MSC']['seconds']; } }
protected function getArticles() { $offset = intval($this->skipFirst); $limit = null; // Maximum number of items if ($this->numberOfItems > 0) { $limit = $this->numberOfItems; } // Handle featured news if ($this->news_featured == 'featured') { $blnFeatured = true; } elseif ($this->news_featured == 'unfeatured') { $blnFeatured = false; } else { $blnFeatured = null; } $this->Template->articles = array(); $this->Template->empty = $GLOBALS['TL_LANG']['MSC']['emptyList']; $objSubmission = $this->objForm->getSubmission(false); if ($objSubmission === null) { $objSubmission = new \HeimrichHannot\FormHybrid\Submission(); } $objSubmission->search = trim($objSubmission->search); $objSubmission->search = preg_replace('/\\{\\{[^\\}]*\\}\\}/', '', $objSubmission->search); if ($objSubmission->from) { $from = strtotime($objSubmission->from); $objSubmission->from = mktime(0, 0, 0, date('m', $from), date('d', $from), date('Y', $from)); } if ($objSubmission->to) { $to = strtotime($objSubmission->to); $objSubmission->to = mktime(23, 59, 59, date('m', $to), date('d', $to), date('Y', $to)); } $arrAliases = array(); if ($objSubmission->search != '') { $objSearch = \Search::searchFor($objSubmission->search); $arrResults = $objSearch->fetchAllAssoc(); if (!empty($arrResults)) { foreach ($arrResults as $arrResult) { $arrAliases[] = str_replace(\Config::get('urlSuffix'), '', basename($arrResult['url'])); } } } // Get the total number of items $intTotal = NewsFilterModel::countPublishedByPidsAndSearch($this->news_archives, $objSubmission, $arrAliases, $blnFeatured); if ($intTotal < 1) { return; } $total = $intTotal - $offset; // Split the results if ($this->perPage > 0 && (!isset($limit) || $this->numberOfItems > $this->perPage)) { // Adjust the overall limit if (isset($limit)) { $total = min($limit, $total); } // Get the current page $id = 'page_n' . $this->id; $page = \Input::get($id) ?: 1; // Do not index or cache the page if the page number is outside the range if ($page < 1 || $page > max(ceil($total / $this->perPage), 1)) { global $objPage; $objPage->noSearch = 1; $objPage->cache = 0; // Send a 404 header header('HTTP/1.1 404 Not Found'); return; } // Set limit and offset $limit = $this->perPage; $offset += (max($page, 1) - 1) * $this->perPage; $skip = intval($this->skipFirst); // Overall limit if ($offset + $limit > $total + $skip) { $limit = $total + $skip - $offset; } // Add the pagination menu $objPagination = new \Pagination($total, $this->perPage, \Config::get('maxPaginationLinks'), $id); $this->Template->pagination = $objPagination->generate("\n "); } // Get the items if (isset($limit)) { $objArticles = NewsFilterModel::findPublishedByPidsAndSearch($this->news_archives, $objSubmission, $arrAliases, $blnFeatured, $limit, $offset); } else { $objArticles = NewsFilterModel::findPublishedByPidsAndSearch($this->news_archives, $objSubmission, $arrAliases, $blnFeatured, 0, $offset); } // Add the articles if ($objArticles !== null) { $this->Template->articles = $this->parseArticles($objArticles); } $this->Template->archives = $this->news_archives; }
protected function findNewsInSearchIndex() { $arrIds = array(); // Reference page if ($this->rootPage > 0) { $arrPages = $this->Database->getChildRecords($this->rootPage, 'tl_page'); array_unshift($arrPages, $this->rootPage); } else { global $objPage; $arrPages = $this->Database->getChildRecords($objPage->rootId, 'tl_page'); } try { $objSearch = \Search::searchFor($this->strKeywords, $this->objFilter->news_filterSearchQueryType === false, $arrPages, 0, 0, $this->objFilter->news_filterFuzzySearch); if ($objSearch->numRows > 0) { $arrUrls = array(); while ($objSearch->next()) { $arrUrls[] = $objSearch->url; } $strKeyWordColumns = ""; $n = 0; foreach ($arrUrls as $i => $strAlias) { $strKeyWordColumns .= ($n > 0 ? " OR " : "") . "{$this->t}.alias = ?"; $arrValues[] = basename($strAlias); $n++; } $arrColumns[] = "({$strKeyWordColumns})"; $objArticles = \HeimrichHannot\NewsPlus\NewsPlusModel::findBy($arrColumns, $arrValues); if ($objArticles !== null) { $arrIds = $objArticles->fetchEach('id'); } return $arrIds; } else { return $arrIds; } } catch (\Exception $e) { $this->log('Website search failed: ' . $e->getMessage(), __METHOD__, TL_ERROR); return $arrIds; } }