oxutilscount.php

Go to the documentation of this file.
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         // disable caching for test modules
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         // current status unique ident
00055         $sActIdent = $this->_getUserViewId();
00056 
00057         // loading from cache
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         // current status unique ident
00080         $sActIdent = $this->_getUserViewId();
00081 
00082         // loading from cache
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         // current category unique ident
00103         $sActIdent = $this->_getUserViewId();
00104 
00105         // loading from cache
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         // current category unique ident
00126         $sActIdent = $this->_getUserViewId();
00127 
00128         // loading from cache
00129         $aManufacturerData = $this->_getManufacturerCache();
00130 
00131         if ( !$aManufacturerData || !isset( $aManufacturerData[$sManufacturerId][$sActIdent] ) ) {
00132             $iCnt = $this->setManufacturerArticleCount( $aManufacturerData, $sManufacturerId, $sActIdent );
00133         } else {
00134             $iCnt = $aManufacturerData[$sManufacturerId][$sActIdent];
00135         }
00136         return $iCnt;
00137     }
00138 
00148     public function setCatArticleCount( $aCache, $sCatId, $sActIdent )
00149     {
00150         $oArticle = oxNew( 'oxarticle' );
00151         $sTable   = $oArticle->getViewName();
00152         $sO2CView = getViewName( 'oxobject2category' );
00153 
00154         // we use distinct if article is assigned to category twice
00155         $sQ  = "select count(*) from (select distinct oxobject2category.oxcatnid, oxobject2category.oxobjectid from $sO2CView as oxobject2category WHERE oxobject2category.oxcatnid = '".$sCatId."' ";
00156         $sQ .= "and oxobject2category.oxobjectid in ( SELECT oxid FROM $sTable where ".$oArticle->getSqlActiveSnippet().") ) as ox2cat";
00157 
00158         $aCache[$sCatId][$sActIdent] = oxDb::getDb()->getOne( $sQ );
00159 
00160         $this->_setCatCache( $aCache );
00161         return $aCache[$sCatId][$sActIdent];
00162     }
00163 
00175     public function setPriceCatArticleCount( $aCache, $sCatId, $sActIdent, $dPriceFrom, $dPriceTo )
00176     {
00177         $oArticle = oxNew( 'oxarticle' );
00178         $sTable   = $oArticle->getViewName();
00179 
00180         $sSubSelect  = "select if(oxparentid='',oxid,oxparentid) as id from oxarticles where oxprice >0 ";
00181         $sSubSelect .= $dPriceTo?"and oxprice <= $dPriceTo ":" ";
00182         $sSubSelect .= "group by id having ";
00183         $sSubSelect .= $dPriceFrom?"min(oxprice) >= $dPriceFrom ":" ";
00184 
00185         $sSelect  = "select count($sTable.oxid) from $sTable where ";
00186         $sSelect .= "$sTable.oxid in ($sSubSelect) ";
00187         $sSelect .= "and $sTable.oxissearch = 1 and ".$oArticle->getSqlActiveSnippet();
00188 
00189         $aCache[$sCatId][$sActIdent] = oxDb::getDb()->getOne( $sSelect );
00190 
00191         $this->_setCatCache( $aCache );
00192         return $aCache[$sCatId][$sActIdent];
00193     }
00194 
00204     public function setVendorArticleCount( $aCache, $sCatId, $sActIdent )
00205     {
00206         // if vendor/category name is 'root', skip counting
00207         if ( $sCatId == 'root' ) {
00208             return 0;
00209         }
00210 
00211         $oArticle = oxNew( 'oxarticle' );
00212         $sTable   = $oArticle->getViewName();
00213 
00214         // select each vendor articles count
00215         $sQ  = "select $sTable.oxvendorid AS vendorId, count(*) from $sTable where ";
00216         $sQ .= "$sTable.oxvendorid <> '' and ".$oArticle->getSqlActiveSnippet()." group by $sTable.oxvendorid ";
00217         $aDbResult = oxDb::getDb()->getAssoc( $sQ );
00218 
00219         foreach ( $aDbResult as $sKey => $sValue ) {
00220             $aCache[$sKey][$sActIdent] = $sValue;
00221         }
00222 
00223         $this->_setVendorCache( $aCache );
00224         return $aCache[$sCatId][$sActIdent];
00225     }
00226 
00236     public function setManufacturerArticleCount( $aCache, $sCatId, $sActIdent )
00237     {
00238         // if Manufacturer/category name is 'root', skip counting
00239         if ( $sCatId == 'root' ) {
00240             return 0;
00241         }
00242 
00243         $oArticle = oxNew( 'oxarticle' );
00244         $sTable   = $oArticle->getViewName();
00245 
00246         // select each Manufacturer articles count
00247         $sQ  = "select $sTable.oxmanufacturerid AS manufacturerId, count(*) from $sTable where ";
00248         $sQ .= "$sTable.oxmanufacturerid <> '' and ".$oArticle->getSqlActiveSnippet()." group by $sTable.oxmanufacturerid ";
00249         $aDbResult = oxDb::getDb()->getAssoc( $sQ );
00250 
00251         foreach ( $aDbResult as $sKey => $sValue ) {
00252             $aCache[$sKey][$sActIdent] = $sValue;
00253         }
00254 
00255         $this->_setManufacturerCache( $aCache );
00256         return $aCache[$sCatId][$sActIdent];
00257     }
00258 
00266     public function resetCatArticleCount( $sCatId = null )
00267     {
00268         if ( !$sCatId ) {
00269             $this->getConfig()->setGlobalParameter( 'aLocalCatCache', null );
00270         } else {   // loading from cache
00271             $aCatData = $this->_getCatCache();
00272             if ( isset( $aCatData[$sCatId] ) ) {
00273                 unset( $aCatData[$sCatId] );
00274                 $this->_setCatCache( $aCatData );
00275             }
00276         }
00277 
00278     }
00279 
00287     public function resetPriceCatArticleCount( $iPrice )
00288     {
00289         // loading from cache
00290         if ( $aCatData = $this->_getCatCache() ) {
00291 
00292             $sTable  = getViewName( 'oxcategories' );
00293             $sSelect = "select $sTable.oxid from $sTable where '$iPrice' >= $sTable.oxpricefrom and '$iPrice' <= $sTable.oxpriceto ";
00294 
00295             $rs = oxDb::getDb()->execute( $sSelect );
00296             if ( $rs != false && $rs->recordCount() > 0 ) {
00297                 while ( !$rs->EOF ) {
00298                     if ( isset( $aCatData[$rs->fields[0]] ) ) {
00299                         unset( $aCatData[$rs->fields[0]] );
00300                     }
00301                     $rs->moveNext();
00302                 }
00303 
00304                 // writing back to cache
00305                 $this->_setCatCache( $aCatData );
00306             }
00307 
00308         }
00309     }
00310 
00319     public function getTagArticleCount( $sTag, $iLang )
00320     {
00321         $sLangExt = oxLang::getInstance()->getLanguageTag( $iLang );
00322         $oDb = oxDb::getDb();
00323 
00324         $oArticle = oxNew("oxarticle");
00325         $sArticleTable  = $oArticle->getViewName();
00326         $sActiveSnippet = $oArticle->getSqlActiveSnippet();
00327 
00328         $sQ = "select count(*) from oxartextends inner join $sArticleTable
00329                on $sArticleTable.oxid = oxartextends.oxid where $sActiveSnippet
00330                and match(oxartextends.oxtags$sLangExt)
00331                against ( ".$oDb->quote( $sTag )." ) ";
00332 
00333         return $oDb->getOne( $sQ );
00334     }
00335 
00343     public function resetVendorArticleCount( $sVendorId = null )
00344     {
00345         if ( !$sVendorId ) {
00346             $this->getConfig()->setGlobalParameter( 'aLocalVendorCache', null );
00347             oxUtils::getInstance()->toFileCache( 'aLocalVendorCache', '' );
00348         } else { // loading from cache
00349             $aVendorData = $this->_getVendorCache();
00350             if ( isset( $aVendorData[$sVendorId] ) ) {
00351                 unset( $aVendorData[$sVendorId] );
00352                 $this->_setVendorCache( $aVendorData );
00353             }
00354         }
00355 
00356     }
00357 
00365     public function resetManufacturerArticleCount( $sManufacturerId = null )
00366     {
00367         if ( !$sManufacturerId ) {
00368             $this->getConfig()->setGlobalParameter( 'aLocalManufacturerCache', null );
00369             oxUtils::getInstance()->toFileCache( 'aLocalManufacturerCache', '' );
00370         } else { // loading from cache
00371             $aManufacturerData = $this->_getManufacturerCache();
00372             if ( isset( $aManufacturerData[$sManufacturerId] ) ) {
00373                 unset( $aManufacturerData[$sManufacturerId] );
00374                 $this->_setManufacturerCache( $aManufacturerData );
00375             }
00376         }
00377 
00378     }
00379 
00385     protected function _getCatCache()
00386     {
00387         $myConfig = $this->getConfig();
00388 
00389         // first look at the local cache
00390         $aLocalCatCache = $myConfig->getGlobalParameter( 'aLocalCatCache' );
00391 
00392         // if local cache is not set - loading from file cache
00393         if ( !$aLocalCatCache ) {
00394             $sLocalCatCache = oxUtils::getInstance()->fromFileCache( 'aLocalCatCache');
00395             if ( $sLocalCatCache ) {
00396                 $aLocalCatCache = unserialize( $sLocalCatCache );
00397             } else {
00398                 $aLocalCatCache = null;
00399             }
00400             $myConfig->setGlobalParameter( 'aLocalCatCache', $aLocalCatCache );
00401         }
00402         return $aLocalCatCache;
00403     }
00404 
00412     protected function _setCatCache( $aCache )
00413     {
00414         $this->getConfig()->setGlobalParameter( 'aLocalCatCache', $aCache );
00415         oxUtils::getInstance()->toFileCache( 'aLocalCatCache', serialize( $aCache ) );
00416     }
00417 
00425     protected function _setVendorCache( $aCache )
00426     {
00427         $this->getConfig()->setGlobalParameter( 'aLocalVendorCache', $aCache );
00428         oxUtils::getInstance()->toFileCache( 'aLocalVendorCache', serialize( $aCache ) );
00429     }
00430 
00438     protected function _setManufacturerCache( $aCache )
00439     {
00440         $this->getConfig()->setGlobalParameter( 'aLocalManufacturerCache', $aCache );
00441         oxUtils::getInstance()->toFileCache( 'aLocalManufacturerCache', serialize( $aCache ) );
00442     }
00443 
00449     protected function _getVendorCache()
00450     {
00451         $myConfig = $this->getConfig();
00452 
00453         // first look at the local cache
00454         $aLocalVendorCache = $myConfig->getGlobalParameter('aLocalVendorCache');
00455         // if local cache is not set - loading from file cache
00456         if ( !$aLocalVendorCache ) {
00457             $sLocalVendorCache = oxUtils::getInstance()->fromFileCache( 'aLocalVendorCache' );
00458             if ( $sLocalVendorCache ) {
00459                 $aLocalVendorCache = unserialize( $sLocalVendorCache );
00460             } else {
00461                 $aLocalVendorCache = null;
00462             }
00463             $myConfig->setGlobalParameter( 'aLocalVendorCache', $aLocalVendorCache );
00464         }
00465         return $aLocalVendorCache;
00466     }
00467 
00473     protected function _getManufacturerCache()
00474     {
00475         $myConfig = $this->getConfig();
00476 
00477         // first look at the local cache
00478         $aLocalManufacturerCache = $myConfig->getGlobalParameter('aLocalManufacturerCache');
00479         // if local cache is not set - loading from file cache
00480         if ( !$aLocalManufacturerCache ) {
00481             $sLocalManufacturerCache = oxUtils::getInstance()->fromFileCache( 'aLocalManufacturerCache' );
00482             if ( $sLocalManufacturerCache ) {
00483                 $aLocalManufacturerCache = unserialize( $sLocalManufacturerCache );
00484             } else {
00485                 $aLocalManufacturerCache = null;
00486             }
00487             $myConfig->setGlobalParameter( 'aLocalManufacturerCache', $aLocalManufacturerCache );
00488         }
00489         return $aLocalManufacturerCache;
00490     }
00491 
00499     protected function _getUserViewId( $blReset = false )
00500     {
00501         if ( $this->_sUserViewId != null && !$blReset ) {
00502             return $this->_sUserViewId;
00503         }
00504 
00505         // loading R&R data from session
00506         $aRRIdx = null;
00507 
00508         $this->_sUserViewId = md5($this->getConfig()->getShopID().oxLang::getInstance()->getLanguageTag().serialize($aRRIdx).(int) $this->isAdmin() );
00509         return $this->_sUserViewId;
00510     }
00511 
00512 }

Generated on Wed Apr 22 12:26:31 2009 for OXID eShop CE by  doxygen 1.5.5