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 $sArtId = $oDb->quote( $sArtId );
00047 $sArticleSelect = " oxarticles.oxid = $sArtId ";
00048 $iAmount = 0;
00049 }
00050
00051 $sField = "oxartextends.oxtags".oxLang::getInstance()->getLanguageTag( $iLang );
00052
00053 $sArtView = getViewName('oxarticles');
00054 $sQ = "select $sField as oxtags from $sArtView as oxarticles left join oxartextends on oxarticles.oxid=oxartextends.oxid where oxarticles.oxactive=1 AND $sArticleSelect";
00055
00056 $rs = $oDb->execute( $sQ );
00057 $aTags = array();
00058 while ( $rs && $rs->recordCount() && !$rs->EOF ) {
00059 $sTags = $this->trimTags( $rs->fields['oxtags'] );
00060 $aArticleTags = explode( ' ', $sTags );
00061 foreach ( $aArticleTags as $sTag ) {
00062 if ( trim( $sTag ) ) {
00063 ++$aTags[$sTag];
00064 }
00065 }
00066 $rs->moveNext();
00067 }
00068
00069
00070 if ( $iAmount ) {
00071 arsort($aTags);
00072 $aTags = array_slice($aTags, 0, $iAmount, true );
00073 }
00074
00075 $aTags = $this->_sortTags( $aTags );
00076 return $aTags;
00077 }
00078
00087 protected function _sortTags( $aTags, $iLang = null )
00088 {
00089 if ( is_array( $aTags ) && count( $aTags ) ) {
00090 $oDb = oxDb::getDb( true );
00091 $sSubQ = '';
00092 foreach ( $aTags as $sKey => $sTag ) {
00093 if ( $sSubQ ) {
00094 $sSubQ .= ' union all ';
00095 }
00096 $sSubQ .= 'select '.$oDb->quote( $sKey ).' as _oxsort, '.$oDb->quote( $sTag ).' as _oxval';
00097 }
00098
00099 $sField = "oxartextends.oxtags".oxLang::getInstance()->getLanguageTag( $iLang );
00100
00101
00102 $sSubQ = "select {$sField} as _oxsort, 'ox_skip' as _oxval from oxartextends limit 1 union $sSubQ";
00103
00104 $sQ = "select _oxtable._oxsort, _oxtable._oxval from ( {$sSubQ} ) as _oxtable order by _oxtable._oxsort desc";
00105
00106 $aTags = array();
00107 $oRs = $oDb->execute( $sQ );
00108 while ( $oRs && $oRs->recordCount() && !$oRs->EOF ) {
00109 if ( $oRs->fields['_oxval'] != 'ox_skip' ) {
00110 $aTags[$oRs->fields['_oxsort']] = $oRs->fields['_oxval'];
00111 }
00112 $oRs->moveNext();
00113 }
00114 }
00115 return $aTags;
00116 }
00117
00127 public function getTagCloud($sArtId = null, $blExtended = false, $iLang = null )
00128 {
00129 $myUtils = oxUtils::getInstance();
00130
00131 $sTagCloud = null;
00132 $sCacheKey = $this->_getCacheKey($blExtended, $iLang );
00133 if ( $this->_sCacheKey && !$sArtId ) {
00134 $sTagCloud = $myUtils->fromFileCache( $sCacheKey );
00135 }
00136
00137 if ( !is_null($sTagCloud) ) {
00138 return $sTagCloud;
00139 }
00140
00141 $aTags = $this->getTags($sArtId, $blExtended, $iLang);
00142 if (!count($aTags)) {
00143 if ($this->_sCacheKey && !$sArtId) {
00144 $sTagCloud = false;
00145 $myUtils->toFileCache($sCacheKey, $sTagCloud);
00146 }
00147 return $sTagCloud;
00148 }
00149
00150 $iMaxHit = max( $aTags);
00151 $blSeoIsActive = $myUtils->seoIsActive();
00152 $oSeoEncoderTag = oxSeoEncoderTag::getInstance();
00153
00154 $iLang = ( $iLang !== null ) ? $iLang : oxLang::getInstance()->getBaseLanguage();
00155 $sUrl = $this->getConfig()->getShopUrl();
00156 $oStr = getStr();
00157
00158 $sTagCloud = false;
00159 foreach ( $aTags as $sTag => $sRelevance ) {
00160 if ( $blSeoIsActive ) {
00161 $sLink = $oSeoEncoderTag->getTagUrl( $sTag, $iLang );
00162 } else {
00163 $sLink = $sUrl . $oSeoEncoderTag->getStdTagUri( $sTag ) . "&lang=" . $iLang;
00164 }
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 }