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         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         // we use distinct if article is assigned to category twice
00154         $sQ = "SELECT count(*) FROM (
00155                    SELECT count(*) FROM $sTable LEFT JOIN $sO2CView ON $sO2CView.oxobjectid=$sTable.oxid
00156                        WHERE $sO2CView.oxcatnid = '".$sCatId."' AND
00157                              $sTable.oxparentid='' AND
00158                              ".$oArticle->getSqlActiveSnippet() ."
00159                        GROUP BY $sTable.oxid
00160                    ) AS ox2cat";
00161 
00162         $aCache[$sCatId][$sActIdent] = oxDb::getDb()->getOne( $sQ );
00163 
00164         $this->_setCatCache( $aCache );
00165         return $aCache[$sCatId][$sActIdent];
00166     }
00167 
00179     public function setPriceCatArticleCount( $aCache, $sCatId, $sActIdent, $dPriceFrom, $dPriceTo )
00180     {
00181         $oArticle = oxNew( 'oxarticle' );
00182         $sTable   = $oArticle->getViewName();
00183 
00184         $sSubSelect  = "select if( oxparentid='', oxid, oxparentid ) as id from {$sTable} where oxprice >= 0 ";
00185         $sSubSelect .= $dPriceTo ? "and oxprice <= " . (double)$dPriceTo . " " : " ";
00186         $sSubSelect .= $dPriceFrom ? "group by id having min( oxprice ) >= " . (double)$dPriceFrom . " " : " ";
00187 
00188         $sSelect  = "select count({$sTable}.oxid) from {$sTable} where ";
00189         $sSelect .= "{$sTable}.oxid in ($sSubSelect) ";
00190         $sSelect .= "and {$sTable}.oxissearch = 1 and ".$oArticle->getSqlActiveSnippet();
00191 
00192         $aCache[$sCatId][$sActIdent] = oxDb::getDb()->getOne( $sSelect );
00193 
00194         $this->_setCatCache( $aCache );
00195         return $aCache[$sCatId][$sActIdent];
00196     }
00197 
00207     public function setVendorArticleCount( $aCache, $sCatId, $sActIdent )
00208     {
00209         // if vendor/category name is 'root', skip counting
00210         if ( $sCatId == 'root' ) {
00211             return 0;
00212         }
00213 
00214         $oArticle = oxNew( 'oxarticle' );
00215         $sTable   = $oArticle->getViewName();
00216 
00217         // select each vendor articles count
00218         $sQ  = "select $sTable.oxvendorid AS vendorId, count(*) from $sTable where ";
00219         $sQ .= "$sTable.oxvendorid <> '' and $sTable.oxparentid = '' and ".$oArticle->getSqlActiveSnippet()." group by $sTable.oxvendorid ";
00220         $aDbResult = oxDb::getDb()->getAssoc( $sQ );
00221 
00222         foreach ( $aDbResult as $sKey => $sValue ) {
00223             $aCache[$sKey][$sActIdent] = $sValue;
00224         }
00225 
00226         $this->_setVendorCache( $aCache );
00227         return $aCache[$sCatId][$sActIdent];
00228     }
00229 
00239     public function setManufacturerArticleCount( $aCache, $sCatId, $sActIdent )
00240     {
00241         // if Manufacturer/category name is 'root', skip counting
00242         if ( $sCatId == 'root' ) {
00243             return 0;
00244         }
00245 
00246         $oArticle = oxNew( 'oxarticle' );
00247         $sTable   = $oArticle->getViewName();
00248 
00249         // select each Manufacturer articles count
00250         $sQ  = "select $sTable.oxmanufacturerid AS manufacturerId, count(*) from $sTable where ";
00251         $sQ .= "$sTable.oxmanufacturerid <> '' and $sTable.oxparentid = '' and ".$oArticle->getSqlActiveSnippet()." group by $sTable.oxmanufacturerid ";
00252         $aDbResult = oxDb::getDb()->getAssoc( $sQ );
00253 
00254         foreach ( $aDbResult as $sKey => $sValue ) {
00255             $aCache[$sKey][$sActIdent] = $sValue;
00256         }
00257 
00258         $this->_setManufacturerCache( $aCache );
00259         return $aCache[$sCatId][$sActIdent];
00260     }
00261 
00269     public function resetCatArticleCount( $sCatId = null )
00270     {
00271         if ( !$sCatId ) {
00272             $this->getConfig()->setGlobalParameter( 'aLocalCatCache', null );
00273         } else {   // loading from cache
00274             $aCatData = $this->_getCatCache();
00275             if ( isset( $aCatData[$sCatId] ) ) {
00276                 unset( $aCatData[$sCatId] );
00277                 $this->_setCatCache( $aCatData );
00278             }
00279         }
00280 
00281     }
00282 
00290     public function resetPriceCatArticleCount( $iPrice )
00291     {
00292         // loading from cache
00293         if ( $aCatData = $this->_getCatCache() ) {
00294 
00295             $sTable  = getViewName( 'oxcategories' );
00296             $sSelect = "select $sTable.oxid from $sTable where " . (double)$iPrice . " >= $sTable.oxpricefrom and " . (double)$iPrice . " <= $sTable.oxpriceto ";
00297 
00298             $rs = oxDb::getDb()->execute( $sSelect );
00299             if ( $rs != false && $rs->recordCount() > 0 ) {
00300                 while ( !$rs->EOF ) {
00301                     if ( isset( $aCatData[$rs->fields[0]] ) ) {
00302                         unset( $aCatData[$rs->fields[0]] );
00303                     }
00304                     $rs->moveNext();
00305                 }
00306 
00307                 // writing back to cache
00308                 $this->_setCatCache( $aCatData );
00309             }
00310 
00311         }
00312     }
00313 
00322     public function getTagArticleCount( $sTag, $iLang )
00323     {
00324         $sLangExt = oxLang::getInstance()->getLanguageTag( $iLang );
00325         $oDb = oxDb::getDb();
00326 
00327         $oArticle = oxNew("oxarticle");
00328         $sArticleTable  = $oArticle->getViewName();
00329         $sActiveSnippet = $oArticle->getSqlActiveSnippet();
00330 
00331         $sQ = "select count(*) from oxartextends inner join {$sArticleTable}
00332                on {$sArticleTable}.oxid = oxartextends.oxid where {$sActiveSnippet}
00333                and {$sArticleTable}.oxissearch = 1
00334                and match(oxartextends.oxtags{$sLangExt})
00335                against ( ".$oDb->quote( $sTag )." ) ";
00336 
00337         return $oDb->getOne( $sQ );
00338     }
00339 
00347     public function resetVendorArticleCount( $sVendorId = null )
00348     {
00349         if ( !$sVendorId ) {
00350             $this->getConfig()->setGlobalParameter( 'aLocalVendorCache', null );
00351             oxUtils::getInstance()->toFileCache( 'aLocalVendorCache', '' );
00352         } else { // loading from cache
00353             $aVendorData = $this->_getVendorCache();
00354             if ( isset( $aVendorData[$sVendorId] ) ) {
00355                 unset( $aVendorData[$sVendorId] );
00356                 $this->_setVendorCache( $aVendorData );
00357             }
00358         }
00359 
00360     }
00361 
00369     public function resetManufacturerArticleCount( $sManufacturerId = null )
00370     {
00371         if ( !$sManufacturerId ) {
00372             $this->getConfig()->setGlobalParameter( 'aLocalManufacturerCache', null );
00373             oxUtils::getInstance()->toFileCache( 'aLocalManufacturerCache', '' );
00374         } else { // loading from cache
00375             $aManufacturerData = $this->_getManufacturerCache();
00376             if ( isset( $aManufacturerData[$sManufacturerId] ) ) {
00377                 unset( $aManufacturerData[$sManufacturerId] );
00378                 $this->_setManufacturerCache( $aManufacturerData );
00379             }
00380         }
00381 
00382     }
00383 
00389     protected function _getCatCache()
00390     {
00391         $myConfig = $this->getConfig();
00392 
00393         // first look at the local cache
00394         $aLocalCatCache = $myConfig->getGlobalParameter( 'aLocalCatCache' );
00395 
00396         // if local cache is not set - loading from file cache
00397         if ( !$aLocalCatCache ) {
00398             $sLocalCatCache = oxUtils::getInstance()->fromFileCache( 'aLocalCatCache');
00399             if ( $sLocalCatCache ) {
00400                 $aLocalCatCache = $sLocalCatCache;
00401             } else {
00402                 $aLocalCatCache = null;
00403             }
00404             $myConfig->setGlobalParameter( 'aLocalCatCache', $aLocalCatCache );
00405         }
00406         return $aLocalCatCache;
00407     }
00408 
00416     protected function _setCatCache( $aCache )
00417     {
00418         $this->getConfig()->setGlobalParameter( 'aLocalCatCache', $aCache );
00419         oxUtils::getInstance()->toFileCache( 'aLocalCatCache', $aCache );
00420     }
00421 
00429     protected function _setVendorCache( $aCache )
00430     {
00431         $this->getConfig()->setGlobalParameter( 'aLocalVendorCache', $aCache );
00432         oxUtils::getInstance()->toFileCache( 'aLocalVendorCache', $aCache );
00433     }
00434 
00442     protected function _setManufacturerCache( $aCache )
00443     {
00444         $this->getConfig()->setGlobalParameter( 'aLocalManufacturerCache', $aCache );
00445         oxUtils::getInstance()->toFileCache( 'aLocalManufacturerCache', $aCache );
00446     }
00447 
00453     protected function _getVendorCache()
00454     {
00455         $myConfig = $this->getConfig();
00456 
00457         // first look at the local cache
00458         $aLocalVendorCache = $myConfig->getGlobalParameter('aLocalVendorCache');
00459         // if local cache is not set - loading from file cache
00460         if ( !$aLocalVendorCache ) {
00461             $sLocalVendorCache = oxUtils::getInstance()->fromFileCache( 'aLocalVendorCache' );
00462             if ( $sLocalVendorCache ) {
00463                 $aLocalVendorCache = $sLocalVendorCache;
00464             } else {
00465                 $aLocalVendorCache = null;
00466             }
00467             $myConfig->setGlobalParameter( 'aLocalVendorCache', $aLocalVendorCache );
00468         }
00469         return $aLocalVendorCache;
00470     }
00471 
00477     protected function _getManufacturerCache()
00478     {
00479         $myConfig = $this->getConfig();
00480 
00481         // first look at the local cache
00482         $aLocalManufacturerCache = $myConfig->getGlobalParameter('aLocalManufacturerCache');
00483         // if local cache is not set - loading from file cache
00484         if ( !$aLocalManufacturerCache ) {
00485             $sLocalManufacturerCache = oxUtils::getInstance()->fromFileCache( 'aLocalManufacturerCache' );
00486             if ( $sLocalManufacturerCache ) {
00487                 $aLocalManufacturerCache = $sLocalManufacturerCache;
00488             } else {
00489                 $aLocalManufacturerCache = null;
00490             }
00491             $myConfig->setGlobalParameter( 'aLocalManufacturerCache', $aLocalManufacturerCache );
00492         }
00493         return $aLocalManufacturerCache;
00494     }
00495 
00503     protected function _getUserViewId( $blReset = false )
00504     {
00505         if ( $this->_sUserViewId != null && !$blReset ) {
00506             return $this->_sUserViewId;
00507         }
00508 
00509         // loading R&R data from session
00510         $aRRIdx = null;
00511 
00512         $this->_sUserViewId = md5($this->getConfig()->getShopID().oxLang::getInstance()->getLanguageTag().serialize($aRRIdx).(int) $this->isAdmin() );
00513         return $this->_sUserViewId;
00514     }
00515 
00516 }

Generated on Mon Oct 26 20:07:17 2009 for OXID eShop CE by  doxygen 1.5.5