OXID eShop CE  4.10.4
 All Classes Namespaces Files Functions Variables Pages
oxseoencoderarticle.php
Go to the documentation of this file.
1 <?php
2 
8 {
9 
15  protected static $_aTitleCache = array();
16 
22  protected function _getUrlExtension()
23  {
24  return '.html';
25  }
26 
36  protected function _getProductForLang($oArticle, $iLang)
37  {
38  if (isset($iLang) && $iLang != $oArticle->getLanguage()) {
39  $sId = $oArticle->getId();
40  $oArticle = oxNew('oxArticle');
41  $oArticle->setSkipAssign(true);
42  $oArticle->loadInLang($iLang, $sId);
43  }
44 
45  return $oArticle;
46  }
47 
58  public function getArticleRecommUri($oArticle, $iLang)
59  {
60  $sSeoUri = null;
61  if ($oRecomm = $this->_getRecomm($oArticle, $iLang)) {
62  //load details link from DB
63  if (!($sSeoUri = $this->_loadFromDb('oxarticle', $oArticle->getId(), $iLang, null, $oRecomm->getId(), true))) {
64 
65  $oArticle = $this->_getProductForLang($oArticle, $iLang);
66 
67  // create title part for uri
68  $sTitle = $this->_prepareArticleTitle($oArticle);
69 
70  // create uri for all categories
71  $sSeoUri = oxRegistry::get("oxSeoEncoderRecomm")->getRecommUri($oRecomm, $iLang);
72  $sSeoUri = $this->_processSeoUrl($sSeoUri . $sTitle, $oArticle->getId(), $iLang);
73 
74  $aStdParams = array('recommid' => $oRecomm->getId(), 'listtype' => $this->_getListType());
75  $this->_saveToDb(
76  'oxarticle',
77  $oArticle->getId(),
78  oxRegistry::get("oxUtilsUrl")->appendUrl(
79  $oArticle->getBaseStdLink($iLang),
80  $aStdParams
81  ),
82  $sSeoUri,
83  $iLang,
84  null,
85  0,
86  $oRecomm->getId()
87  );
88  }
89  }
90 
91  return $sSeoUri;
92  }
93 
104  protected function _getRecomm($oArticle, $iLang)
105  {
106  $oList = null;
107  $oView = $this->getConfig()->getActiveView();
108  if ($oView instanceof oxView) {
109  $oList = $oView->getActiveRecommList();
110  }
111 
112  return $oList;
113  }
114 
120  protected function _getListType()
121  {
122  return $this->getConfig()->getActiveView()->getListType();
123  }
124 
136  public function getArticleTagUri($oArticle, $iLang, $blRegenerate = false)
137  {
138  $sSeoUri = null;
139  if ($sTag = $this->_getTag($oArticle, $iLang)) {
140  $iShopId = $this->getConfig()->getShopId();
141 
142  $oArticleTags = oxNew('oxArticleTagList');
143  $oArticleTags->setArticleId($oArticle->getId());
144  $oArticleTags->getStdTagLink($sTag);
145 
146  $sStdUrl = $oArticleTags->getStdTagLink($sTag);
147  if ($blRegenerate || !($sSeoUri = $this->_loadFromDb('dynamic', $this->getDynamicObjectId($iShopId, $sStdUrl), $iLang))) {
148  // generating new if not found
149  if ($sSeoUri = oxRegistry::get("oxSeoEncoderTag")->getTagUri($sTag, $iLang, $oArticle->getId())) {
150  $sSeoUri .= $this->_prepareArticleTitle($oArticle);
151  $sSeoUri = $this->_processSeoUrl($sSeoUri, $this->_getStaticObjectId($iShopId, $sStdUrl), $iLang);
152  $sSeoUri = $this->_getDynamicUri($sStdUrl, $sSeoUri, $iLang);
153  }
154  }
155  }
156 
157  return $sSeoUri;
158  }
159 
170  protected function _getTag($oArticle, $iLang)
171  {
172  $sTag = null;
173  $oView = $this->getConfig()->getTopActiveView();
174  if ($oView instanceof oxView) {
175  $sTag = $oView->getTag();
176  }
177 
178  return $sTag;
179  }
180 
190  protected function _createArticleCategoryUri($oArticle, $oCategory, $iLang)
191  {
192  startProfile(__FUNCTION__);
193  $oArticle = $this->_getProductForLang($oArticle, $iLang);
194 
195  // create title part for uri
196  $sTitle = $this->_prepareArticleTitle($oArticle);
197 
198  // writing category path
199  $sSeoUri = $this->_processSeoUrl(
200  oxRegistry::get("oxSeoEncoderCategory")->getCategoryUri($oCategory, $iLang) . $sTitle,
201  $oArticle->getId(), $iLang
202  );
203  $sCatId = $oCategory->getId();
204  $this->_saveToDb(
205  'oxarticle',
206  $oArticle->getId(),
207  oxRegistry::get("oxUtilsUrl")->appendUrl(
208  $oArticle->getBaseStdLink($iLang),
209  array('cnid' => $sCatId)
210  ),
211  $sSeoUri,
212  $iLang,
213  null,
214  0,
215  $sCatId
216  );
217 
218  stopProfile(__FUNCTION__);
219 
220  return $sSeoUri;
221  }
222 
232  public function getArticleUri($oArticle, $iLang, $blRegenerate = false)
233  {
234  startProfile(__FUNCTION__);
235 
236  $sActCatId = '';
237 
238  $oActCat = $this->_getCategory($oArticle, $iLang);
239 
240  if ($oActCat instanceof oxCategory) {
241  $sActCatId = $oActCat->getId();
242  } elseif ($oActCat = $this->_getMainCategory($oArticle)) {
243  $sActCatId = $oActCat->getId();
244  }
245 
246  //load details link from DB
247  if ($blRegenerate || !($sSeoUri = $this->_loadFromDb('oxarticle', $oArticle->getId(), $iLang, null, $sActCatId, true))) {
248  if ($oActCat) {
249  $blInCat = false;
250  if ($oActCat->isPriceCategory()) {
251  $blInCat = $oArticle->inPriceCategory($sActCatId);
252  } else {
253  $blInCat = $oArticle->inCategory($sActCatId);
254  }
255  if ($blInCat) {
256  $sSeoUri = $this->_createArticleCategoryUri($oArticle, $oActCat, $iLang);
257  }
258  }
259  }
260 
261  stopProfile(__FUNCTION__);
262 
263  return $sSeoUri;
264  }
265 
274  protected function _getCategory($oArticle, $iLang)
275  {
276  $oCat = null;
277  $oView = $this->getConfig()->getActiveView();
278  if ($oView instanceof oxUBase) {
279  $oCat = $oView->getActiveCategory();
280  } elseif ($oView instanceof oxView) {
281  $oCat = $oView->getActCategory();
282  }
283 
284  return $oCat;
285  }
286 
294  protected function _getMainCategory($oArticle)
295  {
296  $oMainCat = null;
297 
298  // if variant parent id must be used
299  $sArtId = $oArticle->getId();
300  if (isset($oArticle->oxarticles__oxparentid->value) && $oArticle->oxarticles__oxparentid->value) {
301  $sArtId = $oArticle->oxarticles__oxparentid->value;
302  }
303 
304  $oDb = oxDb::getDb();
305  // add main category caching;
306  $sQ = "select oxcatnid from " . getViewName("oxobject2category") . " where oxobjectid = " . $oDb->quote($sArtId) . " order by oxtime";
307  $sIdent = md5($sQ);
308 
309  if (($sMainCatId = $this->_loadFromCache($sIdent, "oxarticle")) === false) {
310  $sMainCatId = $oDb->getOne($sQ);
311  // storing in cache
312  $this->_saveInCache($sIdent, $sMainCatId, "oxarticle");
313  }
314 
315  if ($sMainCatId) {
316  $oMainCat = oxNew("oxCategory");
317  if (!$oMainCat->load($sMainCatId)) {
318  $oMainCat = null;
319  }
320  }
321 
322  return $oMainCat;
323  }
324 
333  public function getArticleMainUri($oArticle, $iLang)
334  {
335  startProfile(__FUNCTION__);
336 
337  $oMainCat = $this->_getMainCategory($oArticle);
338  $sMainCatId = $oMainCat ? $oMainCat->getId() : '';
339 
340  //load default article url from DB
341  if (!($sSeoUri = $this->_loadFromDb('oxarticle', $oArticle->getId(), $iLang, null, $sMainCatId, true))) {
342  // save for main category
343  if ($oMainCat) {
344  $sSeoUri = $this->_createArticleCategoryUri($oArticle, $oMainCat, $iLang);
345  } else {
346  // get default article url
347  $oArticle = $this->_getProductForLang($oArticle, $iLang);
348  $sSeoUri = $this->_processSeoUrl($this->_prepareArticleTitle($oArticle), $oArticle->getId(), $iLang);
349 
350  // save default article url
351  $this->_saveToDb(
352  'oxarticle',
353  $oArticle->getId(),
354  $oArticle->getBaseStdLink($iLang),
355  $sSeoUri,
356  $iLang,
357  null,
358  0,
359  ''
360  );
361  }
362  }
363 
364  stopProfile(__FUNCTION__);
365 
366  return $sSeoUri;
367  }
368 
377  protected function _prepareArticleTitle($oArticle)
378  {
379  $sTitle = '';
380 
381  // create title part for uri
382  if (!($sTitle = $oArticle->oxarticles__oxtitle->value)) {
383  // taking parent article title
384  if (($sParentId = $oArticle->oxarticles__oxparentid->value)) {
385 
386  // looking in cache ..
387  if (!isset(self::$_aTitleCache[$sParentId])) {
388  $oDb = oxDb::getDb();
389  $sQ = "select oxtitle from " . $oArticle->getViewName() . " where oxid = " . $oDb->quote($sParentId);
390  self::$_aTitleCache[$sParentId] = $oDb->getOne($sQ);
391  }
392  $sTitle = self::$_aTitleCache[$sParentId];
393  }
394  }
395 
396  // variant has varselect value
397  if ($oArticle->oxarticles__oxvarselect->value) {
398  $sTitle .= ($sTitle ? ' ' : '') . $oArticle->oxarticles__oxvarselect->value . ' ';
399  } elseif (!$sTitle || ($oArticle->oxarticles__oxparentid->value)) {
400  // in case nothing was found - looking for number
401  $sTitle .= ($sTitle ? ' ' : '') . $oArticle->oxarticles__oxartnum->value;
402  }
403 
404  return $this->_prepareTitle($sTitle, false, $oArticle->getLanguage()) . '.html';
405  }
406 
416  public function getArticleVendorUri($oArticle, $iLang, $blRegenerate = false)
417  {
418  startProfile(__FUNCTION__);
419 
420  $sSeoUri = null;
421  if ($oVendor = $this->_getVendor($oArticle, $iLang)) {
422  //load details link from DB
423  if ($blRegenerate || !($sSeoUri = $this->_loadFromDb('oxarticle', $oArticle->getId(), $iLang, null, $oVendor->getId(), true))) {
424 
425  $oArticle = $this->_getProductForLang($oArticle, $iLang);
426 
427  // create title part for uri
428  $sTitle = $this->_prepareArticleTitle($oArticle);
429 
430  // create uri for all categories
431  $sSeoUri = oxRegistry::get("oxSeoEncoderVendor")->getVendorUri($oVendor, $iLang);
432  $sSeoUri = $this->_processSeoUrl($sSeoUri . $sTitle, $oArticle->getId(), $iLang);
433 
434  $aStdParams = array('cnid' => "v_" . $oVendor->getId(), 'listtype' => $this->_getListType());
435  $this->_saveToDb(
436  'oxarticle',
437  $oArticle->getId(),
438  oxRegistry::get("oxUtilsUrl")->appendUrl(
439  $oArticle->getBaseStdLink($iLang),
440  $aStdParams
441  ),
442  $sSeoUri,
443  $iLang,
444  null,
445  0,
446  $oVendor->getId()
447  );
448  }
449 
450  stopProfile(__FUNCTION__);
451  }
452 
453  return $sSeoUri;
454  }
455 
464  protected function _getVendor($oArticle, $iLang)
465  {
466  $oView = $this->getConfig()->getActiveView();
467 
468  $oVendor = null;
469  if ($sActVendorId = $oArticle->oxarticles__oxvendorid->value) {
470  if ($oView instanceof oxView && ($oActVendor = $oView->getActVendor())) {
471  $oVendor = $oActVendor;
472  } else {
473  $oVendor = oxNew("oxVendor");
474  }
475  if ($oVendor->getId() !== $sActVendorId) {
476  $oVendor = oxNew("oxVendor");
477  if (!$oVendor->loadInLang($iLang, $sActVendorId)) {
478  $oVendor = null;
479  }
480  }
481  }
482 
483  return $oVendor;
484  }
485 
495  public function getArticleManufacturerUri($oArticle, $iLang, $blRegenerate = false)
496  {
497  $sSeoUri = null;
498  startProfile(__FUNCTION__);
499  if ($oManufacturer = $this->_getManufacturer($oArticle, $iLang)) {
500  //load details link from DB
501  if ($blRegenerate || !($sSeoUri = $this->_loadFromDb('oxarticle', $oArticle->getId(), $iLang, null, $oManufacturer->getId(), true))) {
502 
503  $oArticle = $this->_getProductForLang($oArticle, $iLang);
504 
505  // create title part for uri
506  $sTitle = $this->_prepareArticleTitle($oArticle);
507 
508  // create uri for all categories
509  $sSeoUri = oxRegistry::get("oxSeoEncoderManufacturer")->getManufacturerUri($oManufacturer, $iLang);
510  $sSeoUri = $this->_processSeoUrl($sSeoUri . $sTitle, $oArticle->getId(), $iLang);
511 
512  $aStdParams = array('mnid' => $oManufacturer->getId(), 'listtype' => $this->_getListType());
513  $this->_saveToDb(
514  'oxarticle',
515  $oArticle->getId(),
516  oxRegistry::get("oxUtilsUrl")->appendUrl(
517  $oArticle->getBaseStdLink($iLang),
518  $aStdParams
519  ),
520  $sSeoUri,
521  $iLang,
522  null,
523  0,
524  $oManufacturer->getId()
525  );
526  }
527 
528  stopProfile(__FUNCTION__);
529  }
530 
531  return $sSeoUri;
532  }
533 
542  protected function _getManufacturer($oArticle, $iLang)
543  {
544  $oManufacturer = null;
545  if ($sActManufacturerId = $oArticle->oxarticles__oxmanufacturerid->value) {
546  $oView = $this->getConfig()->getActiveView();
547 
548  if ($oView instanceof oxView && ($oActManufacturer = $oView->getActManufacturer())) {
549  $oManufacturer = $oActManufacturer;
550  } else {
551  $oManufacturer = oxNew("oxManufacturer");
552  }
553 
554  if ($oManufacturer->getId() !== $sActManufacturerId || $oManufacturer->getLanguage() != $iLang) {
555  $oManufacturer = oxNew("oxManufacturer");
556  if (!$oManufacturer->loadInLang($iLang, $sActManufacturerId)) {
557  $oManufacturer = null;
558  }
559  }
560  }
561 
562  return $oManufacturer;
563  }
564 
573  public function getArticleMainUrl($oArticle, $iLang = null)
574  {
575  if (!isset($iLang)) {
576  $iLang = $oArticle->getLanguage();
577  }
578 
579  return $this->_getFullUrl($this->getArticleMainUri($oArticle, $iLang), $iLang);
580  }
581 
591  public function getArticleUrl($oArticle, $iLang = null, $iType = 0)
592  {
593  if (!isset($iLang)) {
594  $iLang = $oArticle->getLanguage();
595  }
596 
597  $sUri = null;
598  switch ($iType) {
600  $sUri = $this->getArticleVendorUri($oArticle, $iLang);
601  break;
603  $sUri = $this->getArticleManufacturerUri($oArticle, $iLang);
604  break;
606  $sUri = $this->getArticleTagUri($oArticle, $iLang);
607  break;
608  // @deprecated since v5.3 (2016-06-17); Listmania will be moved to an own module.
610  $sUri = $this->getArticleRecommUri($oArticle, $iLang);
611  break;
612  // END deprecated
613  case OXARTICLE_LINKTYPE_PRICECATEGORY: // goes price category urls to default (category urls)
614  default:
615  $sUri = $this->getArticleUri($oArticle, $iLang);
616  break;
617  }
618 
619  // if was unable to fetch type uri - returning main
620  if (!$sUri) {
621  $sUri = $this->getArticleMainUri($oArticle, $iLang);
622  }
623 
624  return $this->_getFullUrl($sUri, $iLang);
625  }
626 
632  public function onDeleteArticle($oArticle)
633  {
634  $oDb = oxDb::getDb();
635  $sIdQuoted = $oDb->quote($oArticle->getId());
636  $oDb->execute("delete from oxseo where oxobjectid = $sIdQuoted and oxtype = 'oxarticle'");
637  $oDb->execute("delete from oxobject2seodata where oxobjectid = $sIdQuoted");
638  }
639 
648  protected function _getAltUri($sObjectId, $iLang)
649  {
650  $sSeoUrl = null;
651  $oArticle = oxNew("oxArticle");
652  $oArticle->setSkipAssign(true);
653  if ($oArticle->loadInLang($iLang, $sObjectId)) {
654  // choosing URI type to generate
655  switch ($this->_getListType()) {
656  case 'vendor':
657  $sSeoUrl = $this->getArticleVendorUri($oArticle, $iLang, true);
658  break;
659  case 'manufacturer':
660  $sSeoUrl = $this->getArticleManufacturerUri($oArticle, $iLang, true);
661  break;
662  // @deprecated v5.3 (2016-05-04); Tags will be moved to own module.
663  case 'tag':
664  $sSeoUrl = $this->getArticleTagUri($oArticle, $iLang, true);
665  break;
666  // END deprecated
667  default:
668  $sSeoUrl = $this->getArticleUri($oArticle, $iLang, true);
669  break;
670  }
671  }
672 
673  return $sSeoUrl;
674  }
675 }