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->setSkipAssign( true );
00074             $oArticle->loadInLang( $iLang, $sId );
00075         }
00076 
00077         return $oArticle;
00078     }
00079 
00091     protected function _getArticleRecommUri( $oArticle, $iLang )
00092     {
00093         return $this->getArticleRecommUri( $oArticle, $iLang );
00094     }
00095 
00104     public function getArticleRecommUri( $oArticle, $iLang )
00105     {
00106         $sSeoUri = null;
00107         if ( $oRecomm = $this->_getRecomm( $oArticle, $iLang ) ) {
00108             //load details link from DB
00109             if ( !( $sSeoUri = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $oRecomm->getId(), true ) ) ) {
00110 
00111                 $oArticle = $this->_getProductForLang( $oArticle, $iLang );
00112 
00113                 // create title part for uri
00114                 $sTitle = $this->_prepareArticleTitle( $oArticle );
00115 
00116                 // create uri for all categories
00117                 $sSeoUri = oxSeoEncoderRecomm::getInstance()->getRecommUri( $oRecomm, $iLang );
00118                 $sSeoUri = $this->_processSeoUrl( $sSeoUri . $sTitle, $oArticle->getId(), $iLang );
00119 
00120                 $aStdParams = array( 'recommid' => $oRecomm->getId(), 'listtype' => $this->_getListType() );
00121                 $this->_saveToDb(
00122                             'oxarticle',
00123                             $oArticle->getId(),
00124                             oxUtilsUrl::getInstance()->appendUrl(
00125                                     $oArticle->getBaseStdLink( $iLang ),
00126                                     $aStdParams
00127                             ),
00128                             $sSeoUri,
00129                             $iLang,
00130                             null,
00131                             0,
00132                             $oRecomm->getId()
00133                         );
00134             }
00135         }
00136         return $sSeoUri;
00137     }
00138 
00147     protected function _getRecomm( $oArticle, $iLang )
00148     {
00149         $oList = null;
00150         $oView = $this->getConfig()->getActiveView();
00151         if ( $oView instanceof oxView ) {
00152             $oList = $oView->getActiveRecommList();
00153         }
00154         return $oList;
00155     }
00156 
00162     protected function _getListType()
00163     {
00164         return $this->getConfig()->getActiveView()->getListType();
00165     }
00166 
00179     protected function _getArticleTagUri( $oArticle, $iLang, $blRegenerate = false )
00180     {
00181         return $this->getArticleTagUri( $oArticle, $iLang, $blRegenerate );
00182     }
00192     public function getArticleTagUri( $oArticle, $iLang, $blRegenerate = false )
00193     {
00194         $sSeoUri = null;
00195         if ( $sTag = $this->_getTag( $oArticle, $iLang ) ) {
00196             $iShopId = $this->getConfig()->getShopId();
00197             $sStdUrl = $oArticle->getStdTagLink( $sTag );
00198             if ( $blRegenerate || !( $sSeoUri = $this->_loadFromDb( 'dynamic', $this->getDynamicObjectId( $iShopId, $sStdUrl ), $iLang ) ) ) {
00199                 // generating new if not found
00200                 if ( $sSeoUri = oxSeoEncoderTag::getInstance()->getTagUri( $sTag, $iLang, $oArticle->getId() ) ) {
00201                     $sSeoUri .= $this->_prepareArticleTitle( $oArticle );
00202                     $sSeoUri  = $this->_processSeoUrl( $sSeoUri, $this->_getStaticObjectId( $iShopId, $sStdUrl ), $iLang );
00203                     $sSeoUri  = $this->_getDynamicUri( $sStdUrl, $sSeoUri, $iLang );
00204                 }
00205             }
00206         }
00207         return $sSeoUri;
00208     }
00209 
00218     protected function _getTag( $oArticle, $iLang )
00219     {
00220         $sTag = null;
00221         $oView = $this->getConfig()->getActiveView();
00222         if ( $oView instanceof oxView ) {
00223             $sTag = $oView->getTag();
00224         }
00225         return $sTag;
00226     }
00227 
00237     protected function _createArticleCategoryUri( $oArticle, $oCategory, $iLang )
00238     {
00239         startProfile(__FUNCTION__);
00240         $oArticle = $this->_getProductForLang( $oArticle, $iLang );
00241 
00242         // create title part for uri
00243         $sTitle = $this->_prepareArticleTitle( $oArticle );
00244 
00245         // writing category path
00246         $sSeoUri = $this->_processSeoUrl(
00247                             oxSeoEncoderCategory::getInstance()->getCategoryUri( $oCategory, $iLang ).$sTitle,
00248                             $oArticle->getId(), $iLang
00249                         );
00250         $sCatId = $oCategory->getId();
00251         $this->_saveToDb(
00252                     'oxarticle',
00253                     $oArticle->getId(),
00254                     oxUtilsUrl::getInstance()->appendUrl(
00255                             $oArticle->getBaseStdLink( $iLang ),
00256                             array( 'cnid' => $sCatId )
00257                     ),
00258                     $sSeoUri,
00259                     $iLang,
00260                     null,
00261                     0,
00262                     $sCatId
00263                 );
00264 
00265         stopProfile(__FUNCTION__);
00266 
00267         return $sSeoUri;
00268     }
00269 
00282     protected function _getArticleUri( $oArticle, $iLang, $blRegenerate = false )
00283     {
00284         return $this->getArticleUri( $oArticle, $iLang, $blRegenerate );
00285     }
00286 
00296     public function getArticleUri( $oArticle, $iLang, $blRegenerate = false )
00297     {
00298         startProfile(__FUNCTION__);
00299 
00300         $sActCatId = '';
00301         if ( $oActCat = $this->_getCategory( $oArticle, $iLang ) ) {
00302             $sActCatId = $oActCat->getId();
00303         } elseif ( $oActCat = $this->_getMainCategory( $oArticle ) ) {
00304             $sActCatId = $oActCat->getId();
00305         }
00306 
00307         //load details link from DB
00308         if ( $blRegenerate || !( $sSeoUri = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $sActCatId, true ) ) ) {
00309             if ( $oActCat ) {
00310                 $blInCat = false;
00311                 if ( $oActCat->isPriceCategory() ) {
00312                     $blInCat = $oArticle->inPriceCategory( $sActCatId );
00313                 } else {
00314                     $blInCat = $oArticle->inCategory( $sActCatId );
00315                 }
00316                 if ( $blInCat ) {
00317                     $sSeoUri = $this->_createArticleCategoryUri( $oArticle, $oActCat, $iLang );
00318                 }
00319             }
00320         }
00321 
00322         stopProfile(__FUNCTION__);
00323 
00324         return $sSeoUri;
00325     }
00326 
00335     protected function _getCategory( $oArticle, $iLang )
00336     {
00337         $oCat = null;
00338         $oView = $this->getConfig()->getActiveView();
00339         if ( $oView instanceof oxview ) {
00340             $oCat = $oView->getActCategory();
00341         }
00342         return $oCat;
00343     }
00344 
00352     protected function _getMainCategory( $oArticle )
00353     {
00354         $oMainCat = null;
00355 
00356         // if variant parent id must be used
00357         $sArtId = $oArticle->getId();
00358         if ( isset( $oArticle->oxarticles__oxparentid->value ) && $oArticle->oxarticles__oxparentid->value ) {
00359             $sArtId = $oArticle->oxarticles__oxparentid->value;
00360         }
00361 
00362         $oDb = oxDb::getDb();
00363         $sMainCatId = $oDb->getOne( "select oxcatnid from ".getViewName( "oxobject2category" )." where oxobjectid = ".$oDb->quote( $sArtId )." order by oxtime" );
00364         if ( $sMainCatId ) {
00365             $oMainCat = oxNew( "oxcategory" );
00366             $oMainCat->load( $sMainCatId );
00367         }
00368 
00369         return $oMainCat;
00370     }
00371 
00383     protected function _getArticleMainUri( $oArticle, $iLang )
00384     {
00385         return $this->getArticleMainUri( $oArticle, $iLang );
00386     }
00387 
00396     public function getArticleMainUri( $oArticle, $iLang )
00397     {
00398         startProfile(__FUNCTION__);
00399 
00400         $oMainCat   = $this->_getMainCategory( $oArticle );
00401         $sMainCatId = $oMainCat ? $oMainCat->getId() : '';
00402 
00403         //load default article url from DB
00404         if ( !( $sSeoUri = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $sMainCatId, true ) ) ) {
00405             // save for main category
00406             if ( $oMainCat ) {
00407                 $sSeoUri = $this->_createArticleCategoryUri( $oArticle, $oMainCat, $iLang );
00408             } else {
00409                 // get default article url
00410                 $oArticle = $this->_getProductForLang( $oArticle, $iLang );
00411                 $sSeoUri = $this->_processSeoUrl( $this->_prepareArticleTitle( $oArticle ), $oArticle->getId(), $iLang );
00412 
00413                 // save default article url
00414                 $this->_saveToDb(
00415                         'oxarticle',
00416                         $oArticle->getId(),
00417                         $oArticle->getBaseStdLink( $iLang ),
00418                         $sSeoUri,
00419                         $iLang,
00420                         null,
00421                         0,
00422                         ''
00423                     );
00424             }
00425         }
00426 
00427         stopProfile(__FUNCTION__);
00428         return $sSeoUri;
00429     }
00430 
00439     protected function _prepareArticleTitle( $oArticle )
00440     {
00441         $sTitle = '';
00442 
00443         // create title part for uri
00444         if ( !( $sTitle = $oArticle->oxarticles__oxtitle->value ) ) {
00445             // taking parent article title
00446             if ( ( $sParentId = $oArticle->oxarticles__oxparentid->value ) ) {
00447 
00448                 // looking in cache ..
00449                 if ( !isset( self::$_aTitleCache[$sParentId] ) ) {
00450                     $oDb = oxDb::getDb();
00451                     $sQ = "select oxtitle from ".$oArticle->getViewName()." where oxid = ".$oDb->quote( $sParentId );
00452                     self::$_aTitleCache[$sParentId] = $oDb->getOne( $sQ );
00453                 }
00454                 $sTitle = self::$_aTitleCache[$sParentId];
00455             }
00456         }
00457 
00458         // variant has varselect value
00459         if ( $oArticle->oxarticles__oxvarselect->value ) {
00460             $sTitle .= ( $sTitle ? ' ' : '' ) . $oArticle->oxarticles__oxvarselect->value . ' ';
00461         } elseif ( !$sTitle || ( $oArticle->oxarticles__oxparentid->value ) ) {
00462             // in case nothing was found - looking for number
00463             $sTitle .= ( $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         $oArticle->setSkipAssign( true );
00747         if ( $oArticle->loadInLang( $iLang, $sObjectId ) ) {
00748             // choosing URI type to generate
00749             switch( $this->_getListType() ) {
00750                 case 'vendor':
00751                     $sSeoUrl = $this->_getArticleVendorUri( $oArticle, $iLang, true );
00752                     break;
00753                 case 'manufacturer':
00754                     $sSeoUrl = $this->_getArticleManufacturerUri( $oArticle, $iLang, true );
00755                     break;
00756                 case 'tag':
00757                     $sSeoUrl = $this->_getArticleTagUri( $oArticle, $iLang, true );
00758                     break;
00759                 default:
00760                     $sSeoUrl = $this->_getArticleUri( $oArticle, $iLang, true );
00761                     break;
00762             }
00763         }
00764         return $sSeoUrl;
00765     }
00766 }