OXID eShop CE  4.10.4
 All Classes Namespaces Files Functions Variables Pages
oxsearch.php
Go to the documentation of this file.
1 <?php
2 
7 class oxSearch extends oxSuperCfg
8 {
9 
15  protected $_iLanguage = 0;
16 
22  public function __construct()
23  {
24  $this->setLanguage();
25  }
26 
32  public function setLanguage($iLanguage = null)
33  {
34  if (!isset($iLanguage)) {
35  $this->_iLanguage = oxRegistry::getLang()->getBaseLanguage();
36  } else {
37  $this->_iLanguage = $iLanguage;
38  }
39  }
40 
52  public function getSearchArticles($sSearchParamForQuery = false, $sInitialSearchCat = false, $sInitialSearchVendor = false, $sInitialSearchManufacturer = false, $sSortBy = false)
53  {
54  // sets active page
55  $this->iActPage = (int) oxRegistry::getConfig()->getRequestParameter('pgNr');
56  $this->iActPage = ($this->iActPage < 0) ? 0 : $this->iActPage;
57 
58  // load only articles which we show on screen
59  //setting default values to avoid possible errors showing article list
60  $iNrofCatArticles = $this->getConfig()->getConfigParam('iNrofCatArticles');
61  $iNrofCatArticles = $iNrofCatArticles ? $iNrofCatArticles : 10;
62 
63  $oArtList = oxNew('oxarticlelist');
64  $oArtList->setSqlLimit($iNrofCatArticles * $this->iActPage, $iNrofCatArticles);
65 
66  $sSelect = $this->_getSearchSelect($sSearchParamForQuery, $sInitialSearchCat, $sInitialSearchVendor, $sInitialSearchManufacturer, $sSortBy);
67  if ($sSelect) {
68  $oArtList->selectString($sSelect);
69  }
70 
71  return $oArtList;
72  }
73 
84  public function getSearchArticleCount($sSearchParamForQuery = false, $sInitialSearchCat = false, $sInitialSearchVendor = false, $sInitialSearchManufacturer = false)
85  {
86  $iCnt = 0;
87  $sSelect = $this->_getSearchSelect($sSearchParamForQuery, $sInitialSearchCat, $sInitialSearchVendor, $sInitialSearchManufacturer, false);
88  if ($sSelect) {
89 
90  $sPartial = substr($sSelect, strpos($sSelect, ' from '));
91  $sSelect = "select count( " . getViewName('oxarticles', $this->_iLanguage) . ".oxid ) $sPartial ";
92 
93  $iCnt = oxDb::getDb()->getOne($sSelect);
94  }
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  // @deprecated v5.3 (2016-05-04); Tags will be moved to own module.
172  if (in_array('oxlongdesc', $aSearchCols) || in_array('oxtags', $aSearchCols)) {
173  $sDescView = getViewName('oxartextends', $this->_iLanguage);
174  $sDescJoin = " LEFT JOIN {$sDescView} ON {$sArticleTable}.oxid={$sDescView}.oxid ";
175  }
176  // END deprecated
177  }
178 
179  //select articles
180  $sSelect = "select {$sSelectFields}, {$sArticleTable}.oxtimestamp from {$sArticleTable} {$sDescJoin} where ";
181 
182  // must be additional conditions in select if searching in category
183  if ($sInitialSearchCat) {
184  $sCatView = getViewName('oxcategories', $this->_iLanguage);
185  $sInitialSearchCatQuoted = $oDb->quote($sInitialSearchCat);
186  $sSelectCat = "select oxid from {$sCatView} where oxid = $sInitialSearchCatQuoted and (oxpricefrom != '0' or oxpriceto != 0)";
187  if ($oDb->getOne($sSelectCat)) {
188  $sSelect = "select {$sSelectFields}, {$sArticleTable}.oxtimestamp from {$sArticleTable} $sDescJoin " .
189  "where {$sArticleTable}.oxid in ( select {$sArticleTable}.oxid as id from {$sArticleTable}, {$sO2CView} as oxobject2category, {$sCatView} as oxcategories " .
190  "where (oxobject2category.oxcatnid=$sInitialSearchCatQuoted and oxobject2category.oxobjectid={$sArticleTable}.oxid) or (oxcategories.oxid=$sInitialSearchCatQuoted and {$sArticleTable}.oxprice >= oxcategories.oxpricefrom and
191  {$sArticleTable}.oxprice <= oxcategories.oxpriceto )) and ";
192  } else {
193  $sSelect = "select {$sSelectFields} from {$sO2CView} as
194  oxobject2category, {$sArticleTable} {$sDescJoin} where oxobject2category.oxcatnid=$sInitialSearchCatQuoted and
195  oxobject2category.oxobjectid={$sArticleTable}.oxid and ";
196  }
197  }
198 
199  $sSelect .= $oArticle->getSqlActiveSnippet();
200  $sSelect .= " and {$sArticleTable}.oxparentid = '' and {$sArticleTable}.oxissearch = 1 ";
201 
202  if ($sInitialSearchVendor) {
203  $sSelect .= " and {$sArticleTable}.oxvendorid = " . $oDb->quote($sInitialSearchVendor) . " ";
204  }
205 
206  if ($sInitialSearchManufacturer) {
207  $sSelect .= " and {$sArticleTable}.oxmanufacturerid = " . $oDb->quote($sInitialSearchManufacturer) . " ";
208  }
209 
210  $sSelect .= $sWhere;
211 
212  if ($sSortBy) {
213  $sSelect .= " order by {$sSortBy} ";
214  }
215 
216  return $sSelect;
217  }
218 
226  protected function _getWhere($sSearchString)
227  {
228  $oDb = oxDb::getDb();
229  $myConfig = $this->getConfig();
230  $blSep = false;
231  $sArticleTable = getViewName('oxarticles', $this->_iLanguage);
232 
233  $aSearchCols = $myConfig->getConfigParam('aSearchCols');
234  if (!(is_array($aSearchCols) && count($aSearchCols))) {
235  return '';
236  }
237 
238  $oTempArticle = oxNew('oxarticle');
239  $sSearchSep = $myConfig->getConfigParam('blSearchUseAND') ? 'and ' : 'or ';
240  $aSearch = explode(' ', $sSearchString);
241  $sSearch = ' and ( ';
242  $myUtilsString = oxRegistry::get("oxUtilsString");
243  $oLang = oxRegistry::getLang();
244 
245  foreach ($aSearch as $sSearchString) {
246 
247  if (!strlen($sSearchString)) {
248  continue;
249  }
250 
251  if ($blSep) {
252  $sSearch .= $sSearchSep;
253  }
254 
255  $blSep2 = false;
256  $sSearch .= '( ';
257 
258  foreach ($aSearchCols as $sField) {
259 
260  if ($blSep2) {
261  $sSearch .= ' or ';
262  }
263 
264  // @deprecated v5.3 (2016-05-04); Tags will be moved to own module.
265  // as long description now is on different table table must differ
266  if ($sField == 'oxlongdesc' || $sField == 'oxtags') {
267  $sSearchField = getViewName('oxartextends', $this->_iLanguage) . ".{$sField}";
268  } else {
269  $sSearchField = "{$sArticleTable}.{$sField}";
270  }
271  // END deprecated
272 
273  $sSearch .= " {$sSearchField} like " . $oDb->quote("%$sSearchString%");
274 
275  // special chars ?
276  if (($sUml = $myUtilsString->prepareStrForSearch($sSearchString))) {
277  $sSearch .= " or {$sSearchField} like " . $oDb->quote("%$sUml%");
278  }
279 
280  $blSep2 = true;
281  }
282  $sSearch .= ' ) ';
283 
284  $blSep = true;
285  }
286 
287  $sSearch .= ' ) ';
288 
289  return $sSearch;
290  }
291 }