OXID eShop CE  4.8.12
 All Classes Files Functions Variables Pages
oxsearch.php
Go to the documentation of this file.
1 <?php
2 
7 class oxSearch extends oxSuperCfg
8 {
14  protected $_iLanguage = 0;
15 
21  public function __construct()
22  {
23  $this->setLanguage();
24  }
25 
33  public function setLanguage( $iLanguage = null )
34  {
35  if ( !isset( $iLanguage ) ) {
36  $this->_iLanguage = oxRegistry::getLang()->getBaseLanguage();
37  } else {
38  $this->_iLanguage = $iLanguage;
39  }
40  }
41 
53  public function getSearchArticles( $sSearchParamForQuery = false, $sInitialSearchCat = false, $sInitialSearchVendor = false, $sInitialSearchManufacturer = false, $sSortBy = false )
54  {
55  // sets active page
56  $this->iActPage = (int) oxConfig::getParameter( 'pgNr' );
57  $this->iActPage = ($this->iActPage < 0)?0:$this->iActPage;
58 
59  // load only articles which we show on screen
60  //setting default values to avoid possible errors showing article list
61  $iNrofCatArticles = $this->getConfig()->getConfigParam( 'iNrofCatArticles' );
62  $iNrofCatArticles = $iNrofCatArticles?$iNrofCatArticles:10;
63 
64  $oArtList = oxNew( 'oxarticlelist' );
65  $oArtList->setSqlLimit( $iNrofCatArticles * $this->iActPage, $iNrofCatArticles );
66 
67  $sSelect = $this->_getSearchSelect( $sSearchParamForQuery, $sInitialSearchCat, $sInitialSearchVendor, $sInitialSearchManufacturer, $sSortBy );
68  if ( $sSelect ) {
69  $oArtList->selectString( $sSelect );
70  }
71 
72  return $oArtList;
73  }
74 
85  public function getSearchArticleCount( $sSearchParamForQuery = false, $sInitialSearchCat = false, $sInitialSearchVendor = false, $sInitialSearchManufacturer = false )
86  {
87  $iCnt = 0;
88  $sSelect = $this->_getSearchSelect( $sSearchParamForQuery, $sInitialSearchCat, $sInitialSearchVendor, $sInitialSearchManufacturer, false );
89  if ( $sSelect ) {
90 
91  $sPartial = substr( $sSelect, strpos( $sSelect, ' from ' ) );
92  $sSelect = "select count( ".getViewName( 'oxarticles', $this->_iLanguage ).".oxid ) $sPartial ";
93 
94  $iCnt = oxDb::getDb()->getOne( $sSelect );
95  }
96  return $iCnt;
97  }
98 
110  protected function _getSearchSelect( $sSearchParamForQuery = false, $sInitialSearchCat = false, $sInitialSearchVendor = false, $sInitialSearchManufacturer = false, $sSortBy = false)
111  {
112  $oDb = oxDb::getDb();
113 
114  // performance
115  if ( $sInitialSearchCat ) {
116  // lets search this category - is no such category - skip all other code
117  $oCategory = oxNew( 'oxcategory' );
118  $sCatTable = $oCategory->getViewName();
119 
120  $sQ = "select 1 from $sCatTable where $sCatTable.oxid = ".$oDb->quote( $sInitialSearchCat )." ";
121  $sQ .= "and ".$oCategory->getSqlActiveSnippet();
122  if ( !$oDb->getOne( $sQ ) ) {
123  return;
124  }
125  }
126 
127  // performance:
128  if ( $sInitialSearchVendor ) {
129  // lets search this vendor - if no such vendor - skip all other code
130  $oVendor = oxNew( 'oxvendor' );
131  $sVndTable = $oVendor->getViewName();
132 
133  $sQ = "select 1 from $sVndTable where $sVndTable.oxid = ".$oDb->quote( $sInitialSearchVendor )." ";
134  $sQ .= "and ".$oVendor->getSqlActiveSnippet();
135  if ( !$oDb->getOne( $sQ ) ) {
136  return;
137  }
138  }
139 
140  // performance:
141  if ( $sInitialSearchManufacturer ) {
142  // lets search this Manufacturer - if no such Manufacturer - skip all other code
143  $oManufacturer = oxNew( 'oxmanufacturer' );
144  $sManTable = $oManufacturer->getViewName();
145 
146  $sQ = "select 1 from $sManTable where $sManTable.oxid = ".$oDb->quote( $sInitialSearchManufacturer )." ";
147  $sQ .= "and ".$oManufacturer->getSqlActiveSnippet();
148  if ( !$oDb->getOne( $sQ ) ) {
149  return;
150  }
151  }
152 
153  $sWhere = null;
154 
155  if ( $sSearchParamForQuery ) {
156  $sWhere = $this->_getWhere( $sSearchParamForQuery );
157  } elseif ( !$sInitialSearchCat && !$sInitialSearchVendor && !$sInitialSearchManufacturer ) {
158  //no search string
159  return null;
160  }
161 
162  $oArticle = oxNew( 'oxarticle' );
163  $sArticleTable = $oArticle->getViewName();
164  $sO2CView = getViewName( 'oxobject2category' );
165 
166  $sSelectFields = $oArticle->getSelectFields();
167 
168  // longdesc field now is kept on different table
169  $sDescJoin = '';
170  if ( is_array( $aSearchCols = $this->getConfig()->getConfigParam( 'aSearchCols' ) ) ) {
171  if ( in_array( 'oxlongdesc', $aSearchCols ) || in_array( 'oxtags', $aSearchCols ) ) {
172  $sDescView = getViewName( 'oxartextends', $this->_iLanguage );
173  $sDescJoin = " LEFT JOIN {$sDescView} ON {$sArticleTable}.oxid={$sDescView}.oxid ";
174  }
175  }
176 
177  //select articles
178  $sSelect = "select {$sSelectFields}, {$sArticleTable}.oxtimestamp from {$sArticleTable} {$sDescJoin} where ";
179 
180  // must be additional conditions in select if searching in category
181  if ( $sInitialSearchCat ) {
182  $sCatView = getViewName( 'oxcategories', $this->_iLanguage );
183  $sInitialSearchCatQuoted = $oDb->quote( $sInitialSearchCat );
184  $sSelectCat = "select oxid from {$sCatView} where oxid = $sInitialSearchCatQuoted and (oxpricefrom != '0' or oxpriceto != 0)";
185  if ( $oDb->getOne($sSelectCat) ) {
186  $sSelect = "select {$sSelectFields}, {$sArticleTable}.oxtimestamp from {$sArticleTable} $sDescJoin " .
187  "where {$sArticleTable}.oxid in ( select {$sArticleTable}.oxid as id from {$sArticleTable}, {$sO2CView} as oxobject2category, {$sCatView} as oxcategories " .
188  "where (oxobject2category.oxcatnid=$sInitialSearchCatQuoted and oxobject2category.oxobjectid={$sArticleTable}.oxid) or (oxcategories.oxid=$sInitialSearchCatQuoted and {$sArticleTable}.oxprice >= oxcategories.oxpricefrom and
189  {$sArticleTable}.oxprice <= oxcategories.oxpriceto )) and ";
190  } else {
191  $sSelect = "select {$sSelectFields} from {$sO2CView} as
192  oxobject2category, {$sArticleTable} {$sDescJoin} where oxobject2category.oxcatnid=$sInitialSearchCatQuoted and
193  oxobject2category.oxobjectid={$sArticleTable}.oxid and ";
194  }
195  }
196 
197  $sSelect .= $oArticle->getSqlActiveSnippet();
198  $sSelect .= " and {$sArticleTable}.oxparentid = '' and {$sArticleTable}.oxissearch = 1 ";
199 
200  if ( $sInitialSearchVendor ) {
201  $sSelect .= " and {$sArticleTable}.oxvendorid = " . $oDb->quote( $sInitialSearchVendor ) . " ";
202  }
203 
204  if ( $sInitialSearchManufacturer ) {
205  $sSelect .= " and {$sArticleTable}.oxmanufacturerid = " . $oDb->quote( $sInitialSearchManufacturer ) . " ";
206  }
207 
208  $sSelect .= $sWhere;
209 
210  if ( $sSortBy ) {
211  $sSelect .= " order by {$sSortBy} ";
212  }
213 
214  return $sSelect;
215  }
216 
224  protected function _getWhere( $sSearchString )
225  {
226  $oDb = oxDb::getDb();
227  $myConfig = $this->getConfig();
228  $blSep = false;
229  $sArticleTable = getViewName( 'oxarticles', $this->_iLanguage );
230 
231  $aSearchCols = $myConfig->getConfigParam( 'aSearchCols' );
232  if ( !(is_array( $aSearchCols ) && count( $aSearchCols ) ) ) {
233  return '';
234  }
235 
236  $oTempArticle = oxNew( 'oxarticle' );
237  $sSearchSep = $myConfig->getConfigParam( 'blSearchUseAND' )?'and ':'or ';
238  $aSearch = explode( ' ', $sSearchString );
239  $sSearch = ' and ( ';
240  $myUtilsString = oxRegistry::get("oxUtilsString");
241  $oLang = oxRegistry::getLang();
242 
243  foreach ( $aSearch as $sSearchString ) {
244 
245  if ( !strlen( $sSearchString ) ) {
246  continue;
247  }
248 
249  if ( $blSep ) {
250  $sSearch .= $sSearchSep;
251  }
252 
253  $blSep2 = false;
254  $sSearch .= '( ';
255 
256  foreach ( $aSearchCols as $sField ) {
257 
258  if ( $blSep2 ) {
259  $sSearch .= ' or ';
260  }
261 
262  // as long description now is on different table table must differ
263  if ( $sField == 'oxlongdesc' || $sField == 'oxtags' ) {
264  $sSearchField = getViewName( 'oxartextends', $this->_iLanguage ).".{$sField}";
265  } else {
266  $sSearchField = "{$sArticleTable}.{$sField}";
267  }
268 
269  $sSearch .= " {$sSearchField} like ".$oDb->quote( "%$sSearchString%" );
270 
271  // special chars ?
272  if ( ( $sUml = $myUtilsString->prepareStrForSearch( $sSearchString ) ) ) {
273  $sSearch .= " or {$sSearchField} like ".$oDb->quote( "%$sUml%" );
274  }
275 
276  $blSep2 = true;
277  }
278  $sSearch .= ' ) ';
279 
280  $blSep = true;
281  }
282 
283  $sSearch .= ' ) ';
284 
285  return $sSearch;
286  }
287 }
288