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