OXID eShop CE  4.9.7
 All Classes Files Functions Variables Pages
oxseoencodercategory.php
Go to the documentation of this file.
1 <?php
2 
8 {
9 
16  protected $_aCatCache = array();
17 
23  protected function _getUrlExtension()
24  {
25  return '/';
26  }
27 
39  protected function _categoryUrlLoader($oCat, $iLang)
40  {
41  $sSeoUrl = false;
42 
43  $sCacheId = $this->_getCategoryCacheId($oCat, $iLang);
44  if (isset($this->_aCatCache[$sCacheId])) {
45  $sSeoUrl = $this->_aCatCache[$sCacheId];
46  } elseif (($sSeoUrl = $this->_loadFromDb('oxcategory', $oCat->getId(), $iLang))) {
47  // caching
48  $this->_aCatCache[$sCacheId] = $sSeoUrl;
49  }
50 
51  return $sSeoUrl;
52  }
53 
64  private function _getCategoryCacheId($oCat, $iLang)
65  {
66  return $oCat->getId() . '_' . ((int) $iLang);
67  }
68 
78  public function getCategoryUri($oCat, $iLang = null, $blRegenerate = false)
79  {
80  startProfile(__FUNCTION__);
81  $sCatId = $oCat->getId();
82 
83  // skipping external category URLs
84  if ($oCat->oxcategories__oxextlink->value) {
85  $sSeoUrl = null;
86  } else {
87  // not found in cache, process it from the top
88  if (!isset($iLang)) {
89  $iLang = $oCat->getLanguage();
90  }
91 
92  $aCacheMap = array();
93  $aStdLinks = array();
94 
95  while ($oCat && !($sSeoUrl = $this->_categoryUrlLoader($oCat, $iLang))) {
96 
97  if ($iLang != $oCat->getLanguage()) {
98  $sId = $oCat->getId();
99  $oCat = oxNew('oxcategory');
100  $oCat->loadInLang($iLang, $sId);
101  }
102 
103  // prepare oCat title part
104  $sTitle = $this->_prepareTitle($oCat->oxcategories__oxtitle->value, false, $oCat->getLanguage());
105 
106  foreach (array_keys($aCacheMap) as $id) {
107  $aCacheMap[$id] = $sTitle . '/' . $aCacheMap[$id];
108  }
109 
110  $aCacheMap[$oCat->getId()] = $sTitle;
111  $aStdLinks[$oCat->getId()] = $oCat->getBaseStdLink($iLang);
112 
113  // load parent
114  $oCat = $oCat->getParentCategory();
115  }
116 
117  foreach ($aCacheMap as $sId => $sUri) {
118  $this->_aCatCache[$sId . '_' . $iLang] = $this->_processSeoUrl($sSeoUrl . $sUri . '/', $sId, $iLang);
119  $this->_saveToDb('oxcategory', $sId, $aStdLinks[$sId], $this->_aCatCache[$sId . '_' . $iLang], $iLang);
120  }
121 
122  $sSeoUrl = $this->_aCatCache[$sCatId . '_' . $iLang];
123  }
124 
125  stopProfile(__FUNCTION__);
126 
127  return $sSeoUrl;
128  }
129 
130 
141  public function getCategoryPageUrl($oCategory, $iPage, $iLang = null, $blFixed = null)
142  {
143  if (!isset($iLang)) {
144  $iLang = $oCategory->getLanguage();
145  }
146  $sStdUrl = $oCategory->getBaseStdLink($iLang) . '&amp;pgNr=' . $iPage;
147  $sParams = (int) ($iPage + 1);
148 
149  $sStdUrl = $this->_trimUrl($sStdUrl, $iLang);
150  $sSeoUrl = $this->getCategoryUri($oCategory, $iLang) . $sParams . "/";
151 
152  if ($blFixed === null) {
153  $blFixed = $this->_isFixed('oxcategory', $oCategory->getId(), $iLang);
154  }
155 
156  return $this->_getFullUrl($this->_getPageUri($oCategory, 'oxcategory', $sStdUrl, $sSeoUrl, $sParams, $iLang, $blFixed), $iLang);
157  }
158 
170  public function getCategoryUrl($oCategory, $iLang = null)
171  {
172  $sUrl = '';
173  if (!isset($iLang)) {
174  $iLang = $oCategory->getLanguage();
175  }
176  // category may have specified url
177  if (($sSeoUrl = $this->getCategoryUri($oCategory, $iLang))) {
178  $sUrl = $this->_getFullUrl($sSeoUrl, $iLang);
179  }
180 
181  return $sUrl;
182  }
183 
189  public function markRelatedAsExpired($oCategory)
190  {
191  $oDb = oxDb::getDb();
192  $sIdQuoted = $oDb->quote($oCategory->getId());
193 
194  // select it from table instead of using object carrying value
195  // this is because this method is usually called inside update,
196  // where object may already be carrying changed id
197  $aCatInfo = $oDb->getAll("select oxrootid, oxleft, oxright from oxcategories where oxid = $sIdQuoted limit 1");
198  $sCatRootIdQuoted = $oDb->quote($aCatInfo[0][0]);
199 
200  // update sub cats
201  $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";
202  $oDb->execute($sQ);
203 
204  // update subarticles
205  $sQ = "update oxseo as seo1, (select o2c.oxobjectid as id from oxcategories as cat left join oxobject2category"
206  ." as o2c on o2c.oxcatnid=cat.oxid where cat.oxrootid={$sCatRootIdQuoted} and cat.oxleft >= "
207  .((int) $aCatInfo[0][1] )." and cat.oxright <= ".((int) $aCatInfo[0][2] ).") as seo2 "
208  ."set seo1.oxexpired = '1' where seo1.oxtype = 'oxarticle' and seo1.oxobjectid = seo2.id "
209  ."and seo1.oxfixed = 0";
210 
211  $oDb->execute($sQ);
212  }
213 
214 
220  public function onDeleteCategory($oCategory)
221  {
222  $oDb = oxDb::getDb();
223  $sIdQuoted = $oDb->quote($oCategory->getId());
224  $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')");
225  $oDb->execute("delete from oxseo where oxseo.oxtype = 'oxarticle' and oxseo.oxparams = $sIdQuoted");
226  $oDb->execute("delete from oxseo where oxobjectid = $sIdQuoted and oxtype = 'oxcategory'");
227  $oDb->execute("delete from oxobject2seodata where oxobjectid = $sIdQuoted");
228  }
229 
238  protected function _getAltUri($sObjectId, $iLang)
239  {
240  $sSeoUrl = null;
241  $oCat = oxNew("oxcategory");
242  if ($oCat->loadInLang($iLang, $sObjectId)) {
243  $sSeoUrl = $this->getCategoryUri($oCat, $iLang);
244  }
245 
246  return $sSeoUrl;
247  }
248 }