OXID eShop CE  4.8.11
 All Classes Files Functions Variables Pages
oxutilscount.php
Go to the documentation of this file.
1 <?php
2 
6 class oxUtilsCount extends oxSuperCfg
7 {
13  private static $_instance = null;
14 
20  protected $_sUserViewId = null;
21 
29  public static function getInstance()
30  {
31  return oxRegistry::get("oxUtilsCount");
32  }
33 
41  public function getCatArticleCount( $sCatId )
42  {
43  // current status unique ident
44  $sActIdent = $this->_getUserViewId();
45 
46  // loading from cache
47  $aCatData = $this->_getCatCache();
48 
49  if ( !$aCatData || !isset( $aCatData[$sCatId][$sActIdent] ) ) {
50  $iCnt = $this->setCatArticleCount( $aCatData, $sCatId, $sActIdent );
51  } else {
52  $iCnt = $aCatData[$sCatId][$sActIdent];
53  }
54  return $iCnt;
55  }
56 
66  public function getPriceCatArticleCount( $sCatId, $dPriceFrom, $dPriceTo )
67  {
68  // current status unique ident
69  $sActIdent = $this->_getUserViewId();
70 
71  // loading from cache
72  $aCatData = $this->_getCatCache();
73 
74  if ( !$aCatData || !isset( $aCatData[$sCatId][$sActIdent] ) ) {
75  $iCnt = $this->setPriceCatArticleCount( $aCatData, $sCatId, $sActIdent, $dPriceFrom, $dPriceTo );
76  } else {
77  $iCnt = $aCatData[$sCatId][$sActIdent];
78  }
79 
80  return $iCnt;
81  }
82 
90  public function getVendorArticleCount( $sVendorId )
91  {
92  // current category unique ident
93  $sActIdent = $this->_getUserViewId();
94 
95  // loading from cache
96  $aVendorData = $this->_getVendorCache();
97 
98  if ( !$aVendorData || !isset( $aVendorData[$sVendorId][$sActIdent] ) ) {
99  $iCnt = $this->setVendorArticleCount( $aVendorData, $sVendorId, $sActIdent );
100  } else {
101  $iCnt = $aVendorData[$sVendorId][$sActIdent];
102  }
103  return $iCnt;
104  }
105 
113  public function getManufacturerArticleCount( $sManufacturerId )
114  {
115  // current category unique ident
116  $sActIdent = $this->_getUserViewId();
117 
118  // loading from cache
119  $aManufacturerData = $this->_getManufacturerCache();
120  if ( !$aManufacturerData || !isset( $aManufacturerData[$sManufacturerId][$sActIdent] ) ) {
121  $iCnt = $this->setManufacturerArticleCount( $aManufacturerData, $sManufacturerId, $sActIdent );
122  } else {
123  $iCnt = $aManufacturerData[$sManufacturerId][$sActIdent];
124  }
125  return $iCnt;
126  }
127 
137  public function setCatArticleCount( $aCache, $sCatId, $sActIdent )
138  {
139  $oArticle = oxNew( 'oxarticle' );
140  $sTable = $oArticle->getViewName();
141  $sO2CView = getViewName( 'oxobject2category' );
142  $oDb = oxDb::getDb();
143 
144  // we use distinct if article is assigned to category twice
145  $sQ = "SELECT COUNT( DISTINCT $sTable.`oxid` )
146  FROM $sO2CView
147  INNER JOIN $sTable ON $sO2CView.`oxobjectid` = $sTable.`oxid` AND $sTable.`oxparentid` = ''
148  WHERE $sO2CView.`oxcatnid` = " . $oDb->quote( $sCatId ) . " AND " . $oArticle->getSqlActiveSnippet();
149 
150  $aCache[$sCatId][$sActIdent] = $oDb->getOne( $sQ );
151 
152  $this->_setCatCache( $aCache );
153  return $aCache[$sCatId][$sActIdent];
154  }
155 
167  public function setPriceCatArticleCount( $aCache, $sCatId, $sActIdent, $dPriceFrom, $dPriceTo )
168  {
169  $oArticle = oxNew( 'oxarticle' );
170  $sTable = $oArticle->getViewName();
171 
172  $sSelect = "select count({$sTable}.oxid) from {$sTable} where oxvarminprice >= 0 ";
173  $sSelect .= $dPriceTo ? "and oxvarminprice <= " . (double)$dPriceTo . " " : " ";
174  $sSelect .= $dPriceFrom ? "and oxvarminprice >= " . (double)$dPriceFrom . " " : " ";
175  $sSelect .= "and {$sTable}.oxissearch = 1 and ".$oArticle->getSqlActiveSnippet();
176 
177  $aCache[$sCatId][$sActIdent] = oxDb::getDb()->getOne( $sSelect );
178 
179  $this->_setCatCache( $aCache );
180  return $aCache[$sCatId][$sActIdent];
181  }
182 
192  public function setVendorArticleCount( $aCache, $sCatId, $sActIdent )
193  {
194  // if vendor/category name is 'root', skip counting
195  if ( $sCatId == 'root' ) {
196  return 0;
197  }
198 
199  $oArticle = oxNew( 'oxarticle' );
200  $sTable = $oArticle->getViewName();
201 
202  // select each vendor articles count
203  $sQ = "select $sTable.oxvendorid AS vendorId, count(*) from $sTable where ";
204  $sQ .= "$sTable.oxvendorid <> '' and $sTable.oxparentid = '' and ".$oArticle->getSqlActiveSnippet()." group by $sTable.oxvendorid ";
205  $aDbResult = oxDb::getDb()->getAssoc( $sQ );
206 
207  foreach ( $aDbResult as $sKey => $sValue ) {
208  $aCache[$sKey][$sActIdent] = $sValue;
209  }
210 
211  $this->_setVendorCache( $aCache );
212  return $aCache[$sCatId][$sActIdent];
213  }
214 
224  public function setManufacturerArticleCount( $aCache, $sMnfId, $sActIdent )
225  {
226  // if Manufacturer/category name is 'root', skip counting
227  if ( $sMnfId == 'root' ) {
228  return 0;
229  }
230 
231  $oArticle = oxNew( 'oxarticle' );
232  $sArtTable = $oArticle->getViewName();
233  $sManTable = getViewName('oxmanufacturers');
234 
235  // select each Manufacturer articles count
236  //#3485
237  $sQ = "select count($sArtTable.oxid) from $sArtTable where $sArtTable.oxparentid = '' and oxmanufacturerid = '$sMnfId' and ".$oArticle->getSqlActiveSnippet();
238 
239  $iValue = oxDb::getDb()->getOne( $sQ );
240 
241  $aCache[$sMnfId][$sActIdent] = (int) $iValue;
242 
243  $this->_setManufacturerCache( $aCache );
244 
245  return $aCache[$sMnfId][$sActIdent];
246  }
247 
255  public function resetCatArticleCount( $sCatId = null )
256  {
257  if ( !$sCatId ) {
258  $this->getConfig()->setGlobalParameter( 'aLocalCatCache', null );
259  oxRegistry::getUtils()->toFileCache( 'aLocalCatCache', '' );
260  } else {
261  // loading from cache
262  $aCatData = $this->_getCatCache();
263  if ( isset( $aCatData[$sCatId] ) ) {
264  unset( $aCatData[$sCatId] );
265  $this->_setCatCache( $aCatData );
266  }
267  }
268 
269  }
270 
278  public function resetPriceCatArticleCount( $iPrice )
279  {
280  // loading from cache
281  if ( $aCatData = $this->_getCatCache() ) {
282 
283  $sTable = getViewName( 'oxcategories' );
284  $sSelect = "select $sTable.oxid from $sTable where " . (double)$iPrice . " >= $sTable.oxpricefrom and " . (double)$iPrice . " <= $sTable.oxpriceto ";
285 
286  $rs = oxDb::getDb()->select( $sSelect, false, false );
287  if ( $rs != false && $rs->recordCount() > 0 ) {
288  while ( !$rs->EOF ) {
289  if ( isset( $aCatData[$rs->fields[0]] ) ) {
290  unset( $aCatData[$rs->fields[0]] );
291  }
292  $rs->moveNext();
293  }
294 
295  // writing back to cache
296  $this->_setCatCache( $aCatData );
297  }
298 
299  }
300  }
301 
310  public function getTagArticleCount( $sTag, $iLang )
311  {
312  $oDb = oxDb::getDb();
313 
314  $oArticle = oxNew("oxarticle");
315  $sArticleTable = $oArticle->getViewName();
316  $sActiveSnippet = $oArticle->getSqlActiveSnippet();
317  $sViewName = getViewName( 'oxartextends', $iLang );
318 
319  $sQ = "select count(*) from {$sViewName} inner join {$sArticleTable} on ".
320  "{$sArticleTable}.oxid = {$sViewName}.oxid where {$sArticleTable}.oxparentid = '' and {$sArticleTable}.oxissearch = 1 AND match ( {$sViewName}.oxtags ) ".
321  "against( ".$oDb->quote( "\"".$sTag."\"" )." IN BOOLEAN MODE ) and {$sActiveSnippet}";
322 
323  return $oDb->getOne( $sQ );
324  }
325 
333  public function resetVendorArticleCount( $sVendorId = null )
334  {
335  if ( !$sVendorId ) {
336  $this->getConfig()->setGlobalParameter( 'aLocalVendorCache', null );
337  oxRegistry::getUtils()->toFileCache( 'aLocalVendorCache', '' );
338  } else {
339  // loading from cache
340  $aVendorData = $this->_getVendorCache();
341  if ( isset( $aVendorData[$sVendorId] ) ) {
342  unset( $aVendorData[$sVendorId] );
343  $this->_setVendorCache( $aVendorData );
344  }
345  }
346 
347  }
348 
356  public function resetManufacturerArticleCount( $sManufacturerId = null )
357  {
358  if ( !$sManufacturerId ) {
359  $this->getConfig()->setGlobalParameter( 'aLocalManufacturerCache', null );
360  oxRegistry::getUtils()->toFileCache( 'aLocalManufacturerCache', '' );
361  } else {
362  // loading from cache
363  $aManufacturerData = $this->_getManufacturerCache();
364  if ( isset( $aManufacturerData[$sManufacturerId] ) ) {
365  unset( $aManufacturerData[$sManufacturerId] );
366  $this->_setManufacturerCache( $aManufacturerData );
367  }
368  }
369 
370  }
371 
377  protected function _getCatCache()
378  {
379  $myConfig = $this->getConfig();
380 
381  // first look at the local cache
382  $aLocalCatCache = $myConfig->getGlobalParameter( 'aLocalCatCache' );
383 
384  // if local cache is not set - loading from file cache
385  if ( !$aLocalCatCache ) {
386  $sLocalCatCache = oxRegistry::getUtils()->fromFileCache( 'aLocalCatCache');
387  if ( $sLocalCatCache ) {
388  $aLocalCatCache = $sLocalCatCache;
389  } else {
390  $aLocalCatCache = null;
391  }
392  $myConfig->setGlobalParameter( 'aLocalCatCache', $aLocalCatCache );
393  }
394  return $aLocalCatCache;
395  }
396 
404  protected function _setCatCache( $aCache )
405  {
406  $this->getConfig()->setGlobalParameter( 'aLocalCatCache', $aCache );
407  oxRegistry::getUtils()->toFileCache( 'aLocalCatCache', $aCache );
408  }
409 
417  protected function _setVendorCache( $aCache )
418  {
419  $this->getConfig()->setGlobalParameter( 'aLocalVendorCache', $aCache );
420  oxRegistry::getUtils()->toFileCache( 'aLocalVendorCache', $aCache );
421  }
422 
430  protected function _setManufacturerCache( $aCache )
431  {
432  $this->getConfig()->setGlobalParameter( 'aLocalManufacturerCache', $aCache );
433  oxRegistry::getUtils()->toFileCache( 'aLocalManufacturerCache', $aCache );
434  }
435 
441  protected function _getVendorCache()
442  {
443  $myConfig = $this->getConfig();
444 
445  // first look at the local cache
446  $aLocalVendorCache = $myConfig->getGlobalParameter('aLocalVendorCache');
447  // if local cache is not set - loading from file cache
448  if ( !$aLocalVendorCache ) {
449  $sLocalVendorCache = oxRegistry::getUtils()->fromFileCache( 'aLocalVendorCache' );
450  if ( $sLocalVendorCache ) {
451  $aLocalVendorCache = $sLocalVendorCache;
452  } else {
453  $aLocalVendorCache = null;
454  }
455  $myConfig->setGlobalParameter( 'aLocalVendorCache', $aLocalVendorCache );
456  }
457  return $aLocalVendorCache;
458  }
459 
465  protected function _getManufacturerCache()
466  {
467  $myConfig = $this->getConfig();
468 
469  // first look at the local cache
470  $aLocalManufacturerCache = $myConfig->getGlobalParameter('aLocalManufacturerCache');
471  // if local cache is not set - loading from file cache
472  if ( !$aLocalManufacturerCache ) {
473  $sLocalManufacturerCache = oxRegistry::getUtils()->fromFileCache( 'aLocalManufacturerCache' );
474  if ( $sLocalManufacturerCache ) {
475  $aLocalManufacturerCache = $sLocalManufacturerCache;
476  } else {
477  $aLocalManufacturerCache = null;
478  }
479  $myConfig->setGlobalParameter( 'aLocalManufacturerCache', $aLocalManufacturerCache );
480  }
481  return $aLocalManufacturerCache;
482  }
483 
491  protected function _getUserViewId( $blReset = false )
492  {
493  if ( $this->_sUserViewId != null && !$blReset ) {
494  return $this->_sUserViewId;
495  }
496 
497  // loading R&R data from session
498  $aRRIdx = null;
499 
500  $this->_sUserViewId = md5($this->getConfig()->getShopID().oxRegistry::getLang()->getLanguageTag().serialize($aRRIdx).(int) $this->isAdmin() );
501  return $this->_sUserViewId;
502  }
503 
504 }