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

Generated on Wed Apr 22 12:26:31 2009 for OXID eShop CE by  doxygen 1.5.5