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         return $oArtList;
00071     }
00072 
00083     public function getSearchArticleCount( $sSearchParamForQuery = false, $sInitialSearchCat = false, $sInitialSearchVendor = false, $sInitialSearchManufacturer = false )
00084     {
00085         $iCnt = 0;
00086         $sSelect = $this->_getSearchSelect( $sSearchParamForQuery, $sInitialSearchCat, $sInitialSearchVendor, $sInitialSearchManufacturer, false );
00087         if ( $sSelect ) {
00088 
00089             $sPartial = substr( $sSelect, strpos( $sSelect, ' from ' ) );
00090             $sSelect  = "select count( ".getViewName( 'oxarticles' ).".oxid ) $sPartial ";
00091 
00092             $iCnt = oxDb::getDb()->getOne( $sSelect );
00093         }
00094         return $iCnt;
00095     }
00096 
00108     protected function _getSearchSelect( $sSearchParamForQuery = false, $sInitialSearchCat = false, $sInitialSearchVendor = false, $sInitialSearchManufacturer = false, $sSortBy = false)
00109     {
00110         $oDb = oxDb::getDb();
00111 
00112         // performance
00113         if ( $sInitialSearchCat ) {
00114             // lets search this category - is no such category - skip all other code
00115             $oCategory = oxNew( 'oxcategory' );
00116             $sCatTable = $oCategory->getViewName();
00117 
00118             $sQ  = "select 1 from $sCatTable where $sCatTable.oxid = ".$oDb->quote( $sInitialSearchCat )." ";
00119             $sQ .= "and ".$oCategory->getSqlActiveSnippet();
00120             if ( !$oDb->getOne( $sQ ) ) {
00121                 return;
00122             }
00123         }
00124 
00125         // performance:
00126         if ( $sInitialSearchVendor ) {
00127             // lets search this vendor - if no such vendor - skip all other code
00128             $oVendor   = oxNew( 'oxvendor' );
00129             $sVndTable = $oVendor->getViewName();
00130 
00131             $sQ  = "select 1 from $sVndTable where $sVndTable.oxid = ".$oDb->quote( $sInitialSearchVendor )." ";
00132             $sQ .= "and ".$oVendor->getSqlActiveSnippet();
00133             if ( !$oDb->getOne( $sQ ) ) {
00134                 return;
00135             }
00136         }
00137 
00138         // performance:
00139         if ( $sInitialSearchManufacturer ) {
00140             // lets search this Manufacturer - if no such Manufacturer - skip all other code
00141             $oManufacturer   = oxNew( 'oxmanufacturer' );
00142             $sManTable = $oManufacturer->getViewName();
00143 
00144             $sQ  = "select 1 from $sManTable where $sManTable.oxid = ".$oDb->quote( $sInitialSearchManufacturer )." ";
00145             $sQ .= "and ".$oManufacturer->getSqlActiveSnippet();
00146             if ( !$oDb->getOne( $sQ ) ) {
00147                 return;
00148             }
00149         }
00150 
00151         $sWhere = null;
00152 
00153         if ( $sSearchParamForQuery ) {
00154             $sWhere = $this->_getWhere( $sSearchParamForQuery );
00155         } elseif ( !$sInitialSearchCat && !$sInitialSearchVendor && !$sInitialSearchManufacturer ) {
00156             //no search string
00157             return null;
00158         }
00159 
00160         $oArticle = oxNew( 'oxarticle' );
00161         $sArticleTable = $oArticle->getViewName();
00162         $sO2CView      = getViewName( 'oxobject2category' );
00163 
00164         $sSelectFields = $oArticle->getSelectFields();
00165 
00166         // longdesc field now is kept on different table
00167         $sDescTable = '';
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                 $sDescTable = ", {$sDescView} ";
00173                 $sDescJoin  = " {$sDescView}.oxid={$sArticleTable}.oxid and ";
00174             }
00175         }
00176 
00177         //select articles
00178         $sSelect = "select {$sSelectFields} from {$sArticleTable} {$sDescTable} where {$sDescJoin} ";
00179 
00180         // must be additional conditions in select if searching in category
00181         if ( $sInitialSearchCat ) {
00182             $sCatView = getViewName( 'oxcategories' );
00183             $sSelectCat  = "select oxid from {$sCatView} where oxid = '{$sInitialSearchCat}' and (oxpricefrom != '0' or oxpriceto != 0)";
00184             if ( $oDb->getOne($sSelectCat) ) {
00185                 $sSelect = "select {$sSelectFields} from {$sArticleTable} {$sDescTable} " .
00186                            "where {$sArticleTable}.oxid in ( select {$sArticleTable}.oxid as id from {$sArticleTable}, {$sO2CView} as oxobject2category, {$sCatView} as oxcategories " .
00187                            "where (oxobject2category.oxcatnid='{$sInitialSearchCat}' and oxobject2category.oxobjectid={$sArticleTable}.oxid) or (oxcategories.oxid='{$sInitialSearchCat}' and {$sArticleTable}.oxprice >= oxcategories.oxpricefrom and  
00188                             {$sArticleTable}.oxprice <= oxcategories.oxpriceto )) and {$sDescJoin} ";
00189             } else {
00190                 $sSelect = "select {$sSelectFields} from {$sArticleTable}, {$sO2CView} as
00191                             oxobject2category {$sDescTable} where oxobject2category.oxcatnid='{$sInitialSearchCat}' and
00192                             oxobject2category.oxobjectid={$sArticleTable}.oxid and {$sDescJoin} ";
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 = '{$sInitialSearchVendor}' ";
00201         }
00202 
00203         if ( $sInitialSearchManufacturer ) {
00204             $sSelect .= " and {$sArticleTable}.oxmanufacturerid = '{$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' );
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                 $sLanguage = '';
00262                 if ( $this->_iLanguage && $oTempArticle->isMultilingualField( $sField ) ) {
00263                     $sLanguage = $oLang->getLanguageTag( $this->_iLanguage );
00264                 }
00265 
00266                 // as long description now is on different table table must differ
00267                 if ( $sField == 'oxlongdesc' || $sField == 'oxtags' ) {
00268                     $sSearchField = getViewName( 'oxartextends' ).".{$sField}{$sLanguage}";
00269                 } else {
00270                     $sSearchField = "{$sArticleTable}.{$sField}{$sLanguage}";
00271                 }
00272 
00273                 $sSearch .= " {$sSearchField} like ".$oDb->quote( "%$sSearchString%" );
00274 
00275                 // special chars ?
00276                 if ( ( $sUml = $myUtilsString->prepareStrForSearch( $sSearchString ) ) ) {
00277                     $sSearch  .= " or {$sSearchField} like ".$oDb->quote( "%$sUml%" );
00278                 }
00279 
00280                 $blSep2 = true;
00281             }
00282             $sSearch  .= ' ) ';
00283 
00284             $blSep = true;
00285         }
00286 
00287         $sSearch .= ' ) ';
00288 
00289         return $sSearch;
00290     }
00291 }
00292 

Generated on Wed May 13 13:25:51 2009 for OXID eShop CE by  doxygen 1.5.5