Exemplo n.º 1
0
 public function searchAction()
 {
     $this->_helper->layout->disableLayout();
     $this->_helper->viewRenderer->setNoRender(true);
     if ($this->getRequest()->isPost()) {
         $resultsHits = array();
         $searchTerm = filter_var($this->getRequest()->getParam('search'), FILTER_SANITIZE_STRING);
         //preparing searchTerm to fit multiple characters wildcard
         $searchTerm = trim($searchTerm, '*') . '*';
         $resultsPageId = filter_var($this->getRequest()->getParam('resultsPageId'), FILTER_VALIDATE_INT);
         $pageToRedirect = Application_Model_Mappers_PageMapper::getInstance()->find($resultsPageId);
         //            $searchIndexDirPath = $this->_helper->website->getPath() . 'cache/' . Widgets_Search_Search::INDEX_FOLDER;
         //attempt to create search index folder if not exists
         //            if(!is_dir($searchIndexDirPath)) {
         //                if(!Tools_Filesystem_Tools::mkDir($searchIndexDirPath)) {
         //                    $this->_helper->session->searchHits = 'System is unable to create search index directory. Please create it manually. The path is: ' . $searchIndexDirPath;
         //                    $this->redirect($this->_helper->website->getUrl() . $pageToRedirect->getUrl());
         //                }
         //            }
         //            $searchIndexFiles   = Tools_Filesystem_Tools::scanDirectory($searchIndexDirPath);
         //            if(empty($searchIndexFiles)) {
         //                Tools_Search_Tools::renewIndex(true);
         //            }
         $toasterSearchIndex = Tools_Search_Tools::initIndex();
         $toasterSearchIndex->setResultSetLimit(Widgets_Search_Search::SEARCH_LIMIT_RESULT * 10);
         try {
             $searchHits = $toasterSearchIndex->find($searchTerm);
         } catch (Exception $e) {
             $this->_helper->session->searchHits = $this->_helper->language->translate('Nothing found. You need at least 3 characters to start search.');
             $this->redirect($this->_helper->website->getUrl() . $pageToRedirect->getUrl());
         }
         if (is_array($searchHits) && !empty($searchHits)) {
             foreach ($searchHits as $hit) {
                 $resultsHits[] = array('pageId' => $hit->pageId, 'url' => $hit->url, 'h1' => $hit->h1, 'pageTeaser' => $hit->pageTeaser, 'navName' => $hit->navName, 'preview' => base64_decode($hit->pageImage));
             }
             $this->_helper->session->searchHits = $resultsHits;
         } else {
             $this->_helper->session->searchHits = '{$content:nothingfound}';
         }
         $this->redirect($this->_helper->website->getUrl() . $pageToRedirect->getUrl());
     }
 }
Exemplo n.º 2
0
 private function _searchResultsByTerm($searchTerm)
 {
     $searchForm = new Application_Form_Search();
     if ($searchForm->getElement('search')->isValid($searchTerm)) {
         $searchTerm = $searchForm->getElement('search')->getValue();
         $this->_view->pagerData = array('search' => $searchTerm);
         if (mb_strpos($searchTerm, ' ') === false) {
             $searchTerm = trim($searchTerm, '*') . '*';
         }
         if (null === ($searchResults = $this->_cache->load($searchTerm, strtolower(__CLASS__)))) {
             $toasterSearchIndex = Tools_Search_Tools::initIndex();
             $toasterSearchIndex->setResultSetLimit(self::SEARCH_LIMIT_RESULT * 10);
             $pattern = new Zend_Search_Lucene_Index_Term($searchTerm);
             $searchTerm = new Zend_Search_Lucene_Search_Query_Wildcard($pattern);
             $hits = $toasterSearchIndex->find($searchTerm);
             $cacheTags = array('search_' . $searchTerm);
             $searchResults = array_map(function ($hit) use(&$cacheTags) {
                 array_push($cacheTags, 'pageid_' . $hit->pageId);
                 try {
                     // checking if page is in drafts
                     $draft = (bool) $hit->draft;
                 } catch (Zend_Search_Lucene_Exception $e) {
                     // seems we are on old release
                     $draft = false;
                 }
                 if (!$draft) {
                     return array('pageId' => $hit->pageId, 'url' => $hit->url, 'h1' => $hit->h1, 'navName' => $hit->navName, 'teaserText' => $hit->teaserText);
                 }
             }, $hits);
             $searchResults = array_filter($searchResults);
             array_merge($this->_cacheTags, $cacheTags);
             $this->_cache->save($searchTerm, $searchResults, strtolower(__CLASS__), $this->_cacheTags, Helpers_Action_Cache::CACHE_LONG);
         }
         return $searchResults;
     } else {
         $msg = $searchForm->getElement('search')->getMessages();
         $error = $this->_translator->translate('Search error. ' . implode(PHP_EOL, $msg));
         throw new Exceptions_SeotoasterWidgetException($error);
     }
 }