OXID eShop CE  4.8.11
 All Classes Files Functions Variables Pages
oxseoencoderarticle.php
Go to the documentation of this file.
1 <?php
2 
8 {
14  protected static $_instance = null;
15 
21  protected static $_aTitleCache = array();
22 
30  public static function getInstance()
31  {
32  return oxRegistry::get("oxSeoEncoderArticle");
33  }
34 
40  protected function _getUrlExtension()
41  {
42  return '.html';
43  }
44 
54  protected function _getProductForLang( $oArticle, $iLang )
55  {
56  if ( isset( $iLang ) && $iLang != $oArticle->getLanguage() ) {
57  $sId = $oArticle->getId();
58  $oArticle = oxNew( 'oxArticle' );
59  $oArticle->setSkipAssign( true );
60  $oArticle->loadInLang( $iLang, $sId );
61  }
62 
63  return $oArticle;
64  }
65 
74  public function getArticleRecommUri( $oArticle, $iLang )
75  {
76  $sSeoUri = null;
77  if ( $oRecomm = $this->_getRecomm( $oArticle, $iLang ) ) {
78  //load details link from DB
79  if ( !( $sSeoUri = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $oRecomm->getId(), true ) ) ) {
80 
81  $oArticle = $this->_getProductForLang( $oArticle, $iLang );
82 
83  // create title part for uri
84  $sTitle = $this->_prepareArticleTitle( $oArticle );
85 
86  // create uri for all categories
87  $sSeoUri = oxRegistry::get("oxSeoEncoderRecomm")->getRecommUri( $oRecomm, $iLang );
88  $sSeoUri = $this->_processSeoUrl( $sSeoUri . $sTitle, $oArticle->getId(), $iLang );
89 
90  $aStdParams = array( 'recommid' => $oRecomm->getId(), 'listtype' => $this->_getListType() );
91  $this->_saveToDb(
92  'oxarticle',
93  $oArticle->getId(),
94  oxRegistry::get("oxUtilsUrl")->appendUrl(
95  $oArticle->getBaseStdLink( $iLang ),
96  $aStdParams
97  ),
98  $sSeoUri,
99  $iLang,
100  null,
101  0,
102  $oRecomm->getId()
103  );
104  }
105  }
106  return $sSeoUri;
107  }
108 
117  protected function _getRecomm( $oArticle, $iLang )
118  {
119  $oList = null;
120  $oView = $this->getConfig()->getActiveView();
121  if ( $oView instanceof oxView ) {
122  $oList = $oView->getActiveRecommList();
123  }
124  return $oList;
125  }
126 
132  protected function _getListType()
133  {
134  return $this->getConfig()->getActiveView()->getListType();
135  }
136 
146  public function getArticleTagUri( $oArticle, $iLang, $blRegenerate = false )
147  {
148  $sSeoUri = null;
149  if ( $sTag = $this->_getTag( $oArticle, $iLang ) ) {
150  $iShopId = $this->getConfig()->getShopId();
151  $sStdUrl = $oArticle->getStdTagLink( $sTag );
152  if ( $blRegenerate || !( $sSeoUri = $this->_loadFromDb( 'dynamic', $this->getDynamicObjectId( $iShopId, $sStdUrl ), $iLang ) ) ) {
153  // generating new if not found
154  if ( $sSeoUri = oxRegistry::get("oxSeoEncoderTag")->getTagUri( $sTag, $iLang, $oArticle->getId() ) ) {
155  $sSeoUri .= $this->_prepareArticleTitle( $oArticle );
156  $sSeoUri = $this->_processSeoUrl( $sSeoUri, $this->_getStaticObjectId( $iShopId, $sStdUrl ), $iLang );
157  $sSeoUri = $this->_getDynamicUri( $sStdUrl, $sSeoUri, $iLang );
158  }
159  }
160  }
161  return $sSeoUri;
162  }
163 
172  protected function _getTag( $oArticle, $iLang )
173  {
174  $sTag = null;
175  $oView = $this->getConfig()->getTopActiveView();
176  if ( $oView instanceof oxView ) {
177  $sTag = $oView->getTag();
178  }
179  return $sTag;
180  }
181 
191  protected function _createArticleCategoryUri( $oArticle, $oCategory, $iLang )
192  {
193  startProfile(__FUNCTION__);
194  $oArticle = $this->_getProductForLang( $oArticle, $iLang );
195 
196  // create title part for uri
197  $sTitle = $this->_prepareArticleTitle( $oArticle );
198 
199  // writing category path
200  $sSeoUri = $this->_processSeoUrl(
201  oxRegistry::get("oxSeoEncoderCategory")->getCategoryUri( $oCategory, $iLang ).$sTitle,
202  $oArticle->getId(), $iLang
203  );
204  $sCatId = $oCategory->getId();
205  $this->_saveToDb(
206  'oxarticle',
207  $oArticle->getId(),
208  oxRegistry::get("oxUtilsUrl")->appendUrl(
209  $oArticle->getBaseStdLink( $iLang ),
210  array( 'cnid' => $sCatId )
211  ),
212  $sSeoUri,
213  $iLang,
214  null,
215  0,
216  $sCatId
217  );
218 
219  stopProfile(__FUNCTION__);
220 
221  return $sSeoUri;
222  }
223 
233  public function getArticleUri( $oArticle, $iLang, $blRegenerate = false )
234  {
235  startProfile(__FUNCTION__);
236 
237  $sActCatId = '';
238 
239  $oActCat = $this->_getCategory( $oArticle, $iLang );
240 
241  if ( $oActCat instanceof oxCategory ) {
242  $sActCatId = $oActCat->getId();
243  } elseif ( $oActCat = $this->_getMainCategory( $oArticle ) ) {
244  $sActCatId = $oActCat->getId();
245  }
246 
247  //load details link from DB
248  if ( $blRegenerate || !( $sSeoUri = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $sActCatId, true ) ) ) {
249  if ( $oActCat ) {
250  $blInCat = false;
251  if ( $oActCat->isPriceCategory() ) {
252  $blInCat = $oArticle->inPriceCategory( $sActCatId );
253  } else {
254  $blInCat = $oArticle->inCategory( $sActCatId );
255  }
256  if ( $blInCat ) {
257  $sSeoUri = $this->_createArticleCategoryUri( $oArticle, $oActCat, $iLang );
258  }
259  }
260  }
261 
262  stopProfile(__FUNCTION__);
263 
264  return $sSeoUri;
265  }
266 
275  protected function _getCategory( $oArticle, $iLang )
276  {
277  $oCat = null;
278  $oView = $this->getConfig()->getActiveView();
279  if ( $oView instanceof oxUBase ) {
280  $oCat = $oView->getActiveCategory();
281  } elseif ( $oView instanceof oxView ) {
282  $oCat = $oView->getActCategory();
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  return $sSeoUri;
366  }
367 
376  protected function _prepareArticleTitle( $oArticle )
377  {
378  $sTitle = '';
379 
380  // create title part for uri
381  if ( !( $sTitle = $oArticle->oxarticles__oxtitle->value ) ) {
382  // taking parent article title
383  if ( ( $sParentId = $oArticle->oxarticles__oxparentid->value ) ) {
384 
385  // looking in cache ..
386  if ( !isset( self::$_aTitleCache[$sParentId] ) ) {
387  $oDb = oxDb::getDb();
388  $sQ = "select oxtitle from ".$oArticle->getViewName()." where oxid = ".$oDb->quote( $sParentId );
389  self::$_aTitleCache[$sParentId] = $oDb->getOne( $sQ );
390  }
391  $sTitle = self::$_aTitleCache[$sParentId];
392  }
393  }
394 
395  // variant has varselect value
396  if ( $oArticle->oxarticles__oxvarselect->value ) {
397  $sTitle .= ( $sTitle ? ' ' : '' ) . $oArticle->oxarticles__oxvarselect->value . ' ';
398  } elseif ( !$sTitle || ( $oArticle->oxarticles__oxparentid->value ) ) {
399  // in case nothing was found - looking for number
400  $sTitle .= ( $sTitle ? ' ' : '' ) . $oArticle->oxarticles__oxartnum->value;
401  }
402 
403  return $this->_prepareTitle( $sTitle, false, $oArticle->getLanguage() ) . '.html';
404  }
405 
415  public function getArticleVendorUri( $oArticle, $iLang, $blRegenerate = false )
416  {
417  startProfile(__FUNCTION__);
418 
419  $sSeoUri = null;
420  if ( $oVendor = $this->_getVendor( $oArticle, $iLang ) ) {
421  //load details link from DB
422  if ( $blRegenerate || !( $sSeoUri = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $oVendor->getId(), true ) ) ) {
423 
424  $oArticle = $this->_getProductForLang( $oArticle, $iLang );
425 
426  // create title part for uri
427  $sTitle = $this->_prepareArticleTitle( $oArticle );
428 
429  // create uri for all categories
430  $sSeoUri = oxRegistry::get("oxSeoEncoderVendor")->getVendorUri( $oVendor, $iLang );
431  $sSeoUri = $this->_processSeoUrl( $sSeoUri . $sTitle, $oArticle->getId(), $iLang );
432 
433  $aStdParams = array( 'cnid' => "v_".$oVendor->getId(), 'listtype' => $this->_getListType() );
434  $this->_saveToDb(
435  'oxarticle',
436  $oArticle->getId(),
437  oxRegistry::get("oxUtilsUrl")->appendUrl(
438  $oArticle->getBaseStdLink( $iLang ),
439  $aStdParams
440  ),
441  $sSeoUri,
442  $iLang,
443  null,
444  0,
445  $oVendor->getId()
446  );
447  }
448 
449  stopProfile(__FUNCTION__);
450  }
451  return $sSeoUri;
452  }
453 
462  protected function _getVendor( $oArticle, $iLang )
463  {
464  $oView = $this->getConfig()->getActiveView();
465 
466  $oVendor = null;
467  if ( $sActVendorId = $oArticle->oxarticles__oxvendorid->value ) {
468  if ( $oView instanceof oxView && ( $oActVendor = $oView->getActVendor() ) ) {
469  $oVendor = $oActVendor;
470  } else {
471  $oVendor = oxNew( "oxVendor" );
472  }
473  if ( $oVendor->getId() !== $sActVendorId ) {
474  $oVendor = oxNew( "oxVendor" );
475  if ( !$oVendor->loadInLang( $iLang, $sActVendorId ) ) {
476  $oVendor = null;
477  }
478  }
479  }
480 
481  return $oVendor;
482  }
483 
493  public function getArticleManufacturerUri( $oArticle, $iLang, $blRegenerate = false )
494  {
495  $sSeoUri = null;
496  startProfile(__FUNCTION__);
497  if ( $oManufacturer = $this->_getManufacturer( $oArticle, $iLang ) ) {
498  //load details link from DB
499  if ( $blRegenerate || !( $sSeoUri = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $oManufacturer->getId(), true ) ) ) {
500 
501  $oArticle = $this->_getProductForLang( $oArticle, $iLang );
502 
503  // create title part for uri
504  $sTitle = $this->_prepareArticleTitle( $oArticle );
505 
506  // create uri for all categories
507  $sSeoUri = oxRegistry::get("oxSeoEncoderManufacturer")->getManufacturerUri( $oManufacturer, $iLang );
508  $sSeoUri = $this->_processSeoUrl( $sSeoUri . $sTitle, $oArticle->getId(), $iLang );
509 
510  $aStdParams = array( 'mnid' => $oManufacturer->getId(), 'listtype' => $this->_getListType() );
511  $this->_saveToDb(
512  'oxarticle',
513  $oArticle->getId(),
514  oxRegistry::get("oxUtilsUrl")->appendUrl(
515  $oArticle->getBaseStdLink( $iLang ),
516  $aStdParams
517  ),
518  $sSeoUri,
519  $iLang,
520  null,
521  0,
522  $oManufacturer->getId()
523  );
524  }
525 
526  stopProfile(__FUNCTION__);
527  }
528  return $sSeoUri;
529  }
530 
539  protected function _getManufacturer( $oArticle, $iLang )
540  {
541  $oManufacturer = null;
542  if ( $sActManufacturerId = $oArticle->oxarticles__oxmanufacturerid->value ) {
543  $oView = $this->getConfig()->getActiveView();
544 
545  if ( $oView instanceof oxView && ( $oActManufacturer = $oView->getActManufacturer() ) ) {
546  $oManufacturer = $oActManufacturer;
547  } else {
548  $oManufacturer = oxNew( "oxManufacturer" );
549  }
550 
551  if ( $oManufacturer->getId() !== $sActManufacturerId || $oManufacturer->getLanguage() != $iLang ) {
552  $oManufacturer = oxNew( "oxManufacturer" );
553  if ( !$oManufacturer->loadInLang( $iLang, $sActManufacturerId ) ) {
554  $oManufacturer = null;
555  }
556  }
557  }
558 
559  return $oManufacturer;
560  }
561 
570  public function getArticleMainUrl( $oArticle, $iLang = null )
571  {
572  if ( !isset( $iLang ) ) {
573  $iLang = $oArticle->getLanguage();
574  }
575 
576  return $this->_getFullUrl( $this->getArticleMainUri( $oArticle, $iLang ), $iLang );
577  }
578 
588  public function getArticleUrl( $oArticle, $iLang = null, $iType = 0 )
589  {
590  if ( !isset( $iLang ) ) {
591  $iLang = $oArticle->getLanguage();
592  }
593 
594  $sUri = null;
595  switch ( $iType ) {
597  $sUri = $this->getArticleVendorUri( $oArticle, $iLang );
598  break;
600  $sUri = $this->getArticleManufacturerUri( $oArticle, $iLang );
601  break;
603  $sUri = $this->getArticleTagUri( $oArticle, $iLang );
604  break;
606  $sUri = $this->getArticleRecommUri( $oArticle, $iLang );
607  break;
608  case OXARTICLE_LINKTYPE_PRICECATEGORY : // goes price category urls to default (category urls)
609  default:
610  $sUri = $this->getArticleUri( $oArticle, $iLang );
611  break;
612  }
613 
614  // if was unable to fetch type uri - returning main
615  if ( !$sUri ) {
616  $sUri = $this->getArticleMainUri( $oArticle, $iLang );
617  }
618 
619  return $this->_getFullUrl( $sUri, $iLang );
620  }
621 
629  public function onDeleteArticle( $oArticle )
630  {
631  $oDb = oxDb::getDb();
632  $sIdQuoted = $oDb->quote( $oArticle->getId() );
633  $oDb->execute("delete from oxseo where oxobjectid = $sIdQuoted and oxtype = 'oxarticle'");
634  $oDb->execute("delete from oxobject2seodata where oxobjectid = $sIdQuoted");
635  }
636 
645  protected function _getAltUri( $sObjectId, $iLang )
646  {
647  $sSeoUrl = null;
648  $oArticle = oxNew( "oxArticle" );
649  $oArticle->setSkipAssign( true );
650  if ( $oArticle->loadInLang( $iLang, $sObjectId ) ) {
651  // choosing URI type to generate
652  switch( $this->_getListType() ) {
653  case 'vendor':
654  $sSeoUrl = $this->getArticleVendorUri( $oArticle, $iLang, true );
655  break;
656  case 'manufacturer':
657  $sSeoUrl = $this->getArticleManufacturerUri( $oArticle, $iLang, true );
658  break;
659  case 'tag':
660  $sSeoUrl = $this->getArticleTagUri( $oArticle, $iLang, true );
661  break;
662  default:
663  $sSeoUrl = $this->getArticleUri( $oArticle, $iLang, true );
664  break;
665  }
666  }
667  return $sSeoUrl;
668  }
669 }