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         return $iCnt;
00090     }
00091 
00099     public function getVendorArticleCount( $sVendorId )
00100     {
00101         // current category unique ident
00102         $sActIdent = $this->_getUserViewId();
00103 
00104         // loading from cache
00105         $aVendorData = $this->_getVendorCache();
00106 
00107         if ( !$aVendorData || !isset( $aVendorData[$sVendorId][$sActIdent] ) ) {
00108             $iCnt = $this->setVendorArticleCount( $aVendorData, $sVendorId, $sActIdent );
00109         } else {
00110             $iCnt = $aVendorData[$sVendorId][$sActIdent];
00111         }
00112         return $iCnt;
00113     }
00114 
00122     public function getManufacturerArticleCount( $sManufacturerId )
00123     {
00124         // current category unique ident
00125         $sActIdent = $this->_getUserViewId();
00126 
00127         // loading from cache
00128         $aManufacturerData = $this->_getManufacturerCache();
00129         if ( !$aManufacturerData || !isset( $aManufacturerData[$sManufacturerId][$sActIdent] ) ) {
00130             $iCnt = $this->setManufacturerArticleCount( $aManufacturerData, $sManufacturerId, $sActIdent );
00131         } else {
00132             $iCnt = $aManufacturerData[$sManufacturerId][$sActIdent];
00133         }
00134         return $iCnt;
00135     }
00136 
00146     public function setCatArticleCount( $aCache, $sCatId, $sActIdent )
00147     {
00148         $oArticle = oxNew( 'oxarticle' );
00149         $sTable   = $oArticle->getViewName();
00150         $sO2CView = getViewName( 'oxobject2category' );
00151 
00152         // we use distinct if article is assigned to category twice
00153         $sQ = "SELECT count(*) FROM (
00154                    SELECT count(*) FROM $sTable LEFT JOIN $sO2CView ON $sO2CView.oxobjectid=$sTable.oxid
00155                        WHERE $sO2CView.oxcatnid = '".$sCatId."' AND
00156                              $sTable.oxparentid='' AND
00157                              ".$oArticle->getSqlActiveSnippet() ."
00158                        GROUP BY $sTable.oxid
00159                    ) AS ox2cat";
00160 
00161         $aCache[$sCatId][$sActIdent] = oxDb::getDb()->getOne( $sQ );
00162 
00163         $this->_setCatCache( $aCache );
00164         return $aCache[$sCatId][$sActIdent];
00165     }
00166 
00178     public function setPriceCatArticleCount( $aCache, $sCatId, $sActIdent, $dPriceFrom, $dPriceTo )
00179     {
00180         $oArticle = oxNew( 'oxarticle' );
00181         $sTable   = $oArticle->getViewName();
00182 
00183         $sSubSelect  = "select if( oxparentid='', oxid, oxparentid ) as id from {$sTable} where oxprice >= 0 ";
00184         $sSubSelect .= $dPriceTo ? "and oxprice <= " . (double)$dPriceTo . " " : " ";
00185         $sSubSelect .= $dPriceFrom ? "group by id having min( oxprice ) >= " . (double)$dPriceFrom . " " : " ";
00186 
00187         $sSelect  = "select count({$sTable}.oxid) from {$sTable} where ";
00188         $sSelect .= "{$sTable}.oxid in ($sSubSelect) ";
00189         $sSelect .= "and {$sTable}.oxissearch = 1 and ".$oArticle->getSqlActiveSnippet();
00190 
00191         $aCache[$sCatId][$sActIdent] = oxDb::getDb()->getOne( $sSelect );
00192 
00193         $this->_setCatCache( $aCache );
00194         return $aCache[$sCatId][$sActIdent];
00195     }
00196 
00206     public function setVendorArticleCount( $aCache, $sCatId, $sActIdent )
00207     {
00208         // if vendor/category name is 'root', skip counting
00209         if ( $sCatId == 'root' ) {
00210             return 0;
00211         }
00212 
00213         $oArticle = oxNew( 'oxarticle' );
00214         $sTable   = $oArticle->getViewName();
00215 
00216         // select each vendor articles count
00217         $sQ  = "select $sTable.oxvendorid AS vendorId, count(*) from $sTable where ";
00218         $sQ .= "$sTable.oxvendorid <> '' and $sTable.oxparentid = '' and ".$oArticle->getSqlActiveSnippet()." group by $sTable.oxvendorid ";
00219         $aDbResult = oxDb::getDb()->getAssoc( $sQ );
00220 
00221         foreach ( $aDbResult as $sKey => $sValue ) {
00222             $aCache[$sKey][$sActIdent] = $sValue;
00223         }
00224 
00225         $this->_setVendorCache( $aCache );
00226         return $aCache[$sCatId][$sActIdent];
00227     }
00228 
00238     public function setManufacturerArticleCount( $aCache, $sCatId, $sActIdent )
00239     {
00240         // if Manufacturer/category name is 'root', skip counting
00241         if ( $sCatId == 'root' ) {
00242             return 0;
00243         }
00244 
00245         $oArticle = oxNew( 'oxarticle' );
00246         $sArtTable   = $oArticle->getViewName();
00247         $sManTable = getViewName('oxmanufacturers');
00248 
00249         // select each Manufacturer articles count
00250         $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";
00251         $aDbResult = oxDb::getDb()->getAssoc( $sQ );
00252 
00253         foreach ( $aDbResult as $sKey => $sValue ) {
00254             $aCache[$sKey][$sActIdent] = (int) $sValue;
00255         }
00256 
00257         $this->_setManufacturerCache( $aCache );
00258         return $aCache[$sCatId][$sActIdent];
00259     }
00260 
00268     public function resetCatArticleCount( $sCatId = null )
00269     {
00270         if ( !$sCatId ) {
00271             $this->getConfig()->setGlobalParameter( 'aLocalCatCache', null );
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         $sLangExt = oxLang::getInstance()->getLanguageTag( $iLang );
00325         $oDb = oxDb::getDb();
00326 
00327         $oArticle = oxNew("oxarticle");
00328         $sArticleTable  = $oArticle->getViewName();
00329         $sActiveSnippet = $oArticle->getSqlActiveSnippet();
00330 
00331         $sQ = "select count(*) from oxartextends inner join {$sArticleTable}
00332                on {$sArticleTable}.oxid = oxartextends.oxid where {$sActiveSnippet}
00333                and {$sArticleTable}.oxissearch = 1
00334                and match(oxartextends.oxtags{$sLangExt})
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 }

Generated by  doxygen 1.6.2