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 
00030     public static function getInstance()
00031     {
00032         return oxRegistry::get("oxSeoEncoderArticle");
00033     }
00034 
00040     protected function _getUrlExtension()
00041     {
00042         return '.html';
00043     }
00044 
00054     protected function _getProductForLang( $oArticle, $iLang )
00055     {
00056         if ( isset( $iLang ) && $iLang != $oArticle->getLanguage() ) {
00057             $sId = $oArticle->getId();
00058             $oArticle = oxNew( 'oxArticle' );
00059             $oArticle->setSkipAssign( true );
00060             $oArticle->loadInLang( $iLang, $sId );
00061         }
00062 
00063         return $oArticle;
00064     }
00065 
00074     public function getArticleRecommUri( $oArticle, $iLang )
00075     {
00076         $sSeoUri = null;
00077         if ( $oRecomm = $this->_getRecomm( $oArticle, $iLang ) ) {
00078             //load details link from DB
00079             if ( !( $sSeoUri = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $oRecomm->getId(), true ) ) ) {
00080 
00081                 $oArticle = $this->_getProductForLang( $oArticle, $iLang );
00082 
00083                 // create title part for uri
00084                 $sTitle = $this->_prepareArticleTitle( $oArticle );
00085 
00086                 // create uri for all categories
00087                 $sSeoUri = oxRegistry::get("oxSeoEncoderRecomm")->getRecommUri( $oRecomm, $iLang );
00088                 $sSeoUri = $this->_processSeoUrl( $sSeoUri . $sTitle, $oArticle->getId(), $iLang );
00089 
00090                 $aStdParams = array( 'recommid' => $oRecomm->getId(), 'listtype' => $this->_getListType() );
00091                 $this->_saveToDb(
00092                             'oxarticle',
00093                             $oArticle->getId(),
00094                             oxRegistry::get("oxUtilsUrl")->appendUrl(
00095                                     $oArticle->getBaseStdLink( $iLang ),
00096                                     $aStdParams
00097                             ),
00098                             $sSeoUri,
00099                             $iLang,
00100                             null,
00101                             0,
00102                             $oRecomm->getId()
00103                         );
00104             }
00105         }
00106         return $sSeoUri;
00107     }
00108 
00117     protected function _getRecomm( $oArticle, $iLang )
00118     {
00119         $oList = null;
00120         $oView = $this->getConfig()->getActiveView();
00121         if ( $oView instanceof oxView ) {
00122             $oList = $oView->getActiveRecommList();
00123         }
00124         return $oList;
00125     }
00126 
00132     protected function _getListType()
00133     {
00134         return $this->getConfig()->getActiveView()->getListType();
00135     }
00136 
00146     public function getArticleTagUri( $oArticle, $iLang, $blRegenerate = false )
00147     {
00148         $sSeoUri = null;
00149         if ( $sTag = $this->_getTag( $oArticle, $iLang ) ) {
00150             $iShopId = $this->getConfig()->getShopId();
00151             $sStdUrl = $oArticle->getStdTagLink( $sTag );
00152             if ( $blRegenerate || !( $sSeoUri = $this->_loadFromDb( 'dynamic', $this->getDynamicObjectId( $iShopId, $sStdUrl ), $iLang ) ) ) {
00153                 // generating new if not found
00154                 if ( $sSeoUri = oxRegistry::get("oxSeoEncoderTag")->getTagUri( $sTag, $iLang, $oArticle->getId() ) ) {
00155                     $sSeoUri .= $this->_prepareArticleTitle( $oArticle );
00156                     $sSeoUri  = $this->_processSeoUrl( $sSeoUri, $this->_getStaticObjectId( $iShopId, $sStdUrl ), $iLang );
00157                     $sSeoUri  = $this->_getDynamicUri( $sStdUrl, $sSeoUri, $iLang );
00158                 }
00159             }
00160         }
00161         return $sSeoUri;
00162     }
00163 
00172     protected function _getTag( $oArticle, $iLang )
00173     {
00174         $sTag = null;
00175         $oView = $this->getConfig()->getTopActiveView();
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         // create title part for uri
00197         $sTitle = $this->_prepareArticleTitle( $oArticle );
00198 
00199         // writing category path
00200         $sSeoUri = $this->_processSeoUrl(
00201                             oxRegistry::get("oxSeoEncoderCategory")->getCategoryUri( $oCategory, $iLang ).$sTitle,
00202                             $oArticle->getId(), $iLang
00203                         );
00204         $sCatId = $oCategory->getId();
00205         $this->_saveToDb(
00206                     'oxarticle',
00207                     $oArticle->getId(),
00208                     oxRegistry::get("oxUtilsUrl")->appendUrl(
00209                             $oArticle->getBaseStdLink( $iLang ),
00210                             array( 'cnid' => $sCatId )
00211                     ),
00212                     $sSeoUri,
00213                     $iLang,
00214                     null,
00215                     0,
00216                     $sCatId
00217                 );
00218 
00219         stopProfile(__FUNCTION__);
00220 
00221         return $sSeoUri;
00222     }
00223 
00233     public function getArticleUri( $oArticle, $iLang, $blRegenerate = false )
00234     {
00235         startProfile(__FUNCTION__);
00236 
00237         $sActCatId = '';
00238 
00239         $oActCat = $this->_getCategory( $oArticle, $iLang );
00240 
00241         if ( $oActCat instanceof oxCategory ) {
00242             $sActCatId = $oActCat->getId();
00243         } elseif ( $oActCat = $this->_getMainCategory( $oArticle ) ) {
00244             $sActCatId = $oActCat->getId();
00245         }
00246 
00247         //load details link from DB
00248         if ( $blRegenerate || !( $sSeoUri = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $sActCatId, true ) ) ) {
00249             if ( $oActCat ) {
00250                 $blInCat = false;
00251                 if ( $oActCat->isPriceCategory() ) {
00252                     $blInCat = $oArticle->inPriceCategory( $sActCatId );
00253                 } else {
00254                     $blInCat = $oArticle->inCategory( $sActCatId );
00255                 }
00256                 if ( $blInCat ) {
00257                     $sSeoUri = $this->_createArticleCategoryUri( $oArticle, $oActCat, $iLang );
00258                 }
00259             }
00260         }
00261 
00262         stopProfile(__FUNCTION__);
00263 
00264         return $sSeoUri;
00265     }
00266 
00275     protected function _getCategory( $oArticle, $iLang )
00276     {
00277         $oCat = null;
00278         $oView = $this->getConfig()->getActiveView();
00279         if ( $oView instanceof oxUBase ) {
00280             $oCat = $oView->getActiveCategory();
00281         } elseif ( $oView instanceof oxView ) {
00282             $oCat = $oView->getActCategory();
00283         }
00284         return $oCat;
00285     }
00286 
00294     protected function _getMainCategory( $oArticle )
00295     {
00296         $oMainCat = null;
00297 
00298         // if variant parent id must be used
00299         $sArtId = $oArticle->getId();
00300         if ( isset( $oArticle->oxarticles__oxparentid->value ) && $oArticle->oxarticles__oxparentid->value ) {
00301             $sArtId = $oArticle->oxarticles__oxparentid->value;
00302         }
00303 
00304         $oDb = oxDb::getDb();
00305         // add main category caching;
00306         $sQ = "select oxcatnid from ".getViewName( "oxobject2category" )." where oxobjectid = ".$oDb->quote( $sArtId )." order by oxtime";
00307         $sIdent = md5( $sQ );
00308 
00309         if ( ( $sMainCatId = $this->_loadFromCache( $sIdent, "oxarticle" ) ) === false ) {
00310             $sMainCatId = $oDb->getOne( $sQ );
00311             // storing in cache
00312             $this->_saveInCache( $sIdent, $sMainCatId, "oxarticle" );
00313         }
00314 
00315         if ( $sMainCatId ) {
00316             $oMainCat = oxNew( "oxCategory" );
00317             if ( ! $oMainCat->load( $sMainCatId )) {
00318                 $oMainCat = null;
00319             }
00320         }
00321 
00322         return $oMainCat;
00323     }
00324 
00333     public function getArticleMainUri( $oArticle, $iLang )
00334     {
00335         startProfile(__FUNCTION__);
00336 
00337         $oMainCat   = $this->_getMainCategory( $oArticle );
00338         $sMainCatId = $oMainCat ? $oMainCat->getId() : '';
00339 
00340         //load default article url from DB
00341         if ( !( $sSeoUri = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $sMainCatId, true ) ) ) {
00342             // save for main category
00343             if ( $oMainCat ) {
00344                 $sSeoUri = $this->_createArticleCategoryUri( $oArticle, $oMainCat, $iLang );
00345             } else {
00346                 // get default article url
00347                 $oArticle = $this->_getProductForLang( $oArticle, $iLang );
00348                 $sSeoUri = $this->_processSeoUrl( $this->_prepareArticleTitle( $oArticle ), $oArticle->getId(), $iLang );
00349 
00350                 // save default article url
00351                 $this->_saveToDb(
00352                         'oxarticle',
00353                         $oArticle->getId(),
00354                         $oArticle->getBaseStdLink( $iLang ),
00355                         $sSeoUri,
00356                         $iLang,
00357                         null,
00358                         0,
00359                         ''
00360                     );
00361             }
00362         }
00363 
00364         stopProfile(__FUNCTION__);
00365         return $sSeoUri;
00366     }
00367 
00376     protected function _prepareArticleTitle( $oArticle )
00377     {
00378         $sTitle = '';
00379 
00380         // create title part for uri
00381         if ( !( $sTitle = $oArticle->oxarticles__oxtitle->value ) ) {
00382             // taking parent article title
00383             if ( ( $sParentId = $oArticle->oxarticles__oxparentid->value ) ) {
00384 
00385                 // looking in cache ..
00386                 if ( !isset( self::$_aTitleCache[$sParentId] ) ) {
00387                     $oDb = oxDb::getDb();
00388                     $sQ = "select oxtitle from ".$oArticle->getViewName()." where oxid = ".$oDb->quote( $sParentId );
00389                     self::$_aTitleCache[$sParentId] = $oDb->getOne( $sQ );
00390                 }
00391                 $sTitle = self::$_aTitleCache[$sParentId];
00392             }
00393         }
00394 
00395         // variant has varselect value
00396         if ( $oArticle->oxarticles__oxvarselect->value ) {
00397             $sTitle .= ( $sTitle ? ' ' : '' ) . $oArticle->oxarticles__oxvarselect->value . ' ';
00398         } elseif ( !$sTitle || ( $oArticle->oxarticles__oxparentid->value ) ) {
00399             // in case nothing was found - looking for number
00400             $sTitle .= ( $sTitle ? ' ' : '' ) . $oArticle->oxarticles__oxartnum->value;
00401         }
00402 
00403         return $this->_prepareTitle( $sTitle, false, $oArticle->getLanguage() ) . '.html';
00404     }
00405 
00415     public function getArticleVendorUri( $oArticle, $iLang, $blRegenerate = false )
00416     {
00417         startProfile(__FUNCTION__);
00418 
00419         $sSeoUri = null;
00420         if ( $oVendor = $this->_getVendor( $oArticle, $iLang ) ) {
00421             //load details link from DB
00422             if ( $blRegenerate || !( $sSeoUri = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $oVendor->getId(), true ) ) ) {
00423 
00424                 $oArticle = $this->_getProductForLang( $oArticle, $iLang );
00425 
00426                 // create title part for uri
00427                 $sTitle = $this->_prepareArticleTitle( $oArticle );
00428 
00429                 // create uri for all categories
00430                 $sSeoUri = oxRegistry::get("oxSeoEncoderVendor")->getVendorUri( $oVendor, $iLang );
00431                 $sSeoUri = $this->_processSeoUrl( $sSeoUri . $sTitle, $oArticle->getId(), $iLang );
00432 
00433                 $aStdParams = array( 'cnid' => "v_".$oVendor->getId(), 'listtype' => $this->_getListType() );
00434                 $this->_saveToDb(
00435                         'oxarticle',
00436                         $oArticle->getId(),
00437                         oxRegistry::get("oxUtilsUrl")->appendUrl(
00438                                 $oArticle->getBaseStdLink( $iLang ),
00439                                 $aStdParams
00440                         ),
00441                         $sSeoUri,
00442                         $iLang,
00443                         null,
00444                         0,
00445                         $oVendor->getId()
00446                     );
00447             }
00448 
00449             stopProfile(__FUNCTION__);
00450         }
00451         return $sSeoUri;
00452     }
00453 
00462     protected function _getVendor( $oArticle, $iLang )
00463     {
00464         $oView = $this->getConfig()->getActiveView();
00465 
00466         $oVendor = null;
00467         if ( $sActVendorId = $oArticle->oxarticles__oxvendorid->value ) {
00468             if ( $oView instanceof oxView && ( $oActVendor = $oView->getActVendor() ) ) {
00469                 $oVendor = $oActVendor;
00470             } else {
00471                 $oVendor = oxNew( "oxVendor" );
00472             }
00473             if ( $oVendor->getId() !== $sActVendorId ) {
00474                 $oVendor = oxNew( "oxVendor" );
00475                 if ( !$oVendor->loadInLang( $iLang, $sActVendorId ) ) {
00476                     $oVendor = null;
00477                 }
00478             }
00479         }
00480 
00481         return $oVendor;
00482     }
00483 
00493     public function getArticleManufacturerUri( $oArticle, $iLang, $blRegenerate = false )
00494     {
00495         $sSeoUri = null;
00496         startProfile(__FUNCTION__);
00497         if ( $oManufacturer = $this->_getManufacturer( $oArticle, $iLang ) ) {
00498             //load details link from DB
00499             if ( $blRegenerate || !( $sSeoUri = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $oManufacturer->getId(), true ) ) ) {
00500 
00501                 $oArticle = $this->_getProductForLang( $oArticle, $iLang );
00502 
00503                 // create title part for uri
00504                 $sTitle = $this->_prepareArticleTitle( $oArticle );
00505 
00506                 // create uri for all categories
00507                 $sSeoUri = oxRegistry::get("oxSeoEncoderManufacturer")->getManufacturerUri( $oManufacturer, $iLang );
00508                 $sSeoUri = $this->_processSeoUrl( $sSeoUri . $sTitle, $oArticle->getId(), $iLang );
00509 
00510                 $aStdParams = array( 'mnid' => $oManufacturer->getId(), 'listtype' => $this->_getListType() );
00511                 $this->_saveToDb(
00512                         'oxarticle',
00513                         $oArticle->getId(),
00514                         oxRegistry::get("oxUtilsUrl")->appendUrl(
00515                                 $oArticle->getBaseStdLink( $iLang ),
00516                                 $aStdParams
00517                         ),
00518                         $sSeoUri,
00519                         $iLang,
00520                         null,
00521                         0,
00522                         $oManufacturer->getId()
00523                     );
00524             }
00525 
00526             stopProfile(__FUNCTION__);
00527         }
00528         return $sSeoUri;
00529     }
00530 
00539     protected function _getManufacturer( $oArticle, $iLang )
00540     {
00541         $oManufacturer = null;
00542         if ( $sActManufacturerId = $oArticle->oxarticles__oxmanufacturerid->value ) {
00543             $oView = $this->getConfig()->getActiveView();
00544 
00545             if ( $oView instanceof oxView && ( $oActManufacturer = $oView->getActManufacturer() ) ) {
00546                 $oManufacturer = $oActManufacturer;
00547             } else {
00548                 $oManufacturer = oxNew( "oxManufacturer" );
00549             }
00550 
00551             if ( $oManufacturer->getId() !== $sActManufacturerId || $oManufacturer->getLanguage() != $iLang ) {
00552                 $oManufacturer = oxNew( "oxManufacturer" );
00553                 if ( !$oManufacturer->loadInLang( $iLang, $sActManufacturerId ) ) {
00554                     $oManufacturer = null;
00555                 }
00556             }
00557         }
00558 
00559         return $oManufacturer;
00560     }
00561 
00570     public function getArticleMainUrl( $oArticle, $iLang = null )
00571     {
00572         if ( !isset( $iLang ) ) {
00573             $iLang = $oArticle->getLanguage();
00574         }
00575 
00576         return $this->_getFullUrl( $this->getArticleMainUri( $oArticle, $iLang ), $iLang );
00577     }
00578 
00588     public function getArticleUrl( $oArticle, $iLang = null, $iType = 0 )
00589     {
00590         if ( !isset( $iLang ) ) {
00591             $iLang = $oArticle->getLanguage();
00592         }
00593 
00594         $sUri = null;
00595         switch ( $iType ) {
00596             case OXARTICLE_LINKTYPE_VENDOR :
00597                 $sUri = $this->getArticleVendorUri( $oArticle, $iLang );
00598                 break;
00599             case OXARTICLE_LINKTYPE_MANUFACTURER :
00600                 $sUri = $this->getArticleManufacturerUri( $oArticle, $iLang );
00601                 break;
00602             case OXARTICLE_LINKTYPE_TAG :
00603                 $sUri = $this->getArticleTagUri( $oArticle, $iLang );
00604                 break;
00605             case OXARTICLE_LINKTYPE_RECOMM :
00606                 $sUri = $this->getArticleRecommUri( $oArticle, $iLang );
00607                 break;
00608             case OXARTICLE_LINKTYPE_PRICECATEGORY : // goes price category urls to default (category urls)
00609             default:
00610                 $sUri = $this->getArticleUri( $oArticle, $iLang );
00611                 break;
00612         }
00613 
00614         // if was unable to fetch type uri - returning main
00615         if ( !$sUri ) {
00616             $sUri = $this->getArticleMainUri( $oArticle, $iLang );
00617         }
00618 
00619         return $this->_getFullUrl( $sUri, $iLang );
00620     }
00621 
00629     public function onDeleteArticle( $oArticle )
00630     {
00631         $oDb = oxDb::getDb();
00632         $sIdQuoted = $oDb->quote( $oArticle->getId() );
00633         $oDb->execute("delete from oxseo where oxobjectid = $sIdQuoted and oxtype = 'oxarticle'");
00634         $oDb->execute("delete from oxobject2seodata where oxobjectid = $sIdQuoted");
00635     }
00636 
00645     protected function _getAltUri( $sObjectId, $iLang )
00646     {
00647         $sSeoUrl = null;
00648         $oArticle = oxNew( "oxArticle" );
00649         $oArticle->setSkipAssign( true );
00650         if ( $oArticle->loadInLang( $iLang, $sObjectId ) ) {
00651             // choosing URI type to generate
00652             switch( $this->_getListType() ) {
00653                 case 'vendor':
00654                     $sSeoUrl = $this->getArticleVendorUri( $oArticle, $iLang, true );
00655                     break;
00656                 case 'manufacturer':
00657                     $sSeoUrl = $this->getArticleManufacturerUri( $oArticle, $iLang, true );
00658                     break;
00659                 case 'tag':
00660                     $sSeoUrl = $this->getArticleTagUri( $oArticle, $iLang, true );
00661                     break;
00662                 default:
00663                     $sSeoUrl = $this->getArticleUri( $oArticle, $iLang, true );
00664                     break;
00665             }
00666         }
00667         return $sSeoUrl;
00668     }
00669 }