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             self::$_instance = modInstances::getMod( __CLASS__ );
00032         }
00033 
00034         if ( !self::$_instance instanceof oxUtilsCount ) {
00035 
00036             self::$_instance = oxNew( 'oxUtilsCount' );
00037             if ( defined( 'OXID_PHP_UNIT' ) ) {
00038                 modInstances::addMod( __CLASS__, self::$_instance);
00039             }
00040         }
00041         return self::$_instance;
00042     }
00043 
00051     public function getCatArticleCount( $sCatId )
00052     {
00053         // current status unique ident
00054         $sActIdent = $this->_getUserViewId();
00055 
00056         // loading from cache
00057         $aCatData = $this->_getCatCache();
00058 
00059         if ( !$aCatData || !isset( $aCatData[$sCatId][$sActIdent] ) ) {
00060             $iCnt = $this->setCatArticleCount( $aCatData, $sCatId, $sActIdent );
00061         } else {
00062             $iCnt = $aCatData[$sCatId][$sActIdent];
00063         }
00064         return $iCnt;
00065     }
00066 
00076     public function getPriceCatArticleCount( $sCatId, $dPriceFrom, $dPriceTo )
00077     {
00078         // current status unique ident
00079         $sActIdent = $this->_getUserViewId();
00080 
00081         // loading from cache
00082         $aCatData = $this->_getCatCache();
00083 
00084         if ( !$aCatData || !isset( $aCatData[$sCatId][$sActIdent] ) ) {
00085             $iCnt = $this->setPriceCatArticleCount( $aCatData, $sCatId, $sActIdent, $dPriceFrom, $dPriceTo );
00086         } else {
00087             $iCnt = $aCatData[$sCatId][$sActIdent];
00088         }
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 $sO2CView LEFT JOIN $sTable 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         $sSelect  = "select count({$sTable}.oxid) from {$sTable} where oxvarminprice >= 0 ";
00185         $sSelect .= $dPriceTo ? "and oxvarminprice <= " . (double)$dPriceTo . " " : " ";
00186         $sSelect .= $dPriceFrom ? "and oxvarminprice  >= " . (double)$dPriceFrom . " " : " ";
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 $sTable.oxparentid = '' 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         $sArtTable   = $oArticle->getViewName();
00245         $sManTable = getViewName('oxmanufacturers');
00246 
00247         // select each Manufacturer articles count
00248         $sQ = "select oxmanufacturers.oxid, count($sArtTable.oxid) from $sManTable as oxmanufacturers left outer join $sArtTable on $sArtTable.oxmanufacturerid=oxmanufacturers.oxid and $sArtTable.oxparentid = '' and ".$oArticle->getSqlActiveSnippet()." group by oxmanufacturers.oxid";
00249         $aDbResult = oxDb::getDb()->getAssoc( $sQ );
00250 
00251         foreach ( $aDbResult as $sKey => $sValue ) {
00252             $aCache[$sKey][$sActIdent] = (int) $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 {
00271             // loading from cache
00272             $aCatData = $this->_getCatCache();
00273             if ( isset( $aCatData[$sCatId] ) ) {
00274                 unset( $aCatData[$sCatId] );
00275                 $this->_setCatCache( $aCatData );
00276             }
00277         }
00278 
00279     }
00280 
00288     public function resetPriceCatArticleCount( $iPrice )
00289     {
00290         // loading from cache
00291         if ( $aCatData = $this->_getCatCache() ) {
00292 
00293             $sTable  = getViewName( 'oxcategories' );
00294             $sSelect = "select $sTable.oxid from $sTable where " . (double)$iPrice . " >= $sTable.oxpricefrom and " . (double)$iPrice . " <= $sTable.oxpriceto ";
00295 
00296             $rs = oxDb::getDb()->execute( $sSelect );
00297             if ( $rs != false && $rs->recordCount() > 0 ) {
00298                 while ( !$rs->EOF ) {
00299                     if ( isset( $aCatData[$rs->fields[0]] ) ) {
00300                         unset( $aCatData[$rs->fields[0]] );
00301                     }
00302                     $rs->moveNext();
00303                 }
00304 
00305                 // writing back to cache
00306                 $this->_setCatCache( $aCatData );
00307             }
00308 
00309         }
00310     }
00311 
00320     public function getTagArticleCount( $sTag, $iLang )
00321     {
00322         $oDb = oxDb::getDb();
00323 
00324         $oArticle = oxNew("oxarticle");
00325         $sArticleTable  = $oArticle->getViewName();
00326         $sActiveSnippet = $oArticle->getSqlActiveSnippet();
00327         $sViewName = getViewName( 'oxartextends', $iLang );
00328 
00329         $sQ = "select count(*) from {$sViewName} inner join {$sArticleTable}
00330                on {$sArticleTable}.oxid = {$sViewName}.oxid where {$sActiveSnippet}
00331                and {$sArticleTable}.oxissearch = 1 and match( {$sViewName}.oxtags )
00332                against ( ".$oDb->quote( $sTag )." IN BOOLEAN MODE ) ";
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 {
00350             // loading from cache
00351             $aVendorData = $this->_getVendorCache();
00352             if ( isset( $aVendorData[$sVendorId] ) ) {
00353                 unset( $aVendorData[$sVendorId] );
00354                 $this->_setVendorCache( $aVendorData );
00355             }
00356         }
00357 
00358     }
00359 
00367     public function resetManufacturerArticleCount( $sManufacturerId = null )
00368     {
00369         if ( !$sManufacturerId ) {
00370             $this->getConfig()->setGlobalParameter( 'aLocalManufacturerCache', null );
00371             oxUtils::getInstance()->toFileCache( 'aLocalManufacturerCache', '' );
00372         } else {
00373             // loading from cache
00374             $aManufacturerData = $this->_getManufacturerCache();
00375             if ( isset( $aManufacturerData[$sManufacturerId] ) ) {
00376                 unset( $aManufacturerData[$sManufacturerId] );
00377                 $this->_setManufacturerCache( $aManufacturerData );
00378             }
00379         }
00380 
00381     }
00382 
00388     protected function _getCatCache()
00389     {
00390         $myConfig = $this->getConfig();
00391 
00392         // first look at the local cache
00393         $aLocalCatCache = $myConfig->getGlobalParameter( 'aLocalCatCache' );
00394 
00395         // if local cache is not set - loading from file cache
00396         if ( !$aLocalCatCache ) {
00397             $sLocalCatCache = oxUtils::getInstance()->fromFileCache( 'aLocalCatCache');
00398             if ( $sLocalCatCache ) {
00399                 $aLocalCatCache = $sLocalCatCache;
00400             } else {
00401                 $aLocalCatCache = null;
00402             }
00403             $myConfig->setGlobalParameter( 'aLocalCatCache', $aLocalCatCache );
00404         }
00405         return $aLocalCatCache;
00406     }
00407 
00415     protected function _setCatCache( $aCache )
00416     {
00417         $this->getConfig()->setGlobalParameter( 'aLocalCatCache', $aCache );
00418         oxUtils::getInstance()->toFileCache( 'aLocalCatCache', $aCache );
00419     }
00420 
00428     protected function _setVendorCache( $aCache )
00429     {
00430         $this->getConfig()->setGlobalParameter( 'aLocalVendorCache', $aCache );
00431         oxUtils::getInstance()->toFileCache( 'aLocalVendorCache', $aCache );
00432     }
00433 
00441     protected function _setManufacturerCache( $aCache )
00442     {
00443         $this->getConfig()->setGlobalParameter( 'aLocalManufacturerCache', $aCache );
00444         oxUtils::getInstance()->toFileCache( 'aLocalManufacturerCache', $aCache );
00445     }
00446 
00452     protected function _getVendorCache()
00453     {
00454         $myConfig = $this->getConfig();
00455 
00456         // first look at the local cache
00457         $aLocalVendorCache = $myConfig->getGlobalParameter('aLocalVendorCache');
00458         // if local cache is not set - loading from file cache
00459         if ( !$aLocalVendorCache ) {
00460             $sLocalVendorCache = oxUtils::getInstance()->fromFileCache( 'aLocalVendorCache' );
00461             if ( $sLocalVendorCache ) {
00462                 $aLocalVendorCache = $sLocalVendorCache;
00463             } else {
00464                 $aLocalVendorCache = null;
00465             }
00466             $myConfig->setGlobalParameter( 'aLocalVendorCache', $aLocalVendorCache );
00467         }
00468         return $aLocalVendorCache;
00469     }
00470 
00476     protected function _getManufacturerCache()
00477     {
00478         $myConfig = $this->getConfig();
00479 
00480         // first look at the local cache
00481         $aLocalManufacturerCache = $myConfig->getGlobalParameter('aLocalManufacturerCache');
00482         // if local cache is not set - loading from file cache
00483         if ( !$aLocalManufacturerCache ) {
00484             $sLocalManufacturerCache = oxUtils::getInstance()->fromFileCache( 'aLocalManufacturerCache' );
00485             if ( $sLocalManufacturerCache ) {
00486                 $aLocalManufacturerCache = $sLocalManufacturerCache;
00487             } else {
00488                 $aLocalManufacturerCache = null;
00489             }
00490             $myConfig->setGlobalParameter( 'aLocalManufacturerCache', $aLocalManufacturerCache );
00491         }
00492         return $aLocalManufacturerCache;
00493     }
00494 
00502     protected function _getUserViewId( $blReset = false )
00503     {
00504         if ( $this->_sUserViewId != null && !$blReset ) {
00505             return $this->_sUserViewId;
00506         }
00507 
00508         // loading R&R data from session
00509         $aRRIdx = null;
00510 
00511         $this->_sUserViewId = md5($this->getConfig()->getShopID().oxLang::getInstance()->getLanguageTag().serialize($aRRIdx).(int) $this->isAdmin() );
00512         return $this->_sUserViewId;
00513     }
00514 
00515 }