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 
00029     public static function getInstance()
00030     {
00031         return oxRegistry::get("oxUtilsCount");
00032     }
00033 
00041     public function getCatArticleCount( $sCatId )
00042     {
00043         // current status unique ident
00044         $sActIdent = $this->_getUserViewId();
00045 
00046         // loading from cache
00047         $aCatData = $this->_getCatCache();
00048 
00049         if ( !$aCatData || !isset( $aCatData[$sCatId][$sActIdent] ) ) {
00050             $iCnt = $this->setCatArticleCount( $aCatData, $sCatId, $sActIdent );
00051         } else {
00052             $iCnt = $aCatData[$sCatId][$sActIdent];
00053         }
00054         return $iCnt;
00055     }
00056 
00066     public function getPriceCatArticleCount( $sCatId, $dPriceFrom, $dPriceTo )
00067     {
00068         // current status unique ident
00069         $sActIdent = $this->_getUserViewId();
00070 
00071         // loading from cache
00072         $aCatData = $this->_getCatCache();
00073 
00074         if ( !$aCatData || !isset( $aCatData[$sCatId][$sActIdent] ) ) {
00075             $iCnt = $this->setPriceCatArticleCount( $aCatData, $sCatId, $sActIdent, $dPriceFrom, $dPriceTo );
00076         } else {
00077             $iCnt = $aCatData[$sCatId][$sActIdent];
00078         }
00079 
00080         return $iCnt;
00081     }
00082 
00090     public function getVendorArticleCount( $sVendorId )
00091     {
00092         // current category unique ident
00093         $sActIdent = $this->_getUserViewId();
00094 
00095         // loading from cache
00096         $aVendorData = $this->_getVendorCache();
00097 
00098         if ( !$aVendorData || !isset( $aVendorData[$sVendorId][$sActIdent] ) ) {
00099             $iCnt = $this->setVendorArticleCount( $aVendorData, $sVendorId, $sActIdent );
00100         } else {
00101             $iCnt = $aVendorData[$sVendorId][$sActIdent];
00102         }
00103         return $iCnt;
00104     }
00105 
00113     public function getManufacturerArticleCount( $sManufacturerId )
00114     {
00115         // current category unique ident
00116         $sActIdent = $this->_getUserViewId();
00117 
00118         // loading from cache
00119         $aManufacturerData = $this->_getManufacturerCache();
00120         if ( !$aManufacturerData || !isset( $aManufacturerData[$sManufacturerId][$sActIdent] ) ) {
00121             $iCnt = $this->setManufacturerArticleCount( $aManufacturerData, $sManufacturerId, $sActIdent );
00122         } else {
00123             $iCnt = $aManufacturerData[$sManufacturerId][$sActIdent];
00124         }
00125         return $iCnt;
00126     }
00127 
00137     public function setCatArticleCount( $aCache, $sCatId, $sActIdent )
00138     {
00139         $oArticle = oxNew( 'oxarticle' );
00140         $sTable   = $oArticle->getViewName();
00141         $sO2CView = getViewName( 'oxobject2category' );
00142         $oDb = oxDb::getDb();
00143 
00144         // we use distinct if article is assigned to category twice
00145         $sQ = "SELECT COUNT( DISTINCT $sTable.`oxid` )
00146                FROM $sO2CView
00147                    INNER JOIN $sTable ON $sO2CView.`oxobjectid` = $sTable.`oxid` AND $sTable.`oxparentid` = ''
00148                WHERE $sO2CView.`oxcatnid` = " . $oDb->quote( $sCatId ) . " AND " . $oArticle->getSqlActiveSnippet();
00149 
00150         $aCache[$sCatId][$sActIdent] = $oDb->getOne( $sQ );
00151 
00152         $this->_setCatCache( $aCache );
00153         return $aCache[$sCatId][$sActIdent];
00154     }
00155 
00167     public function setPriceCatArticleCount( $aCache, $sCatId, $sActIdent, $dPriceFrom, $dPriceTo )
00168     {
00169         $oArticle = oxNew( 'oxarticle' );
00170         $sTable   = $oArticle->getViewName();
00171 
00172         $sSelect  = "select count({$sTable}.oxid) from {$sTable} where oxvarminprice >= 0 ";
00173         $sSelect .= $dPriceTo ? "and oxvarminprice <= " . (double)$dPriceTo . " " : " ";
00174         $sSelect .= $dPriceFrom ? "and oxvarminprice  >= " . (double)$dPriceFrom . " " : " ";
00175         $sSelect .= "and {$sTable}.oxissearch = 1 and ".$oArticle->getSqlActiveSnippet();
00176 
00177         $aCache[$sCatId][$sActIdent] = oxDb::getDb()->getOne( $sSelect );
00178 
00179         $this->_setCatCache( $aCache );
00180         return $aCache[$sCatId][$sActIdent];
00181     }
00182 
00192     public function setVendorArticleCount( $aCache, $sCatId, $sActIdent )
00193     {
00194         // if vendor/category name is 'root', skip counting
00195         if ( $sCatId == 'root' ) {
00196             return 0;
00197         }
00198 
00199         $oArticle = oxNew( 'oxarticle' );
00200         $sTable   = $oArticle->getViewName();
00201 
00202         // select each vendor articles count
00203         $sQ  = "select $sTable.oxvendorid AS vendorId, count(*) from $sTable where ";
00204         $sQ .= "$sTable.oxvendorid <> '' and $sTable.oxparentid = '' and ".$oArticle->getSqlActiveSnippet()." group by $sTable.oxvendorid ";
00205         $aDbResult = oxDb::getDb()->getAssoc( $sQ );
00206 
00207         foreach ( $aDbResult as $sKey => $sValue ) {
00208             $aCache[$sKey][$sActIdent] = $sValue;
00209         }
00210 
00211         $this->_setVendorCache( $aCache );
00212         return $aCache[$sCatId][$sActIdent];
00213     }
00214 
00224     public function setManufacturerArticleCount( $aCache, $sMnfId, $sActIdent )
00225     {
00226         // if Manufacturer/category name is 'root', skip counting
00227         if ( $sMnfId == 'root' ) {
00228             return 0;
00229         }
00230 
00231         $oArticle = oxNew( 'oxarticle' );
00232         $sArtTable   = $oArticle->getViewName();
00233         $sManTable = getViewName('oxmanufacturers');
00234 
00235         // select each Manufacturer articles count
00236         //#3485
00237         $sQ = "select count($sArtTable.oxid) from $sArtTable where $sArtTable.oxparentid = '' and oxmanufacturerid = '$sMnfId' and ".$oArticle->getSqlActiveSnippet();
00238 
00239         $iValue = oxDb::getDb()->getOne( $sQ );
00240 
00241         $aCache[$sMnfId][$sActIdent] = (int) $iValue;
00242 
00243         $this->_setManufacturerCache( $aCache );
00244 
00245         return $aCache[$sMnfId][$sActIdent];
00246     }
00247 
00255     public function resetCatArticleCount( $sCatId = null )
00256     {
00257         if ( !$sCatId ) {
00258             $this->getConfig()->setGlobalParameter( 'aLocalCatCache', null );
00259             oxRegistry::getUtils()->toFileCache( 'aLocalCatCache', '' );
00260         } else {
00261             // loading from cache
00262             $aCatData = $this->_getCatCache();
00263             if ( isset( $aCatData[$sCatId] ) ) {
00264                 unset( $aCatData[$sCatId] );
00265                 $this->_setCatCache( $aCatData );
00266             }
00267         }
00268 
00269     }
00270 
00278     public function resetPriceCatArticleCount( $iPrice )
00279     {
00280         // loading from cache
00281         if ( $aCatData = $this->_getCatCache() ) {
00282 
00283             $sTable  = getViewName( 'oxcategories' );
00284             $sSelect = "select $sTable.oxid from $sTable where " . (double)$iPrice . " >= $sTable.oxpricefrom and " . (double)$iPrice . " <= $sTable.oxpriceto ";
00285 
00286             $rs = oxDb::getDb()->select( $sSelect, false, false );
00287             if ( $rs != false && $rs->recordCount() > 0 ) {
00288                 while ( !$rs->EOF ) {
00289                     if ( isset( $aCatData[$rs->fields[0]] ) ) {
00290                         unset( $aCatData[$rs->fields[0]] );
00291                     }
00292                     $rs->moveNext();
00293                 }
00294 
00295                 // writing back to cache
00296                 $this->_setCatCache( $aCatData );
00297             }
00298 
00299         }
00300     }
00301 
00310     public function getTagArticleCount( $sTag, $iLang )
00311     {
00312         $oDb = oxDb::getDb();
00313 
00314         $oArticle = oxNew("oxarticle");
00315         $sArticleTable  = $oArticle->getViewName();
00316         $sActiveSnippet = $oArticle->getSqlActiveSnippet();
00317         $sViewName = getViewName( 'oxartextends', $iLang );
00318 
00319         $sQ = "select count(*) from {$sViewName} inner join {$sArticleTable} on ".
00320               "{$sArticleTable}.oxid = {$sViewName}.oxid where {$sArticleTable}.oxparentid = '' and {$sArticleTable}.oxissearch = 1 AND match ( {$sViewName}.oxtags ) ".
00321               "against( ".$oDb->quote( "\"".$sTag."\"" )." IN BOOLEAN MODE ) and {$sActiveSnippet}";
00322 
00323         return $oDb->getOne( $sQ );
00324     }
00325 
00333     public function resetVendorArticleCount( $sVendorId = null )
00334     {
00335         if ( !$sVendorId ) {
00336             $this->getConfig()->setGlobalParameter( 'aLocalVendorCache', null );
00337             oxRegistry::getUtils()->toFileCache( 'aLocalVendorCache', '' );
00338         } else {
00339             // loading from cache
00340             $aVendorData = $this->_getVendorCache();
00341             if ( isset( $aVendorData[$sVendorId] ) ) {
00342                 unset( $aVendorData[$sVendorId] );
00343                 $this->_setVendorCache( $aVendorData );
00344             }
00345         }
00346 
00347     }
00348 
00356     public function resetManufacturerArticleCount( $sManufacturerId = null )
00357     {
00358         if ( !$sManufacturerId ) {
00359             $this->getConfig()->setGlobalParameter( 'aLocalManufacturerCache', null );
00360             oxRegistry::getUtils()->toFileCache( 'aLocalManufacturerCache', '' );
00361         } else {
00362             // loading from cache
00363             $aManufacturerData = $this->_getManufacturerCache();
00364             if ( isset( $aManufacturerData[$sManufacturerId] ) ) {
00365                 unset( $aManufacturerData[$sManufacturerId] );
00366                 $this->_setManufacturerCache( $aManufacturerData );
00367             }
00368         }
00369 
00370     }
00371 
00377     protected function _getCatCache()
00378     {
00379         $myConfig = $this->getConfig();
00380 
00381         // first look at the local cache
00382         $aLocalCatCache = $myConfig->getGlobalParameter( 'aLocalCatCache' );
00383 
00384         // if local cache is not set - loading from file cache
00385         if ( !$aLocalCatCache ) {
00386             $sLocalCatCache = oxRegistry::getUtils()->fromFileCache( 'aLocalCatCache');
00387             if ( $sLocalCatCache ) {
00388                 $aLocalCatCache = $sLocalCatCache;
00389             } else {
00390                 $aLocalCatCache = null;
00391             }
00392             $myConfig->setGlobalParameter( 'aLocalCatCache', $aLocalCatCache );
00393         }
00394         return $aLocalCatCache;
00395     }
00396 
00404     protected function _setCatCache( $aCache )
00405     {
00406         $this->getConfig()->setGlobalParameter( 'aLocalCatCache', $aCache );
00407         oxRegistry::getUtils()->toFileCache( 'aLocalCatCache', $aCache );
00408     }
00409 
00417     protected function _setVendorCache( $aCache )
00418     {
00419         $this->getConfig()->setGlobalParameter( 'aLocalVendorCache', $aCache );
00420         oxRegistry::getUtils()->toFileCache( 'aLocalVendorCache', $aCache );
00421     }
00422 
00430     protected function _setManufacturerCache( $aCache )
00431     {
00432         $this->getConfig()->setGlobalParameter( 'aLocalManufacturerCache', $aCache );
00433         oxRegistry::getUtils()->toFileCache( 'aLocalManufacturerCache', $aCache );
00434     }
00435 
00441     protected function _getVendorCache()
00442     {
00443         $myConfig = $this->getConfig();
00444 
00445         // first look at the local cache
00446         $aLocalVendorCache = $myConfig->getGlobalParameter('aLocalVendorCache');
00447         // if local cache is not set - loading from file cache
00448         if ( !$aLocalVendorCache ) {
00449             $sLocalVendorCache = oxRegistry::getUtils()->fromFileCache( 'aLocalVendorCache' );
00450             if ( $sLocalVendorCache ) {
00451                 $aLocalVendorCache = $sLocalVendorCache;
00452             } else {
00453                 $aLocalVendorCache = null;
00454             }
00455             $myConfig->setGlobalParameter( 'aLocalVendorCache', $aLocalVendorCache );
00456         }
00457         return $aLocalVendorCache;
00458     }
00459 
00465     protected function _getManufacturerCache()
00466     {
00467         $myConfig = $this->getConfig();
00468 
00469         // first look at the local cache
00470         $aLocalManufacturerCache = $myConfig->getGlobalParameter('aLocalManufacturerCache');
00471         // if local cache is not set - loading from file cache
00472         if ( !$aLocalManufacturerCache ) {
00473             $sLocalManufacturerCache = oxRegistry::getUtils()->fromFileCache( 'aLocalManufacturerCache' );
00474             if ( $sLocalManufacturerCache ) {
00475                 $aLocalManufacturerCache = $sLocalManufacturerCache;
00476             } else {
00477                 $aLocalManufacturerCache = null;
00478             }
00479             $myConfig->setGlobalParameter( 'aLocalManufacturerCache', $aLocalManufacturerCache );
00480         }
00481         return $aLocalManufacturerCache;
00482     }
00483 
00491     protected function _getUserViewId( $blReset = false )
00492     {
00493         if ( $this->_sUserViewId != null && !$blReset ) {
00494             return $this->_sUserViewId;
00495         }
00496 
00497         // loading R&R data from session
00498         $aRRIdx = null;
00499 
00500         $this->_sUserViewId = md5($this->getConfig()->getShopID().oxRegistry::getLang()->getLanguageTag().serialize($aRRIdx).(int) $this->isAdmin() );
00501         return $this->_sUserViewId;
00502     }
00503 
00504 }