OXID eShop CE  4.10.7
 All Classes Namespaces Files Functions Variables Pages
oxtagcloud.php
Go to the documentation of this file.
1 <?php
2 
3 // @deprecated v5.3 (2016-05-04); Tags will be moved to own module.
4 if (!defined('OXTAGCLOUD_MINFONT')) {
5  define('OXTAGCLOUD_MINFONT', 100);
6  define('OXTAGCLOUD_MAXFONT', 400);
7  define('OXTAGCLOUD_MINOCCURENCETOSHOW', 2);
8  define('OXTAGCLOUD_STARTPAGECOUNT', 20);
9  define('OXTAGCLOUD_EXTENDEDCOUNT', 200);
10 }
11 // END deprecated
12 
18 class oxTagCloud extends oxSuperCfg
19 {
20 
26  protected $_sCacheKey = "tagcloud";
27 
33  protected $_iMaxHit = null;
34 
40  protected $_aCloudArray = null;
41 
47  protected $_blExtended = false;
48 
49 
56  protected $_iTagMaxLength = 60;
57 
61  public function __construct()
62  {
63  }
64 
70  public function getTagMaxLength()
71  {
72  return $this->_iTagMaxLength;
73  }
74 
80  public function setExtendedMode($blExtended)
81  {
82  $this->_blExtended = $blExtended;
83  }
84 
90  public function isExtended()
91  {
92  return $this->_blExtended;
93  }
94 
100  public function setTagList(oxITagList $oTagList)
101  {
102  $this->_oTagList = $oTagList;
103  }
104 
110  public function getTagList()
111  {
112  return $this->_oTagList;
113  }
114 
120  public function setCloudArray($aTagCloudArray)
121  {
122  $sCacheIdent = $this->_formCacheKey();
123  $this->_aCloudArray[$sCacheIdent] = $aTagCloudArray;
124  }
125 
131  public function getCloudArray()
132  {
133  $sCacheIdent = $this->_formCacheKey();
134  if (!isset($this->_aCloudArray[$sCacheIdent])) {
135  $oTagList = $this->getTagList();
136 
137  $this->_aCloudArray[$sCacheIdent] = $this->formCloudArray($oTagList);
138  }
139 
140  return $this->_aCloudArray[$sCacheIdent];
141  }
142 
150  public function formCloudArray(oxITagList $oTagList)
151  {
152  // checking if current data is already loaded
153  if ($oTagList->getCacheId()) {
154  $sCacheIdent = $this->_formCacheKey($oTagList->getCacheId());
155  $myUtils = oxRegistry::getUtils();
156  // checking cache
157  $aCloudArray = $myUtils->fromFileCache($sCacheIdent);
158  }
159 
160  // loading cloud info
161  if ($aCloudArray === null) {
162  $oTagList->loadList();
163  $oTagSet = $oTagList->get();
164  if (count($oTagSet->get()) > $this->getMaxAmount()) {
165  $oTagSet->sortByHitCount();
166  $oTagSet->slice(0, $this->getMaxAmount());
167  }
168  $oTagSet->sort();
169  $aCloudArray = $oTagSet->get();
170  // updating cache
171  if ($sCacheIdent) {
172  $myUtils->toFileCache($sCacheIdent, $aCloudArray);
173  }
174  }
175 
176  return $aCloudArray;
177  }
178 
186  public function getTagSize($sTag)
187  {
188  $aCloudArray = $this->getCloudArray();
189  if (is_null($aCloudArray[$sTag])) {
190  return 1;
191  }
192  $iCurrSize = $this->_getFontSize($aCloudArray[$sTag]->getHitCount(), $this->_getMaxHit());
193 
194  // calculating min size
195  return floor($iCurrSize / OXTAGCLOUD_MINFONT) * OXTAGCLOUD_MINFONT;
196  }
197 
203  public function getMaxAmount()
204  {
205  if ($this->isExtended()) {
206  return OXTAGCLOUD_EXTENDEDCOUNT;
207  } else {
208  return OXTAGCLOUD_STARTPAGECOUNT;
209  }
210  }
211 
217  public function resetTagCache($iLang = null)
218  {
219  if ($iLang) {
220  $this->setLanguageId($iLang);
221  }
222  $this->resetCache();
223  }
224 
228  public function resetCache()
229  {
230  $myUtils = oxRegistry::getUtils();
231 
232  $sCacheId = null;
233  if (($oTagList = $this->getTagList()) !== null) {
234  $sCacheId = $oTagList->getCacheId();
235  }
236 
237  $myUtils->toFileCache($this->_formCacheKey($sCacheId), null);
238 
239  $this->_aCloudArray = null;
240  }
241 
249  protected function _formCacheKey($sTagListCacheId = null)
250  {
251  $sExtended = $this->isExtended() ? '1' : '0';
252 
253  return $this->_sCacheKey . "_" . $this->getConfig()->getShopId() . "_" . $sExtended . "_" . $sTagListCacheId;
254  }
255 
261  protected function _getMaxHit()
262  {
263  if ($this->_iMaxHit === null) {
264  $aHits = array_map(array($this, '_getTagHitCount'), $this->getCloudArray());
265  $this->_iMaxHit = max($aHits);
266  }
267 
268  return $this->_iMaxHit;
269  }
270 
278  protected function _getTagHitCount($oTag)
279  {
280  return $oTag->getHitCount();
281  }
282 
291  protected function _getFontSize($iHit, $iMaxHit)
292  {
293  //handling special case
294  if ($iMaxHit <= OXTAGCLOUD_MINOCCURENCETOSHOW || !$iMaxHit) {
295  return OXTAGCLOUD_MINFONT;
296  }
297 
298  $iFontDiff = OXTAGCLOUD_MAXFONT - OXTAGCLOUD_MINFONT;
299  $iMaxHitDiff = $iMaxHit - OXTAGCLOUD_MINOCCURENCETOSHOW;
300  $iHitDiff = $iHit - OXTAGCLOUD_MINOCCURENCETOSHOW;
301 
302  if ($iHitDiff < 0) {
303  $iHitDiff = 0;
304  }
305 
306  $iSize = round($iHitDiff * $iFontDiff / $iMaxHitDiff) + OXTAGCLOUD_MINFONT;
307 
308  return $iSize;
309  }
310 }