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         }
00460 
00461         // in case nothing was found - looking for number
00462         if ( !$sTitle ) {
00463             $sTitle .= $oArticle->oxarticles__oxartnum->value;
00464         }
00465 
00466         return $this->_prepareTitle( $sTitle ) . '.html';
00467     }
00468 
00481     protected function _getArticleVendorUri( $oArticle, $iLang, $blRegenerate = false )
00482     {
00483         return $this->getArticleVendorUri( $oArticle, $iLang, $blRegenerate );
00484     }
00485 
00495     public function getArticleVendorUri( $oArticle, $iLang, $blRegenerate = false )
00496     {
00497         startProfile(__FUNCTION__);
00498 
00499         $sSeoUri = null;
00500         if ( $oVendor = $this->_getVendor( $oArticle, $iLang ) ) {
00501             //load details link from DB
00502             if ( $blRegenerate || !( $sSeoUri = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $oVendor->getId(), true ) ) ) {
00503 
00504                 $oArticle = $this->_getProductForLang( $oArticle, $iLang );
00505 
00506                 // create title part for uri
00507                 $sTitle = $this->_prepareArticleTitle( $oArticle );
00508 
00509                 // create uri for all categories
00510                 $sSeoUri = oxSeoEncoderVendor::getInstance()->getVendorUri( $oVendor, $iLang );
00511                 $sSeoUri = $this->_processSeoUrl( $sSeoUri . $sTitle, $oArticle->getId(), $iLang );
00512 
00513                 $aStdParams = array( 'cnid' => "v_".$oVendor->getId(), 'listtype' => $this->_getListType() );
00514                 $this->_saveToDb(
00515                         'oxarticle',
00516                         $oArticle->getId(),
00517                         oxUtilsUrl::getInstance()->appendUrl(
00518                                 $oArticle->getBaseStdLink( $iLang ),
00519                                 $aStdParams
00520                         ),
00521                         $sSeoUri,
00522                         $iLang,
00523                         null,
00524                         0,
00525                         $oVendor->getId()
00526                     );
00527             }
00528 
00529             stopProfile(__FUNCTION__);
00530         }
00531         return $sSeoUri;
00532     }
00533 
00542     protected function _getVendor( $oArticle, $iLang )
00543     {
00544         $oView = $this->getConfig()->getActiveView();
00545 
00546         $oVendor = null;
00547         if ( $sActVendorId = $oArticle->oxarticles__oxvendorid->value ) {
00548             if ( $oView instanceof oxView && ( $oActVendor = $oView->getActVendor() ) ) {
00549                 $oVendor = $oActVendor;
00550             } else {
00551                 $oVendor = oxNew( "oxVendor" );
00552             }
00553             if ( $oVendor->getId() !== $sActVendorId ) {
00554                 $oVendor = oxNew( "oxVendor" );
00555                 if ( !$oVendor->loadInLang( $iLang, $sActVendorId ) ) {
00556                     $oVendor = null;
00557                 }
00558             }
00559         }
00560 
00561         return $oVendor;
00562     }
00563 
00576     protected function _getArticleManufacturerUri( $oArticle, $iLang, $blRegenerate = false )
00577     {
00578         return $this->getArticleManufacturerUri( $oArticle, $iLang, $blRegenerate );
00579     }
00580 
00590     public function getArticleManufacturerUri( $oArticle, $iLang, $blRegenerate = false )
00591     {
00592         $sSeoUri = null;
00593         startProfile(__FUNCTION__);
00594         if ( $oManufacturer = $this->_getManufacturer( $oArticle, $iLang ) ) {
00595             //load details link from DB
00596             if ( $blRegenerate || !( $sSeoUri = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $oManufacturer->getId(), true ) ) ) {
00597 
00598                 $oArticle = $this->_getProductForLang( $oArticle, $iLang );
00599 
00600                 // create title part for uri
00601                 $sTitle = $this->_prepareArticleTitle( $oArticle );
00602 
00603                 // create uri for all categories
00604                 $sSeoUri = oxSeoEncoderManufacturer::getInstance()->getManufacturerUri( $oManufacturer, $iLang );
00605                 $sSeoUri = $this->_processSeoUrl( $sSeoUri . $sTitle, $oArticle->getId(), $iLang );
00606 
00607                 $aStdParams = array( 'mnid' => $oManufacturer->getId(), 'listtype' => $this->_getListType() );
00608                 $this->_saveToDb(
00609                         'oxarticle',
00610                         $oArticle->getId(),
00611                         oxUtilsUrl::getInstance()->appendUrl(
00612                                 $oArticle->getBaseStdLink( $iLang ),
00613                                 $aStdParams
00614                         ),
00615                         $sSeoUri,
00616                         $iLang,
00617                         null,
00618                         0,
00619                         $oManufacturer->getId()
00620                     );
00621             }
00622 
00623             stopProfile(__FUNCTION__);
00624         }
00625         return $sSeoUri;
00626     }
00627 
00636     protected function _getManufacturer( $oArticle, $iLang )
00637     {
00638         $oManufacturer = null;
00639         if ( $sActManufacturerId = $oArticle->oxarticles__oxmanufacturerid->value ) {
00640             $oView = $this->getConfig()->getActiveView();
00641 
00642             if ( $oView instanceof oxView && ( $oActManufacturer = $oView->getActManufacturer() ) ) {
00643                 $oManufacturer = $oActManufacturer;
00644             } else {
00645                 $oManufacturer = oxNew( "oxManufacturer" );
00646             }
00647 
00648             if ( $oManufacturer->getId() !== $sActManufacturerId || $oManufacturer->getLanguage() != $iLang ) {
00649                 $oManufacturer = oxNew( "oxManufacturer" );
00650                 if ( !$oManufacturer->loadInLang( $iLang, $sActManufacturerId ) ) {
00651                     $oManufacturer = null;
00652                 }
00653             }
00654         }
00655 
00656         return $oManufacturer;
00657     }
00658 
00667     public function getArticleMainUrl( $oArticle, $iLang = null )
00668     {
00669         if ( !isset( $iLang ) ) {
00670             $iLang = $oArticle->getLanguage();
00671         }
00672 
00673         return $this->_getFullUrl( $this->_getArticleMainUri( $oArticle, $iLang ), $iLang );
00674     }
00675 
00685     public function getArticleUrl( $oArticle, $iLang = null, $iType = 0 )
00686     {
00687         if ( !isset( $iLang ) ) {
00688             $iLang = $oArticle->getLanguage();
00689         }
00690 
00691         $sUri = null;
00692         switch ( $iType ) {
00693             case OXARTICLE_LINKTYPE_VENDOR :
00694                 $sUri = $this->_getArticleVendorUri( $oArticle, $iLang );
00695                 break;
00696             case OXARTICLE_LINKTYPE_MANUFACTURER :
00697                 $sUri = $this->_getArticleManufacturerUri( $oArticle, $iLang );
00698                 break;
00699             case OXARTICLE_LINKTYPE_TAG :
00700                 $sUri = $this->_getArticleTagUri( $oArticle, $iLang );
00701                 break;
00702             case OXARTICLE_LINKTYPE_RECOMM :
00703                 $sUri = $this->_getArticleRecommUri( $oArticle, $iLang );
00704                 break;
00705             case OXARTICLE_LINKTYPE_PRICECATEGORY : // goes price category urls to default (category urls)
00706             default:
00707                 $sUri = $this->_getArticleUri( $oArticle, $iLang );
00708                 break;
00709         }
00710 
00711         // if was unable to fetch type uri - returning main
00712         if ( !$sUri ) {
00713             $sUri = $this->_getArticleMainUri( $oArticle, $iLang );
00714         }
00715 
00716         return $this->_getFullUrl( $sUri, $iLang );
00717     }
00718 
00726     public function onDeleteArticle( $oArticle )
00727     {
00728         $oDb = oxDb::getDb();
00729         $sIdQuoted = $oDb->quote( $oArticle->getId() );
00730         $oDb->execute("delete from oxseo where oxobjectid = $sIdQuoted and oxtype = 'oxarticle'");
00731         $oDb->execute("delete from oxobject2seodata where oxobjectid = $sIdQuoted");
00732     }
00733 
00742     protected function _getAltUri( $sObjectId, $iLang )
00743     {
00744         $sSeoUrl = null;
00745         $oArticle = oxNew( "oxarticle" );
00746         if ( $oArticle->loadInLang( $iLang, $sObjectId ) ) {
00747             // choosing URI type to generate
00748             switch( $this->_getListType() ) {
00749                 case 'vendor':
00750                     $sSeoUrl = $this->_getArticleVendorUri( $oArticle, $iLang, true );
00751                     break;
00752                 case 'manufacturer':
00753                     $sSeoUrl = $this->_getArticleManufacturerUri( $oArticle, $iLang, true );
00754                     break;
00755                 case 'tag':
00756                     $sSeoUrl = $this->_getArticleTagUri( $oArticle, $iLang, true );
00757                     break;
00758                 default:
00759                     $sSeoUrl = $this->_getArticleUri( $oArticle, $iLang, true );
00760                     break;
00761             }
00762         }
00763         return $sSeoUrl;
00764     }
00765 }