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 
00075     public function setMaxLength($iTagMaxLength)
00076     {
00077         $this->_iTagMaxLength = $iTagMaxLength;
00078     }
00079 
00085     public function getMaxLength()
00086     {
00087         return $this->_iTagMaxLength;
00088     }
00089 
00096     public function set($sTag, $blPrepare = true)
00097     {
00098         $this->_sTag = $blPrepare ? $this->prepare($sTag) : $sTag;
00099         $this->setLink();
00100     }
00101 
00107     public function get()
00108     {
00109         return $this->_sTag;
00110     }
00111 
00117     public function setHitCount($iHitCount)
00118     {
00119         $this->_iHitCount = $iHitCount;
00120     }
00121 
00127     public function getHitCount()
00128     {
00129         return $this->_iHitCount;
00130     }
00131 
00135     public function increaseHitCount()
00136     {
00137         $this->_iHitCount++;
00138     }
00139 
00145     public function isValid()
00146     {
00147         $blValid = strlen($this->_sTag) > 0 ? true : false;
00148         if ($blValid && in_array($this->_sTag, $this->_aForbiddenTags)) {
00149             $blValid = false;
00150         }
00151 
00152         return $blValid;
00153     }
00154 
00160     public function getLink()
00161     {
00162         if (is_null($this->_sTagLink)) {
00163             $this->_sTagLink = $this->formLink($this->get());
00164         }
00165 
00166         return $this->_sTagLink;
00167     }
00168 
00174     public function setLink($sTagLink = null)
00175     {
00176         $this->_sTagLink = $sTagLink;
00177     }
00178 
00184     public function getTitle()
00185     {
00186         return getStr()->htmlentities($this->get());
00187     }
00188 
00194     public function __toString()
00195     {
00196         return $this->get();
00197     }
00198 
00207     public function prepare($sTag)
00208     {
00209         $sTag = $this->stripMetaChars($sTag);
00210         $oStr = getStr();
00211         $iLen = $oStr->strlen($sTag);
00212         if ($iLen > $this->getMaxLength()) {
00213             $sTag = trim($oStr->substr($sTag, 0, $this->getMaxLength()));
00214         }
00215 
00216         return $oStr->strtolower($sTag);
00217     }
00218 
00226     public function stripMetaChars($sText)
00227     {
00228         $oStr = getStr();
00229 
00230         // Remove meta chars
00231         $sText = str_replace($this->_aMetaChars, ' ', $sText);
00232 
00233         // Replace multiple spaces with single space
00234         $sText = $oStr->preg_replace("/\s+/", " ", trim($sText));
00235 
00236         return $sText;
00237     }
00238 
00246     public function formLink($sTag)
00247     {
00248         $oSeoEncoderTag = oxRegistry::get("oxSeoEncoderTag");
00249 
00250         $iLang = oxRegistry::getLang()->getBaseLanguage();
00251 
00252         $sUrl = false;
00253         if (oxRegistry::getUtils()->seoIsActive()) {
00254             $sUrl = $oSeoEncoderTag->getTagUrl($sTag, $iLang);
00255         }
00256 
00257         return $sUrl ? $sUrl : $this->getConfig()->getShopUrl() . $oSeoEncoderTag->getStdTagUri($sTag) . "&amp;lang=" . $iLang;
00258     }
00259 
00263     public function addUnderscores()
00264     {
00265         $oStr = getStr();
00266         $aTagParts = explode(' ', $this->get());
00267         foreach ($aTagParts as &$sTagPart) {
00268             if ($oStr->strlen($sTagPart) < OXTAGCLOUD_MINTAGLENGTH) {
00269                 $sTagPart .= str_repeat("_", OXTAGCLOUD_MINTAGLENGTH - $oStr->strlen($sTagPart));
00270             }
00271         }
00272         unset($sTagPart);
00273         $this->set(implode(' ', $aTagParts), false);
00274     }
00275 
00276 
00280     public function removeUnderscores()
00281     {
00282         $oStr = getStr();
00283         $sRes = '';
00284         if ($oStr->preg_match_all("/([\s\-]?)([^\s\-]+)([\s\-]?)/", $this->get(), $aMatches)) {
00285             foreach ($aMatches[2] as $iKey => $sMatch) {
00286                 if ($oStr->strlen($sMatch) <= OXTAGCLOUD_MINTAGLENGTH) {
00287                     $sMatch = rtrim($sMatch, "_");
00288                 }
00289                 $sRes .= $aMatches[1][$iKey] . $sMatch . $aMatches[3][$iKey];
00290             }
00291         }
00292         $this->set($sRes, false);
00293     }
00294 }