00001 <?php
00002
00006 class oxUtilsCount extends oxSuperCfg
00007 {
00013 private static $_instance = null;
00014
00020 protected $_sUserViewId = null;
00021
00027 public static function getInstance()
00028 {
00029
00030 if ( defined( 'OXID_PHP_UNIT' ) ) {
00031 static $inst = array();
00032 self::$_instance = $inst[oxClassCacheKey()];
00033 }
00034
00035 if ( !self::$_instance instanceof oxUtilsCount ) {
00036
00037 self::$_instance = oxNew( 'oxUtilsCount' );
00038 if ( defined( 'OXID_PHP_UNIT' ) ) {
00039 $inst[oxClassCacheKey()] = self::$_instance;
00040 }
00041 }
00042 return self::$_instance;
00043 }
00044
00052 public function getCatArticleCount( $sCatId )
00053 {
00054
00055 $sActIdent = $this->_getUserViewId();
00056
00057
00058 $aCatData = $this->_getCatCache();
00059
00060 if ( !$aCatData || !isset( $aCatData[$sCatId][$sActIdent] ) ) {
00061 $iCnt = $this->setCatArticleCount( $aCatData, $sCatId, $sActIdent );
00062 } else {
00063 $iCnt = $aCatData[$sCatId][$sActIdent];
00064 }
00065 return $iCnt;
00066 }
00067
00077 public function getPriceCatArticleCount( $sCatId, $dPriceFrom, $dPriceTo )
00078 {
00079
00080 $sActIdent = $this->_getUserViewId();
00081
00082
00083 $aCatData = $this->_getCatCache();
00084
00085 if ( !$aCatData || !isset( $aCatData[$sCatId][$sActIdent] ) ) {
00086 $iCnt = $this->setPriceCatArticleCount( $aCatData, $sCatId, $sActIdent, $dPriceFrom, $dPriceTo );
00087 } else {
00088 $iCnt = $aCatData[$sCatId][$sActIdent];
00089 }
00090 return $iCnt;
00091 }
00092
00100 public function getVendorArticleCount( $sVendorId )
00101 {
00102
00103 $sActIdent = $this->_getUserViewId();
00104
00105
00106 $aVendorData = $this->_getVendorCache();
00107
00108 if ( !$aVendorData || !isset( $aVendorData[$sVendorId][$sActIdent] ) ) {
00109 $iCnt = $this->setVendorArticleCount( $aVendorData, $sVendorId, $sActIdent );
00110 } else {
00111 $iCnt = $aVendorData[$sVendorId][$sActIdent];
00112 }
00113 return $iCnt;
00114 }
00115
00123 public function getManufacturerArticleCount( $sManufacturerId )
00124 {
00125
00126 $sActIdent = $this->_getUserViewId();
00127
00128
00129 $aManufacturerData = $this->_getManufacturerCache();
00130 if ( !$aManufacturerData || !isset( $aManufacturerData[$sManufacturerId][$sActIdent] ) ) {
00131 $iCnt = $this->setManufacturerArticleCount( $aManufacturerData, $sManufacturerId, $sActIdent );
00132 } else {
00133 $iCnt = $aManufacturerData[$sManufacturerId][$sActIdent];
00134 }
00135 return $iCnt;
00136 }
00137
00147 public function setCatArticleCount( $aCache, $sCatId, $sActIdent )
00148 {
00149 $oArticle = oxNew( 'oxarticle' );
00150 $sTable = $oArticle->getViewName();
00151 $sO2CView = getViewName( 'oxobject2category' );
00152
00153
00154 $sQ = "select count(*) from (select distinct oxobject2category.oxcatnid, oxobject2category.oxobjectid from $sO2CView as oxobject2category WHERE oxobject2category.oxcatnid = '".$sCatId."' ";
00155 $sQ .= "and oxobject2category.oxobjectid in ( SELECT oxid FROM $sTable where ".$oArticle->getSqlActiveSnippet().") ) as ox2cat";
00156
00157 $aCache[$sCatId][$sActIdent] = oxDb::getDb()->getOne( $sQ );
00158
00159 $this->_setCatCache( $aCache );
00160 return $aCache[$sCatId][$sActIdent];
00161 }
00162
00174 public function setPriceCatArticleCount( $aCache, $sCatId, $sActIdent, $dPriceFrom, $dPriceTo )
00175 {
00176 $oArticle = oxNew( 'oxarticle' );
00177 $sTable = $oArticle->getViewName();
00178
00179 $sSubSelect = "select if( oxparentid='', oxid, oxparentid ) as id from {$sTable} where oxprice >= 0 ";
00180 $sSubSelect .= $dPriceTo ? "and oxprice <= " . (double)$dPriceTo . " " : " ";
00181 $sSubSelect .= $dPriceFrom ? "group by id having min( oxprice ) >= " . (double)$dPriceFrom . " " : " ";
00182
00183 $sSelect = "select count({$sTable}.oxid) from {$sTable} where ";
00184 $sSelect .= "{$sTable}.oxid in ($sSubSelect) ";
00185 $sSelect .= "and {$sTable}.oxissearch = 1 and ".$oArticle->getSqlActiveSnippet();
00186
00187 $aCache[$sCatId][$sActIdent] = oxDb::getDb()->getOne( $sSelect );
00188
00189 $this->_setCatCache( $aCache );
00190 return $aCache[$sCatId][$sActIdent];
00191 }
00192
00202 public function setVendorArticleCount( $aCache, $sCatId, $sActIdent )
00203 {
00204
00205 if ( $sCatId == 'root' ) {
00206 return 0;
00207 }
00208
00209 $oArticle = oxNew( 'oxarticle' );
00210 $sTable = $oArticle->getViewName();
00211
00212
00213 $sQ = "select $sTable.oxvendorid AS vendorId, count(*) from $sTable where ";
00214 $sQ .= "$sTable.oxvendorid <> '' and $sTable.oxparentid = '' and ".$oArticle->getSqlActiveSnippet()." group by $sTable.oxvendorid ";
00215 $aDbResult = oxDb::getDb()->getAssoc( $sQ );
00216
00217 foreach ( $aDbResult as $sKey => $sValue ) {
00218 $aCache[$sKey][$sActIdent] = $sValue;
00219 }
00220
00221 $this->_setVendorCache( $aCache );
00222 return $aCache[$sCatId][$sActIdent];
00223 }
00224
00234 public function setManufacturerArticleCount( $aCache, $sCatId, $sActIdent )
00235 {
00236
00237 if ( $sCatId == 'root' ) {
00238 return 0;
00239 }
00240
00241 $oArticle = oxNew( 'oxarticle' );
00242 $sTable = $oArticle->getViewName();
00243
00244
00245 $sQ = "select $sTable.oxmanufacturerid AS manufacturerId, count(*) from $sTable where ";
00246 $sQ .= "$sTable.oxmanufacturerid <> '' and $sTable.oxparentid = '' and ".$oArticle->getSqlActiveSnippet()." group by $sTable.oxmanufacturerid ";
00247 $aDbResult = oxDb::getDb()->getAssoc( $sQ );
00248
00249 foreach ( $aDbResult as $sKey => $sValue ) {
00250 $aCache[$sKey][$sActIdent] = $sValue;
00251 }
00252
00253 $this->_setManufacturerCache( $aCache );
00254 return $aCache[$sCatId][$sActIdent];
00255 }
00256
00264 public function resetCatArticleCount( $sCatId = null )
00265 {
00266 if ( !$sCatId ) {
00267 $this->getConfig()->setGlobalParameter( 'aLocalCatCache', null );
00268 } else {
00269 $aCatData = $this->_getCatCache();
00270 if ( isset( $aCatData[$sCatId] ) ) {
00271 unset( $aCatData[$sCatId] );
00272 $this->_setCatCache( $aCatData );
00273 }
00274 }
00275
00276 }
00277
00285 public function resetPriceCatArticleCount( $iPrice )
00286 {
00287
00288 if ( $aCatData = $this->_getCatCache() ) {
00289
00290 $sTable = getViewName( 'oxcategories' );
00291 $sSelect = "select $sTable.oxid from $sTable where " . (double)$iPrice . " >= $sTable.oxpricefrom and " . (double)$iPrice . " <= $sTable.oxpriceto ";
00292
00293 $rs = oxDb::getDb()->execute( $sSelect );
00294 if ( $rs != false && $rs->recordCount() > 0 ) {
00295 while ( !$rs->EOF ) {
00296 if ( isset( $aCatData[$rs->fields[0]] ) ) {
00297 unset( $aCatData[$rs->fields[0]] );
00298 }
00299 $rs->moveNext();
00300 }
00301
00302
00303 $this->_setCatCache( $aCatData );
00304 }
00305
00306 }
00307 }
00308
00317 public function getTagArticleCount( $sTag, $iLang )
00318 {
00319 $sLangExt = oxLang::getInstance()->getLanguageTag( $iLang );
00320 $oDb = oxDb::getDb();
00321
00322 $oArticle = oxNew("oxarticle");
00323 $sArticleTable = $oArticle->getViewName();
00324 $sActiveSnippet = $oArticle->getSqlActiveSnippet();
00325
00326 $sQ = "select count(*) from oxartextends inner join {$sArticleTable}
00327 on {$sArticleTable}.oxid = oxartextends.oxid where {$sActiveSnippet}
00328 and {$sArticleTable}.oxissearch = 1
00329 and match(oxartextends.oxtags{$sLangExt})
00330 against ( ".$oDb->quote( $sTag )." ) ";
00331
00332 return $oDb->getOne( $sQ );
00333 }
00334
00342 public function resetVendorArticleCount( $sVendorId = null )
00343 {
00344 if ( !$sVendorId ) {
00345 $this->getConfig()->setGlobalParameter( 'aLocalVendorCache', null );
00346 oxUtils::getInstance()->toFileCache( 'aLocalVendorCache', '' );
00347 } else {
00348 $aVendorData = $this->_getVendorCache();
00349 if ( isset( $aVendorData[$sVendorId] ) ) {
00350 unset( $aVendorData[$sVendorId] );
00351 $this->_setVendorCache( $aVendorData );
00352 }
00353 }
00354
00355 }
00356
00364 public function resetManufacturerArticleCount( $sManufacturerId = null )
00365 {
00366 if ( !$sManufacturerId ) {
00367 $this->getConfig()->setGlobalParameter( 'aLocalManufacturerCache', null );
00368 oxUtils::getInstance()->toFileCache( 'aLocalManufacturerCache', '' );
00369 } else {
00370 $aManufacturerData = $this->_getManufacturerCache();
00371 if ( isset( $aManufacturerData[$sManufacturerId] ) ) {
00372 unset( $aManufacturerData[$sManufacturerId] );
00373 $this->_setManufacturerCache( $aManufacturerData );
00374 }
00375 }
00376
00377 }
00378
00384 protected function _getCatCache()
00385 {
00386 $myConfig = $this->getConfig();
00387
00388
00389 $aLocalCatCache = $myConfig->getGlobalParameter( 'aLocalCatCache' );
00390
00391
00392 if ( !$aLocalCatCache ) {
00393 $sLocalCatCache = oxUtils::getInstance()->fromFileCache( 'aLocalCatCache');
00394 if ( $sLocalCatCache ) {
00395 $aLocalCatCache = $sLocalCatCache;
00396 } else {
00397 $aLocalCatCache = null;
00398 }
00399 $myConfig->setGlobalParameter( 'aLocalCatCache', $aLocalCatCache );
00400 }
00401 return $aLocalCatCache;
00402 }
00403
00411 protected function _setCatCache( $aCache )
00412 {
00413 $this->getConfig()->setGlobalParameter( 'aLocalCatCache', $aCache );
00414 oxUtils::getInstance()->toFileCache( 'aLocalCatCache', $aCache );
00415 }
00416
00424 protected function _setVendorCache( $aCache )
00425 {
00426 $this->getConfig()->setGlobalParameter( 'aLocalVendorCache', $aCache );
00427 oxUtils::getInstance()->toFileCache( 'aLocalVendorCache', $aCache );
00428 }
00429
00437 protected function _setManufacturerCache( $aCache )
00438 {
00439 $this->getConfig()->setGlobalParameter( 'aLocalManufacturerCache', $aCache );
00440 oxUtils::getInstance()->toFileCache( 'aLocalManufacturerCache', $aCache );
00441 }
00442
00448 protected function _getVendorCache()
00449 {
00450 $myConfig = $this->getConfig();
00451
00452
00453 $aLocalVendorCache = $myConfig->getGlobalParameter('aLocalVendorCache');
00454
00455 if ( !$aLocalVendorCache ) {
00456 $sLocalVendorCache = oxUtils::getInstance()->fromFileCache( 'aLocalVendorCache' );
00457 if ( $sLocalVendorCache ) {
00458 $aLocalVendorCache = $sLocalVendorCache;
00459 } else {
00460 $aLocalVendorCache = null;
00461 }
00462 $myConfig->setGlobalParameter( 'aLocalVendorCache', $aLocalVendorCache );
00463 }
00464 return $aLocalVendorCache;
00465 }
00466
00472 protected function _getManufacturerCache()
00473 {
00474 $myConfig = $this->getConfig();
00475
00476
00477 $aLocalManufacturerCache = $myConfig->getGlobalParameter('aLocalManufacturerCache');
00478
00479 if ( !$aLocalManufacturerCache ) {
00480 $sLocalManufacturerCache = oxUtils::getInstance()->fromFileCache( 'aLocalManufacturerCache' );
00481 if ( $sLocalManufacturerCache ) {
00482 $aLocalManufacturerCache = $sLocalManufacturerCache;
00483 } else {
00484 $aLocalManufacturerCache = null;
00485 }
00486 $myConfig->setGlobalParameter( 'aLocalManufacturerCache', $aLocalManufacturerCache );
00487 }
00488 return $aLocalManufacturerCache;
00489 }
00490
00498 protected function _getUserViewId( $blReset = false )
00499 {
00500 if ( $this->_sUserViewId != null && !$blReset ) {
00501 return $this->_sUserViewId;
00502 }
00503
00504
00505 $aRRIdx = null;
00506
00507 $this->_sUserViewId = md5($this->getConfig()->getShopID().oxLang::getInstance()->getLanguageTag().serialize($aRRIdx).(int) $this->isAdmin() );
00508 return $this->_sUserViewId;
00509 }
00510
00511 }