OXID eShop CE  4.9.6
 All Classes Files Functions Variables Pages
oxtagcloud.php
Go to the documentation of this file.
1 <?php
2 
3 if (!defined('OXTAGCLOUD_MINFONT')) {
4  define('OXTAGCLOUD_MINFONT', 100);
5  define('OXTAGCLOUD_MAXFONT', 400);
6  define('OXTAGCLOUD_MINOCCURENCETOSHOW', 2);
7  define('OXTAGCLOUD_STARTPAGECOUNT', 20);
8  define('OXTAGCLOUD_EXTENDEDCOUNT', 200);
9 }
10 
15 class oxTagCloud extends oxSuperCfg
16 {
17 
23  protected $_sCacheKey = "tagcloud";
24 
30  protected $_iMaxHit = null;
31 
37  protected $_aCloudArray = null;
38 
44  protected $_blExtended = false;
45 
46 
53  protected $_iTagMaxLength = 60;
54 
58  public function __construct()
59  {
60  }
61 
67  public function getTagMaxLength()
68  {
69  return $this->_iTagMaxLength;
70  }
71 
77  public function setExtendedMode($blExtended)
78  {
79  $this->_blExtended = $blExtended;
80  }
81 
87  public function isExtended()
88  {
89  return $this->_blExtended;
90  }
91 
97  public function setTagList(oxITagList $oTagList)
98  {
99  $this->_oTagList = $oTagList;
100  }
101 
107  public function getTagList()
108  {
109  return $this->_oTagList;
110  }
111 
117  public function setCloudArray($aTagCloudArray)
118  {
119  $sCacheIdent = $this->_formCacheKey();
120  $this->_aCloudArray[$sCacheIdent] = $aTagCloudArray;
121  }
122 
128  public function getCloudArray()
129  {
130  $sCacheIdent = $this->_formCacheKey();
131  if (!isset($this->_aCloudArray[$sCacheIdent])) {
132  $oTagList = $this->getTagList();
133 
134  $this->_aCloudArray[$sCacheIdent] = $this->formCloudArray($oTagList);
135  }
136 
137  return $this->_aCloudArray[$sCacheIdent];
138  }
139 
147  public function formCloudArray(oxITagList $oTagList)
148  {
149  // checking if current data is already loaded
150  if ($oTagList->getCacheId()) {
151  $sCacheIdent = $this->_formCacheKey($oTagList->getCacheId());
152  $myUtils = oxRegistry::getUtils();
153  // checking cache
154  $aCloudArray = $myUtils->fromFileCache($sCacheIdent);
155  }
156 
157  // loading cloud info
158  if ($aCloudArray === null) {
159  $oTagList->loadList();
160  $oTagSet = $oTagList->get();
161  if (count($oTagSet->get()) > $this->getMaxAmount()) {
162  $oTagSet->sortByHitCount();
163  $oTagSet->slice(0, $this->getMaxAmount());
164  }
165  $oTagSet->sort();
166  $aCloudArray = $oTagSet->get();
167  // updating cache
168  if ($sCacheIdent) {
169  $myUtils->toFileCache($sCacheIdent, $aCloudArray);
170  }
171  }
172 
173  return $aCloudArray;
174  }
175 
183  public function getTagSize($sTag)
184  {
185  $aCloudArray = $this->getCloudArray();
186  if (is_null($aCloudArray[$sTag])) {
187  return 1;
188  }
189  $iCurrSize = $this->_getFontSize($aCloudArray[$sTag]->getHitCount(), $this->_getMaxHit());
190 
191  // calculating min size
192  return floor($iCurrSize / OXTAGCLOUD_MINFONT) * OXTAGCLOUD_MINFONT;
193  }
194 
200  public function getMaxAmount()
201  {
202  if ($this->isExtended()) {
203  return OXTAGCLOUD_EXTENDEDCOUNT;
204  } else {
205  return OXTAGCLOUD_STARTPAGECOUNT;
206  }
207  }
208 
214  public function resetTagCache($iLang = null)
215  {
216  if ($iLang) {
217  $this->setLanguageId($iLang);
218  }
219  $this->resetCache();
220  }
221 
225  public function resetCache()
226  {
227  $myUtils = oxRegistry::getUtils();
228 
229  $sCacheId = null;
230  if (($oTagList = $this->getTagList()) !== null) {
231  $sCacheId = $oTagList->getCacheId();
232  }
233 
234  $myUtils->toFileCache($this->_formCacheKey($sCacheId), null);
235 
236  $this->_aCloudArray = null;
237  }
238 
246  protected function _formCacheKey($sTagListCacheId = null)
247  {
248  $sExtended = $this->isExtended() ? '1' : '0';
249 
250  return $this->_sCacheKey . "_" . $this->getConfig()->getShopId() . "_" . $sExtended . "_" . $sTagListCacheId;
251  }
252 
258  protected function _getMaxHit()
259  {
260  if ($this->_iMaxHit === null) {
261  $aHits = array_map(array($this, '_getTagHitCount'), $this->getCloudArray());
262  $this->_iMaxHit = max($aHits);
263  }
264 
265  return $this->_iMaxHit;
266  }
267 
275  protected function _getTagHitCount($oTag)
276  {
277  return $oTag->getHitCount();
278  }
279 
288  protected function _getFontSize($iHit, $iMaxHit)
289  {
290  //handling special case
291  if ($iMaxHit <= OXTAGCLOUD_MINOCCURENCETOSHOW || !$iMaxHit) {
292  return OXTAGCLOUD_MINFONT;
293  }
294 
295  $iFontDiff = OXTAGCLOUD_MAXFONT - OXTAGCLOUD_MINFONT;
296  $iMaxHitDiff = $iMaxHit - OXTAGCLOUD_MINOCCURENCETOSHOW;
297  $iHitDiff = $iHit - OXTAGCLOUD_MINOCCURENCETOSHOW;
298 
299  if ($iHitDiff < 0) {
300  $iHitDiff = 0;
301  }
302 
303  $iSize = round($iHitDiff * $iFontDiff / $iMaxHitDiff) + OXTAGCLOUD_MINFONT;
304 
305  return $iSize;
306  }
307 }