OXID eShop CE  4.9.5
 All Classes 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 
56  public function getArticleRecommUri($oArticle, $iLang)
57  {
58  $sSeoUri = null;
59  if ($oRecomm = $this->_getRecomm($oArticle, $iLang)) {
60  //load details link from DB
61  if (!($sSeoUri = $this->_loadFromDb('oxarticle', $oArticle->getId(), $iLang, null, $oRecomm->getId(), true))) {
62 
63  $oArticle = $this->_getProductForLang($oArticle, $iLang);
64 
65  // create title part for uri
66  $sTitle = $this->_prepareArticleTitle($oArticle);
67 
68  // create uri for all categories
69  $sSeoUri = oxRegistry::get("oxSeoEncoderRecomm")->getRecommUri($oRecomm, $iLang);
70  $sSeoUri = $this->_processSeoUrl($sSeoUri . $sTitle, $oArticle->getId(), $iLang);
71 
72  $aStdParams = array('recommid' => $oRecomm->getId(), 'listtype' => $this->_getListType());
73  $this->_saveToDb(
74  'oxarticle',
75  $oArticle->getId(),
76  oxRegistry::get("oxUtilsUrl")->appendUrl(
77  $oArticle->getBaseStdLink($iLang),
78  $aStdParams
79  ),
80  $sSeoUri,
81  $iLang,
82  null,
83  0,
84  $oRecomm->getId()
85  );
86  }
87  }
88 
89  return $sSeoUri;
90  }
91 
100  protected function _getRecomm($oArticle, $iLang)
101  {
102  $oList = null;
103  $oView = $this->getConfig()->getActiveView();
104  if ($oView instanceof oxView) {
105  $oList = $oView->getActiveRecommList();
106  }
107 
108  return $oList;
109  }
110 
116  protected function _getListType()
117  {
118  return $this->getConfig()->getActiveView()->getListType();
119  }
120 
130  public function getArticleTagUri($oArticle, $iLang, $blRegenerate = false)
131  {
132  $sSeoUri = null;
133  if ($sTag = $this->_getTag($oArticle, $iLang)) {
134  $iShopId = $this->getConfig()->getShopId();
135 
136  $oArticleTags = oxNew('oxArticleTagList');
137  $oArticleTags->setArticleId($oArticle->getId());
138  $oArticleTags->getStdTagLink($sTag);
139 
140  $sStdUrl = $oArticleTags->getStdTagLink($sTag);
141  if ($blRegenerate || !($sSeoUri = $this->_loadFromDb('dynamic', $this->getDynamicObjectId($iShopId, $sStdUrl), $iLang))) {
142  // generating new if not found
143  if ($sSeoUri = oxRegistry::get("oxSeoEncoderTag")->getTagUri($sTag, $iLang, $oArticle->getId())) {
144  $sSeoUri .= $this->_prepareArticleTitle($oArticle);
145  $sSeoUri = $this->_processSeoUrl($sSeoUri, $this->_getStaticObjectId($iShopId, $sStdUrl), $iLang);
146  $sSeoUri = $this->_getDynamicUri($sStdUrl, $sSeoUri, $iLang);
147  }
148  }
149  }
150 
151  return $sSeoUri;
152  }
153 
162  protected function _getTag($oArticle, $iLang)
163  {
164  $sTag = null;
165  $oView = $this->getConfig()->getTopActiveView();
166  if ($oView instanceof oxView) {
167  $sTag = $oView->getTag();
168  }
169 
170  return $sTag;
171  }
172 
182  protected function _createArticleCategoryUri($oArticle, $oCategory, $iLang)
183  {
184  startProfile(__FUNCTION__);
185  $oArticle = $this->_getProductForLang($oArticle, $iLang);
186 
187  // create title part for uri
188  $sTitle = $this->_prepareArticleTitle($oArticle);
189 
190  // writing category path
191  $sSeoUri = $this->_processSeoUrl(
192  oxRegistry::get("oxSeoEncoderCategory")->getCategoryUri($oCategory, $iLang) . $sTitle,
193  $oArticle->getId(), $iLang
194  );
195  $sCatId = $oCategory->getId();
196  $this->_saveToDb(
197  'oxarticle',
198  $oArticle->getId(),
199  oxRegistry::get("oxUtilsUrl")->appendUrl(
200  $oArticle->getBaseStdLink($iLang),
201  array('cnid' => $sCatId)
202  ),
203  $sSeoUri,
204  $iLang,
205  null,
206  0,
207  $sCatId
208  );
209 
210  stopProfile(__FUNCTION__);
211 
212  return $sSeoUri;
213  }
214 
224  public function getArticleUri($oArticle, $iLang, $blRegenerate = false)
225  {
226  startProfile(__FUNCTION__);
227 
228  $sActCatId = '';
229 
230  $oActCat = $this->_getCategory($oArticle, $iLang);
231 
232  if ($oActCat instanceof oxCategory) {
233  $sActCatId = $oActCat->getId();
234  } elseif ($oActCat = $this->_getMainCategory($oArticle)) {
235  $sActCatId = $oActCat->getId();
236  }
237 
238  //load details link from DB
239  if ($blRegenerate || !($sSeoUri = $this->_loadFromDb('oxarticle', $oArticle->getId(), $iLang, null, $sActCatId, true))) {
240  if ($oActCat) {
241  $blInCat = false;
242  if ($oActCat->isPriceCategory()) {
243  $blInCat = $oArticle->inPriceCategory($sActCatId);
244  } else {
245  $blInCat = $oArticle->inCategory($sActCatId);
246  }
247  if ($blInCat) {
248  $sSeoUri = $this->_createArticleCategoryUri($oArticle, $oActCat, $iLang);
249  }
250  }
251  }
252 
253  stopProfile(__FUNCTION__);
254 
255  return $sSeoUri;
256  }
257 
266  protected function _getCategory($oArticle, $iLang)
267  {
268  $oCat = null;
269  $oView = $this->getConfig()->getActiveView();
270  if ($oView instanceof oxUBase) {
271  $oCat = $oView->getActiveCategory();
272  } elseif ($oView instanceof oxView) {
273  $oCat = $oView->getActCategory();
274  }
275 
276  return $oCat;
277  }
278 
286  protected function _getMainCategory($oArticle)
287  {
288  $oMainCat = null;
289 
290  // if variant parent id must be used
291  $sArtId = $oArticle->getId();
292  if (isset($oArticle->oxarticles__oxparentid->value) && $oArticle->oxarticles__oxparentid->value) {
293  $sArtId = $oArticle->oxarticles__oxparentid->value;
294  }
295 
296  $oDb = oxDb::getDb();
297  // add main category caching;
298  $sQ = "select oxcatnid from " . getViewName("oxobject2category") . " where oxobjectid = " . $oDb->quote($sArtId) . " order by oxtime";
299  $sIdent = md5($sQ);
300 
301  if (($sMainCatId = $this->_loadFromCache($sIdent, "oxarticle")) === false) {
302  $sMainCatId = $oDb->getOne($sQ);
303  // storing in cache
304  $this->_saveInCache($sIdent, $sMainCatId, "oxarticle");
305  }
306 
307  if ($sMainCatId) {
308  $oMainCat = oxNew("oxCategory");
309  if (!$oMainCat->load($sMainCatId)) {
310  $oMainCat = null;
311  }
312  }
313 
314  return $oMainCat;
315  }
316 
325  public function getArticleMainUri($oArticle, $iLang)
326  {
327  startProfile(__FUNCTION__);
328 
329  $oMainCat = $this->_getMainCategory($oArticle);
330  $sMainCatId = $oMainCat ? $oMainCat->getId() : '';
331 
332  //load default article url from DB
333  if (!($sSeoUri = $this->_loadFromDb('oxarticle', $oArticle->getId(), $iLang, null, $sMainCatId, true))) {
334  // save for main category
335  if ($oMainCat) {
336  $sSeoUri = $this->_createArticleCategoryUri($oArticle, $oMainCat, $iLang);
337  } else {
338  // get default article url
339  $oArticle = $this->_getProductForLang($oArticle, $iLang);
340  $sSeoUri = $this->_processSeoUrl($this->_prepareArticleTitle($oArticle), $oArticle->getId(), $iLang);
341 
342  // save default article url
343  $this->_saveToDb(
344  'oxarticle',
345  $oArticle->getId(),
346  $oArticle->getBaseStdLink($iLang),
347  $sSeoUri,
348  $iLang,
349  null,
350  0,
351  ''
352  );
353  }
354  }
355 
356  stopProfile(__FUNCTION__);
357 
358  return $sSeoUri;
359  }
360 
369  protected function _prepareArticleTitle($oArticle)
370  {
371  $sTitle = '';
372 
373  // create title part for uri
374  if (!($sTitle = $oArticle->oxarticles__oxtitle->value)) {
375  // taking parent article title
376  if (($sParentId = $oArticle->oxarticles__oxparentid->value)) {
377 
378  // looking in cache ..
379  if (!isset(self::$_aTitleCache[$sParentId])) {
380  $oDb = oxDb::getDb();
381  $sQ = "select oxtitle from " . $oArticle->getViewName() . " where oxid = " . $oDb->quote($sParentId);
382  self::$_aTitleCache[$sParentId] = $oDb->getOne($sQ);
383  }
384  $sTitle = self::$_aTitleCache[$sParentId];
385  }
386  }
387 
388  // variant has varselect value
389  if ($oArticle->oxarticles__oxvarselect->value) {
390  $sTitle .= ($sTitle ? ' ' : '') . $oArticle->oxarticles__oxvarselect->value . ' ';
391  } elseif (!$sTitle || ($oArticle->oxarticles__oxparentid->value)) {
392  // in case nothing was found - looking for number
393  $sTitle .= ($sTitle ? ' ' : '') . $oArticle->oxarticles__oxartnum->value;
394  }
395 
396  return $this->_prepareTitle($sTitle, false, $oArticle->getLanguage()) . '.html';
397  }
398 
408  public function getArticleVendorUri($oArticle, $iLang, $blRegenerate = false)
409  {
410  startProfile(__FUNCTION__);
411 
412  $sSeoUri = null;
413  if ($oVendor = $this->_getVendor($oArticle, $iLang)) {
414  //load details link from DB
415  if ($blRegenerate || !($sSeoUri = $this->_loadFromDb('oxarticle', $oArticle->getId(), $iLang, null, $oVendor->getId(), true))) {
416 
417  $oArticle = $this->_getProductForLang($oArticle, $iLang);
418 
419  // create title part for uri
420  $sTitle = $this->_prepareArticleTitle($oArticle);
421 
422  // create uri for all categories
423  $sSeoUri = oxRegistry::get("oxSeoEncoderVendor")->getVendorUri($oVendor, $iLang);
424  $sSeoUri = $this->_processSeoUrl($sSeoUri . $sTitle, $oArticle->getId(), $iLang);
425 
426  $aStdParams = array('cnid' => "v_" . $oVendor->getId(), 'listtype' => $this->_getListType());
427  $this->_saveToDb(
428  'oxarticle',
429  $oArticle->getId(),
430  oxRegistry::get("oxUtilsUrl")->appendUrl(
431  $oArticle->getBaseStdLink($iLang),
432  $aStdParams
433  ),
434  $sSeoUri,
435  $iLang,
436  null,
437  0,
438  $oVendor->getId()
439  );
440  }
441 
442  stopProfile(__FUNCTION__);
443  }
444 
445  return $sSeoUri;
446  }
447 
456  protected function _getVendor($oArticle, $iLang)
457  {
458  $oView = $this->getConfig()->getActiveView();
459 
460  $oVendor = null;
461  if ($sActVendorId = $oArticle->oxarticles__oxvendorid->value) {
462  if ($oView instanceof oxView && ($oActVendor = $oView->getActVendor())) {
463  $oVendor = $oActVendor;
464  } else {
465  $oVendor = oxNew("oxVendor");
466  }
467  if ($oVendor->getId() !== $sActVendorId) {
468  $oVendor = oxNew("oxVendor");
469  if (!$oVendor->loadInLang($iLang, $sActVendorId)) {
470  $oVendor = null;
471  }
472  }
473  }
474 
475  return $oVendor;
476  }
477 
487  public function getArticleManufacturerUri($oArticle, $iLang, $blRegenerate = false)
488  {
489  $sSeoUri = null;
490  startProfile(__FUNCTION__);
491  if ($oManufacturer = $this->_getManufacturer($oArticle, $iLang)) {
492  //load details link from DB
493  if ($blRegenerate || !($sSeoUri = $this->_loadFromDb('oxarticle', $oArticle->getId(), $iLang, null, $oManufacturer->getId(), true))) {
494 
495  $oArticle = $this->_getProductForLang($oArticle, $iLang);
496 
497  // create title part for uri
498  $sTitle = $this->_prepareArticleTitle($oArticle);
499 
500  // create uri for all categories
501  $sSeoUri = oxRegistry::get("oxSeoEncoderManufacturer")->getManufacturerUri($oManufacturer, $iLang);
502  $sSeoUri = $this->_processSeoUrl($sSeoUri . $sTitle, $oArticle->getId(), $iLang);
503 
504  $aStdParams = array('mnid' => $oManufacturer->getId(), 'listtype' => $this->_getListType());
505  $this->_saveToDb(
506  'oxarticle',
507  $oArticle->getId(),
508  oxRegistry::get("oxUtilsUrl")->appendUrl(
509  $oArticle->getBaseStdLink($iLang),
510  $aStdParams
511  ),
512  $sSeoUri,
513  $iLang,
514  null,
515  0,
516  $oManufacturer->getId()
517  );
518  }
519 
520  stopProfile(__FUNCTION__);
521  }
522 
523  return $sSeoUri;
524  }
525 
534  protected function _getManufacturer($oArticle, $iLang)
535  {
536  $oManufacturer = null;
537  if ($sActManufacturerId = $oArticle->oxarticles__oxmanufacturerid->value) {
538  $oView = $this->getConfig()->getActiveView();
539 
540  if ($oView instanceof oxView && ($oActManufacturer = $oView->getActManufacturer())) {
541  $oManufacturer = $oActManufacturer;
542  } else {
543  $oManufacturer = oxNew("oxManufacturer");
544  }
545 
546  if ($oManufacturer->getId() !== $sActManufacturerId || $oManufacturer->getLanguage() != $iLang) {
547  $oManufacturer = oxNew("oxManufacturer");
548  if (!$oManufacturer->loadInLang($iLang, $sActManufacturerId)) {
549  $oManufacturer = null;
550  }
551  }
552  }
553 
554  return $oManufacturer;
555  }
556 
565  public function getArticleMainUrl($oArticle, $iLang = null)
566  {
567  if (!isset($iLang)) {
568  $iLang = $oArticle->getLanguage();
569  }
570 
571  return $this->_getFullUrl($this->getArticleMainUri($oArticle, $iLang), $iLang);
572  }
573 
583  public function getArticleUrl($oArticle, $iLang = null, $iType = 0)
584  {
585  if (!isset($iLang)) {
586  $iLang = $oArticle->getLanguage();
587  }
588 
589  $sUri = null;
590  switch ($iType) {
592  $sUri = $this->getArticleVendorUri($oArticle, $iLang);
593  break;
595  $sUri = $this->getArticleManufacturerUri($oArticle, $iLang);
596  break;
598  $sUri = $this->getArticleTagUri($oArticle, $iLang);
599  break;
601  $sUri = $this->getArticleRecommUri($oArticle, $iLang);
602  break;
603  case OXARTICLE_LINKTYPE_PRICECATEGORY: // goes price category urls to default (category urls)
604  default:
605  $sUri = $this->getArticleUri($oArticle, $iLang);
606  break;
607  }
608 
609  // if was unable to fetch type uri - returning main
610  if (!$sUri) {
611  $sUri = $this->getArticleMainUri($oArticle, $iLang);
612  }
613 
614  return $this->_getFullUrl($sUri, $iLang);
615  }
616 
622  public function onDeleteArticle($oArticle)
623  {
624  $oDb = oxDb::getDb();
625  $sIdQuoted = $oDb->quote($oArticle->getId());
626  $oDb->execute("delete from oxseo where oxobjectid = $sIdQuoted and oxtype = 'oxarticle'");
627  $oDb->execute("delete from oxobject2seodata where oxobjectid = $sIdQuoted");
628  }
629 
638  protected function _getAltUri($sObjectId, $iLang)
639  {
640  $sSeoUrl = null;
641  $oArticle = oxNew("oxArticle");
642  $oArticle->setSkipAssign(true);
643  if ($oArticle->loadInLang($iLang, $sObjectId)) {
644  // choosing URI type to generate
645  switch ($this->_getListType()) {
646  case 'vendor':
647  $sSeoUrl = $this->getArticleVendorUri($oArticle, $iLang, true);
648  break;
649  case 'manufacturer':
650  $sSeoUrl = $this->getArticleManufacturerUri($oArticle, $iLang, true);
651  break;
652  case 'tag':
653  $sSeoUrl = $this->getArticleTagUri($oArticle, $iLang, true);
654  break;
655  default:
656  $sSeoUrl = $this->getArticleUri($oArticle, $iLang, true);
657  break;
658  }
659  }
660 
661  return $sSeoUrl;
662  }
663 }