OXID eShop CE  4.8.10
 All Classes Files Functions Variables Pages
oxseoencodercategory.php
Go to the documentation of this file.
1 <?php
2 
8 {
12  protected static $_instance = null;
13 
20  protected $_aCatCache = array();
21 
29  public static function getInstance()
30  {
31  return oxRegistry::get("oxSeoEncoderCategory");
32  }
33 
39  protected function _getUrlExtension()
40  {
41  return '/';
42  }
43 
55  protected function _categoryUrlLoader( $oCat, $iLang )
56  {
57  $sSeoUrl = false;
58 
59  $sCacheId = $this->_getCategoryCacheId( $oCat, $iLang );
60  if ( isset( $this->_aCatCache[$sCacheId] ) ) {
61  $sSeoUrl = $this->_aCatCache[ $sCacheId ];
62  } elseif ( ( $sSeoUrl = $this->_loadFromDb( 'oxcategory', $oCat->getId(), $iLang ) ) ) {
63  // caching
64  $this->_aCatCache[ $sCacheId ] = $sSeoUrl;
65  }
66 
67  return $sSeoUrl;
68  }
69 
80  private function _getCategoryCacheId( $oCat, $iLang )
81  {
82  return $oCat->getId() . '_' . ( (int) $iLang );
83  }
84 
94  public function getCategoryUri( $oCat, $iLang = null, $blRegenerate = false )
95  {
96  startProfile(__FUNCTION__);
97  $sCatId = $oCat->getId();
98 
99  // skipping external category URLs
100  if ( $oCat->oxcategories__oxextlink->value ) {
101  $sSeoUrl = null;
102  } else {
103  // not found in cache, process it from the top
104  if (!isset($iLang)) {
105  $iLang = $oCat->getLanguage();
106  }
107 
108  $aCacheMap = array();
109  $aStdLinks = array();
110 
111  while ( $oCat && !($sSeoUrl = $this->_categoryUrlLoader( $oCat, $iLang ) )) {
112 
113  if ($iLang != $oCat->getLanguage()) {
114  $sId = $oCat->getId();
115  $oCat = oxNew('oxcategory');
116  $oCat->loadInLang($iLang, $sId);
117  }
118 
119  // prepare oCat title part
120  $sTitle = $this->_prepareTitle( $oCat->oxcategories__oxtitle->value, false, $oCat->getLanguage() );
121 
122  foreach ( array_keys( $aCacheMap ) as $id ) {
123  $aCacheMap[$id] = $sTitle . '/' . $aCacheMap[$id];
124  }
125 
126  $aCacheMap[$oCat->getId()] = $sTitle;
127  $aStdLinks[$oCat->getId()] = $oCat->getBaseStdLink($iLang);
128 
129  // load parent
130  $oCat = $oCat->getParentCategory();
131  }
132 
133  foreach ( $aCacheMap as $sId => $sUri ) {
134  $this->_aCatCache[$sId.'_'.$iLang] = $this->_processSeoUrl( $sSeoUrl.$sUri.'/', $sId, $iLang );
135  $this->_saveToDb( 'oxcategory', $sId, $aStdLinks[$sId], $this->_aCatCache[$sId.'_'.$iLang], $iLang );
136  }
137 
138  $sSeoUrl = $this->_aCatCache[$sCatId.'_'.$iLang];
139  }
140 
141  stopProfile(__FUNCTION__);
142 
143  return $sSeoUrl;
144  }
145 
146 
157  public function getCategoryPageUrl( $oCategory, $iPage, $iLang = null, $blFixed = null )
158  {
159  if (!isset($iLang)) {
160  $iLang = $oCategory->getLanguage();
161  }
162  $sStdUrl = $oCategory->getBaseStdLink($iLang) . '&amp;pgNr=' . $iPage;
163  $sParams = (int) ($iPage + 1);
164 
165  $sStdUrl = $this->_trimUrl( $sStdUrl, $iLang );
166  $sSeoUrl = $this->getCategoryUri( $oCategory, $iLang ) . $sParams . "/";
167 
168  if ( $blFixed === null ) {
169  $blFixed = $this->_isFixed( 'oxcategory', $oCategory->getId(), $iLang );
170  }
171  return $this->_getFullUrl( $this->_getPageUri( $oCategory, 'oxcategory', $sStdUrl, $sSeoUrl, $sParams, $iLang, $blFixed ), $iLang );
172  }
173 
185  public function getCategoryUrl( $oCategory, $iLang = null )
186  {
187  $sUrl = '';
188  if (!isset($iLang)) {
189  $iLang = $oCategory->getLanguage();
190  }
191  // category may have specified url
192  if ( ( $sSeoUrl = $this->getCategoryUri( $oCategory, $iLang ) ) ) {
193  $sUrl = $this->_getFullUrl( $sSeoUrl, $iLang );
194  }
195  return $sUrl;
196  }
197 
205  public function markRelatedAsExpired( $oCategory )
206  {
207  $oDb = oxDb::getDb();
208  $sIdQuoted = $oDb->quote($oCategory->getId());
209 
210  // select it from table instead of using object carrying value
211  // this is because this method is usually called inside update,
212  // where object may already be carrying changed id
213  $aCatInfo = $oDb->getAll("select oxrootid, oxleft, oxright from oxcategories where oxid = $sIdQuoted limit 1");
214  $sCatRootIdQuoted = $oDb->quote( $aCatInfo[0][0] );
215 
216  // update sub cats
217  $sQ = "update oxseo as seo1, (select oxid from oxcategories where oxrootid={$sCatRootIdQuoted} and oxleft > ".((int) $aCatInfo[0][1] )." and oxright < ".((int) $aCatInfo[0][2] ).") as seo2 set seo1.oxexpired = '1' where seo1.oxtype = 'oxcategory' and seo1.oxobjectid = seo2.oxid";
218  $oDb->execute( $sQ );
219 
220  // update subarticles
221  $sQ = "update oxseo as seo1, (select o2c.oxobjectid as id from oxcategories as cat left join oxobject2category"
222  ." as o2c on o2c.oxcatnid=cat.oxid where cat.oxrootid={$sCatRootIdQuoted} and cat.oxleft >= "
223  .((int) $aCatInfo[0][1] )." and cat.oxright <= ".((int) $aCatInfo[0][2] ).") as seo2 "
224  ."set seo1.oxexpired = '1' where seo1.oxtype = 'oxarticle' and seo1.oxobjectid = seo2.id "
225  ."and seo1.oxfixed = 0";
226 
227  $oDb->execute( $sQ );
228  }
229 
230 
238  public function onDeleteCategory( $oCategory )
239  {
240  $oDb = oxDb::getDb();
241  $sIdQuoted = $oDb->quote($oCategory->getId());
242  $oDb->execute("update oxseo, (select oxseourl from oxseo where oxobjectid = $sIdQuoted and oxtype = 'oxcategory') as test set oxseo.oxexpired=1 where oxseo.oxseourl like concat(test.oxseourl, '%') and (oxtype = 'oxcategory' or oxtype = 'oxarticle')");
243  $oDb->execute("delete from oxseo where oxseo.oxtype = 'oxarticle' and oxseo.oxparams = $sIdQuoted" );
244  $oDb->execute("delete from oxseo where oxobjectid = $sIdQuoted and oxtype = 'oxcategory'");
245  $oDb->execute("delete from oxobject2seodata where oxobjectid = $sIdQuoted");
246  }
247 
256  protected function _getAltUri( $sObjectId, $iLang )
257  {
258  $sSeoUrl = null;
259  $oCat = oxNew( "oxcategory" );
260  if ( $oCat->loadInLang( $iLang, $sObjectId ) ) {
261  $sSeoUrl = $this->getCategoryUri( $oCat, $iLang );
262  }
263  return $sSeoUrl;
264  }
265 }