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         $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         $sArtTable   = $oArticle->getViewName();
00248         $sManTable = getViewName('oxmanufacturers');
00249 
00250         // select each Manufacturer articles count
00251         $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";
00252         $aDbResult = oxDb::getDb()->getAssoc( $sQ );
00253 
00254         foreach ( $aDbResult as $sKey => $sValue ) {
00255             $aCache[$sKey][$sActIdent] = (int) $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 {
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()->execute( $sSelect );
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         $sLangExt = oxLang::getInstance()->getLanguageTag( $iLang );
00326         $oDb = oxDb::getDb();
00327 
00328         $oArticle = oxNew("oxarticle");
00329         $sArticleTable  = $oArticle->getViewName();
00330         $sActiveSnippet = $oArticle->getSqlActiveSnippet();
00331 
00332         $sQ = "select count(*) from oxartextends inner join {$sArticleTable}
00333                on {$sArticleTable}.oxid = oxartextends.oxid where {$sActiveSnippet}
00334                and {$sArticleTable}.oxissearch = 1
00335                and match(oxartextends.oxtags{$sLangExt})
00336                against ( ".$oDb->quote( $sTag )." IN BOOLEAN MODE ) ";
00337 
00338         return $oDb->getOne( $sQ );
00339     }
00340 
00348     public function resetVendorArticleCount( $sVendorId = null )
00349     {
00350         if ( !$sVendorId ) {
00351             $this->getConfig()->setGlobalParameter( 'aLocalVendorCache', null );
00352             oxUtils::getInstance()->toFileCache( 'aLocalVendorCache', '' );
00353         } else {
00354             // loading from cache
00355             $aVendorData = $this->_getVendorCache();
00356             if ( isset( $aVendorData[$sVendorId] ) ) {
00357                 unset( $aVendorData[$sVendorId] );
00358                 $this->_setVendorCache( $aVendorData );
00359             }
00360         }
00361 
00362     }
00363 
00371     public function resetManufacturerArticleCount( $sManufacturerId = null )
00372     {
00373         if ( !$sManufacturerId ) {
00374             $this->getConfig()->setGlobalParameter( 'aLocalManufacturerCache', null );
00375             oxUtils::getInstance()->toFileCache( 'aLocalManufacturerCache', '' );
00376         } else {
00377             // loading from cache
00378             $aManufacturerData = $this->_getManufacturerCache();
00379             if ( isset( $aManufacturerData[$sManufacturerId] ) ) {
00380                 unset( $aManufacturerData[$sManufacturerId] );
00381                 $this->_setManufacturerCache( $aManufacturerData );
00382             }
00383         }
00384 
00385     }
00386 
00392     protected function _getCatCache()
00393     {
00394         $myConfig = $this->getConfig();
00395 
00396         // first look at the local cache
00397         $aLocalCatCache = $myConfig->getGlobalParameter( 'aLocalCatCache' );
00398 
00399         // if local cache is not set - loading from file cache
00400         if ( !$aLocalCatCache ) {
00401             $sLocalCatCache = oxUtils::getInstance()->fromFileCache( 'aLocalCatCache');
00402             if ( $sLocalCatCache ) {
00403                 $aLocalCatCache = $sLocalCatCache;
00404             } else {
00405                 $aLocalCatCache = null;
00406             }
00407             $myConfig->setGlobalParameter( 'aLocalCatCache', $aLocalCatCache );
00408         }
00409         return $aLocalCatCache;
00410     }
00411 
00419     protected function _setCatCache( $aCache )
00420     {
00421         $this->getConfig()->setGlobalParameter( 'aLocalCatCache', $aCache );
00422         oxUtils::getInstance()->toFileCache( 'aLocalCatCache', $aCache );
00423     }
00424 
00432     protected function _setVendorCache( $aCache )
00433     {
00434         $this->getConfig()->setGlobalParameter( 'aLocalVendorCache', $aCache );
00435         oxUtils::getInstance()->toFileCache( 'aLocalVendorCache', $aCache );
00436     }
00437 
00445     protected function _setManufacturerCache( $aCache )
00446     {
00447         $this->getConfig()->setGlobalParameter( 'aLocalManufacturerCache', $aCache );
00448         oxUtils::getInstance()->toFileCache( 'aLocalManufacturerCache', $aCache );
00449     }
00450 
00456     protected function _getVendorCache()
00457     {
00458         $myConfig = $this->getConfig();
00459 
00460         // first look at the local cache
00461         $aLocalVendorCache = $myConfig->getGlobalParameter('aLocalVendorCache');
00462         // if local cache is not set - loading from file cache
00463         if ( !$aLocalVendorCache ) {
00464             $sLocalVendorCache = oxUtils::getInstance()->fromFileCache( 'aLocalVendorCache' );
00465             if ( $sLocalVendorCache ) {
00466                 $aLocalVendorCache = $sLocalVendorCache;
00467             } else {
00468                 $aLocalVendorCache = null;
00469             }
00470             $myConfig->setGlobalParameter( 'aLocalVendorCache', $aLocalVendorCache );
00471         }
00472         return $aLocalVendorCache;
00473     }
00474 
00480     protected function _getManufacturerCache()
00481     {
00482         $myConfig = $this->getConfig();
00483 
00484         // first look at the local cache
00485         $aLocalManufacturerCache = $myConfig->getGlobalParameter('aLocalManufacturerCache');
00486         // if local cache is not set - loading from file cache
00487         if ( !$aLocalManufacturerCache ) {
00488             $sLocalManufacturerCache = oxUtils::getInstance()->fromFileCache( 'aLocalManufacturerCache' );
00489             if ( $sLocalManufacturerCache ) {
00490                 $aLocalManufacturerCache = $sLocalManufacturerCache;
00491             } else {
00492                 $aLocalManufacturerCache = null;
00493             }
00494             $myConfig->setGlobalParameter( 'aLocalManufacturerCache', $aLocalManufacturerCache );
00495         }
00496         return $aLocalManufacturerCache;
00497     }
00498 
00506     protected function _getUserViewId( $blReset = false )
00507     {
00508         if ( $this->_sUserViewId != null && !$blReset ) {
00509             return $this->_sUserViewId;
00510         }
00511 
00512         // loading R&R data from session
00513         $aRRIdx = null;
00514 
00515         $this->_sUserViewId = md5($this->getConfig()->getShopID().oxLang::getInstance()->getLanguageTag().serialize($aRRIdx).(int) $this->isAdmin() );
00516         return $this->_sUserViewId;
00517     }
00518 
00519 }