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, $sMnfId, $sActIdent )
00238     {
00239         // if Manufacturer/category name is 'root', skip counting
00240         if ( $sMnfId == '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         //#3485
00250         $sQ = "select count($sArtTable.oxid) from $sArtTable where $sArtTable.oxparentid = '' and oxmanufacturerid = '$sMnfId' and ".$oArticle->getSqlActiveSnippet();
00251 
00252         $iValue = oxDb::getDb()->getOne( $sQ );
00253 
00254         $aCache[$sMnfId][$sActIdent] = (int) $iValue;
00255 
00256         $this->_setManufacturerCache( $aCache );
00257 
00258         return $aCache[$sMnfId][$sActIdent];
00259     }
00260 
00268     public function resetCatArticleCount( $sCatId = null )
00269     {
00270         if ( !$sCatId ) {
00271             $this->getConfig()->setGlobalParameter( 'aLocalCatCache', null );
00272             oxUtils::getInstance()->toFileCache( 'aLocalCatCache', '' );
00273         } else {
00274             // loading from cache
00275             $aCatData = $this->_getCatCache();
00276             if ( isset( $aCatData[$sCatId] ) ) {
00277                 unset( $aCatData[$sCatId] );
00278                 $this->_setCatCache( $aCatData );
00279             }
00280         }
00281 
00282     }
00283 
00291     public function resetPriceCatArticleCount( $iPrice )
00292     {
00293         // loading from cache
00294         if ( $aCatData = $this->_getCatCache() ) {
00295 
00296             $sTable  = getViewName( 'oxcategories' );
00297             $sSelect = "select $sTable.oxid from $sTable where " . (double)$iPrice . " >= $sTable.oxpricefrom and " . (double)$iPrice . " <= $sTable.oxpriceto ";
00298 
00299             $rs = oxDb::getDb()->select( $sSelect, false, false );
00300             if ( $rs != false && $rs->recordCount() > 0 ) {
00301                 while ( !$rs->EOF ) {
00302                     if ( isset( $aCatData[$rs->fields[0]] ) ) {
00303                         unset( $aCatData[$rs->fields[0]] );
00304                     }
00305                     $rs->moveNext();
00306                 }
00307 
00308                 // writing back to cache
00309                 $this->_setCatCache( $aCatData );
00310             }
00311 
00312         }
00313     }
00314 
00323     public function getTagArticleCount( $sTag, $iLang )
00324     {
00325         $oDb = oxDb::getDb();
00326 
00327         $oArticle = oxNew("oxarticle");
00328         $sArticleTable  = $oArticle->getViewName();
00329         $sActiveSnippet = $oArticle->getSqlActiveSnippet();
00330         $sViewName = getViewName( 'oxartextends', $iLang );
00331 
00332         $sQ = "select count(*) from {$sViewName} inner join {$sArticleTable} on ".
00333               "{$sArticleTable}.oxid = {$sViewName}.oxid where {$sArticleTable}.oxparentid = '' and {$sArticleTable}.oxissearch = 1 AND match ( {$sViewName}.oxtags ) ".
00334               "against( ".$oDb->quote( "\"".$sTag."\"" )." IN BOOLEAN MODE ) and {$sActiveSnippet}";
00335 
00336         return $oDb->getOne( $sQ );
00337     }
00338 
00346     public function resetVendorArticleCount( $sVendorId = null )
00347     {
00348         if ( !$sVendorId ) {
00349             $this->getConfig()->setGlobalParameter( 'aLocalVendorCache', null );
00350             oxUtils::getInstance()->toFileCache( 'aLocalVendorCache', '' );
00351         } else {
00352             // 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 {
00375             // loading from cache
00376             $aManufacturerData = $this->_getManufacturerCache();
00377             if ( isset( $aManufacturerData[$sManufacturerId] ) ) {
00378                 unset( $aManufacturerData[$sManufacturerId] );
00379                 $this->_setManufacturerCache( $aManufacturerData );
00380             }
00381         }
00382 
00383     }
00384 
00390     protected function _getCatCache()
00391     {
00392         $myConfig = $this->getConfig();
00393 
00394         // first look at the local cache
00395         $aLocalCatCache = $myConfig->getGlobalParameter( 'aLocalCatCache' );
00396 
00397         // if local cache is not set - loading from file cache
00398         if ( !$aLocalCatCache ) {
00399             $sLocalCatCache = oxUtils::getInstance()->fromFileCache( 'aLocalCatCache');
00400             if ( $sLocalCatCache ) {
00401                 $aLocalCatCache = $sLocalCatCache;
00402             } else {
00403                 $aLocalCatCache = null;
00404             }
00405             $myConfig->setGlobalParameter( 'aLocalCatCache', $aLocalCatCache );
00406         }
00407         return $aLocalCatCache;
00408     }
00409 
00417     protected function _setCatCache( $aCache )
00418     {
00419         $this->getConfig()->setGlobalParameter( 'aLocalCatCache', $aCache );
00420         oxUtils::getInstance()->toFileCache( 'aLocalCatCache', $aCache );
00421     }
00422 
00430     protected function _setVendorCache( $aCache )
00431     {
00432         $this->getConfig()->setGlobalParameter( 'aLocalVendorCache', $aCache );
00433         oxUtils::getInstance()->toFileCache( 'aLocalVendorCache', $aCache );
00434     }
00435 
00443     protected function _setManufacturerCache( $aCache )
00444     {
00445         $this->getConfig()->setGlobalParameter( 'aLocalManufacturerCache', $aCache );
00446         oxUtils::getInstance()->toFileCache( 'aLocalManufacturerCache', $aCache );
00447     }
00448 
00454     protected function _getVendorCache()
00455     {
00456         $myConfig = $this->getConfig();
00457 
00458         // first look at the local cache
00459         $aLocalVendorCache = $myConfig->getGlobalParameter('aLocalVendorCache');
00460         // if local cache is not set - loading from file cache
00461         if ( !$aLocalVendorCache ) {
00462             $sLocalVendorCache = oxUtils::getInstance()->fromFileCache( 'aLocalVendorCache' );
00463             if ( $sLocalVendorCache ) {
00464                 $aLocalVendorCache = $sLocalVendorCache;
00465             } else {
00466                 $aLocalVendorCache = null;
00467             }
00468             $myConfig->setGlobalParameter( 'aLocalVendorCache', $aLocalVendorCache );
00469         }
00470         return $aLocalVendorCache;
00471     }
00472 
00478     protected function _getManufacturerCache()
00479     {
00480         $myConfig = $this->getConfig();
00481 
00482         // first look at the local cache
00483         $aLocalManufacturerCache = $myConfig->getGlobalParameter('aLocalManufacturerCache');
00484         // if local cache is not set - loading from file cache
00485         if ( !$aLocalManufacturerCache ) {
00486             $sLocalManufacturerCache = oxUtils::getInstance()->fromFileCache( 'aLocalManufacturerCache' );
00487             if ( $sLocalManufacturerCache ) {
00488                 $aLocalManufacturerCache = $sLocalManufacturerCache;
00489             } else {
00490                 $aLocalManufacturerCache = null;
00491             }
00492             $myConfig->setGlobalParameter( 'aLocalManufacturerCache', $aLocalManufacturerCache );
00493         }
00494         return $aLocalManufacturerCache;
00495     }
00496 
00504     protected function _getUserViewId( $blReset = false )
00505     {
00506         if ( $this->_sUserViewId != null && !$blReset ) {
00507             return $this->_sUserViewId;
00508         }
00509 
00510         // loading R&R data from session
00511         $aRRIdx = null;
00512 
00513         $this->_sUserViewId = md5($this->getConfig()->getShopID().oxLang::getInstance()->getLanguageTag().serialize($aRRIdx).(int) $this->isAdmin() );
00514         return $this->_sUserViewId;
00515     }
00516 
00517 }