NewsFeaturesDownloadsDevelopmentSupportAbout Us

lifetype-1.1.6/class/action/searchaction.class.php

Go to the documentation of this file.
00001 <?php
00002 
00006     include_once( PLOG_CLASS_PATH."class/action/blogaction.class.php" );
00007     include_once( PLOG_CLASS_PATH."class/dao/searchengine.class.php" );
00008         include_once( PLOG_CLASS_PATH."class/view/blogtemplatedview.class.php" );
00009     include_once( PLOG_CLASS_PATH."class/data/validator/stringvalidator.class.php" );
00010     include_once( PLOG_CLASS_PATH."class/data/textfilter.class.php" );
00011     include_once( PLOG_CLASS_PATH."class/net/http/httpvars.class.php" );
00012         include_once( PLOG_CLASS_PATH."class/view/errorview.class.php" );
00013         include_once( PLOG_CLASS_PATH."class/data/pager/pager.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                         $tf = new Textfilter();                 
00034             $this->_searchTerms = $tf->filterAllHTML( $this->_request->getValue( "searchTerms" ));
00035                 
00036                         // check if the search feature is disabled in this site...
00037                         $config =& Config::getConfig();
00038                         if( !$config->getValue( "search_engine_enabled" )) {
00039                                 $this->_view = new ErrorView( $this->_blogInfo, "error_search_engine_disabled" );
00040                                 $this->setCommonData();
00041                                 
00042                                 return false;
00043                         }
00044                         
00045                         // create the view and make sure that it hasn't been cached
00046             $this->_view = new BlogTemplatedView( $this->_blogInfo, VIEW_SEARCH_TEMPLATE, Array( "searchTerms" => $this->_searchTerms, "page" => $this->_page ));
00047                         if( $this->_view->isCached()) {
00048                                 return true;
00049                         }
00050                         
00051                         // calculate how many results per page
00052                         $blogSettings = $this->_blogInfo->getSettings();
00053             $itemsPerPage = $blogSettings->getValue( 'show_posts_max' );
00054                         
00055                         // get the array with the results
00056             $searchEngine = new SearchEngine();
00057             $searchResults = $searchEngine->search( $this->_blogInfo->getId(), 
00058                                                     $this->_searchTerms,
00059                                                                                                         POST_STATUS_PUBLISHED,
00060                                                                                                         false,
00061                                                                                                         $this->_page,             // page
00062                                                                                                         $itemsPerPage   // items per page
00063                                                                                                    );
00064                         // and the total number of items
00065                         $numSearchResults = $searchEngine->getNumSearchResults( $this->_blogInfo->getId(),
00066                                                                                 $this->_searchTerms,
00067                                                                                 POST_STATUS_PUBLISHED,
00068                                                                                 false );
00069                                                                                                                                                 
00070             // MARKWU: I add the searchterms variable for smarty/plog template
00071             $searchTerms = $searchEngine->getAdaptSearchTerms( $this->_searchTerms );                   
00072             
00073             // if no search results, return an error message
00074             if( count($searchResults) == 0 ) {
00075                 $this->_view = new ErrorView( $this->_blogInfo, "error_no_search_results" );
00076                 $this->setCommonData();
00077                 
00078                 return true;
00079             }
00080             
00081             // if only one search result, we can see it straight away 
00082             if( count($searchResults) == 1 && $numSearchResults == 1 ) {
00083                                 // only one search result, we can redirect the view via the URL,
00084                                 // so that the right permalink appears in the address bar
00085                 $request = HttpVars::getRequest();
00086                 $searchResult = array_pop( $searchResults );
00087                 $article = $searchResult->getResult();
00088                                 $url = $this->_blogInfo->getBlogRequestGenerator();
00089                                 // we need to deactivate the XHTML mode of the request generator or else
00090                                 // we'll get things escaped twice!
00091                                 $url->setXHTML( false );
00092                                 $permalink = $url->postPermalink( $article );
00093                                 
00094                                 // load the view and redirect the flow
00095                                 include_once( PLOG_CLASS_PATH."class/view/redirectview.class.php" );
00096                                 $this->_view = new RedirectView( $permalink );
00097                                 
00098                                 return( true );
00099             }
00100             
00101             // or else, show a list with all the posts that match the requested
00102             // search terms
00103             $this->_view->setValue( "searchresults", $searchResults );
00104             // MARKWU: Now, I can use the searchterms to get the keyword
00105             $this->_view->setValue( "searchterms", $searchTerms );
00106             // MARKWU:
00107                         $config =& Config::getConfig();
00108             $urlmode = $config->getValue( "request_format_mode" );                      
00109             $this->_view->setValue( "urlmode", $urlmode );
00110 
00111                         // build the pager
00112                 $url = $this->_blogInfo->getBlogRequestGenerator();
00113                         $basePageUrl = $url->getIndexUrl()."?op=Search&amp;searchTerms=".$this->_searchTerms."&amp;page=";
00114                         $pager = new Pager( $basePageUrl,            // url to the next page
00115                                     $this->_page,            // current page
00116                                     $numSearchResults,            // total number of search results
00117                                     $itemsPerPage );
00118         
00119                 $this->_view->setValue( 'pager', $pager );
00120 
00121             $this->setCommonData();
00122             
00123             return true;
00124         }
00125     }
00126 ?>