oxtagcloud.php

Go to the documentation of this file.
00001 <?php
00002 
00003 if (!defined('OXTAGCLOUD_MINFONT')) {
00004     define('OXTAGCLOUD_MINFONT', 100);
00005     define('OXTAGCLOUD_MAXFONT', 400);
00006     define('OXTAGCLOUD_MINOCCURENCETOSHOW', 2);
00007     define('OXTAGCLOUD_STARTPAGECOUNT', 20);
00008     define('OXTAGCLOUD_EXTENDEDCOUNT', 200);
00009 }
00010 
00015 class oxTagCloud extends oxSuperCfg
00016 {
00017 
00023     protected $_sCacheKey = "tagcloud";
00024 
00030     protected $_iMaxHit = null;
00031 
00037     protected $_aCloudArray = null;
00038 
00044     protected $_blExtended = false;
00045 
00046 
00053     protected $_iTagMaxLength = 60;
00054 
00058     public function __construct()
00059     {
00060     }
00061 
00067     public function getTagMaxLength()
00068     {
00069         return $this->_iTagMaxLength;
00070     }
00071 
00077     public function setExtendedMode($blExtended)
00078     {
00079         $this->_blExtended = $blExtended;
00080     }
00081 
00087     public function isExtended()
00088     {
00089         return $this->_blExtended;
00090     }
00091 
00097     public function setTagList(oxITagList $oTagList)
00098     {
00099         $this->_oTagList = $oTagList;
00100     }
00101 
00107     public function getTagList()
00108     {
00109         return $this->_oTagList;
00110     }
00111 
00117     public function setCloudArray($aTagCloudArray)
00118     {
00119         $sCacheIdent = $this->_formCacheKey();
00120         $this->_aCloudArray[$sCacheIdent] = $aTagCloudArray;
00121     }
00122 
00128     public function getCloudArray()
00129     {
00130         $sCacheIdent = $this->_formCacheKey();
00131         if (!isset($this->_aCloudArray[$sCacheIdent])) {
00132             $oTagList = $this->getTagList();
00133 
00134             $this->_aCloudArray[$sCacheIdent] = $this->formCloudArray($oTagList);
00135         }
00136 
00137         return $this->_aCloudArray[$sCacheIdent];
00138     }
00139 
00147     public function formCloudArray(oxITagList $oTagList)
00148     {
00149         // checking if current data is already loaded
00150         if ($oTagList->getCacheId()) {
00151             $sCacheIdent = $this->_formCacheKey($oTagList->getCacheId());
00152             $myUtils = oxRegistry::getUtils();
00153             // checking cache
00154             $aCloudArray = $myUtils->fromFileCache($sCacheIdent);
00155         }
00156 
00157         // loading cloud info
00158         if ($aCloudArray === null) {
00159             $oTagList->loadList();
00160             $oTagSet = $oTagList->get();
00161             if (count($oTagSet->get()) > $this->getMaxAmount()) {
00162                 $oTagSet->sortByHitCount();
00163                 $oTagSet->slice(0, $this->getMaxAmount());
00164             }
00165             $oTagSet->sort();
00166             $aCloudArray = $oTagSet->get();
00167             // updating cache
00168             if ($sCacheIdent) {
00169                 $myUtils->toFileCache($sCacheIdent, $aCloudArray);
00170             }
00171         }
00172 
00173         return $aCloudArray;
00174     }
00175 
00183     public function getTagSize($sTag)
00184     {
00185         $aCloudArray = $this->getCloudArray();
00186         if (is_null($aCloudArray[$sTag])) {
00187             return 1;
00188         }
00189         $iCurrSize = $this->_getFontSize($aCloudArray[$sTag]->getHitCount(), $this->_getMaxHit());
00190 
00191         // calculating min size
00192         return floor($iCurrSize / OXTAGCLOUD_MINFONT) * OXTAGCLOUD_MINFONT;
00193     }
00194 
00200     public function getMaxAmount()
00201     {
00202         if ($this->isExtended()) {
00203             return OXTAGCLOUD_EXTENDEDCOUNT;
00204         } else {
00205             return OXTAGCLOUD_STARTPAGECOUNT;
00206         }
00207     }
00208 
00214     public function resetTagCache($iLang = null)
00215     {
00216         if ($iLang) {
00217             $this->setLanguageId($iLang);
00218         }
00219         $this->resetCache();
00220     }
00221 
00225     public function resetCache()
00226     {
00227         $myUtils = oxRegistry::getUtils();
00228 
00229         $sCacheId = null;
00230         if (($oTagList = $this->getTagList()) !== null) {
00231             $sCacheId = $oTagList->getCacheId();
00232         }
00233 
00234         $myUtils->toFileCache($this->_formCacheKey($sCacheId), null);
00235 
00236         $this->_aCloudArray = null;
00237     }
00238 
00246     protected function _formCacheKey($sTagListCacheId = null)
00247     {
00248         $sExtended = $this->isExtended() ? '1' : '0';
00249 
00250         return $this->_sCacheKey . "_" . $this->getConfig()->getShopId() . "_" . $sExtended . "_" . $sTagListCacheId;
00251     }
00252 
00258     protected function _getMaxHit()
00259     {
00260         if ($this->_iMaxHit === null) {
00261             $aHits = array_map(array($this, '_getTagHitCount'), $this->getCloudArray());
00262             $this->_iMaxHit = max($aHits);
00263         }
00264 
00265         return $this->_iMaxHit;
00266     }
00267 
00275     protected function _getTagHitCount($oTag)
00276     {
00277         return $oTag->getHitCount();
00278     }
00279 
00288     protected function _getFontSize($iHit, $iMaxHit)
00289     {
00290         //handling special case
00291         if ($iMaxHit <= OXTAGCLOUD_MINOCCURENCETOSHOW || !$iMaxHit) {
00292             return OXTAGCLOUD_MINFONT;
00293         }
00294 
00295         $iFontDiff = OXTAGCLOUD_MAXFONT - OXTAGCLOUD_MINFONT;
00296         $iMaxHitDiff = $iMaxHit - OXTAGCLOUD_MINOCCURENCETOSHOW;
00297         $iHitDiff = $iHit - OXTAGCLOUD_MINOCCURENCETOSHOW;
00298 
00299         if ($iHitDiff < 0) {
00300             $iHitDiff = 0;
00301         }
00302 
00303         $iSize = round($iHitDiff * $iFontDiff / $iMaxHitDiff) + OXTAGCLOUD_MINFONT;
00304 
00305         return $iSize;
00306     }
00307 }