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         $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}
00333                on {$sArticleTable}.oxid = {$sViewName}.oxid where {$sActiveSnippet}
00334                and {$sArticleTable}.oxissearch = 1 and match( {$sViewName}.oxtags )
00335                against ( ".$oDb->quote( $sTag )." IN BOOLEAN MODE ) ";
00336 
00337         return $oDb->getOne( $sQ );
00338     }
00339 
00347     public function resetVendorArticleCount( $sVendorId = null )
00348     {
00349         if ( !$sVendorId ) {
00350             $this->getConfig()->setGlobalParameter( 'aLocalVendorCache', null );
00351             oxUtils::getInstance()->toFileCache( 'aLocalVendorCache', '' );
00352         } else {
00353             // loading from cache
00354             $aVendorData = $this->_getVendorCache();
00355             if ( isset( $aVendorData[$sVendorId] ) ) {
00356                 unset( $aVendorData[$sVendorId] );
00357                 $this->_setVendorCache( $aVendorData );
00358             }
00359         }
00360 
00361     }
00362 
00370     public function resetManufacturerArticleCount( $sManufacturerId = null )
00371     {
00372         if ( !$sManufacturerId ) {
00373             $this->getConfig()->setGlobalParameter( 'aLocalManufacturerCache', null );
00374             oxUtils::getInstance()->toFileCache( 'aLocalManufacturerCache', '' );
00375         } else {
00376             // loading from cache
00377             $aManufacturerData = $this->_getManufacturerCache();
00378             if ( isset( $aManufacturerData[$sManufacturerId] ) ) {
00379                 unset( $aManufacturerData[$sManufacturerId] );
00380                 $this->_setManufacturerCache( $aManufacturerData );
00381             }
00382         }
00383 
00384     }
00385 
00391     protected function _getCatCache()
00392     {
00393         $myConfig = $this->getConfig();
00394 
00395         // first look at the local cache
00396         $aLocalCatCache = $myConfig->getGlobalParameter( 'aLocalCatCache' );
00397 
00398         // if local cache is not set - loading from file cache
00399         if ( !$aLocalCatCache ) {
00400             $sLocalCatCache = oxUtils::getInstance()->fromFileCache( 'aLocalCatCache');
00401             if ( $sLocalCatCache ) {
00402                 $aLocalCatCache = $sLocalCatCache;
00403             } else {
00404                 $aLocalCatCache = null;
00405             }
00406             $myConfig->setGlobalParameter( 'aLocalCatCache', $aLocalCatCache );
00407         }
00408         return $aLocalCatCache;
00409     }
00410 
00418     protected function _setCatCache( $aCache )
00419     {
00420         $this->getConfig()->setGlobalParameter( 'aLocalCatCache', $aCache );
00421         oxUtils::getInstance()->toFileCache( 'aLocalCatCache', $aCache );
00422     }
00423 
00431     protected function _setVendorCache( $aCache )
00432     {
00433         $this->getConfig()->setGlobalParameter( 'aLocalVendorCache', $aCache );
00434         oxUtils::getInstance()->toFileCache( 'aLocalVendorCache', $aCache );
00435     }
00436 
00444     protected function _setManufacturerCache( $aCache )
00445     {
00446         $this->getConfig()->setGlobalParameter( 'aLocalManufacturerCache', $aCache );
00447         oxUtils::getInstance()->toFileCache( 'aLocalManufacturerCache', $aCache );
00448     }
00449 
00455     protected function _getVendorCache()
00456     {
00457         $myConfig = $this->getConfig();
00458 
00459         // first look at the local cache
00460         $aLocalVendorCache = $myConfig->getGlobalParameter('aLocalVendorCache');
00461         // if local cache is not set - loading from file cache
00462         if ( !$aLocalVendorCache ) {
00463             $sLocalVendorCache = oxUtils::getInstance()->fromFileCache( 'aLocalVendorCache' );
00464             if ( $sLocalVendorCache ) {
00465                 $aLocalVendorCache = $sLocalVendorCache;
00466             } else {
00467                 $aLocalVendorCache = null;
00468             }
00469             $myConfig->setGlobalParameter( 'aLocalVendorCache', $aLocalVendorCache );
00470         }
00471         return $aLocalVendorCache;
00472     }
00473 
00479     protected function _getManufacturerCache()
00480     {
00481         $myConfig = $this->getConfig();
00482 
00483         // first look at the local cache
00484         $aLocalManufacturerCache = $myConfig->getGlobalParameter('aLocalManufacturerCache');
00485         // if local cache is not set - loading from file cache
00486         if ( !$aLocalManufacturerCache ) {
00487             $sLocalManufacturerCache = oxUtils::getInstance()->fromFileCache( 'aLocalManufacturerCache' );
00488             if ( $sLocalManufacturerCache ) {
00489                 $aLocalManufacturerCache = $sLocalManufacturerCache;
00490             } else {
00491                 $aLocalManufacturerCache = null;
00492             }
00493             $myConfig->setGlobalParameter( 'aLocalManufacturerCache', $aLocalManufacturerCache );
00494         }
00495         return $aLocalManufacturerCache;
00496     }
00497 
00505     protected function _getUserViewId( $blReset = false )
00506     {
00507         if ( $this->_sUserViewId != null && !$blReset ) {
00508             return $this->_sUserViewId;
00509         }
00510 
00511         // loading R&R data from session
00512         $aRRIdx = null;
00513 
00514         $this->_sUserViewId = md5($this->getConfig()->getShopID().oxLang::getInstance()->getLanguageTag().serialize($aRRIdx).(int) $this->isAdmin() );
00515         return $this->_sUserViewId;
00516     }
00517 
00518 }