00001 <?php
00002
00003 if (!defined('OXTAGCLOUD_MINFONT')) {
00004 define('OXTAGCLOUD_MINFONT', 100);
00005 define('OXTAGCLOUD_MAXFONT', 400);
00006 define('OXTAGCLOUD_MINOCCURENCETOSHOW', 2);
00007
00008 define('OXTAGCLOUD_MINTAGLENGTH', 4);
00009 define('OXTAGCLOUD_STARTPAGECOUNT', 20);
00010 define('OXTAGCLOUD_EXTENDEDCOUNT', 200);
00011 }
00012
00017 class oxTagCloud extends oxSuperCfg
00018 {
00024 protected $_sCacheKey = "tagcloud_";
00025
00035 public function getTags($sArtId = null, $blExtended = false, $iLang = null )
00036 {
00037 $oDb = oxDb::getDb(true);
00038 if ($blExtended) {
00039 $iAmount = OXTAGCLOUD_EXTENDEDCOUNT;
00040 } else {
00041 $iAmount = OXTAGCLOUD_STARTPAGECOUNT;
00042 }
00043
00044 $sArticleSelect = " 1 ";
00045 if ( $sArtId ) {
00046 $sArticleSelect = " oxarticles.oxid = ".$oDb->quote( $sArtId )." ";
00047 $iAmount = 0;
00048 }
00049
00050 $sField = "oxartextends.oxtags".oxLang::getInstance()->getLanguageTag( $iLang );
00051
00052 $sArtView = getViewName('oxarticles');
00053 $sQ = "select $sField as oxtags from $sArtView as oxarticles left join oxartextends on oxarticles.oxid=oxartextends.oxid where oxarticles.oxactive=1 AND $sArticleSelect";
00054
00055 $rs = $oDb->execute( $sQ );
00056 $aTags = array();
00057 while ( $rs && $rs->recordCount() && !$rs->EOF ) {
00058 $sTags = $this->trimTags( $rs->fields['oxtags'] );
00059 $aArticleTags = explode( ' ', $sTags );
00060 foreach ( $aArticleTags as $sTag ) {
00061 if ( trim( $sTag ) ) {
00062 ++$aTags[$sTag];
00063 }
00064 }
00065 $rs->moveNext();
00066 }
00067
00068
00069 if ( $iAmount ) {
00070 arsort($aTags);
00071 $aTags = array_slice($aTags, 0, $iAmount, true );
00072 }
00073
00074 $aTags = $this->_sortTags( $aTags );
00075 return $aTags;
00076 }
00077
00086 protected function _sortTags( $aTags, $iLang = null )
00087 {
00088 if ( is_array( $aTags ) && count( $aTags ) ) {
00089 $oDb = oxDb::getDb( true );
00090 $sSubQ = '';
00091 foreach ( $aTags as $sKey => $sTag ) {
00092 if ( $sSubQ ) {
00093 $sSubQ .= ' union all ';
00094 }
00095 $sSubQ .= 'select '.$oDb->quote( $sKey ).' as _oxsort, '.$oDb->quote( $sTag ).' as _oxval';
00096 }
00097
00098 $sField = "oxartextends.oxtags".oxLang::getInstance()->getLanguageTag( $iLang );
00099
00100
00101 $sSubQ = "select {$sField} as _oxsort, 'ox_skip' as _oxval from oxartextends limit 1 union $sSubQ";
00102
00103 $sQ = "select _oxtable._oxsort, _oxtable._oxval from ( {$sSubQ} ) as _oxtable order by _oxtable._oxsort desc";
00104
00105 $aTags = array();
00106 $oRs = $oDb->execute( $sQ );
00107 while ( $oRs && $oRs->recordCount() && !$oRs->EOF ) {
00108 if ( $oRs->fields['_oxval'] != 'ox_skip' ) {
00109 $aTags[$oRs->fields['_oxsort']] = $oRs->fields['_oxval'];
00110 }
00111 $oRs->moveNext();
00112 }
00113 }
00114 return $aTags;
00115 }
00116
00126 public function getTagCloud($sArtId = null, $blExtended = false, $iLang = null )
00127 {
00128 $myUtils = oxUtils::getInstance();
00129
00130 $sTagCloud = null;
00131 $sCacheKey = $this->_getCacheKey($blExtended, $iLang );
00132 if ( $this->_sCacheKey && !$sArtId ) {
00133 $sTagCloud = $myUtils->fromFileCache( $sCacheKey );
00134 }
00135
00136 if ( !is_null($sTagCloud) ) {
00137 return $sTagCloud;
00138 }
00139
00140 $aTags = $this->getTags($sArtId, $blExtended, $iLang);
00141 if (!count($aTags)) {
00142 if ($this->_sCacheKey && !$sArtId) {
00143 $sTagCloud = false;
00144 $myUtils->toFileCache($sCacheKey, $sTagCloud);
00145 }
00146 return $sTagCloud;
00147 }
00148
00149 $iMaxHit = max( $aTags);
00150 $blSeoIsActive = $myUtils->seoIsActive();
00151 $oSeoEncoderTag = oxSeoEncoderTag::getInstance();
00152
00153 $iLang = ( $iLang !== null ) ? $iLang : oxLang::getInstance()->getBaseLanguage();
00154 $sUrl = $this->getConfig()->getShopUrl();
00155 $oStr = getStr();
00156
00157 $sTagCloud = false;
00158 foreach ( $aTags as $sTag => $sRelevance ) {
00159 if ( $blSeoIsActive ) {
00160 $sLink = $oSeoEncoderTag->getTagUrl( $sTag, $iLang );
00161 } else {
00162 $sLink = $sUrl . $oSeoEncoderTag->getStdTagUri( $sTag ) . "&lang=" . $iLang;
00163 }
00164 $iFontSize = $this->_getFontSize( $sRelevance, $iMaxHit );
00165 $sTagCloud .= "<a style='font-size:". $iFontSize ."%;' class='tagitem_". $iFontSize . "' href='$sLink'>".$oStr->htmlentities($sTag)."</a> ";
00166 }
00167
00168 if ( $this->_sCacheKey && !$sArtId ) {
00169 $myUtils->toFileCache( $sCacheKey, $sTagCloud );
00170 }
00171
00172 return $sTagCloud;
00173 }
00174
00183 protected function _getFontSize($iHit, $iMaxHit)
00184 {
00185
00186 if ($iMaxHit <= OXTAGCLOUD_MINOCCURENCETOSHOW || !$iMaxHit) {
00187 return OXTAGCLOUD_MINFONT;
00188 }
00189
00190 $iFontDiff = OXTAGCLOUD_MAXFONT - OXTAGCLOUD_MINFONT;
00191 $iMaxHitDiff = $iMaxHit - OXTAGCLOUD_MINOCCURENCETOSHOW;
00192 $iHitDiff = $iHit - OXTAGCLOUD_MINOCCURENCETOSHOW;
00193
00194 if ($iHitDiff < 0) {
00195 $iHitDiff = 0;
00196 }
00197
00198 $iSize = round($iHitDiff * $iFontDiff / $iMaxHitDiff) + OXTAGCLOUD_MINFONT;
00199
00200 return $iSize;
00201 }
00202
00211 public function prepareTags( $sTags )
00212 {
00213 $aTags = explode( ' ', $sTags );
00214 $sRes = '';
00215 $oStr = getStr();
00216 foreach ( $aTags as $sTag ) {
00217 if ( ( $iLen = $oStr->strlen( $sTag ) ) ) {
00218 if ( $iLen < OXTAGCLOUD_MINTAGLENGTH ) {
00219 $sTag .= str_repeat( '_', OXTAGCLOUD_MINTAGLENGTH - $iLen );
00220 }
00221
00222 $sRes .= $oStr->strtolower( $sTag ) . " ";
00223 }
00224 }
00225
00226 return trim( $sRes );
00227 }
00228
00236 public function trimTags($sTags)
00237 {
00238 $aTags = explode(' ', $sTags);
00239 $sRes = '';
00240 $oStr = getStr();
00241 foreach ( $aTags as $sTag ) {
00242 if ( $oStr->strlen( $sTag ) ) {
00243 $sRes .= rtrim( $sTag, '_' ) . " ";
00244 }
00245 }
00246
00247 return trim( $sRes );
00248 }
00249
00257 public function resetTagCache( $iLang = null )
00258 {
00259 $myUtils = oxUtils::getInstance();
00260
00261 $sCacheKey1 = $this->_getCacheKey( true, $iLang );
00262 $myUtils->toFileCache( $sCacheKey1, null );
00263
00264 $sCacheKey2 = $this->_getCacheKey( false, $iLang );
00265 $myUtils->toFileCache( $sCacheKey2, null );
00266 }
00267
00276 protected function _getCacheKey( $blExtended, $iLang = null )
00277 {
00278 return $this->_sCacheKey."_".$this->getConfig()->getShopId()."_".( ( $iLang !== null ) ? $iLang : oxLang::getInstance()->getBaseLanguage() ) ."_".$blExtended;
00279 }
00280
00281 }