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     //depends on mysql server configuration
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 
00034     public function getTags($sArtId = null, $blExtended = false)
00035     {
00036         $oDb = oxDb::getDb(true);
00037         if ($blExtended) {
00038             $iAmount = OXTAGCLOUD_EXTENDEDCOUNT;
00039         } else {
00040             $iAmount = OXTAGCLOUD_STARTPAGECOUNT;
00041         }
00042 
00043         $sArticleSelect = " 1 ";
00044         if ( $sArtId ) {
00045             $sArtId = $oDb->quote( $sArtId );
00046             $sArticleSelect = " oxarticles.oxid = $sArtId ";
00047             $iAmount = 0;
00048         }
00049 
00050         $sField = "oxartextends.oxtags".oxLang::getInstance()->getLanguageTag();
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         //$sQ = "select $sField from oxartextends where $sArticleSelect";
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         //taking only top tags
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 
00085     protected function _sortTags( $aTags )
00086     {
00087         if ( is_array( $aTags ) && count( $aTags ) ) {
00088             $oDb = oxDb::getDb( true );
00089             $sSubQ = '';
00090             foreach ( $aTags as $sKey => $sTag ) {
00091                 if ( $sSubQ ) {
00092                     $sSubQ .= ' union all ';
00093                 }
00094                 $sSubQ .= 'select '.$oDb->quote( $sKey ).' as _oxsort, '.$oDb->quote( $sTag ).' as _oxval';
00095             }
00096 
00097             $sField = "oxartextends.oxtags".oxLang::getInstance()->getLanguageTag();
00098 
00099             // forcing collation
00100             $sSubQ = "select {$sField} as _oxsort, 'ox_skip' as _oxval from oxartextends limit 1 union $sSubQ";
00101 
00102             $sQ = "select _oxtable._oxsort, _oxtable._oxval from ( {$sSubQ} ) as _oxtable order by _oxtable._oxsort desc";
00103 
00104             $aTags = array();
00105             $oRs = $oDb->execute( $sQ );
00106             while ( $oRs && $oRs->recordCount() && !$oRs->EOF ) {
00107                 if ( $oRs->fields['_oxval'] != 'ox_skip' ) {
00108                     $aTags[$oRs->fields['_oxsort']] = $oRs->fields['_oxval'];
00109                 }
00110                 $oRs->moveNext();
00111             }
00112         }
00113         return $aTags;
00114     }
00115 
00124     public function getTagCloud($sArtId = null, $blExtended = false)
00125     {
00126         $myUtils = oxUtils::getInstance();
00127 
00128         $sTagCloud = null;
00129         $sCacheKey = $this->_getCacheKey($blExtended);
00130         if ( $this->_sCacheKey && !$sArtId ) {
00131             $sTagCloud = $myUtils->fromFileCache( $sCacheKey );
00132         }
00133 
00134         if ( !is_null($sTagCloud) ) {
00135             return $sTagCloud;
00136         }
00137 
00138         $aTags = $this->getTags($sArtId, $blExtended);
00139         if (!count($aTags)) {
00140             if ($this->_sCacheKey && !$sArtId) {
00141                 $sTagCloud = false;
00142                 $myUtils->toFileCache($sCacheKey, $sTagCloud);
00143             }
00144             return $sTagCloud;
00145         }
00146 
00147         $iMaxHit = max( $aTags);
00148         $blSeoIsActive = $myUtils->seoIsActive();
00149         if ( $blSeoIsActive) {
00150             $oSeoEncoder = oxSeoEncoder::getInstance();
00151         }
00152 
00153         $iLang = oxLang::getInstance()->getBaseLanguage();
00154         $sUrl = $this->getConfig()->getShopUrl();
00155         $oStr = getStr();
00156 
00157         $sTagCloud = false;
00158         foreach ($aTags as $sTag => $sRelevance) {
00159             $sLink = $sUrl."index.php?cl=tag&amp;searchtag=".rawurlencode($sTag)."&amp;lang=".$iLang;
00160             if ( $blSeoIsActive) {
00161                 $sLink = $oSeoEncoder->getDynamicUrl( "index.php?cl=tag&amp;searchtag=".rawurlencode($sTag), "tag/$sTag/", $iLang );
00162             }
00163             $sTagCloud .= "<a style='font-size:". $this->_getFontSize($sRelevance, $iMaxHit) ."%;' href='$sLink'>".$oStr->htmlentities($sTag)."</a> ";
00164         }
00165 
00166         if ($this->_sCacheKey && !$sArtId) {
00167             $myUtils->toFileCache($sCacheKey, $sTagCloud);
00168         }
00169 
00170         return $sTagCloud;
00171     }
00172 
00181     protected function _getFontSize($iHit, $iMaxHit)
00182     {
00183         //handling special case
00184         if ($iMaxHit <= OXTAGCLOUD_MINOCCURENCETOSHOW || !$iMaxHit) {
00185             return OXTAGCLOUD_MINFONT;
00186         }
00187 
00188         $iFontDiff = OXTAGCLOUD_MAXFONT - OXTAGCLOUD_MINFONT;
00189         $iMaxHitDiff = $iMaxHit - OXTAGCLOUD_MINOCCURENCETOSHOW;
00190         $iHitDiff = $iHit - OXTAGCLOUD_MINOCCURENCETOSHOW;
00191 
00192         if ($iHitDiff < 0) {
00193             $iHitDiff = 0;
00194         }
00195 
00196         $iSize = round($iHitDiff * $iFontDiff / $iMaxHitDiff) + OXTAGCLOUD_MINFONT;
00197 
00198         return $iSize;
00199     }
00200 
00209     public function prepareTags( $sTags )
00210     {
00211         $aTags = explode( ' ', $sTags );
00212         $sRes = '';
00213         $oStr = getStr();
00214         foreach ( $aTags as $sTag ) {
00215             if ( ( $iLen = $oStr->strlen( $sTag ) ) ) {
00216                 if ( $iLen < OXTAGCLOUD_MINTAGLENGTH ) {
00217                     $sTag .= str_repeat( '_', OXTAGCLOUD_MINTAGLENGTH - $iLen );
00218                 }
00219 
00220                 $sRes .= $oStr->strtolower( $sTag ) . " ";
00221             }
00222         }
00223 
00224         return trim( $sRes );
00225     }
00226 
00234     public function trimTags($sTags)
00235     {
00236         $aTags = explode(' ', $sTags);
00237         $sRes = '';
00238         $oStr = getStr();
00239         foreach ( $aTags as $sTag ) {
00240             if ( $oStr->strlen( $sTag ) ) {
00241                 $sRes .= rtrim( $sTag, '_' ) . " ";
00242             }
00243         }
00244 
00245         return trim( $sRes );
00246     }
00247 
00253     public function resetTagCache()
00254     {
00255         $myUtils = oxUtils::getInstance();
00256 
00257         $sCacheKey1 = $this->_getCacheKey(true);
00258         $myUtils->toFileCache($sCacheKey1, null);
00259 
00260         $sCacheKey2 = $this->_getCacheKey(false);
00261         $myUtils->toFileCache($sCacheKey2, null);
00262     }
00263 
00271     protected function _getCacheKey($blExtended)
00272     {
00273         return $this->_sCacheKey."_".$this->getConfig()->getShopId()."_".oxLang::getInstance()->getBaseLanguage()."_".$blExtended;
00274     }
00275 
00276 }

Generated on Wed May 13 13:25:51 2009 for OXID eShop CE by  doxygen 1.5.5