OXID eShop CE  4.8.12
 All Classes Files Functions Variables Pages
oxtag.php
Go to the documentation of this file.
1 <?php
2 
3 if (!defined('OXTAGCLOUD_MINTAGLENGTH')) {
4  define('OXTAGCLOUD_MINTAGLENGTH', 4);
5 }
10 class oxTag extends oxSuperCfg
11 {
12 
18  protected $_sTag = '';
19 
23  protected $_aForbiddenTags = array(
24  'admin', 'application', 'core', 'export', 'modules', 'out', 'setup', 'tmp'
25  );
26 
32  protected $_sTagLink = null;
33 
40  protected $_iTagMaxLength = 60;
41 
47  protected $_iHitCount = 1;
48 
55  protected $_aMetaChars = array('+','-','>','<','(',')','~','*','"','\'','\\','[',']','{','}',';',':','.','/','|','!','@','#','$','%','^','&','?','=','`');
56 
62  public function __construct( $sTag = null )
63  {
65  if ( $sTag !== null ) {
66  $this->set( $sTag );
67  }
68  }
69 
77  public function setMaxLength( $iTagMaxLength )
78  {
79  $this->_iTagMaxLength = $iTagMaxLength;
80  }
81 
87  public function getMaxLength()
88  {
89  return $this->_iTagMaxLength;
90  }
91 
100  public function set( $sTag, $blPrepare = true )
101  {
102  $this->_sTag = $blPrepare? $this->prepare( $sTag ) : $sTag;
103  $this->setLink();
104  }
105 
111  public function get()
112  {
113  return $this->_sTag;
114  }
115 
123  public function setHitCount( $iHitCount )
124  {
125  $this->_iHitCount = $iHitCount;
126  }
127 
133  public function getHitCount()
134  {
135  return $this->_iHitCount;
136  }
137 
143  public function increaseHitCount()
144  {
145  $this->_iHitCount++;
146  }
147 
153  public function isValid()
154  {
155  $blValid = strlen($this->_sTag) > 0? true : false;
156  if ( $blValid && in_array( $this->_sTag, $this->_aForbiddenTags ) ) {
157  $blValid = false;
158  }
159  return $blValid;
160  }
161 
167  public function getLink()
168  {
169  if ( is_null($this->_sTagLink) ) {
170  $this->_sTagLink = $this->formLink( $this->get() );
171  }
172  return $this->_sTagLink;
173  }
174 
182  public function setLink( $sTagLink = null )
183  {
184  $this->_sTagLink = $sTagLink;
185  }
186 
192  public function getTitle()
193  {
194  return getStr()->htmlentities( $this->get() );
195  }
196 
202  public function __toString()
203  {
204  return $this->get();
205  }
206 
215  public function prepare( $sTag )
216  {
217  $sTag = $this->stripMetaChars($sTag);
218  $oStr = getStr();
219  $iLen = $oStr->strlen( $sTag );
220  if ( $iLen > $this->getMaxLength() ) {
221  $sTag = trim( $oStr->substr( $sTag, 0, $this->getMaxLength() ) );
222  }
223  return $oStr->strtolower( $sTag );
224  }
225 
233  public function stripMetaChars( $sText )
234  {
235  $oStr = getStr();
236 
237  // Remove meta chars
238  $sText = str_replace($this->_aMetaChars, ' ', $sText);
239 
240  // Replace multiple spaces with single space
241  $sText = $oStr->preg_replace( "/\s+/", " ", trim( $sText ) );
242 
243  return $sText;
244  }
245 
253  public function formLink( $sTag )
254  {
255  $oSeoEncoderTag = oxRegistry::get("oxSeoEncoderTag");
256 
257  $iLang = oxRegistry::getLang()->getBaseLanguage();
258 
259  $sUrl = false;
260  if ( oxRegistry::getUtils()->seoIsActive() ) {
261  $sUrl = $oSeoEncoderTag->getTagUrl( $sTag, $iLang );
262  }
263  return $sUrl ? $sUrl : $this->getConfig()->getShopUrl() . $oSeoEncoderTag->getStdTagUri( $sTag ) . "&amp;lang=" . $iLang;
264  }
265 
271  public function addUnderscores()
272  {
273  $oStr = getStr();
274  $aTagParts = explode( ' ', $this->get() );
275  foreach ($aTagParts as &$sTagPart) {
276  if ( $oStr->strlen( $sTagPart ) < OXTAGCLOUD_MINTAGLENGTH ) {
277  $sTagPart .= str_repeat( "_", OXTAGCLOUD_MINTAGLENGTH - $oStr->strlen( $sTagPart ) );
278  }
279  }
280  unset($sTagPart);
281  $this->set( implode( ' ', $aTagParts ), false );
282  }
283 
284 
290  public function removeUnderscores()
291  {
292  $oStr = getStr();
293  $sRes = '';
294  if ( $oStr->preg_match_all( "/([\s\-]?)([^\s\-]+)([\s\-]?)/", $this->get(), $aMatches ) ) {
295  foreach ( $aMatches[2] as $iKey => $sMatch ) {
296  if ( $oStr->strlen( $sMatch ) <= OXTAGCLOUD_MINTAGLENGTH ) {
297  $sMatch = rtrim( $sMatch, "_" );
298  }
299  $sRes .= $aMatches[1][$iKey] . $sMatch . $aMatches[3][$iKey];
300  }
301  }
302  $this->set( $sRes, false );
303  }
304 }