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
00125 public function setCatArticleCount( $aCache, $sCatId, $sActIdent )
00126 {
00127 $oArticle = oxNew( 'oxarticle' );
00128 $sTable = $oArticle->getViewName();
00129 $sO2CView = getViewName( 'oxobject2category' );
00130
00131
00132 $sQ = "select count(*) from (select distinct oxobject2category.oxcatnid, oxobject2category.oxobjectid from $sO2CView as oxobject2category WHERE oxobject2category.oxcatnid = '".$sCatId."' ";
00133 $sQ .= "and oxobject2category.oxobjectid in ( SELECT oxid FROM $sTable where ".$oArticle->getSqlActiveSnippet().") ) as ox2cat";
00134
00135 $aCache[$sCatId][$sActIdent] = oxDb::getDb()->getOne( $sQ );
00136
00137 $this->_setCatCache( $aCache );
00138 return $aCache[$sCatId][$sActIdent];
00139 }
00140
00152 public function setPriceCatArticleCount( $aCache, $sCatId, $sActIdent, $dPriceFrom, $dPriceTo )
00153 {
00154 $oArticle = oxNew( 'oxarticle' );
00155 $sTable = $oArticle->getViewName();
00156
00157 $sSubSelect = "select if(oxparentid='',oxid,oxparentid) as id from oxarticles where oxprice >0 ";
00158 $sSubSelect .= $dPriceTo?"and oxprice <= $dPriceTo ":" ";
00159 $sSubSelect .= "group by id having ";
00160 $sSubSelect .= $dPriceFrom?"min(oxprice) >= $dPriceFrom ":" ";
00161
00162 $sSelect = "select count($sTable.oxid) from $sTable where ";
00163 $sSelect .= "$sTable.oxid in ($sSubSelect) ";
00164 $sSelect .= "and $sTable.oxissearch = 1 and ".$oArticle->getSqlActiveSnippet();
00165
00166 $aCache[$sCatId][$sActIdent] = oxDb::getDb()->getOne( $sSelect );
00167
00168 $this->_setCatCache( $aCache );
00169 return $aCache[$sCatId][$sActIdent];
00170 }
00171
00181 public function setVendorArticleCount( $aCache, $sCatId, $sActIdent )
00182 {
00183
00184 if ( $sCatId == 'root' ) {
00185 return 0;
00186 }
00187
00188 $oArticle = oxNew( 'oxarticle' );
00189 $sTable = $oArticle->getViewName();
00190
00191
00192 $sQ = "select $sTable.oxvendorid AS vendorId, count(*) from $sTable where ";
00193 $sQ .= "$sTable.oxvendorid <> '' and ".$oArticle->getSqlActiveSnippet()." group by $sTable.oxvendorid ";
00194 $aDbResult = oxDb::getDb()->getAssoc( $sQ );
00195
00196 foreach ( $aDbResult as $sKey => $sValue ) {
00197 $aCache[$sKey][$sActIdent] = $sValue;
00198 }
00199
00200 $this->_setVendorCache( $aCache );
00201 return $aCache[$sCatId][$sActIdent];
00202 }
00203
00211 public function resetCatArticleCount( $sCatId = null )
00212 {
00213 if ( !$sCatId ) {
00214 $this->getConfig()->setGlobalParameter( 'aLocalCatCache', null );
00215 } else {
00216 $aCatData = $this->_getCatCache();
00217 if ( isset( $aCatData[$sCatId] ) ) {
00218 unset( $aCatData[$sCatId] );
00219 $this->_setCatCache( $aCatData );
00220 }
00221 }
00222
00223 }
00224
00232 public function resetPriceCatArticleCount( $iPrice )
00233 {
00234
00235 if ( $aCatData = $this->_getCatCache() ) {
00236
00237 $sTable = getViewName( 'oxcategories' );
00238 $sSelect = "select $sTable.oxid from $sTable where '$iPrice' >= $sTable.oxpricefrom and '$iPrice' <= $sTable.oxpriceto ";
00239
00240 $rs = oxDb::getDb()->execute( $sSelect );
00241 if ( $rs != false && $rs->recordCount() > 0 ) {
00242 while ( !$rs->EOF ) {
00243 if ( isset( $aCatData[$rs->fields[0]] ) ) {
00244 unset( $aCatData[$rs->fields[0]] );
00245 }
00246 $rs->moveNext();
00247 }
00248
00249
00250 $this->_setCatCache( $aCatData );
00251 }
00252
00253 }
00254 }
00255
00264 public function getTagArticleCount( $sTag, $iLang )
00265 {
00266 $sLangExt = oxLang::getInstance()->getLanguageTag( $iLang );
00267
00268 $oArticle = oxNew("oxarticle");
00269 $sArticleTable = $oArticle->getViewName();
00270 $sActiveSnippet = $oArticle->getSqlActiveSnippet();
00271
00272 $sQ = "select count(*) from oxartextends inner join $sArticleTable
00273 on $sArticleTable.oxid = oxartextends.oxid where $sActiveSnippet
00274 and match(oxartextends.oxtags$sLangExt)
00275 against ( ".oxDb::getDb()->Quote( $sTag )." ) ";
00276
00277 return oxDb::getDb()->getOne( $sQ );
00278 }
00279
00287 public function resetVendorArticleCount( $sVendorId = null )
00288 {
00289 if ( !$sVendorId ) {
00290 $this->getConfig()->setGlobalParameter( 'aLocalVendorCache', null );
00291 oxUtils::getInstance()->toFileCache( 'aLocalVendorCache', '' );
00292 } else {
00293 $aVendorData = $this->_getVendorCache();
00294 if ( isset( $aVendorData[$sVendorId] ) ) {
00295 unset( $aVendorData[$sVendorId] );
00296 $this->_setVendorCache( $aVendorData );
00297 }
00298 }
00299
00300 }
00301
00307 protected function _getCatCache()
00308 {
00309 $myConfig = $this->getConfig();
00310
00311
00312 $aLocalCatCache = $myConfig->getGlobalParameter( 'aLocalCatCache' );
00313
00314
00315 if ( !$aLocalCatCache ) {
00316 $sLocalCatCache = oxUtils::getInstance()->fromFileCache( 'aLocalCatCache');
00317 if ( $sLocalCatCache ) {
00318 $aLocalCatCache = unserialize( $sLocalCatCache );
00319 } else {
00320 $aLocalCatCache = null;
00321 }
00322 $myConfig->setGlobalParameter( 'aLocalCatCache', $aLocalCatCache );
00323 }
00324 return $aLocalCatCache;
00325 }
00326
00334 protected function _setCatCache( $aCache )
00335 {
00336 $this->getConfig()->setGlobalParameter( 'aLocalCatCache', $aCache );
00337 oxUtils::getInstance()->toFileCache( 'aLocalCatCache', serialize( $aCache ) );
00338 }
00339
00347 protected function _setVendorCache( $aCache )
00348 {
00349 $this->getConfig()->setGlobalParameter( 'aLocalVendorCache', $aCache );
00350 oxUtils::getInstance()->toFileCache( 'aLocalVendorCache', serialize( $aCache ) );
00351 }
00352
00358 protected function _getVendorCache()
00359 {
00360 $myConfig = $this->getConfig();
00361
00362
00363 $aLocalVendorCache = $myConfig->getGlobalParameter('aLocalVendorCache');
00364
00365 if ( !$aLocalVendorCache ) {
00366 $sLocalVendorCache = oxUtils::getInstance()->fromFileCache( 'aLocalVendorCache' );
00367 if ( $sLocalVendorCache ) {
00368 $aLocalVendorCache = unserialize( $sLocalVendorCache );
00369 } else {
00370 $aLocalVendorCache = null;
00371 }
00372 $myConfig->setGlobalParameter( 'aLocalVendorCache', $aLocalVendorCache );
00373 }
00374 return $aLocalVendorCache;
00375 }
00376
00384 protected function _getUserViewId( $blReset = false )
00385 {
00386 if ( $this->_sUserViewId != null && !$blReset ) {
00387 return $this->_sUserViewId;
00388 }
00389
00390
00391 $aRRIdx = null;
00392
00393 $this->_sUserViewId = md5($this->getConfig()->getShopID().oxLang::getInstance()->getLanguageTag().serialize($aRRIdx).(int) $this->isAdmin() );
00394 return $this->_sUserViewId;
00395 }
00396
00397 }