OXID eShop CE  4.10.6
 All Classes Namespaces 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 
294  public function getTagArticleCount($sTag, $iLang)
295  {
296  $oDb = oxDb::getDb();
297 
298  $oArticle = oxNew("oxarticle");
299  $sArticleTable = $oArticle->getViewName();
300  $sActiveSnippet = $oArticle->getSqlActiveSnippet();
301  $sViewName = getViewName('oxartextends', $iLang);
302 
303  $sQ = "select count(*) from {$sViewName} inner join {$sArticleTable} on " .
304  "{$sArticleTable}.oxid = {$sViewName}.oxid where {$sArticleTable}.oxparentid = '' and {$sArticleTable}.oxissearch = 1 AND match ( {$sViewName}.oxtags ) " .
305  "against( " . $oDb->quote("\"" . $sTag . "\"") . " IN BOOLEAN MODE ) and {$sActiveSnippet}";
306 
307  return $oDb->getOne($sQ);
308  }
309 
315  public function resetVendorArticleCount($sVendorId = null)
316  {
317  if (!$sVendorId) {
318  $this->getConfig()->setGlobalParameter('aLocalVendorCache', null);
319  oxRegistry::getUtils()->toFileCache('aLocalVendorCache', '');
320  } else {
321  // loading from cache
322  $aVendorData = $this->_getVendorCache();
323  if (isset($aVendorData[$sVendorId])) {
324  unset($aVendorData[$sVendorId]);
325  $this->_setVendorCache($aVendorData);
326  }
327  }
328  }
329 
335  public function resetManufacturerArticleCount($sManufacturerId = null)
336  {
337  if (!$sManufacturerId) {
338  $this->getConfig()->setGlobalParameter('aLocalManufacturerCache', null);
339  oxRegistry::getUtils()->toFileCache('aLocalManufacturerCache', '');
340  } else {
341  // loading from cache
342  $aManufacturerData = $this->_getManufacturerCache();
343  if (isset($aManufacturerData[$sManufacturerId])) {
344  unset($aManufacturerData[$sManufacturerId]);
345  $this->_setManufacturerCache($aManufacturerData);
346  }
347  }
348  }
349 
355  protected function _getCatCache()
356  {
357  $myConfig = $this->getConfig();
358 
359  // first look at the local cache
360  $aLocalCatCache = $myConfig->getGlobalParameter('aLocalCatCache');
361 
362  // if local cache is not set - loading from file cache
363  if (!$aLocalCatCache) {
364  $sLocalCatCache = oxRegistry::getUtils()->fromFileCache('aLocalCatCache');
365  if ($sLocalCatCache) {
366  $aLocalCatCache = $sLocalCatCache;
367  } else {
368  $aLocalCatCache = null;
369  }
370  $myConfig->setGlobalParameter('aLocalCatCache', $aLocalCatCache);
371  }
372 
373  return $aLocalCatCache;
374  }
375 
381  protected function _setCatCache($aCache)
382  {
383  $this->getConfig()->setGlobalParameter('aLocalCatCache', $aCache);
384  oxRegistry::getUtils()->toFileCache('aLocalCatCache', $aCache);
385  }
386 
392  protected function _setVendorCache($aCache)
393  {
394  $this->getConfig()->setGlobalParameter('aLocalVendorCache', $aCache);
395  oxRegistry::getUtils()->toFileCache('aLocalVendorCache', $aCache);
396  }
397 
403  protected function _setManufacturerCache($aCache)
404  {
405  $this->getConfig()->setGlobalParameter('aLocalManufacturerCache', $aCache);
406  oxRegistry::getUtils()->toFileCache('aLocalManufacturerCache', $aCache);
407  }
408 
414  protected function _getVendorCache()
415  {
416  $myConfig = $this->getConfig();
417 
418  // first look at the local cache
419  $aLocalVendorCache = $myConfig->getGlobalParameter('aLocalVendorCache');
420  // if local cache is not set - loading from file cache
421  if (!$aLocalVendorCache) {
422  $sLocalVendorCache = oxRegistry::getUtils()->fromFileCache('aLocalVendorCache');
423  if ($sLocalVendorCache) {
424  $aLocalVendorCache = $sLocalVendorCache;
425  } else {
426  $aLocalVendorCache = null;
427  }
428  $myConfig->setGlobalParameter('aLocalVendorCache', $aLocalVendorCache);
429  }
430 
431  return $aLocalVendorCache;
432  }
433 
439  protected function _getManufacturerCache()
440  {
441  $myConfig = $this->getConfig();
442 
443  // first look at the local cache
444  $aLocalManufacturerCache = $myConfig->getGlobalParameter('aLocalManufacturerCache');
445  // if local cache is not set - loading from file cache
446  if (!$aLocalManufacturerCache) {
447  $sLocalManufacturerCache = oxRegistry::getUtils()->fromFileCache('aLocalManufacturerCache');
448  if ($sLocalManufacturerCache) {
449  $aLocalManufacturerCache = $sLocalManufacturerCache;
450  } else {
451  $aLocalManufacturerCache = null;
452  }
453  $myConfig->setGlobalParameter('aLocalManufacturerCache', $aLocalManufacturerCache);
454  }
455 
456  return $aLocalManufacturerCache;
457  }
458 
466  protected function _getUserViewId($blReset = false)
467  {
468  if ($this->_sUserViewId != null && !$blReset) {
469  return $this->_sUserViewId;
470  }
471 
472  // loading R&R data from session
473  $aRRIdx = null;
474 
475  $this->_sUserViewId = md5($this->getConfig()->getShopID() . oxRegistry::getLang()->getLanguageTag() . serialize($aRRIdx) . (int) $this->isAdmin());
476 
477  return $this->_sUserViewId;
478  }
479 }