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         $oDb = oxDb::getDb();
00153 
00154         // we use distinct if article is assigned to category twice
00155         $sQ = "SELECT count(*) FROM (
00156                    SELECT count(*) FROM $sO2CView LEFT JOIN $sTable ON $sO2CView.oxobjectid=$sTable.oxid
00157                        WHERE $sO2CView.oxcatnid = ".$oDb->quote( $sCatId ) ." AND
00158                              $sTable.oxparentid='' AND
00159                              ".$oArticle->getSqlActiveSnippet() ."
00160                        GROUP BY $sTable.oxid
00161                    ) AS ox2cat";
00162 
00163         $aCache[$sCatId][$sActIdent] = $oDb->getOne( $sQ );
00164 
00165         $this->_setCatCache( $aCache );
00166         return $aCache[$sCatId][$sActIdent];
00167     }
00168 
00180     public function setPriceCatArticleCount( $aCache, $sCatId, $sActIdent, $dPriceFrom, $dPriceTo )
00181     {
00182         $oArticle = oxNew( 'oxarticle' );
00183         $sTable   = $oArticle->getViewName();
00184 
00185         $sSelect  = "select count({$sTable}.oxid) from {$sTable} where oxvarminprice >= 0 ";
00186         $sSelect .= $dPriceTo ? "and oxvarminprice <= " . (double)$dPriceTo . " " : " ";
00187         $sSelect .= $dPriceFrom ? "and oxvarminprice  >= " . (double)$dPriceFrom . " " : " ";
00188         $sSelect .= "and {$sTable}.oxissearch = 1 and ".$oArticle->getSqlActiveSnippet();
00189 
00190         $aCache[$sCatId][$sActIdent] = oxDb::getDb()->getOne( $sSelect );
00191 
00192         $this->_setCatCache( $aCache );
00193         return $aCache[$sCatId][$sActIdent];
00194     }
00195 
00205     public function setVendorArticleCount( $aCache, $sCatId, $sActIdent )
00206     {
00207         // if vendor/category name is 'root', skip counting
00208         if ( $sCatId == 'root' ) {
00209             return 0;
00210         }
00211 
00212         $oArticle = oxNew( 'oxarticle' );
00213         $sTable   = $oArticle->getViewName();
00214 
00215         // select each vendor articles count
00216         $sQ  = "select $sTable.oxvendorid AS vendorId, count(*) from $sTable where ";
00217         $sQ .= "$sTable.oxvendorid <> '' and $sTable.oxparentid = '' and ".$oArticle->getSqlActiveSnippet()." group by $sTable.oxvendorid ";
00218         $aDbResult = oxDb::getDb()->getAssoc( $sQ );
00219 
00220         foreach ( $aDbResult as $sKey => $sValue ) {
00221             $aCache[$sKey][$sActIdent] = $sValue;
00222         }
00223 
00224         $this->_setVendorCache( $aCache );
00225         return $aCache[$sCatId][$sActIdent];
00226     }
00227 
00237     public function setManufacturerArticleCount( $aCache, $sCatId, $sActIdent )
00238     {
00239         // if Manufacturer/category name is 'root', skip counting
00240         if ( $sCatId == 'root' ) {
00241             return 0;
00242         }
00243 
00244         $oArticle = oxNew( 'oxarticle' );
00245         $sArtTable   = $oArticle->getViewName();
00246         $sManTable = getViewName('oxmanufacturers');
00247 
00248         // select each Manufacturer articles count
00249         $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";
00250         $aDbResult = oxDb::getDb()->getAssoc( $sQ );
00251 
00252         foreach ( $aDbResult as $sKey => $sValue ) {
00253             $aCache[$sKey][$sActIdent] = (int) $sValue;
00254         }
00255 
00256         $this->_setManufacturerCache( $aCache );
00257         return $aCache[$sCatId][$sActIdent];
00258     }
00259 
00267     public function resetCatArticleCount( $sCatId = null )
00268     {
00269         if ( !$sCatId ) {
00270             $this->getConfig()->setGlobalParameter( 'aLocalCatCache', null );
00271             oxUtils::getInstance()->toFileCache( 'aLocalCatCache', '' );
00272         } else {
00273             // 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         $oDb = oxDb::getDb();
00325 
00326         $oArticle = oxNew("oxarticle");
00327         $sArticleTable  = $oArticle->getViewName();
00328         $sActiveSnippet = $oArticle->getSqlActiveSnippet();
00329         $sViewName = getViewName( 'oxartextends', $iLang );
00330 
00331         $sQ = "select count(*) from {$sViewName} inner join {$sArticleTable} on ".
00332               "{$sArticleTable}.oxid = {$sViewName}.oxid where {$sArticleTable}.oxparentid = '' and {$sArticleTable}.oxissearch = 1 AND match ( {$sViewName}.oxtags ) ".
00333               "against( ".$oDb->quote( "\"".$sTag."\"" )." IN BOOLEAN MODE ) and {$sActiveSnippet}";
00334 
00335         return $oDb->getOne( $sQ );
00336     }
00337 
00345     public function resetVendorArticleCount( $sVendorId = null )
00346     {
00347         if ( !$sVendorId ) {
00348             $this->getConfig()->setGlobalParameter( 'aLocalVendorCache', null );
00349             oxUtils::getInstance()->toFileCache( 'aLocalVendorCache', '' );
00350         } else {
00351             // loading from cache
00352             $aVendorData = $this->_getVendorCache();
00353             if ( isset( $aVendorData[$sVendorId] ) ) {
00354                 unset( $aVendorData[$sVendorId] );
00355                 $this->_setVendorCache( $aVendorData );
00356             }
00357         }
00358 
00359     }
00360 
00368     public function resetManufacturerArticleCount( $sManufacturerId = null )
00369     {
00370         if ( !$sManufacturerId ) {
00371             $this->getConfig()->setGlobalParameter( 'aLocalManufacturerCache', null );
00372             oxUtils::getInstance()->toFileCache( 'aLocalManufacturerCache', '' );
00373         } else {
00374             // 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 }