00001 <?php
00002
00007 class oxSeoEncoderArticle extends oxSeoEncoder
00008 {
00014 protected static $_instance = null;
00015
00021 protected static $_aTitleCache = array();
00022
00028 public static function getInstance()
00029 {
00030 if ( defined( 'OXID_PHP_UNIT' ) ) {
00031 self::$_instance = modInstances::getMod( __CLASS__ );
00032 }
00033
00034 if (!self::$_instance) {
00035 self::$_instance = oxNew("oxSeoEncoderArticle");
00036 if ( defined( 'OXID_PHP_UNIT' ) ) {
00037 modInstances::addMod( __CLASS__, self::$_instance);
00038 }
00039 }
00040
00041 if ( defined( 'OXID_PHP_UNIT' ) ) {
00042
00043 self::$_instance->_aSeoCache = array();
00044 }
00045
00046 return self::$_instance;
00047 }
00048
00054 protected function _getUrlExtension()
00055 {
00056 return '.html';
00057 }
00058
00068 protected function _getProductForLang( $oArticle, $iLang )
00069 {
00070 if ( isset( $iLang ) && $iLang != $oArticle->getLanguage() ) {
00071 $sId = $oArticle->getId();
00072 $oArticle = oxNew( 'oxarticle' );
00073 $oArticle->loadInLang( $iLang, $sId );
00074 }
00075
00076 return $oArticle;
00077 }
00078
00087 protected function _getArticleRecommUri( $oArticle, $iLang )
00088 {
00089 $sSeoUri = null;
00090 if ( $oRecomm = $this->_getRecomm( $oArticle, $iLang ) ) {
00091
00092 if ( !( $sSeoUri = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $oRecomm->getId(), true ) ) ) {
00093
00094 $oArticle = $this->_getProductForLang( $oArticle, $iLang );
00095
00096
00097 $sTitle = $this->_prepareArticleTitle( $oArticle );
00098
00099
00100 $sSeoUri = oxSeoEncoderRecomm::getInstance()->getRecommUri( $oRecomm, $iLang );
00101 $sSeoUri = $this->_processSeoUrl( $sSeoUri . $sTitle, $oArticle->getId(), $iLang );
00102
00103 $aStdParams = array( 'recommid' => $oRecomm->getId(), 'listtype' => $this->_getListType() );
00104 $this->_saveToDb( 'oxarticle', $oArticle->getId(), $oArticle->getStdLink( $iLang, $aStdParams ), $sSeoUri, $iLang, null, 0, false, false, $oRecomm->getId() );
00105 }
00106 }
00107 return $sSeoUri;
00108 }
00109
00118 protected function _getRecomm( $oArticle, $iLang )
00119 {
00120 $oList = null;
00121 $oView = $this->getConfig()->getActiveView();
00122 if ( $oView instanceof oxView ) {
00123 $oList = $oView->getActiveRecommList();
00124 }
00125 return $oList;
00126 }
00127
00133 protected function _getListType()
00134 {
00135 return $this->getConfig()->getActiveView()->getListType();
00136 }
00137
00147 protected function _getArticleTagUri( $oArticle, $iLang, $blRegenerate = false )
00148 {
00149 $sSeoUri = null;
00150 if ( $sTag = $this->_getTag( $oArticle, $iLang ) ) {
00151 $iShopId = $this->getConfig()->getShopId();
00152 $sStdUrl = $oArticle->getStdTagLink( $sTag );
00153 if ( $blRegenerate || !( $sSeoUri = $this->_loadFromDb( 'dynamic', $this->getDynamicObjectId( $iShopId, $sStdUrl ), $iLang ) ) ) {
00154
00155 $sSeoUri = oxSeoEncoderTag::getInstance()->getTagUri( $sTag, $iLang );
00156 $sSeoUri .= $this->_prepareArticleTitle( $oArticle );
00157 $sSeoUri = $this->_processSeoUrl( $sSeoUri, $this->_getStaticObjectId( $iShopId, $sStdUrl ), $iLang );
00158 $sSeoUri = $this->_getDynamicUri( $sStdUrl, $sSeoUri, $iLang );
00159 }
00160 }
00161 return $sSeoUri;
00162 }
00163
00172 protected function _getTag( $oArticle, $iLang )
00173 {
00174 $sTag = null;
00175 $oView = $this->getConfig()->getActiveView();
00176 if ( $oView instanceof oxView ) {
00177 $sTag = $oView->getTag();
00178 }
00179 return $sTag;
00180 }
00181
00191 protected function _createArticleCategoryUri( $oArticle, $oCategory, $iLang )
00192 {
00193 startProfile(__FUNCTION__);
00194 $oArticle = $this->_getProductForLang( $oArticle, $iLang );
00195
00196
00197 $sTitle = $this->_prepareArticleTitle( $oArticle );
00198
00199
00200 $sSeoUri = $this->_processSeoUrl(
00201 oxSeoEncoderCategory::getInstance()->getCategoryUri( $oCategory, $iLang ).$sTitle,
00202 $oArticle->getId(), $iLang
00203 );
00204 $sCatId = $oCategory->getId();
00205 $this->_saveToDb( 'oxarticle', $oArticle->getId(), $oArticle->getStdLink( $iLang, array( 'cnid' => $sCatId ) ), $sSeoUri, $iLang, null, 0, false, false, $sCatId);
00206
00207 stopProfile(__FUNCTION__);
00208
00209 return $sSeoUri;
00210 }
00211
00221 protected function _getArticleUri( $oArticle, $iLang, $blRegenerate = false )
00222 {
00223 startProfile(__FUNCTION__);
00224
00225 $sActCatId = '';
00226 if ( $oActCat = $this->_getCategory( $oArticle, $iLang ) ) {
00227 $sActCatId = $oActCat->getId();
00228 }
00229
00230
00231 if ( $blRegenerate || !( $sSeoUri = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $sActCatId, true ) ) ) {
00232 if ( $oActCat ) {
00233 $blInCat = false;
00234 if ( $oActCat->isPriceCategory() ) {
00235 $blInCat = $oArticle->inPriceCategory( $sActCatId );
00236 } else {
00237 $blInCat = $oArticle->inCategory( $sActCatId );
00238 }
00239 if ( $blInCat ) {
00240 $sSeoUri = $this->_createArticleCategoryUri( $oArticle, $oActCat, $iLang );
00241 }
00242 }
00243 }
00244
00245 stopProfile(__FUNCTION__);
00246
00247 return $sSeoUri;
00248 }
00249
00258 protected function _getCategory( $oArticle, $iLang )
00259 {
00260 $oCat = null;
00261 $oView = $this->getConfig()->getActiveView();
00262 if ( $oView instanceof oxview ) {
00263 $oCat = $oView->getActCategory();
00264 }
00265 return $oCat;
00266 }
00267
00276 protected function _getArticleMainUri( $oArticle, $iLang )
00277 {
00278 startProfile(__FUNCTION__);
00279
00280
00281 $sArtId = $oArticle->getId();
00282 if ( isset( $oArticle->oxarticles__oxparentid->value ) && $oArticle->oxarticles__oxparentid->value ) {
00283 $sArtId = $oArticle->oxarticles__oxparentid->value;
00284 }
00285
00286 if ( !( $sMainCatId = oxDb::getDb()->getOne( "select oxcatnid from ".getViewName( "oxobject2category" )." where oxobjectid = '{$sArtId}' order by oxtime" ) ) ) {
00287 $sMainCatId = '';
00288 }
00289
00290
00291 if ( !( $sSeoUri = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $sMainCatId, true ) ) ) {
00292 if ( $sMainCatId ) {
00293 $oMainCat = oxNew( "oxcategory" );
00294 $oMainCat->load( $sMainCatId );
00295
00296 $sSeoUri = $this->_createArticleCategoryUri( $oArticle, $oMainCat, $iLang );
00297 } else {
00298
00299 $oArticle = $this->_getProductForLang( $oArticle, $iLang );
00300 $sSeoUri = $this->_processSeoUrl( $this->_prepareArticleTitle( $oArticle ), $oArticle->getId(), $iLang );
00301
00302
00303 $this->_saveToDb( 'oxarticle', $oArticle->getId(), $oArticle->getStdLink( $iLang ), $sSeoUri, $iLang, null, 0, false, false, '' );
00304 }
00305 }
00306
00307 stopProfile(__FUNCTION__);
00308 return $sSeoUri;
00309 }
00310
00319 protected function _prepareArticleTitle( $oArticle )
00320 {
00321 $sTitle = '';
00322
00323
00324 if ( !( $sTitle = $oArticle->oxarticles__oxtitle->value ) ) {
00325
00326 if ( ( $sParentId = $oArticle->oxarticles__oxparentid->value ) ) {
00327
00328
00329 if ( !isset( self::$_aTitleCache[$sParentId] ) ) {
00330 $oDb = oxDb::getDb();
00331 $sQ = "select oxtitle from oxarticles where oxid = ".$oDb->quote( $sParentId );
00332 self::$_aTitleCache[$sParentId] = $oDb->getOne( $sQ );
00333 }
00334 $sTitle = self::$_aTitleCache[$sParentId];
00335 }
00336 }
00337
00338
00339 if ( $oArticle->oxarticles__oxvarselect->value ) {
00340 $sTitle .= ( $sTitle ? ' ' : '' ).$oArticle->oxarticles__oxvarselect->value . ' ';
00341 }
00342
00343
00344 if ( !$sTitle ) {
00345 $sTitle .= $oArticle->oxarticles__oxartnum->value;
00346 }
00347
00348 return $this->_prepareTitle( $sTitle ) . '.html';
00349 }
00350
00360 protected function _getArticleVendorUri( $oArticle, $iLang, $blRegenerate = false )
00361 {
00362 startProfile(__FUNCTION__);
00363
00364 $sSeoUri = null;
00365 if ( $oVendor = $this->_getVendor( $oArticle, $iLang ) ) {
00366
00367 if ( $blRegenerate || !( $sSeoUri = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $oVendor->getId(), true ) ) ) {
00368
00369 $oArticle = $this->_getProductForLang( $oArticle, $iLang );
00370
00371
00372 $sTitle = $this->_prepareArticleTitle( $oArticle );
00373
00374
00375 $sSeoUri = oxSeoEncoderVendor::getInstance()->getVendorUri( $oVendor, $iLang );
00376 $sSeoUri = $this->_processSeoUrl( $sSeoUri . $sTitle, $oArticle->getId(), $iLang );
00377
00378 $aStdParams = array( 'cnid' => "v_".$oVendor->getId(), 'listtype' => $this->_getListType() );
00379 $this->_saveToDb( 'oxarticle', $oArticle->getId(), $oArticle->getStdLink( $iLang, $aStdParams ), $sSeoUri, $iLang, null, 0, false, false, $oVendor->getId() );
00380 }
00381
00382 stopProfile(__FUNCTION__);
00383 }
00384 return $sSeoUri;
00385 }
00386
00395 protected function _getVendor( $oArticle, $iLang )
00396 {
00397 $oView = $this->getConfig()->getActiveView();
00398
00399 $oVendor = null;
00400 if ( $sActVendorId = $oArticle->oxarticles__oxvendorid->value ) {
00401 if ( $oView instanceof oxView && ( $oActVendor = $oView->getActVendor() ) ) {
00402 $oVendor = $oActVendor;
00403 } else {
00404 $oVendor = oxNew( "oxVendor" );
00405 }
00406 if ( $oVendor->getId() !== $sActVendorId ) {
00407 if ( !$oVendor->loadInLang( $iLang, $sActVendorId ) ) {
00408 $oVendor = null;
00409 }
00410 }
00411 }
00412
00413 return $oVendor;
00414 }
00415
00425 protected function _getArticleManufacturerUri( $oArticle, $iLang, $blRegenerate = false )
00426 {
00427 $sSeoUri = null;
00428 startProfile(__FUNCTION__);
00429 if ( $oManufacturer = $this->_getManufacturer( $oArticle, $iLang ) ) {
00430
00431 if ( $blRegenerate || !( $sSeoUri = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $oManufacturer->getId(), true ) ) ) {
00432
00433 $oArticle = $this->_getProductForLang( $oArticle, $iLang );
00434
00435
00436 $sTitle = $this->_prepareArticleTitle( $oArticle );
00437
00438
00439 $sSeoUri = oxSeoEncoderManufacturer::getInstance()->getManufacturerUri( $oManufacturer, $iLang );
00440 $sSeoUri = $this->_processSeoUrl( $sSeoUri . $sTitle, $oArticle->getId(), $iLang );
00441
00442 $aStdParams = array( 'mnid' => $oManufacturer->getId(), 'listtype' => $this->_getListType() );
00443 $this->_saveToDb( 'oxarticle', $oArticle->getId(), $oArticle->getStdLink( $iLang, $aStdParams ), $sSeoUri, $iLang, null, 0, false, false, $oManufacturer->getId() );
00444 }
00445
00446 stopProfile(__FUNCTION__);
00447 }
00448 return $sSeoUri;
00449 }
00450
00459 protected function _getManufacturer( $oArticle, $iLang )
00460 {
00461 $oManufacturer = null;
00462 if ( $sActManufacturerId = $oArticle->oxarticles__oxmanufacturerid->value ) {
00463 $oView = $this->getConfig()->getActiveView();
00464 if ( $oView instanceof oxView && ( $oActManufacturer = $oView->getActManufacturer() ) ) {
00465 $oManufacturer = $oActManufacturer;
00466 } else {
00467 $oManufacturer = oxNew( "oxManufacturer" );
00468 }
00469 if ( $oManufacturer->getId() !== $sActManufacturerId || $oManufacturer->getLanguage() != $iLang ) {
00470 if ( !$oManufacturer->loadInLang( $iLang, $sActManufacturerId ) ) {
00471 $oManufacturer = null;
00472 }
00473 }
00474 }
00475
00476 return $oManufacturer;
00477 }
00478
00487 public function getArticleMainUrl( $oArticle, $iLang = null )
00488 {
00489 if ( !isset( $iLang ) ) {
00490 $iLang = $oArticle->getLanguage();
00491 }
00492
00493 return $this->_getFullUrl( $this->_getArticleMainUri( $oArticle, $iLang ), $iLang );
00494 }
00495
00505 public function getArticleUrl( $oArticle, $iLang = null, $iType = 0 )
00506 {
00507 if ( !isset( $iLang ) ) {
00508 $iLang = $oArticle->getLanguage();
00509 }
00510
00511 $sUri = null;
00512 switch ( $iType ) {
00513 case OXARTICLE_LINKTYPE_VENDOR :
00514 $sUri = $this->_getArticleVendorUri( $oArticle, $iLang );
00515 break;
00516 case OXARTICLE_LINKTYPE_MANUFACTURER :
00517 $sUri = $this->_getArticleManufacturerUri( $oArticle, $iLang );
00518 break;
00519 case OXARTICLE_LINKTYPE_TAG :
00520 $sUri = $this->_getArticleTagUri( $oArticle, $iLang );
00521 break;
00522 case OXARTICLE_LINKTYPE_RECOMM :
00523 $sUri = $this->_getArticleRecommUri( $oArticle, $iLang );
00524 break;
00525 case OXARTICLE_LINKTYPE_PRICECATEGORY :
00526 default:
00527 $sUri = $this->_getArticleUri( $oArticle, $iLang );
00528 break;
00529 }
00530
00531
00532 if ( !$sUri ) {
00533 $sUri = $this->_getArticleMainUri( $oArticle, $iLang );
00534 }
00535
00536 return $this->_getFullUrl( $sUri, $iLang );
00537 }
00538
00546 public function onDeleteArticle( $oArticle )
00547 {
00548 $sIdQuoted = oxDb::getDb()->quote($oArticle->getId());
00549 oxDb::getDb()->execute("delete from oxseo where oxobjectid = $sIdQuoted and oxtype = 'oxarticle'");
00550 }
00551
00560 protected function _getAltUri( $sObjectId, $iLang )
00561 {
00562 $sSeoUrl = null;
00563 $oArticle = oxNew( "oxarticle" );
00564 if ( $oArticle->loadInLang( $iLang, $sObjectId ) ) {
00565
00566 switch( $this->_getListType() ) {
00567 case 'vendor':
00568 $sSeoUrl = $this->_getArticleVendorUri( $oArticle, $iLang, true );
00569 break;
00570 case 'manufacturer':
00571 $sSeoUrl = $this->_getArticleManufacturerUri( $oArticle, $iLang, true );
00572 break;
00573 case 'tag':
00574 $sSeoUrl = $this->_getArticleTagUri( $oArticle, $iLang, true );
00575 break;
00576 default:
00577 $sSeoUrl = $this->_getArticleUri( $oArticle, $iLang, true );
00578 break;
00579 }
00580 }
00581 return $sSeoUrl;
00582 }
00583 }