oxtag.php

Go to the documentation of this file.
00001 <?php
00002 
00003 if (!defined('OXTAGCLOUD_MINTAGLENGTH')) {
00004     define('OXTAGCLOUD_MINTAGLENGTH', 4);
00005 }
00010 class oxTag extends oxSuperCfg
00011 {
00012 
00018     protected $_sTag = '';
00019 
00023     protected $_aForbiddenTags = array(
00024         'admin', 'application', 'core', 'export', 'modules', 'out', 'setup', 'tmp'
00025     );
00026 
00032     protected $_sTagLink = null;
00033 
00040     protected $_iTagMaxLength = 60;
00041 
00047     protected $_iHitCount = 1;
00048 
00055     protected $_aMetaChars = array('+','-','>','<','(',')','~','*','"','\'','\\','[',']','{','}',';',':','.','/','|','!','@','#','$','%','^','&','?','=','`');
00056 
00062     public function __construct( $sTag = null )
00063     {
00064         parent::__construct();
00065         if ( $sTag !== null ) {
00066             $this->set( $sTag );
00067         }
00068     }
00069 
00077     public function setMaxLength( $iTagMaxLength )
00078     {
00079         $this->_iTagMaxLength = $iTagMaxLength;
00080     }
00081 
00087     public function getMaxLength()
00088     {
00089         return $this->_iTagMaxLength;
00090     }
00091 
00100     public function set( $sTag, $blPrepare = true )
00101     {
00102         $this->_sTag = $blPrepare? $this->prepare( $sTag ) : $sTag;
00103         $this->setLink();
00104     }
00105 
00111     public function get()
00112     {
00113         return $this->_sTag;
00114     }
00115 
00123     public function setHitCount( $iHitCount )
00124     {
00125         $this->_iHitCount = $iHitCount;
00126     }
00127 
00133     public function getHitCount()
00134     {
00135         return $this->_iHitCount;
00136     }
00137 
00143     public function increaseHitCount()
00144     {
00145         $this->_iHitCount++;
00146     }
00147 
00153     public function isValid()
00154     {
00155         $blValid = strlen($this->_sTag) > 0? true : false;
00156         if ( $blValid && in_array( $this->_sTag, $this->_aForbiddenTags ) ) {
00157             $blValid = false;
00158         }
00159         return $blValid;
00160     }
00161 
00167     public function getLink()
00168     {
00169         if ( is_null($this->_sTagLink) ) {
00170              $this->_sTagLink = $this->formLink( $this->get() );
00171         }
00172         return $this->_sTagLink;
00173     }
00174 
00182     public function setLink( $sTagLink = null )
00183     {
00184         $this->_sTagLink = $sTagLink;
00185     }
00186 
00192     public function getTitle()
00193     {
00194         return getStr()->htmlentities( $this->get() );
00195     }
00196 
00202     public function __toString()
00203     {
00204         return $this->get();
00205     }
00206 
00215     public function prepare( $sTag )
00216     {
00217         $sTag = $this->stripMetaChars($sTag);
00218         $oStr = getStr();
00219         $iLen = $oStr->strlen( $sTag );
00220         if ( $iLen > $this->getMaxLength() ) {
00221             $sTag = trim( $oStr->substr( $sTag, 0, $this->getMaxLength() ) );
00222         }
00223         return $oStr->strtolower( $sTag );
00224     }
00225 
00233     public function stripMetaChars( $sText )
00234     {
00235         $oStr  = getStr();
00236 
00237         // Remove meta chars
00238         $sText = str_replace($this->_aMetaChars, ' ', $sText);
00239 
00240         // Replace multiple spaces with single space
00241         $sText = $oStr->preg_replace( "/\s+/", " ", trim( $sText ) );
00242 
00243         return $sText;
00244     }
00245 
00253     public function formLink( $sTag )
00254     {
00255         $oSeoEncoderTag = oxRegistry::get("oxSeoEncoderTag");
00256 
00257         $iLang = oxRegistry::getLang()->getBaseLanguage();
00258 
00259         $sUrl = false;
00260         if ( oxRegistry::getUtils()->seoIsActive() ) {
00261             $sUrl = $oSeoEncoderTag->getTagUrl( $sTag, $iLang );
00262         }
00263         return $sUrl ? $sUrl : $this->getConfig()->getShopUrl() . $oSeoEncoderTag->getStdTagUri( $sTag ) . "&amp;lang=" . $iLang;
00264     }
00265 
00271     public function addUnderscores()
00272     {
00273         $oStr = getStr();
00274         $aTagParts = explode( ' ', $this->get() );
00275         foreach ($aTagParts as &$sTagPart) {
00276             if ( $oStr->strlen( $sTagPart ) < OXTAGCLOUD_MINTAGLENGTH ) {
00277                 $sTagPart .= str_repeat( "_", OXTAGCLOUD_MINTAGLENGTH - $oStr->strlen( $sTagPart ) );
00278             }
00279         }
00280         unset($sTagPart);
00281         $this->set( implode( ' ', $aTagParts ), false );
00282     }
00283 
00284 
00290     public function removeUnderscores()
00291     {
00292         $oStr = getStr();
00293         $sRes = '';
00294         if ( $oStr->preg_match_all( "/([\s\-]?)([^\s\-]+)([\s\-]?)/", $this->get(), $aMatches ) ) {
00295             foreach ( $aMatches[2] as $iKey => $sMatch ) {
00296                 if ( $oStr->strlen( $sMatch ) <= OXTAGCLOUD_MINTAGLENGTH ) {
00297                     $sMatch = rtrim( $sMatch, "_" );
00298                 }
00299                 $sRes .= $aMatches[1][$iKey] . $sMatch . $aMatches[3][$iKey];
00300             }
00301         }
00302         $this->set( $sRes, false );
00303     }
00304 }