NewsNewsFeaturesDownloadsDevelopmentSupportForumDocumentsAbout Us

searchaction.class.php

查看本檔案說明文件.
00001 <?php
00002 
00008     include_once( PLOG_CLASS_PATH."class/action/blogaction.class.php" );
00009     include_once( PLOG_CLASS_PATH."class/dao/searchengine.class.php" );
00010     include_once( PLOG_CLASS_PATH."class/view/blogtemplatedview.class.php" );
00011     include_once( PLOG_CLASS_PATH."class/data/validator/stringvalidator.class.php" );
00012     include_once( PLOG_CLASS_PATH."class/net/http/httpvars.class.php" );
00013     include_once( PLOG_CLASS_PATH."class/view/errorview.class.php" );
00014 
00015     define( "VIEW_SEARCH_TEMPLATE", "searchresults" );
00016 
00017     class SearchAction extends BlogAction
00018     {
00019         var $_searchTerms;
00020     
00021         function SearchAction( $actionInfo, $request )
00022         {
00023             $this->BlogAction( $actionInfo, $request );
00024             
00025             // data validatdion
00026             $this->registerFieldValidator( "searchTerms", new StringValidator());
00027             $this->setValidationErrorView( new ErrorView( $this->_blogInfo, "error_incorrect_search_terms" ));
00028         }
00029         
00030         function perform()
00031         {
00032             // get the search terms that have already been validated...
00033             $this->_searchTerms = $this->_request->getValue( "searchTerms" );       
00034         
00035             // check if the search feature is disabled in this site...
00036             $config =& Config::getConfig();
00037             if( !$config->getValue( "search_engine_enabled" )) {
00038                 $this->_view = new ErrorView( $this->_blogInfo, "error_search_engine_disabled" );
00039                 $this->setCommonData();
00040                 
00041                 return false;
00042             }
00043             
00044             // create the view and make sure that it hasn't been cached
00045             $this->_view = new BlogTemplatedView( $this->_blogInfo, VIEW_SEARCH_TEMPLATE, Array( "searchTerms" => $this->_searchTerms ));
00046             if( $this->_view->isCached()) {
00047                 return true;
00048             }
00049             
00050             $searchEngine = new SearchEngine();
00051             $searchResults = $searchEngine->search( $this->_blogInfo->getId(), $this->_searchTerms );
00052             
00053             // MARKWU: I add the searchterms variable for smarty/plog template
00054             $searchTerms = $searchEngine->getAdaptSearchTerms( $this->_searchTerms );           
00055             
00056             // if no search results, return an error message
00057             if( count($searchResults) == 0 ) {
00058                 $this->_view = new ErrorView( $this->_blogInfo, "error_no_search_results" );
00059                 $this->setCommonData();
00060                 
00061                 return true;
00062             }
00063             
00064             // if only one search result, we can see it straight away 
00065             if( count($searchResults) == 1 ) {
00066                 // we have to refill the $_REQUEST array, since the next action
00067                 // is going to need some of the parameters
00068                 $request =& HttpVars::getRequest();
00069                 $searchResult = array_pop( $searchResults );
00070                 $article = $searchResult->getArticle();
00071                 $request["articleId"] = $article->getId();
00072                 $request["blogId"] = $this->_blogInfo->getId();
00073                 HttpVars::setRequest( $request );
00074                 
00075                 // since there is only one article, we can use the ViewArticleAction action
00076                 // to display that article, instead of copy-pasting the code and doing it here.
00077                 // You just have to love MVC and OOP :)
00078                 return Controller::setForwardAction( "ViewArticle" );
00079             }
00080             
00081             // or else, show a list with all the posts that match the requested
00082             // search terms
00083             $this->_view->setValue( "searchresults", $searchResults );
00084             // MARKWU: Now, I can use the searchterms to get the keyword
00085             $this->_view->setValue( "searchterms", $searchTerms );
00086             // MARKWU:
00087             $config =& Config::getConfig();
00088             $urlmode   = $config->getValue( "request_format_mode" );            
00089             $this->_view->setValue( "urlmode", $urlmode );
00090 
00091             $this->setCommonData();
00092             
00093             return true;
00094         }
00095     }
00096 ?>