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' ).".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' );
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' );
00182             $sSelectCat  = "select oxid from {$sCatView} where oxid = '{$sInitialSearchCat}' and (oxpricefrom != '0' or oxpriceto != 0)";
00183             if ( $oDb->getOne($sSelectCat) ) {
00184                 $sSelect = "select {$sSelectFields} from {$sArticleTable} $sDescJoin " .
00185                            "where {$sArticleTable}.oxid in ( select {$sArticleTable}.oxid as id from {$sArticleTable}, {$sO2CView} as oxobject2category, {$sCatView} as oxcategories " .
00186                            "where (oxobject2category.oxcatnid='{$sInitialSearchCat}' and oxobject2category.oxobjectid={$sArticleTable}.oxid) or (oxcategories.oxid='{$sInitialSearchCat}' and {$sArticleTable}.oxprice >= oxcategories.oxpricefrom and
00187                             {$sArticleTable}.oxprice <= oxcategories.oxpriceto )) and ";
00188             } else {
00189                 $sSelect = "select {$sSelectFields} from {$sO2CView} as
00190                             oxobject2category, {$sArticleTable} {$sDescJoin} where oxobject2category.oxcatnid='{$sInitialSearchCat}' and
00191                             oxobject2category.oxobjectid={$sArticleTable}.oxid and ";
00192             }
00193         }
00194 
00195         $sSelect .= $oArticle->getSqlActiveSnippet();
00196         $sSelect .= " and {$sArticleTable}.oxparentid = '' and {$sArticleTable}.oxissearch = 1 ";
00197 
00198         if ( $sInitialSearchVendor ) {
00199             $sSelect .= " and {$sArticleTable}.oxvendorid = '{$sInitialSearchVendor}' ";
00200         }
00201 
00202         if ( $sInitialSearchManufacturer ) {
00203             $sSelect .= " and {$sArticleTable}.oxmanufacturerid = '{$sInitialSearchManufacturer}' ";
00204         }
00205 
00206         $sSelect .= $sWhere;
00207 
00208         if ( $sSortBy ) {
00209             $sSelect .= " order by {$sSortBy} ";
00210         }
00211 
00212         return $sSelect;
00213     }
00214 
00222     protected function _getWhere( $sSearchString )
00223     {
00224         $oDb = oxDb::getDb();
00225         $myConfig = $this->getConfig();
00226         $blSep    = false;
00227         $sArticleTable = getViewName( 'oxarticles' );
00228 
00229         $aSearchCols = $myConfig->getConfigParam( 'aSearchCols' );
00230         if ( !(is_array( $aSearchCols ) && count( $aSearchCols ) ) ) {
00231             return '';
00232         }
00233 
00234         $oTempArticle = oxNew( 'oxarticle' );
00235         $sSearchSep   = $myConfig->getConfigParam( 'blSearchUseAND' )?'and ':'or ';
00236         $aSearch  = explode( ' ', $sSearchString );
00237         $sSearch  = ' and ( ';
00238         $myUtilsString = oxUtilsString::getInstance();
00239         $oLang = oxLang::getInstance();
00240 
00241         foreach ( $aSearch as $sSearchString ) {
00242 
00243             if ( !strlen( $sSearchString ) ) {
00244                 continue;
00245             }
00246 
00247             if ( $blSep ) {
00248                 $sSearch .= $sSearchSep;
00249             }
00250 
00251             $blSep2 = false;
00252             $sSearch  .= '( ';
00253 
00254             foreach ( $aSearchCols as $sField ) {
00255 
00256                 if ( $blSep2 ) {
00257                     $sSearch  .= ' or ';
00258                 }
00259 
00260                 $sLanguage = '';
00261                 if ( $this->_iLanguage && $oTempArticle->isMultilingualField( $sField ) ) {
00262                     $sLanguage = $oLang->getLanguageTag( $this->_iLanguage );
00263                 }
00264 
00265                 // as long description now is on different table table must differ
00266                 if ( $sField == 'oxlongdesc' || $sField == 'oxtags' ) {
00267                     $sSearchField = getViewName( 'oxartextends' ).".{$sField}{$sLanguage}";
00268                 } else {
00269                     $sSearchField = "{$sArticleTable}.{$sField}{$sLanguage}";
00270                 }
00271 
00272                 $sSearch .= " {$sSearchField} like ".$oDb->quote( "%$sSearchString%" );
00273 
00274                 // special chars ?
00275                 if ( ( $sUml = $myUtilsString->prepareStrForSearch( $sSearchString ) ) ) {
00276                     $sSearch  .= " or {$sSearchField} like ".$oDb->quote( "%$sUml%" );
00277                 }
00278 
00279                 $blSep2 = true;
00280             }
00281             $sSearch  .= ' ) ';
00282 
00283             $blSep = true;
00284         }
00285 
00286         $sSearch .= ' ) ';
00287 
00288         return $sSearch;
00289     }
00290 }
00291 

Generated on Tue Aug 18 09:21:06 2009 for OXID eShop CE by  doxygen 1.5.5