oxutilscount.php

Go to the documentation of this file.
00001 <?php
00002 
00006 class oxUtilsCount extends oxSuperCfg
00007 {
00008 
00014     protected $_sUserViewId = null;
00015 
00023     public function getCatArticleCount($sCatId)
00024     {
00025         // current status unique ident
00026         $sActIdent = $this->_getUserViewId();
00027 
00028         // loading from cache
00029         $aCatData = $this->_getCatCache();
00030 
00031         if (!$aCatData || !isset($aCatData[$sCatId][$sActIdent])) {
00032             $iCnt = $this->setCatArticleCount($aCatData, $sCatId, $sActIdent);
00033         } else {
00034             $iCnt = $aCatData[$sCatId][$sActIdent];
00035         }
00036 
00037         return $iCnt;
00038     }
00039 
00049     public function getPriceCatArticleCount($sCatId, $dPriceFrom, $dPriceTo)
00050     {
00051         // current status unique ident
00052         $sActIdent = $this->_getUserViewId();
00053 
00054         // loading from cache
00055         $aCatData = $this->_getCatCache();
00056 
00057         if (!$aCatData || !isset($aCatData[$sCatId][$sActIdent])) {
00058             $iCnt = $this->setPriceCatArticleCount($aCatData, $sCatId, $sActIdent, $dPriceFrom, $dPriceTo);
00059         } else {
00060             $iCnt = $aCatData[$sCatId][$sActIdent];
00061         }
00062 
00063         return $iCnt;
00064     }
00065 
00073     public function getVendorArticleCount($sVendorId)
00074     {
00075         // current category unique ident
00076         $sActIdent = $this->_getUserViewId();
00077 
00078         // loading from cache
00079         $aVendorData = $this->_getVendorCache();
00080 
00081         if (!$aVendorData || !isset($aVendorData[$sVendorId][$sActIdent])) {
00082             $iCnt = $this->setVendorArticleCount($aVendorData, $sVendorId, $sActIdent);
00083         } else {
00084             $iCnt = $aVendorData[$sVendorId][$sActIdent];
00085         }
00086 
00087         return $iCnt;
00088     }
00089 
00097     public function getManufacturerArticleCount($sManufacturerId)
00098     {
00099         // current category unique ident
00100         $sActIdent = $this->_getUserViewId();
00101 
00102         // loading from cache
00103         $aManufacturerData = $this->_getManufacturerCache();
00104         if (!$aManufacturerData || !isset($aManufacturerData[$sManufacturerId][$sActIdent])) {
00105             $iCnt = $this->setManufacturerArticleCount($aManufacturerData, $sManufacturerId, $sActIdent);
00106         } else {
00107             $iCnt = $aManufacturerData[$sManufacturerId][$sActIdent];
00108         }
00109 
00110         return $iCnt;
00111     }
00112 
00122     public function setCatArticleCount($aCache, $sCatId, $sActIdent)
00123     {
00124         $oArticle = oxNew('oxarticle');
00125         $sTable = $oArticle->getViewName();
00126         $sO2CView = getViewName('oxobject2category');
00127         $oDb = oxDb::getDb();
00128 
00129         // we use distinct if article is assigned to category twice
00130         $sQ = "SELECT COUNT( DISTINCT $sTable.`oxid` )
00131                FROM $sO2CView
00132                    INNER JOIN $sTable ON $sO2CView.`oxobjectid` = $sTable.`oxid` AND $sTable.`oxparentid` = ''
00133                WHERE $sO2CView.`oxcatnid` = " . $oDb->quote($sCatId) . " AND " . $oArticle->getSqlActiveSnippet();
00134 
00135         $aCache[$sCatId][$sActIdent] = $oDb->getOne($sQ);
00136 
00137         $this->_setCatCache($aCache);
00138 
00139         return $aCache[$sCatId][$sActIdent];
00140     }
00141 
00153     public function setPriceCatArticleCount($aCache, $sCatId, $sActIdent, $dPriceFrom, $dPriceTo)
00154     {
00155         $oArticle = oxNew('oxarticle');
00156         $sTable = $oArticle->getViewName();
00157 
00158         $sSelect = "select count({$sTable}.oxid) from {$sTable} where oxvarminprice >= 0 ";
00159         $sSelect .= $dPriceTo ? "and oxvarminprice <= " . (double) $dPriceTo . " " : " ";
00160         $sSelect .= $dPriceFrom ? "and oxvarminprice  >= " . (double) $dPriceFrom . " " : " ";
00161         $sSelect .= "and {$sTable}.oxissearch = 1 and " . $oArticle->getSqlActiveSnippet();
00162 
00163         $aCache[$sCatId][$sActIdent] = oxDb::getDb()->getOne($sSelect);
00164 
00165         $this->_setCatCache($aCache);
00166 
00167         return $aCache[$sCatId][$sActIdent];
00168     }
00169 
00179     public function setVendorArticleCount($aCache, $sCatId, $sActIdent)
00180     {
00181         // if vendor/category name is 'root', skip counting
00182         if ($sCatId == 'root') {
00183             return 0;
00184         }
00185 
00186         $oArticle = oxNew('oxarticle');
00187         $sTable = $oArticle->getViewName();
00188 
00189         // select each vendor articles count
00190         $sQ = "select $sTable.oxvendorid AS vendorId, count(*) from $sTable where ";
00191         $sQ .= "$sTable.oxvendorid <> '' and $sTable.oxparentid = '' and " . $oArticle->getSqlActiveSnippet() . " group by $sTable.oxvendorid ";
00192         $aDbResult = oxDb::getDb()->getAssoc($sQ);
00193 
00194         foreach ($aDbResult as $sKey => $sValue) {
00195             $aCache[$sKey][$sActIdent] = $sValue;
00196         }
00197 
00198         $this->_setVendorCache($aCache);
00199 
00200         return $aCache[$sCatId][$sActIdent];
00201     }
00202 
00212     public function setManufacturerArticleCount($aCache, $sMnfId, $sActIdent)
00213     {
00214         // if Manufacturer/category name is 'root', skip counting
00215         if ($sMnfId == 'root') {
00216             return 0;
00217         }
00218 
00219         $oArticle = oxNew('oxarticle');
00220         $sArtTable = $oArticle->getViewName();
00221         $sManTable = getViewName('oxmanufacturers');
00222 
00223         // select each Manufacturer articles count
00224         //#3485
00225         $sQ = "select count($sArtTable.oxid) from $sArtTable where $sArtTable.oxparentid = '' and oxmanufacturerid = '$sMnfId' and " . $oArticle->getSqlActiveSnippet();
00226 
00227         $iValue = oxDb::getDb()->getOne($sQ);
00228 
00229         $aCache[$sMnfId][$sActIdent] = (int) $iValue;
00230 
00231         $this->_setManufacturerCache($aCache);
00232 
00233         return $aCache[$sMnfId][$sActIdent];
00234     }
00235 
00241     public function resetCatArticleCount($sCatId = null)
00242     {
00243         if (!$sCatId) {
00244             $this->getConfig()->setGlobalParameter('aLocalCatCache', null);
00245             oxRegistry::getUtils()->toFileCache('aLocalCatCache', '');
00246         } else {
00247             // loading from cache
00248             $aCatData = $this->_getCatCache();
00249             if (isset($aCatData[$sCatId])) {
00250                 unset($aCatData[$sCatId]);
00251                 $this->_setCatCache($aCatData);
00252             }
00253         }
00254     }
00255 
00261     public function resetPriceCatArticleCount($iPrice)
00262     {
00263         // loading from cache
00264         if ($aCatData = $this->_getCatCache()) {
00265 
00266             $sTable = getViewName('oxcategories');
00267             $sSelect = "select $sTable.oxid from $sTable where " . (double) $iPrice . " >= $sTable.oxpricefrom and " . (double) $iPrice . " <= $sTable.oxpriceto ";
00268 
00269             $rs = oxDb::getDb()->select($sSelect, false, false);
00270             if ($rs != false && $rs->recordCount() > 0) {
00271                 while (!$rs->EOF) {
00272                     if (isset($aCatData[$rs->fields[0]])) {
00273                         unset($aCatData[$rs->fields[0]]);
00274                     }
00275                     $rs->moveNext();
00276                 }
00277 
00278                 // writing back to cache
00279                 $this->_setCatCache($aCatData);
00280             }
00281         }
00282     }
00283 
00292     public function getTagArticleCount($sTag, $iLang)
00293     {
00294         $oDb = oxDb::getDb();
00295 
00296         $oArticle = oxNew("oxarticle");
00297         $sArticleTable = $oArticle->getViewName();
00298         $sActiveSnippet = $oArticle->getSqlActiveSnippet();
00299         $sViewName = getViewName('oxartextends', $iLang);
00300 
00301         $sQ = "select count(*) from {$sViewName} inner join {$sArticleTable} on " .
00302               "{$sArticleTable}.oxid = {$sViewName}.oxid where {$sArticleTable}.oxparentid = '' and {$sArticleTable}.oxissearch = 1 AND match ( {$sViewName}.oxtags ) " .
00303               "against( " . $oDb->quote("\"" . $sTag . "\"") . " IN BOOLEAN MODE ) and {$sActiveSnippet}";
00304 
00305         return $oDb->getOne($sQ);
00306     }
00307 
00313     public function resetVendorArticleCount($sVendorId = null)
00314     {
00315         if (!$sVendorId) {
00316             $this->getConfig()->setGlobalParameter('aLocalVendorCache', null);
00317             oxRegistry::getUtils()->toFileCache('aLocalVendorCache', '');
00318         } else {
00319             // loading from cache
00320             $aVendorData = $this->_getVendorCache();
00321             if (isset($aVendorData[$sVendorId])) {
00322                 unset($aVendorData[$sVendorId]);
00323                 $this->_setVendorCache($aVendorData);
00324             }
00325         }
00326     }
00327 
00333     public function resetManufacturerArticleCount($sManufacturerId = null)
00334     {
00335         if (!$sManufacturerId) {
00336             $this->getConfig()->setGlobalParameter('aLocalManufacturerCache', null);
00337             oxRegistry::getUtils()->toFileCache('aLocalManufacturerCache', '');
00338         } else {
00339             // loading from cache
00340             $aManufacturerData = $this->_getManufacturerCache();
00341             if (isset($aManufacturerData[$sManufacturerId])) {
00342                 unset($aManufacturerData[$sManufacturerId]);
00343                 $this->_setManufacturerCache($aManufacturerData);
00344             }
00345         }
00346     }
00347 
00353     protected function _getCatCache()
00354     {
00355         $myConfig = $this->getConfig();
00356 
00357         // first look at the local cache
00358         $aLocalCatCache = $myConfig->getGlobalParameter('aLocalCatCache');
00359 
00360         // if local cache is not set - loading from file cache
00361         if (!$aLocalCatCache) {
00362             $sLocalCatCache = oxRegistry::getUtils()->fromFileCache('aLocalCatCache');
00363             if ($sLocalCatCache) {
00364                 $aLocalCatCache = $sLocalCatCache;
00365             } else {
00366                 $aLocalCatCache = null;
00367             }
00368             $myConfig->setGlobalParameter('aLocalCatCache', $aLocalCatCache);
00369         }
00370 
00371         return $aLocalCatCache;
00372     }
00373 
00379     protected function _setCatCache($aCache)
00380     {
00381         $this->getConfig()->setGlobalParameter('aLocalCatCache', $aCache);
00382         oxRegistry::getUtils()->toFileCache('aLocalCatCache', $aCache);
00383     }
00384 
00390     protected function _setVendorCache($aCache)
00391     {
00392         $this->getConfig()->setGlobalParameter('aLocalVendorCache', $aCache);
00393         oxRegistry::getUtils()->toFileCache('aLocalVendorCache', $aCache);
00394     }
00395 
00401     protected function _setManufacturerCache($aCache)
00402     {
00403         $this->getConfig()->setGlobalParameter('aLocalManufacturerCache', $aCache);
00404         oxRegistry::getUtils()->toFileCache('aLocalManufacturerCache', $aCache);
00405     }
00406 
00412     protected function _getVendorCache()
00413     {
00414         $myConfig = $this->getConfig();
00415 
00416         // first look at the local cache
00417         $aLocalVendorCache = $myConfig->getGlobalParameter('aLocalVendorCache');
00418         // if local cache is not set - loading from file cache
00419         if (!$aLocalVendorCache) {
00420             $sLocalVendorCache = oxRegistry::getUtils()->fromFileCache('aLocalVendorCache');
00421             if ($sLocalVendorCache) {
00422                 $aLocalVendorCache = $sLocalVendorCache;
00423             } else {
00424                 $aLocalVendorCache = null;
00425             }
00426             $myConfig->setGlobalParameter('aLocalVendorCache', $aLocalVendorCache);
00427         }
00428 
00429         return $aLocalVendorCache;
00430     }
00431 
00437     protected function _getManufacturerCache()
00438     {
00439         $myConfig = $this->getConfig();
00440 
00441         // first look at the local cache
00442         $aLocalManufacturerCache = $myConfig->getGlobalParameter('aLocalManufacturerCache');
00443         // if local cache is not set - loading from file cache
00444         if (!$aLocalManufacturerCache) {
00445             $sLocalManufacturerCache = oxRegistry::getUtils()->fromFileCache('aLocalManufacturerCache');
00446             if ($sLocalManufacturerCache) {
00447                 $aLocalManufacturerCache = $sLocalManufacturerCache;
00448             } else {
00449                 $aLocalManufacturerCache = null;
00450             }
00451             $myConfig->setGlobalParameter('aLocalManufacturerCache', $aLocalManufacturerCache);
00452         }
00453 
00454         return $aLocalManufacturerCache;
00455     }
00456 
00464     protected function _getUserViewId($blReset = false)
00465     {
00466         if ($this->_sUserViewId != null && !$blReset) {
00467             return $this->_sUserViewId;
00468         }
00469 
00470         // loading R&R data from session
00471         $aRRIdx = null;
00472 
00473         $this->_sUserViewId = md5($this->getConfig()->getShopID() . oxRegistry::getLang()->getLanguageTag() . serialize($aRRIdx) . (int) $this->isAdmin());
00474 
00475         return $this->_sUserViewId;
00476     }
00477 }