oxsearch.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class oxSearch extends oxSuperCfg
00008 {
00014     protected $_iLanguage = 0;
00015 
00021     public function __construct()
00022     {
00023         $this->setLanguage();
00024     }
00025 
00033     public function setLanguage( $iLanguage = null )
00034     {
00035         if ( !isset( $iLanguage ) ) {
00036             $this->_iLanguage = oxRegistry::getLang()->getBaseLanguage();
00037         } else {
00038             $this->_iLanguage = $iLanguage;
00039         }
00040     }
00041 
00053     public function getSearchArticles( $sSearchParamForQuery = false, $sInitialSearchCat = false, $sInitialSearchVendor = false, $sInitialSearchManufacturer = false, $sSortBy = false )
00054     {
00055         // sets active page
00056         $this->iActPage = (int) oxConfig::getParameter( 'pgNr' );
00057         $this->iActPage = ($this->iActPage < 0)?0:$this->iActPage;
00058 
00059         // load only articles which we show on screen
00060         //setting default values to avoid possible errors showing article list
00061         $iNrofCatArticles = $this->getConfig()->getConfigParam( 'iNrofCatArticles' );
00062         $iNrofCatArticles = $iNrofCatArticles?$iNrofCatArticles:10;
00063 
00064         $oArtList = oxNew( 'oxarticlelist' );
00065         $oArtList->setSqlLimit( $iNrofCatArticles * $this->iActPage, $iNrofCatArticles );
00066 
00067         $sSelect = $this->_getSearchSelect( $sSearchParamForQuery, $sInitialSearchCat, $sInitialSearchVendor, $sInitialSearchManufacturer, $sSortBy );
00068         if ( $sSelect ) {
00069             $oArtList->selectString( $sSelect );
00070         }
00071 
00072         return $oArtList;
00073     }
00074 
00085     public function getSearchArticleCount( $sSearchParamForQuery = false, $sInitialSearchCat = false, $sInitialSearchVendor = false, $sInitialSearchManufacturer = false )
00086     {
00087         $iCnt = 0;
00088         $sSelect = $this->_getSearchSelect( $sSearchParamForQuery, $sInitialSearchCat, $sInitialSearchVendor, $sInitialSearchManufacturer, false );
00089         if ( $sSelect ) {
00090 
00091             $sPartial = substr( $sSelect, strpos( $sSelect, ' from ' ) );
00092             $sSelect  = "select count( ".getViewName( 'oxarticles', $this->_iLanguage ).".oxid ) $sPartial ";
00093 
00094             $iCnt = oxDb::getDb()->getOne( $sSelect );
00095         }
00096         return $iCnt;
00097     }
00098 
00110     protected function _getSearchSelect( $sSearchParamForQuery = false, $sInitialSearchCat = false, $sInitialSearchVendor = false, $sInitialSearchManufacturer = false, $sSortBy = false)
00111     {
00112         $oDb = oxDb::getDb();
00113 
00114         // performance
00115         if ( $sInitialSearchCat ) {
00116             // lets search this category - is no such category - skip all other code
00117             $oCategory = oxNew( 'oxcategory' );
00118             $sCatTable = $oCategory->getViewName();
00119 
00120             $sQ  = "select 1 from $sCatTable where $sCatTable.oxid = ".$oDb->quote( $sInitialSearchCat )." ";
00121             $sQ .= "and ".$oCategory->getSqlActiveSnippet();
00122             if ( !$oDb->getOne( $sQ ) ) {
00123                 return;
00124             }
00125         }
00126 
00127         // performance:
00128         if ( $sInitialSearchVendor ) {
00129             // lets search this vendor - if no such vendor - skip all other code
00130             $oVendor   = oxNew( 'oxvendor' );
00131             $sVndTable = $oVendor->getViewName();
00132 
00133             $sQ  = "select 1 from $sVndTable where $sVndTable.oxid = ".$oDb->quote( $sInitialSearchVendor )." ";
00134             $sQ .= "and ".$oVendor->getSqlActiveSnippet();
00135             if ( !$oDb->getOne( $sQ ) ) {
00136                 return;
00137             }
00138         }
00139 
00140         // performance:
00141         if ( $sInitialSearchManufacturer ) {
00142             // lets search this Manufacturer - if no such Manufacturer - skip all other code
00143             $oManufacturer   = oxNew( 'oxmanufacturer' );
00144             $sManTable = $oManufacturer->getViewName();
00145 
00146             $sQ  = "select 1 from $sManTable where $sManTable.oxid = ".$oDb->quote( $sInitialSearchManufacturer )." ";
00147             $sQ .= "and ".$oManufacturer->getSqlActiveSnippet();
00148             if ( !$oDb->getOne( $sQ ) ) {
00149                 return;
00150             }
00151         }
00152 
00153         $sWhere = null;
00154 
00155         if ( $sSearchParamForQuery ) {
00156             $sWhere = $this->_getWhere( $sSearchParamForQuery );
00157         } elseif ( !$sInitialSearchCat && !$sInitialSearchVendor && !$sInitialSearchManufacturer ) {
00158             //no search string
00159             return null;
00160         }
00161 
00162         $oArticle = oxNew( 'oxarticle' );
00163         $sArticleTable = $oArticle->getViewName();
00164         $sO2CView      = getViewName( 'oxobject2category' );
00165 
00166         $sSelectFields = $oArticle->getSelectFields();
00167 
00168         // longdesc field now is kept on different table
00169         $sDescJoin  = '';
00170         if ( is_array( $aSearchCols = $this->getConfig()->getConfigParam( 'aSearchCols' ) ) ) {
00171             if ( in_array( 'oxlongdesc', $aSearchCols ) || in_array( 'oxtags', $aSearchCols ) ) {
00172                 $sDescView  = getViewName( 'oxartextends', $this->_iLanguage );
00173                 $sDescJoin  = " LEFT JOIN {$sDescView} ON {$sArticleTable}.oxid={$sDescView}.oxid ";
00174             }
00175         }
00176 
00177         //select articles
00178         $sSelect = "select {$sSelectFields}, {$sArticleTable}.oxtimestamp from {$sArticleTable} {$sDescJoin} where ";
00179 
00180         // must be additional conditions in select if searching in category
00181         if ( $sInitialSearchCat ) {
00182             $sCatView = getViewName( 'oxcategories', $this->_iLanguage );
00183             $sInitialSearchCatQuoted = $oDb->quote( $sInitialSearchCat );
00184             $sSelectCat  = "select oxid from {$sCatView} where oxid = $sInitialSearchCatQuoted and (oxpricefrom != '0' or oxpriceto != 0)";
00185             if ( $oDb->getOne($sSelectCat) ) {
00186                 $sSelect = "select {$sSelectFields}, {$sArticleTable}.oxtimestamp from {$sArticleTable} $sDescJoin " .
00187                            "where {$sArticleTable}.oxid in ( select {$sArticleTable}.oxid as id from {$sArticleTable}, {$sO2CView} as oxobject2category, {$sCatView} as oxcategories " .
00188                            "where (oxobject2category.oxcatnid=$sInitialSearchCatQuoted and oxobject2category.oxobjectid={$sArticleTable}.oxid) or (oxcategories.oxid=$sInitialSearchCatQuoted and {$sArticleTable}.oxprice >= oxcategories.oxpricefrom and
00189                             {$sArticleTable}.oxprice <= oxcategories.oxpriceto )) and ";
00190             } else {
00191                 $sSelect = "select {$sSelectFields} from {$sO2CView} as
00192                             oxobject2category, {$sArticleTable} {$sDescJoin} where oxobject2category.oxcatnid=$sInitialSearchCatQuoted and
00193                             oxobject2category.oxobjectid={$sArticleTable}.oxid and ";
00194             }
00195         }
00196 
00197         $sSelect .= $oArticle->getSqlActiveSnippet();
00198         $sSelect .= " and {$sArticleTable}.oxparentid = '' and {$sArticleTable}.oxissearch = 1 ";
00199 
00200         if ( $sInitialSearchVendor ) {
00201             $sSelect .= " and {$sArticleTable}.oxvendorid = " . $oDb->quote( $sInitialSearchVendor ) . " ";
00202         }
00203 
00204         if ( $sInitialSearchManufacturer ) {
00205             $sSelect .= " and {$sArticleTable}.oxmanufacturerid = " . $oDb->quote( $sInitialSearchManufacturer ) . " ";
00206         }
00207 
00208         $sSelect .= $sWhere;
00209 
00210         if ( $sSortBy ) {
00211             $sSelect .= " order by {$sSortBy} ";
00212         }
00213 
00214         return $sSelect;
00215     }
00216 
00224     protected function _getWhere( $sSearchString )
00225     {
00226         $oDb = oxDb::getDb();
00227         $myConfig = $this->getConfig();
00228         $blSep    = false;
00229         $sArticleTable = getViewName( 'oxarticles', $this->_iLanguage );
00230 
00231         $aSearchCols = $myConfig->getConfigParam( 'aSearchCols' );
00232         if ( !(is_array( $aSearchCols ) && count( $aSearchCols ) ) ) {
00233             return '';
00234         }
00235 
00236         $oTempArticle = oxNew( 'oxarticle' );
00237         $sSearchSep   = $myConfig->getConfigParam( 'blSearchUseAND' )?'and ':'or ';
00238         $aSearch  = explode( ' ', $sSearchString );
00239         $sSearch  = ' and ( ';
00240         $myUtilsString = oxRegistry::get("oxUtilsString");
00241         $oLang = oxRegistry::getLang();
00242 
00243         foreach ( $aSearch as $sSearchString ) {
00244 
00245             if ( !strlen( $sSearchString ) ) {
00246                 continue;
00247             }
00248 
00249             if ( $blSep ) {
00250                 $sSearch .= $sSearchSep;
00251             }
00252 
00253             $blSep2 = false;
00254             $sSearch  .= '( ';
00255 
00256             foreach ( $aSearchCols as $sField ) {
00257 
00258                 if ( $blSep2 ) {
00259                     $sSearch  .= ' or ';
00260                 }
00261 
00262                 // as long description now is on different table table must differ
00263                 if ( $sField == 'oxlongdesc' || $sField == 'oxtags' ) {
00264                     $sSearchField = getViewName( 'oxartextends', $this->_iLanguage ).".{$sField}";
00265                 } else {
00266                     $sSearchField = "{$sArticleTable}.{$sField}";
00267                 }
00268 
00269                 $sSearch .= " {$sSearchField} like ".$oDb->quote( "%$sSearchString%" );
00270 
00271                 // special chars ?
00272                 if ( ( $sUml = $myUtilsString->prepareStrForSearch( $sSearchString ) ) ) {
00273                     $sSearch  .= " or {$sSearchField} like ".$oDb->quote( "%$sUml%" );
00274                 }
00275 
00276                 $blSep2 = true;
00277             }
00278             $sSearch  .= ' ) ';
00279 
00280             $blSep = true;
00281         }
00282 
00283         $sSearch .= ' ) ';
00284 
00285         return $sSearch;
00286     }
00287 }
00288