oxseoencoderarticle.php

Go to the documentation of this file.
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             // resetting cache
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 
00090     protected function _getArticleRecommUri( $oArticle, $iLang )
00091     {
00092         return $this->getArticleRecommUri( $oArticle, $iLang );
00093     }
00094 
00103     public function getArticleRecommUri( $oArticle, $iLang )
00104     {
00105         $sSeoUri = null;
00106         if ( $oRecomm = $this->_getRecomm( $oArticle, $iLang ) ) {
00107             //load details link from DB
00108             if ( !( $sSeoUri = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $oRecomm->getId(), true ) ) ) {
00109 
00110                 $oArticle = $this->_getProductForLang( $oArticle, $iLang );
00111 
00112                 // create title part for uri
00113                 $sTitle = $this->_prepareArticleTitle( $oArticle );
00114 
00115                 // create uri for all categories
00116                 $sSeoUri = oxSeoEncoderRecomm::getInstance()->getRecommUri( $oRecomm, $iLang );
00117                 $sSeoUri = $this->_processSeoUrl( $sSeoUri . $sTitle, $oArticle->getId(), $iLang );
00118 
00119                 $aStdParams = array( 'recommid' => $oRecomm->getId(), 'listtype' => $this->_getListType() );
00120                 $this->_saveToDb(
00121                             'oxarticle',
00122                             $oArticle->getId(),
00123                             oxUtilsUrl::getInstance()->appendUrl(
00124                                     $oArticle->getBaseStdLink( $iLang ),
00125                                     $aStdParams
00126                             ),
00127                             $sSeoUri,
00128                             $iLang,
00129                             null,
00130                             0,
00131                             $oRecomm->getId()
00132                         );
00133             }
00134         }
00135         return $sSeoUri;
00136     }
00137 
00146     protected function _getRecomm( $oArticle, $iLang )
00147     {
00148         $oList = null;
00149         $oView = $this->getConfig()->getActiveView();
00150         if ( $oView instanceof oxView ) {
00151             $oList = $oView->getActiveRecommList();
00152         }
00153         return $oList;
00154     }
00155 
00161     protected function _getListType()
00162     {
00163         return $this->getConfig()->getActiveView()->getListType();
00164     }
00165 
00178     protected function _getArticleTagUri( $oArticle, $iLang, $blRegenerate = false )
00179     {
00180         return $this->getArticleTagUri( $oArticle, $iLang, $blRegenerate );
00181     }
00191     public function getArticleTagUri( $oArticle, $iLang, $blRegenerate = false )
00192     {
00193         $sSeoUri = null;
00194         if ( $sTag = $this->_getTag( $oArticle, $iLang ) ) {
00195             $iShopId = $this->getConfig()->getShopId();
00196             $sStdUrl = $oArticle->getStdTagLink( $sTag );
00197             if ( $blRegenerate || !( $sSeoUri = $this->_loadFromDb( 'dynamic', $this->getDynamicObjectId( $iShopId, $sStdUrl ), $iLang ) ) ) {
00198                 // generating new if not found
00199                 if ( $sSeoUri = oxSeoEncoderTag::getInstance()->getTagUri( $sTag, $iLang, $oArticle->getId() ) ) {
00200                     $sSeoUri .= $this->_prepareArticleTitle( $oArticle );
00201                     $sSeoUri  = $this->_processSeoUrl( $sSeoUri, $this->_getStaticObjectId( $iShopId, $sStdUrl ), $iLang );
00202                     $sSeoUri  = $this->_getDynamicUri( $sStdUrl, $sSeoUri, $iLang );
00203                 }
00204             }
00205         }
00206         return $sSeoUri;
00207     }
00208 
00217     protected function _getTag( $oArticle, $iLang )
00218     {
00219         $sTag = null;
00220         $oView = $this->getConfig()->getActiveView();
00221         if ( $oView instanceof oxView ) {
00222             $sTag = $oView->getTag();
00223         }
00224         return $sTag;
00225     }
00226 
00236     protected function _createArticleCategoryUri( $oArticle, $oCategory, $iLang )
00237     {
00238         startProfile(__FUNCTION__);
00239         $oArticle = $this->_getProductForLang( $oArticle, $iLang );
00240 
00241         // create title part for uri
00242         $sTitle = $this->_prepareArticleTitle( $oArticle );
00243 
00244         // writing category path
00245         $sSeoUri = $this->_processSeoUrl(
00246                             oxSeoEncoderCategory::getInstance()->getCategoryUri( $oCategory, $iLang ).$sTitle,
00247                             $oArticle->getId(), $iLang
00248                         );
00249         $sCatId = $oCategory->getId();
00250         $this->_saveToDb(
00251                     'oxarticle',
00252                     $oArticle->getId(),
00253                     oxUtilsUrl::getInstance()->appendUrl(
00254                             $oArticle->getBaseStdLink( $iLang ),
00255                             array( 'cnid' => $sCatId )
00256                     ),
00257                     $sSeoUri,
00258                     $iLang,
00259                     null,
00260                     0,
00261                     $sCatId
00262                 );
00263 
00264         stopProfile(__FUNCTION__);
00265 
00266         return $sSeoUri;
00267     }
00268 
00281     protected function _getArticleUri( $oArticle, $iLang, $blRegenerate = false )
00282     {
00283         return $this->getArticleUri( $oArticle, $iLang, $blRegenerate );
00284     }
00285 
00295     public function getArticleUri( $oArticle, $iLang, $blRegenerate = false )
00296     {
00297         startProfile(__FUNCTION__);
00298 
00299         $sActCatId = '';
00300         if ( $oActCat = $this->_getCategory( $oArticle, $iLang ) ) {
00301             $sActCatId = $oActCat->getId();
00302         } elseif ( $oActCat = $this->_getMainCategory( $oArticle ) ) {
00303             $sActCatId = $oActCat->getId();
00304         }
00305 
00306         //load details link from DB
00307         if ( $blRegenerate || !( $sSeoUri = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $sActCatId, true ) ) ) {
00308             if ( $oActCat ) {
00309                 $blInCat = false;
00310                 if ( $oActCat->isPriceCategory() ) {
00311                     $blInCat = $oArticle->inPriceCategory( $sActCatId );
00312                 } else {
00313                     $blInCat = $oArticle->inCategory( $sActCatId );
00314                 }
00315                 if ( $blInCat ) {
00316                     $sSeoUri = $this->_createArticleCategoryUri( $oArticle, $oActCat, $iLang );
00317                 }
00318             }
00319         }
00320 
00321         stopProfile(__FUNCTION__);
00322 
00323         return $sSeoUri;
00324     }
00325 
00334     protected function _getCategory( $oArticle, $iLang )
00335     {
00336         $oCat = null;
00337         $oView = $this->getConfig()->getActiveView();
00338         if ( $oView instanceof oxview ) {
00339             $oCat = $oView->getActCategory();
00340         }
00341         return $oCat;
00342     }
00343 
00351     protected function _getMainCategory( $oArticle )
00352     {
00353         $oMainCat = null;
00354 
00355         // if variant parent id must be used
00356         $sArtId = $oArticle->getId();
00357         if ( isset( $oArticle->oxarticles__oxparentid->value ) && $oArticle->oxarticles__oxparentid->value ) {
00358             $sArtId = $oArticle->oxarticles__oxparentid->value;
00359         }
00360 
00361         $sMainCatId = oxDb::getDb()->getOne( "select oxcatnid from ".getViewName( "oxobject2category" )." where oxobjectid = '{$sArtId}' order by oxtime" );
00362         if ( $sMainCatId ) {
00363             $oMainCat = oxNew( "oxcategory" );
00364             $oMainCat->load( $sMainCatId );
00365         }
00366 
00367         return $oMainCat;
00368     }
00369 
00381     protected function _getArticleMainUri( $oArticle, $iLang )
00382     {
00383         return $this->getArticleMainUri( $oArticle, $iLang );
00384     }
00385 
00394     public function getArticleMainUri( $oArticle, $iLang )
00395     {
00396         startProfile(__FUNCTION__);
00397 
00398         $oMainCat   = $this->_getMainCategory( $oArticle );
00399         $sMainCatId = $oMainCat ? $oMainCat->getId() : '';
00400 
00401         //load default article url from DB
00402         if ( !( $sSeoUri = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $sMainCatId, true ) ) ) {
00403             // save for main category
00404             if ( $oMainCat ) {
00405                 $sSeoUri = $this->_createArticleCategoryUri( $oArticle, $oMainCat, $iLang );
00406             } else {
00407                 // get default article url
00408                 $oArticle = $this->_getProductForLang( $oArticle, $iLang );
00409                 $sSeoUri = $this->_processSeoUrl( $this->_prepareArticleTitle( $oArticle ), $oArticle->getId(), $iLang );
00410 
00411                 // save default article url
00412                 $this->_saveToDb(
00413                         'oxarticle',
00414                         $oArticle->getId(),
00415                         $oArticle->getBaseStdLink( $iLang ),
00416                         $sSeoUri,
00417                         $iLang,
00418                         null,
00419                         0,
00420                         ''
00421                     );
00422             }
00423         }
00424 
00425         stopProfile(__FUNCTION__);
00426         return $sSeoUri;
00427     }
00428 
00437     protected function _prepareArticleTitle( $oArticle )
00438     {
00439         $sTitle = '';
00440 
00441         // create title part for uri
00442         if ( !( $sTitle = $oArticle->oxarticles__oxtitle->value ) ) {
00443             // taking parent article title
00444             if ( ( $sParentId = $oArticle->oxarticles__oxparentid->value ) ) {
00445 
00446                 // looking in cache ..
00447                 if ( !isset( self::$_aTitleCache[$sParentId] ) ) {
00448                     $oDb = oxDb::getDb();
00449                     $sQ = "select oxtitle from ".$oArticle->getViewName()." where oxid = ".$oDb->quote( $sParentId );
00450                     self::$_aTitleCache[$sParentId] = $oDb->getOne( $sQ );
00451                 }
00452                 $sTitle = self::$_aTitleCache[$sParentId];
00453             }
00454         }
00455 
00456         // variant has varselect value
00457         if ( $oArticle->oxarticles__oxvarselect->value ) {
00458             $sTitle .= ( $sTitle ? ' ' : '' ) . $oArticle->oxarticles__oxvarselect->value . ' ';
00459         } elseif ( !$sTitle || ( $oArticle->oxarticles__oxparentid->value ) ) {
00460             // in case nothing was found - looking for number
00461             $sTitle .= ( $sTitle ? ' ' : '' ) . $oArticle->oxarticles__oxartnum->value;
00462         }
00463 
00464         return $this->_prepareTitle( $sTitle ) . '.html';
00465     }
00466 
00479     protected function _getArticleVendorUri( $oArticle, $iLang, $blRegenerate = false )
00480     {
00481         return $this->getArticleVendorUri( $oArticle, $iLang, $blRegenerate );
00482     }
00483 
00493     public function getArticleVendorUri( $oArticle, $iLang, $blRegenerate = false )
00494     {
00495         startProfile(__FUNCTION__);
00496 
00497         $sSeoUri = null;
00498         if ( $oVendor = $this->_getVendor( $oArticle, $iLang ) ) {
00499             //load details link from DB
00500             if ( $blRegenerate || !( $sSeoUri = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $oVendor->getId(), true ) ) ) {
00501 
00502                 $oArticle = $this->_getProductForLang( $oArticle, $iLang );
00503 
00504                 // create title part for uri
00505                 $sTitle = $this->_prepareArticleTitle( $oArticle );
00506 
00507                 // create uri for all categories
00508                 $sSeoUri = oxSeoEncoderVendor::getInstance()->getVendorUri( $oVendor, $iLang );
00509                 $sSeoUri = $this->_processSeoUrl( $sSeoUri . $sTitle, $oArticle->getId(), $iLang );
00510 
00511                 $aStdParams = array( 'cnid' => "v_".$oVendor->getId(), 'listtype' => $this->_getListType() );
00512                 $this->_saveToDb(
00513                         'oxarticle',
00514                         $oArticle->getId(),
00515                         oxUtilsUrl::getInstance()->appendUrl(
00516                                 $oArticle->getBaseStdLink( $iLang ),
00517                                 $aStdParams
00518                         ),
00519                         $sSeoUri,
00520                         $iLang,
00521                         null,
00522                         0,
00523                         $oVendor->getId()
00524                     );
00525             }
00526 
00527             stopProfile(__FUNCTION__);
00528         }
00529         return $sSeoUri;
00530     }
00531 
00540     protected function _getVendor( $oArticle, $iLang )
00541     {
00542         $oView = $this->getConfig()->getActiveView();
00543 
00544         $oVendor = null;
00545         if ( $sActVendorId = $oArticle->oxarticles__oxvendorid->value ) {
00546             if ( $oView instanceof oxView && ( $oActVendor = $oView->getActVendor() ) ) {
00547                 $oVendor = $oActVendor;
00548             } else {
00549                 $oVendor = oxNew( "oxVendor" );
00550             }
00551             if ( $oVendor->getId() !== $sActVendorId ) {
00552                 $oVendor = oxNew( "oxVendor" );
00553                 if ( !$oVendor->loadInLang( $iLang, $sActVendorId ) ) {
00554                     $oVendor = null;
00555                 }
00556             }
00557         }
00558 
00559         return $oVendor;
00560     }
00561 
00574     protected function _getArticleManufacturerUri( $oArticle, $iLang, $blRegenerate = false )
00575     {
00576         return $this->getArticleManufacturerUri( $oArticle, $iLang, $blRegenerate );
00577     }
00578 
00588     public function getArticleManufacturerUri( $oArticle, $iLang, $blRegenerate = false )
00589     {
00590         $sSeoUri = null;
00591         startProfile(__FUNCTION__);
00592         if ( $oManufacturer = $this->_getManufacturer( $oArticle, $iLang ) ) {
00593             //load details link from DB
00594             if ( $blRegenerate || !( $sSeoUri = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $oManufacturer->getId(), true ) ) ) {
00595 
00596                 $oArticle = $this->_getProductForLang( $oArticle, $iLang );
00597 
00598                 // create title part for uri
00599                 $sTitle = $this->_prepareArticleTitle( $oArticle );
00600 
00601                 // create uri for all categories
00602                 $sSeoUri = oxSeoEncoderManufacturer::getInstance()->getManufacturerUri( $oManufacturer, $iLang );
00603                 $sSeoUri = $this->_processSeoUrl( $sSeoUri . $sTitle, $oArticle->getId(), $iLang );
00604 
00605                 $aStdParams = array( 'mnid' => $oManufacturer->getId(), 'listtype' => $this->_getListType() );
00606                 $this->_saveToDb(
00607                         'oxarticle',
00608                         $oArticle->getId(),
00609                         oxUtilsUrl::getInstance()->appendUrl(
00610                                 $oArticle->getBaseStdLink( $iLang ),
00611                                 $aStdParams
00612                         ),
00613                         $sSeoUri,
00614                         $iLang,
00615                         null,
00616                         0,
00617                         $oManufacturer->getId()
00618                     );
00619             }
00620 
00621             stopProfile(__FUNCTION__);
00622         }
00623         return $sSeoUri;
00624     }
00625 
00634     protected function _getManufacturer( $oArticle, $iLang )
00635     {
00636         $oManufacturer = null;
00637         if ( $sActManufacturerId = $oArticle->oxarticles__oxmanufacturerid->value ) {
00638             $oView = $this->getConfig()->getActiveView();
00639 
00640             if ( $oView instanceof oxView && ( $oActManufacturer = $oView->getActManufacturer() ) ) {
00641                 $oManufacturer = $oActManufacturer;
00642             } else {
00643                 $oManufacturer = oxNew( "oxManufacturer" );
00644             }
00645 
00646             if ( $oManufacturer->getId() !== $sActManufacturerId || $oManufacturer->getLanguage() != $iLang ) {
00647                 $oManufacturer = oxNew( "oxManufacturer" );
00648                 if ( !$oManufacturer->loadInLang( $iLang, $sActManufacturerId ) ) {
00649                     $oManufacturer = null;
00650                 }
00651             }
00652         }
00653 
00654         return $oManufacturer;
00655     }
00656 
00665     public function getArticleMainUrl( $oArticle, $iLang = null )
00666     {
00667         if ( !isset( $iLang ) ) {
00668             $iLang = $oArticle->getLanguage();
00669         }
00670 
00671         return $this->_getFullUrl( $this->_getArticleMainUri( $oArticle, $iLang ), $iLang );
00672     }
00673 
00683     public function getArticleUrl( $oArticle, $iLang = null, $iType = 0 )
00684     {
00685         if ( !isset( $iLang ) ) {
00686             $iLang = $oArticle->getLanguage();
00687         }
00688 
00689         $sUri = null;
00690         switch ( $iType ) {
00691             case OXARTICLE_LINKTYPE_VENDOR :
00692                 $sUri = $this->_getArticleVendorUri( $oArticle, $iLang );
00693                 break;
00694             case OXARTICLE_LINKTYPE_MANUFACTURER :
00695                 $sUri = $this->_getArticleManufacturerUri( $oArticle, $iLang );
00696                 break;
00697             case OXARTICLE_LINKTYPE_TAG :
00698                 $sUri = $this->_getArticleTagUri( $oArticle, $iLang );
00699                 break;
00700             case OXARTICLE_LINKTYPE_RECOMM :
00701                 $sUri = $this->_getArticleRecommUri( $oArticle, $iLang );
00702                 break;
00703             case OXARTICLE_LINKTYPE_PRICECATEGORY : // goes price category urls to default (category urls)
00704             default:
00705                 $sUri = $this->_getArticleUri( $oArticle, $iLang );
00706                 break;
00707         }
00708 
00709         // if was unable to fetch type uri - returning main
00710         if ( !$sUri ) {
00711             $sUri = $this->_getArticleMainUri( $oArticle, $iLang );
00712         }
00713 
00714         return $this->_getFullUrl( $sUri, $iLang );
00715     }
00716 
00724     public function onDeleteArticle( $oArticle )
00725     {
00726         $oDb = oxDb::getDb();
00727         $sIdQuoted = $oDb->quote( $oArticle->getId() );
00728         $oDb->execute("delete from oxseo where oxobjectid = $sIdQuoted and oxtype = 'oxarticle'");
00729         $oDb->execute("delete from oxobject2seodata where oxobjectid = $sIdQuoted");
00730     }
00731 
00740     protected function _getAltUri( $sObjectId, $iLang )
00741     {
00742         $sSeoUrl = null;
00743         $oArticle = oxNew( "oxarticle" );
00744         if ( $oArticle->loadInLang( $iLang, $sObjectId ) ) {
00745             // choosing URI type to generate
00746             switch( $this->_getListType() ) {
00747                 case 'vendor':
00748                     $sSeoUrl = $this->_getArticleVendorUri( $oArticle, $iLang, true );
00749                     break;
00750                 case 'manufacturer':
00751                     $sSeoUrl = $this->_getArticleManufacturerUri( $oArticle, $iLang, true );
00752                     break;
00753                 case 'tag':
00754                     $sSeoUrl = $this->_getArticleTagUri( $oArticle, $iLang, true );
00755                     break;
00756                 default:
00757                     $sSeoUrl = $this->_getArticleUri( $oArticle, $iLang, true );
00758                     break;
00759             }
00760         }
00761         return $sSeoUrl;
00762     }
00763 }