oxsearch.php

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