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 {$sArticleTable}.oxissearch = 1
00331                and match(oxartextends.oxtags{$sLangExt})
00332                against ( ".$oDb->quote( $sTag )." ) ";
00333 
00334         return $oDb->getOne( $sQ );
00335     }
00336 
00344     public function resetVendorArticleCount( $sVendorId = null )
00345     {
00346         if ( !$sVendorId ) {
00347             $this->getConfig()->setGlobalParameter( 'aLocalVendorCache', null );
00348             oxUtils::getInstance()->toFileCache( 'aLocalVendorCache', '' );
00349         } else { // loading from cache
00350             $aVendorData = $this->_getVendorCache();
00351             if ( isset( $aVendorData[$sVendorId] ) ) {
00352                 unset( $aVendorData[$sVendorId] );
00353                 $this->_setVendorCache( $aVendorData );
00354             }
00355         }
00356 
00357     }
00358 
00366     public function resetManufacturerArticleCount( $sManufacturerId = null )
00367     {
00368         if ( !$sManufacturerId ) {
00369             $this->getConfig()->setGlobalParameter( 'aLocalManufacturerCache', null );
00370             oxUtils::getInstance()->toFileCache( 'aLocalManufacturerCache', '' );
00371         } else { // loading from cache
00372             $aManufacturerData = $this->_getManufacturerCache();
00373             if ( isset( $aManufacturerData[$sManufacturerId] ) ) {
00374                 unset( $aManufacturerData[$sManufacturerId] );
00375                 $this->_setManufacturerCache( $aManufacturerData );
00376             }
00377         }
00378 
00379     }
00380 
00386     protected function _getCatCache()
00387     {
00388         $myConfig = $this->getConfig();
00389 
00390         // first look at the local cache
00391         $aLocalCatCache = $myConfig->getGlobalParameter( 'aLocalCatCache' );
00392 
00393         // if local cache is not set - loading from file cache
00394         if ( !$aLocalCatCache ) {
00395             $sLocalCatCache = oxUtils::getInstance()->fromFileCache( 'aLocalCatCache');
00396             if ( $sLocalCatCache ) {
00397                 $aLocalCatCache = unserialize( $sLocalCatCache );
00398             } else {
00399                 $aLocalCatCache = null;
00400             }
00401             $myConfig->setGlobalParameter( 'aLocalCatCache', $aLocalCatCache );
00402         }
00403         return $aLocalCatCache;
00404     }
00405 
00413     protected function _setCatCache( $aCache )
00414     {
00415         $this->getConfig()->setGlobalParameter( 'aLocalCatCache', $aCache );
00416         oxUtils::getInstance()->toFileCache( 'aLocalCatCache', serialize( $aCache ) );
00417     }
00418 
00426     protected function _setVendorCache( $aCache )
00427     {
00428         $this->getConfig()->setGlobalParameter( 'aLocalVendorCache', $aCache );
00429         oxUtils::getInstance()->toFileCache( 'aLocalVendorCache', serialize( $aCache ) );
00430     }
00431 
00439     protected function _setManufacturerCache( $aCache )
00440     {
00441         $this->getConfig()->setGlobalParameter( 'aLocalManufacturerCache', $aCache );
00442         oxUtils::getInstance()->toFileCache( 'aLocalManufacturerCache', serialize( $aCache ) );
00443     }
00444 
00450     protected function _getVendorCache()
00451     {
00452         $myConfig = $this->getConfig();
00453 
00454         // first look at the local cache
00455         $aLocalVendorCache = $myConfig->getGlobalParameter('aLocalVendorCache');
00456         // if local cache is not set - loading from file cache
00457         if ( !$aLocalVendorCache ) {
00458             $sLocalVendorCache = oxUtils::getInstance()->fromFileCache( 'aLocalVendorCache' );
00459             if ( $sLocalVendorCache ) {
00460                 $aLocalVendorCache = unserialize( $sLocalVendorCache );
00461             } else {
00462                 $aLocalVendorCache = null;
00463             }
00464             $myConfig->setGlobalParameter( 'aLocalVendorCache', $aLocalVendorCache );
00465         }
00466         return $aLocalVendorCache;
00467     }
00468 
00474     protected function _getManufacturerCache()
00475     {
00476         $myConfig = $this->getConfig();
00477 
00478         // first look at the local cache
00479         $aLocalManufacturerCache = $myConfig->getGlobalParameter('aLocalManufacturerCache');
00480         // if local cache is not set - loading from file cache
00481         if ( !$aLocalManufacturerCache ) {
00482             $sLocalManufacturerCache = oxUtils::getInstance()->fromFileCache( 'aLocalManufacturerCache' );
00483             if ( $sLocalManufacturerCache ) {
00484                 $aLocalManufacturerCache = unserialize( $sLocalManufacturerCache );
00485             } else {
00486                 $aLocalManufacturerCache = null;
00487             }
00488             $myConfig->setGlobalParameter( 'aLocalManufacturerCache', $aLocalManufacturerCache );
00489         }
00490         return $aLocalManufacturerCache;
00491     }
00492 
00500     protected function _getUserViewId( $blReset = false )
00501     {
00502         if ( $this->_sUserViewId != null && !$blReset ) {
00503             return $this->_sUserViewId;
00504         }
00505 
00506         // loading R&R data from session
00507         $aRRIdx = null;
00508 
00509         $this->_sUserViewId = md5($this->getConfig()->getShopID().oxLang::getInstance()->getLanguageTag().serialize($aRRIdx).(int) $this->isAdmin() );
00510         return $this->_sUserViewId;
00511     }
00512 
00513 }

Generated on Tue Aug 18 09:21:06 2009 for OXID eShop CE by  doxygen 1.5.5