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 = oxLang::getInstance()->getBaseLanguage();
00037 } else {
00038 $this->_iLanguage = $iLanguage;
00039 }
00040 }
00041
00052 public function getSearchArticles( $sSearchParamForQuery = false, $sInitialSearchCat = false, $sInitialSearchVendor = false, $sSortBy = false )
00053 {
00054
00055 $this->iActPage = (int) oxConfig::getParameter( 'pgNr' );
00056 $this->iActPage = ($this->iActPage < 0)?0:$this->iActPage;
00057
00058
00059
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, $sSortBy );
00067 if ( $sSelect ) {
00068 $oArtList->selectString( $sSelect );
00069 }
00070 return $oArtList;
00071 }
00072
00082 public function getSearchArticleCount( $sSearchParamForQuery = false, $sInitialSearchCat = false, $sInitialSearchVendor = false )
00083 {
00084 $iCnt = 0;
00085 $sSelect = $this->_getSearchSelect( $sSearchParamForQuery, $sInitialSearchCat, $sInitialSearchVendor, false );
00086 if ( $sSelect ) {
00087
00088 $sPartial = substr( $sSelect, strpos( $sSelect, ' from ' ) );
00089 $sSelect = "select count( ".getViewName( 'oxarticles' ).".oxid ) $sPartial ";
00090
00091 $iCnt = oxDb::getDb()->getOne( $sSelect );
00092 }
00093 return $iCnt;
00094 }
00095
00106 protected function _getSearchSelect( $sSearchParamForQuery = false, $sInitialSearchCat = false, $sInitialSearchVendor = false, $sSortBy = false)
00107 {
00108
00109 if ( $sInitialSearchCat ) {
00110
00111 $oCategory = oxNew( 'oxcategory' );
00112 $sCatTable = $oCategory->getViewName();
00113
00114 $sQ = "select 1 from $sCatTable where $sCatTable.oxid = ".oxDb::getDb()->quote( $sInitialSearchCat )." ";
00115 $sQ .= "and ".$oCategory->getSqlActiveSnippet();
00116 if ( !oxDb::getDb()->getOne( $sQ ) ) {
00117 return;
00118 }
00119 }
00120
00121
00122 if ( $sInitialSearchVendor ) {
00123
00124 $oVendor = oxNew( 'oxvendor' );
00125 $sVndTable = $oVendor->getViewName();
00126
00127 $sQ = "select 1 from $sVndTable where $sVndTable.oxid = ".oxDb::getDb()->quote( $sInitialSearchVendor )." ";
00128 $sQ .= "and ".$oVendor->getSqlActiveSnippet();
00129 if ( !oxDb::getDb()->getOne( $sQ ) ) {
00130 return;
00131 }
00132 }
00133
00134 $sWhere = null;
00135
00136 if ( $sSearchParamForQuery ) {
00137 $sWhere = $this->_getWhere( $sSearchParamForQuery );
00138 } elseif ( !$sInitialSearchCat && !$sInitialSearchVendor ) {
00139
00140 return null;
00141 }
00142
00143 $oArticle = oxNew( 'oxarticle' );
00144 $sArticleTable = $oArticle->getViewName();
00145 $sO2CView = getViewName( 'oxobject2category' );
00146
00147 $sSelectFields = $oArticle->getSelectFields();
00148
00149
00150 $sDescTable = '';
00151 $sDescJoin = '';
00152 if ( is_array( $aSearchCols = oxConfig::getInstance()->getConfigParam( 'aSearchCols' ) ) ) {
00153 if ( in_array( 'oxlongdesc', $aSearchCols ) || in_array( 'oxtags', $aSearchCols ) ) {
00154 $sDescView = getViewName( 'oxartextends' );
00155 $sDescTable = ", {$sDescView} ";
00156 $sDescJoin = " {$sDescView}.oxid={$sArticleTable}.oxid and ";
00157 }
00158 }
00159
00160
00161 $sSelect = "select {$sSelectFields} from {$sArticleTable} {$sDescTable} where {$sDescJoin} ";
00162
00163
00164 if ( $sInitialSearchCat ) {
00165 $sSelect = "select {$sSelectFields} from {$sArticleTable}, {$sO2CView} as
00166 oxobject2category {$sDescTable} where oxobject2category.oxcatnid='{$sInitialSearchCat}' and
00167 oxobject2category.oxobjectid={$sArticleTable}.oxid and {$sDescJoin} ";
00168 }
00169
00170 $sSelect .= $oArticle->getSqlActiveSnippet();
00171 $sSelect .= " and {$sArticleTable}.oxparentid = '' and {$sArticleTable}.oxissearch = 1 ";
00172
00173 if ( $sInitialSearchVendor ) {
00174 $sSelect .= " and {$sArticleTable}.oxvendorid = '{$sInitialSearchVendor}' ";
00175 }
00176
00177 $sSelect .= $sWhere;
00178
00179 if ( $sSortBy ) {
00180 $sSelect .= " order by {$sSortBy} ";
00181 }
00182
00183 return $sSelect;
00184 }
00185
00193 protected function _getWhere( $sSearchString )
00194 {
00195 $myConfig = $this->getConfig();
00196 $blSep = false;
00197 $sArticleTable = getViewName( 'oxarticles' );
00198
00199 $aSearchCols = $myConfig->getConfigParam( 'aSearchCols' );
00200 if ( !(is_array( $aSearchCols ) && count( $aSearchCols ) ) ) {
00201 return '';
00202 }
00203
00204 $oTempArticle = oxNew( 'oxarticle' );
00205 $sSearchSep = $myConfig->getConfigParam( 'blSearchUseAND' )?'and ':'or ';
00206 $aSearch = explode( ' ', $sSearchString );
00207 $sSearch = ' and ( ';
00208
00209 foreach ( $aSearch as $sSearchString ) {
00210
00211 if ( !strlen( $sSearchString ) ) {
00212 continue;
00213 }
00214
00215 if ( $blSep ) {
00216 $sSearch .= $sSearchSep;
00217 }
00218
00219 $blSep2 = false;
00220 $sSearch .= '( ';
00221
00222 foreach ( $aSearchCols as $sField ) {
00223
00224 if ( $blSep2 ) {
00225 $sSearch .= ' or ';
00226 }
00227
00228 $sLanguage = '';
00229 if ( $this->_iLanguage && $oTempArticle->isMultilingualField( $sField ) ) {
00230 $sLanguage = oxLang::getInstance()->getLanguageTag( $this->_iLanguage );
00231 }
00232
00233
00234 if ( $sField == 'oxlongdesc' || $sField == 'oxtags' ) {
00235 $sSearchField = getViewName( 'oxartextends' ).".{$sField}{$sLanguage}";
00236 } else {
00237 $sSearchField = "{$sArticleTable}.{$sField}{$sLanguage}";
00238 }
00239
00240 $sSearch .= " {$sSearchField} like ".oxDb::getDb()->quote( "%$sSearchString%" );
00241
00242
00243 if ( ( $sUml = oxUtilsString::getInstance()->prepareStrForSearch( $sSearchString ) ) ) {
00244 $sSearch .= " or {$sSearchField} like ".oxDb::getDb()->quote( "%$sUml%" );
00245 }
00246
00247 $blSep2 = true;
00248 }
00249 $sSearch .= ' ) ';
00250
00251 $blSep = true;
00252 }
00253
00254 $sSearch .= ' ) ';
00255
00256 return $sSearch;
00257 }
00258 }
00259