OXID eShop CE  4.9.6
 All Classes Files Functions Variables Pages
oxutilscount.php
Go to the documentation of this file.
1 <?php
2 
6 class oxUtilsCount extends oxSuperCfg
7 {
8 
14  protected $_sUserViewId = null;
15 
23  public function getCatArticleCount($sCatId)
24  {
25  // current status unique ident
26  $sActIdent = $this->_getUserViewId();
27 
28  // loading from cache
29  $aCatData = $this->_getCatCache();
30 
31  if (!$aCatData || !isset($aCatData[$sCatId][$sActIdent])) {
32  $iCnt = $this->setCatArticleCount($aCatData, $sCatId, $sActIdent);
33  } else {
34  $iCnt = $aCatData[$sCatId][$sActIdent];
35  }
36 
37  return $iCnt;
38  }
39 
49  public function getPriceCatArticleCount($sCatId, $dPriceFrom, $dPriceTo)
50  {
51  // current status unique ident
52  $sActIdent = $this->_getUserViewId();
53 
54  // loading from cache
55  $aCatData = $this->_getCatCache();
56 
57  if (!$aCatData || !isset($aCatData[$sCatId][$sActIdent])) {
58  $iCnt = $this->setPriceCatArticleCount($aCatData, $sCatId, $sActIdent, $dPriceFrom, $dPriceTo);
59  } else {
60  $iCnt = $aCatData[$sCatId][$sActIdent];
61  }
62 
63  return $iCnt;
64  }
65 
73  public function getVendorArticleCount($sVendorId)
74  {
75  // current category unique ident
76  $sActIdent = $this->_getUserViewId();
77 
78  // loading from cache
79  $aVendorData = $this->_getVendorCache();
80 
81  if (!$aVendorData || !isset($aVendorData[$sVendorId][$sActIdent])) {
82  $iCnt = $this->setVendorArticleCount($aVendorData, $sVendorId, $sActIdent);
83  } else {
84  $iCnt = $aVendorData[$sVendorId][$sActIdent];
85  }
86 
87  return $iCnt;
88  }
89 
97  public function getManufacturerArticleCount($sManufacturerId)
98  {
99  // current category unique ident
100  $sActIdent = $this->_getUserViewId();
101 
102  // loading from cache
103  $aManufacturerData = $this->_getManufacturerCache();
104  if (!$aManufacturerData || !isset($aManufacturerData[$sManufacturerId][$sActIdent])) {
105  $iCnt = $this->setManufacturerArticleCount($aManufacturerData, $sManufacturerId, $sActIdent);
106  } else {
107  $iCnt = $aManufacturerData[$sManufacturerId][$sActIdent];
108  }
109 
110  return $iCnt;
111  }
112 
122  public function setCatArticleCount($aCache, $sCatId, $sActIdent)
123  {
124  $oArticle = oxNew('oxarticle');
125  $sTable = $oArticle->getViewName();
126  $sO2CView = getViewName('oxobject2category');
127  $oDb = oxDb::getDb();
128 
129  // we use distinct if article is assigned to category twice
130  $sQ = "SELECT COUNT( DISTINCT $sTable.`oxid` )
131  FROM $sO2CView
132  INNER JOIN $sTable ON $sO2CView.`oxobjectid` = $sTable.`oxid` AND $sTable.`oxparentid` = ''
133  WHERE $sO2CView.`oxcatnid` = " . $oDb->quote($sCatId) . " AND " . $oArticle->getSqlActiveSnippet();
134 
135  $aCache[$sCatId][$sActIdent] = $oDb->getOne($sQ);
136 
137  $this->_setCatCache($aCache);
138 
139  return $aCache[$sCatId][$sActIdent];
140  }
141 
153  public function setPriceCatArticleCount($aCache, $sCatId, $sActIdent, $dPriceFrom, $dPriceTo)
154  {
155  $oArticle = oxNew('oxarticle');
156  $sTable = $oArticle->getViewName();
157 
158  $sSelect = "select count({$sTable}.oxid) from {$sTable} where oxvarminprice >= 0 ";
159  $sSelect .= $dPriceTo ? "and oxvarminprice <= " . (double) $dPriceTo . " " : " ";
160  $sSelect .= $dPriceFrom ? "and oxvarminprice >= " . (double) $dPriceFrom . " " : " ";
161  $sSelect .= "and {$sTable}.oxissearch = 1 and " . $oArticle->getSqlActiveSnippet();
162 
163  $aCache[$sCatId][$sActIdent] = oxDb::getDb()->getOne($sSelect);
164 
165  $this->_setCatCache($aCache);
166 
167  return $aCache[$sCatId][$sActIdent];
168  }
169 
179  public function setVendorArticleCount($aCache, $sCatId, $sActIdent)
180  {
181  // if vendor/category name is 'root', skip counting
182  if ($sCatId == 'root') {
183  return 0;
184  }
185 
186  $oArticle = oxNew('oxarticle');
187  $sTable = $oArticle->getViewName();
188 
189  // select each vendor articles count
190  $sQ = "select $sTable.oxvendorid AS vendorId, count(*) from $sTable where ";
191  $sQ .= "$sTable.oxvendorid <> '' and $sTable.oxparentid = '' and " . $oArticle->getSqlActiveSnippet() . " group by $sTable.oxvendorid ";
192  $aDbResult = oxDb::getDb()->getAssoc($sQ);
193 
194  foreach ($aDbResult as $sKey => $sValue) {
195  $aCache[$sKey][$sActIdent] = $sValue;
196  }
197 
198  $this->_setVendorCache($aCache);
199 
200  return $aCache[$sCatId][$sActIdent];
201  }
202 
212  public function setManufacturerArticleCount($aCache, $sMnfId, $sActIdent)
213  {
214  // if Manufacturer/category name is 'root', skip counting
215  if ($sMnfId == 'root') {
216  return 0;
217  }
218 
219  $oArticle = oxNew('oxarticle');
220  $sArtTable = $oArticle->getViewName();
221  $sManTable = getViewName('oxmanufacturers');
222 
223  // select each Manufacturer articles count
224  //#3485
225  $sQ = "select count($sArtTable.oxid) from $sArtTable where $sArtTable.oxparentid = '' and oxmanufacturerid = '$sMnfId' and " . $oArticle->getSqlActiveSnippet();
226 
227  $iValue = oxDb::getDb()->getOne($sQ);
228 
229  $aCache[$sMnfId][$sActIdent] = (int) $iValue;
230 
231  $this->_setManufacturerCache($aCache);
232 
233  return $aCache[$sMnfId][$sActIdent];
234  }
235 
241  public function resetCatArticleCount($sCatId = null)
242  {
243  if (!$sCatId) {
244  $this->getConfig()->setGlobalParameter('aLocalCatCache', null);
245  oxRegistry::getUtils()->toFileCache('aLocalCatCache', '');
246  } else {
247  // loading from cache
248  $aCatData = $this->_getCatCache();
249  if (isset($aCatData[$sCatId])) {
250  unset($aCatData[$sCatId]);
251  $this->_setCatCache($aCatData);
252  }
253  }
254  }
255 
261  public function resetPriceCatArticleCount($iPrice)
262  {
263  // loading from cache
264  if ($aCatData = $this->_getCatCache()) {
265 
266  $sTable = getViewName('oxcategories');
267  $sSelect = "select $sTable.oxid from $sTable where " . (double) $iPrice . " >= $sTable.oxpricefrom and " . (double) $iPrice . " <= $sTable.oxpriceto ";
268 
269  $rs = oxDb::getDb()->select($sSelect, false, false);
270  if ($rs != false && $rs->recordCount() > 0) {
271  while (!$rs->EOF) {
272  if (isset($aCatData[$rs->fields[0]])) {
273  unset($aCatData[$rs->fields[0]]);
274  }
275  $rs->moveNext();
276  }
277 
278  // writing back to cache
279  $this->_setCatCache($aCatData);
280  }
281  }
282  }
283 
292  public function getTagArticleCount($sTag, $iLang)
293  {
294  $oDb = oxDb::getDb();
295 
296  $oArticle = oxNew("oxarticle");
297  $sArticleTable = $oArticle->getViewName();
298  $sActiveSnippet = $oArticle->getSqlActiveSnippet();
299  $sViewName = getViewName('oxartextends', $iLang);
300 
301  $sQ = "select count(*) from {$sViewName} inner join {$sArticleTable} on " .
302  "{$sArticleTable}.oxid = {$sViewName}.oxid where {$sArticleTable}.oxparentid = '' and {$sArticleTable}.oxissearch = 1 AND match ( {$sViewName}.oxtags ) " .
303  "against( " . $oDb->quote("\"" . $sTag . "\"") . " IN BOOLEAN MODE ) and {$sActiveSnippet}";
304 
305  return $oDb->getOne($sQ);
306  }
307 
313  public function resetVendorArticleCount($sVendorId = null)
314  {
315  if (!$sVendorId) {
316  $this->getConfig()->setGlobalParameter('aLocalVendorCache', null);
317  oxRegistry::getUtils()->toFileCache('aLocalVendorCache', '');
318  } else {
319  // loading from cache
320  $aVendorData = $this->_getVendorCache();
321  if (isset($aVendorData[$sVendorId])) {
322  unset($aVendorData[$sVendorId]);
323  $this->_setVendorCache($aVendorData);
324  }
325  }
326  }
327 
333  public function resetManufacturerArticleCount($sManufacturerId = null)
334  {
335  if (!$sManufacturerId) {
336  $this->getConfig()->setGlobalParameter('aLocalManufacturerCache', null);
337  oxRegistry::getUtils()->toFileCache('aLocalManufacturerCache', '');
338  } else {
339  // loading from cache
340  $aManufacturerData = $this->_getManufacturerCache();
341  if (isset($aManufacturerData[$sManufacturerId])) {
342  unset($aManufacturerData[$sManufacturerId]);
343  $this->_setManufacturerCache($aManufacturerData);
344  }
345  }
346  }
347 
353  protected function _getCatCache()
354  {
355  $myConfig = $this->getConfig();
356 
357  // first look at the local cache
358  $aLocalCatCache = $myConfig->getGlobalParameter('aLocalCatCache');
359 
360  // if local cache is not set - loading from file cache
361  if (!$aLocalCatCache) {
362  $sLocalCatCache = oxRegistry::getUtils()->fromFileCache('aLocalCatCache');
363  if ($sLocalCatCache) {
364  $aLocalCatCache = $sLocalCatCache;
365  } else {
366  $aLocalCatCache = null;
367  }
368  $myConfig->setGlobalParameter('aLocalCatCache', $aLocalCatCache);
369  }
370 
371  return $aLocalCatCache;
372  }
373 
379  protected function _setCatCache($aCache)
380  {
381  $this->getConfig()->setGlobalParameter('aLocalCatCache', $aCache);
382  oxRegistry::getUtils()->toFileCache('aLocalCatCache', $aCache);
383  }
384 
390  protected function _setVendorCache($aCache)
391  {
392  $this->getConfig()->setGlobalParameter('aLocalVendorCache', $aCache);
393  oxRegistry::getUtils()->toFileCache('aLocalVendorCache', $aCache);
394  }
395 
401  protected function _setManufacturerCache($aCache)
402  {
403  $this->getConfig()->setGlobalParameter('aLocalManufacturerCache', $aCache);
404  oxRegistry::getUtils()->toFileCache('aLocalManufacturerCache', $aCache);
405  }
406 
412  protected function _getVendorCache()
413  {
414  $myConfig = $this->getConfig();
415 
416  // first look at the local cache
417  $aLocalVendorCache = $myConfig->getGlobalParameter('aLocalVendorCache');
418  // if local cache is not set - loading from file cache
419  if (!$aLocalVendorCache) {
420  $sLocalVendorCache = oxRegistry::getUtils()->fromFileCache('aLocalVendorCache');
421  if ($sLocalVendorCache) {
422  $aLocalVendorCache = $sLocalVendorCache;
423  } else {
424  $aLocalVendorCache = null;
425  }
426  $myConfig->setGlobalParameter('aLocalVendorCache', $aLocalVendorCache);
427  }
428 
429  return $aLocalVendorCache;
430  }
431 
437  protected function _getManufacturerCache()
438  {
439  $myConfig = $this->getConfig();
440 
441  // first look at the local cache
442  $aLocalManufacturerCache = $myConfig->getGlobalParameter('aLocalManufacturerCache');
443  // if local cache is not set - loading from file cache
444  if (!$aLocalManufacturerCache) {
445  $sLocalManufacturerCache = oxRegistry::getUtils()->fromFileCache('aLocalManufacturerCache');
446  if ($sLocalManufacturerCache) {
447  $aLocalManufacturerCache = $sLocalManufacturerCache;
448  } else {
449  $aLocalManufacturerCache = null;
450  }
451  $myConfig->setGlobalParameter('aLocalManufacturerCache', $aLocalManufacturerCache);
452  }
453 
454  return $aLocalManufacturerCache;
455  }
456 
464  protected function _getUserViewId($blReset = false)
465  {
466  if ($this->_sUserViewId != null && !$blReset) {
467  return $this->_sUserViewId;
468  }
469 
470  // loading R&R data from session
471  $aRRIdx = null;
472 
473  $this->_sUserViewId = md5($this->getConfig()->getShopID() . oxRegistry::getLang()->getLanguageTag() . serialize($aRRIdx) . (int) $this->isAdmin());
474 
475  return $this->_sUserViewId;
476  }
477 }