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         if ( $oActCat = $this->_getCategory( $oArticle, $iLang ) ) {
00239             $sActCatId = $oActCat->getId();
00240         } elseif ( $oActCat = $this->_getMainCategory( $oArticle ) ) {
00241             $sActCatId = $oActCat->getId();
00242         }
00243 
00244         //load details link from DB
00245         if ( $blRegenerate || !( $sSeoUri = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $sActCatId, true ) ) ) {
00246             if ( $oActCat ) {
00247                 $blInCat = false;
00248                 if ( $oActCat->isPriceCategory() ) {
00249                     $blInCat = $oArticle->inPriceCategory( $sActCatId );
00250                 } else {
00251                     $blInCat = $oArticle->inCategory( $sActCatId );
00252                 }
00253                 if ( $blInCat ) {
00254                     $sSeoUri = $this->_createArticleCategoryUri( $oArticle, $oActCat, $iLang );
00255                 }
00256             }
00257         }
00258 
00259         stopProfile(__FUNCTION__);
00260 
00261         return $sSeoUri;
00262     }
00263 
00272     protected function _getCategory( $oArticle, $iLang )
00273     {
00274         $oCat = null;
00275         $oView = $this->getConfig()->getActiveView();
00276         if ( $oView instanceof oxview ) {
00277             $oCat = $oView->getActCategory();
00278         }
00279         return $oCat;
00280     }
00281 
00289     protected function _getMainCategory( $oArticle )
00290     {
00291         $oMainCat = null;
00292 
00293         // if variant parent id must be used
00294         $sArtId = $oArticle->getId();
00295         if ( isset( $oArticle->oxarticles__oxparentid->value ) && $oArticle->oxarticles__oxparentid->value ) {
00296             $sArtId = $oArticle->oxarticles__oxparentid->value;
00297         }
00298 
00299         $oDb = oxDb::getDb();
00300         // add manin category chaching;
00301         $sQ = "select oxcatnid from ".getViewName( "oxobject2category" )." where oxobjectid = ".$oDb->quote( $sArtId )." order by oxtime";
00302         $sIdent = md5( $sQ );
00303 
00304         if ( ( $sMainCatId = $this->_loadFromCache( $sIdent, "oxarticle" ) ) === false ) {
00305             $sMainCatId = $oDb->getOne( $sQ );
00306             // storing in cache
00307             $this->_saveInCache( $sIdent, $sMainCatId, "oxarticle" );
00308         }
00309 
00310         if ( $sMainCatId ) {
00311             $oMainCat = oxNew( "oxcategory" );
00312             if ( ! $oMainCat->load( $sMainCatId )) {
00313                 $oMainCat = null;
00314             }
00315         }
00316 
00317         return $oMainCat;
00318     }
00319 
00328     public function getArticleMainUri( $oArticle, $iLang )
00329     {
00330         startProfile(__FUNCTION__);
00331 
00332         $oMainCat   = $this->_getMainCategory( $oArticle );
00333         $sMainCatId = $oMainCat ? $oMainCat->getId() : '';
00334 
00335         //load default article url from DB
00336         if ( !( $sSeoUri = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $sMainCatId, true ) ) ) {
00337             // save for main category
00338             if ( $oMainCat ) {
00339                 $sSeoUri = $this->_createArticleCategoryUri( $oArticle, $oMainCat, $iLang );
00340             } else {
00341                 // get default article url
00342                 $oArticle = $this->_getProductForLang( $oArticle, $iLang );
00343                 $sSeoUri = $this->_processSeoUrl( $this->_prepareArticleTitle( $oArticle ), $oArticle->getId(), $iLang );
00344 
00345                 // save default article url
00346                 $this->_saveToDb(
00347                         'oxarticle',
00348                         $oArticle->getId(),
00349                         $oArticle->getBaseStdLink( $iLang ),
00350                         $sSeoUri,
00351                         $iLang,
00352                         null,
00353                         0,
00354                         ''
00355                     );
00356             }
00357         }
00358 
00359         stopProfile(__FUNCTION__);
00360         return $sSeoUri;
00361     }
00362 
00371     protected function _prepareArticleTitle( $oArticle )
00372     {
00373         $sTitle = '';
00374 
00375         // create title part for uri
00376         if ( !( $sTitle = $oArticle->oxarticles__oxtitle->value ) ) {
00377             // taking parent article title
00378             if ( ( $sParentId = $oArticle->oxarticles__oxparentid->value ) ) {
00379 
00380                 // looking in cache ..
00381                 if ( !isset( self::$_aTitleCache[$sParentId] ) ) {
00382                     $oDb = oxDb::getDb();
00383                     $sQ = "select oxtitle from ".$oArticle->getViewName()." where oxid = ".$oDb->quote( $sParentId );
00384                     self::$_aTitleCache[$sParentId] = $oDb->getOne( $sQ );
00385                 }
00386                 $sTitle = self::$_aTitleCache[$sParentId];
00387             }
00388         }
00389 
00390         // variant has varselect value
00391         if ( $oArticle->oxarticles__oxvarselect->value ) {
00392             $sTitle .= ( $sTitle ? ' ' : '' ) . $oArticle->oxarticles__oxvarselect->value . ' ';
00393         } elseif ( !$sTitle || ( $oArticle->oxarticles__oxparentid->value ) ) {
00394             // in case nothing was found - looking for number
00395             $sTitle .= ( $sTitle ? ' ' : '' ) . $oArticle->oxarticles__oxartnum->value;
00396         }
00397 
00398         return $this->_prepareTitle( $sTitle, false, $oArticle->getLanguage() ) . '.html';
00399     }
00400 
00410     public function getArticleVendorUri( $oArticle, $iLang, $blRegenerate = false )
00411     {
00412         startProfile(__FUNCTION__);
00413 
00414         $sSeoUri = null;
00415         if ( $oVendor = $this->_getVendor( $oArticle, $iLang ) ) {
00416             //load details link from DB
00417             if ( $blRegenerate || !( $sSeoUri = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $oVendor->getId(), true ) ) ) {
00418 
00419                 $oArticle = $this->_getProductForLang( $oArticle, $iLang );
00420 
00421                 // create title part for uri
00422                 $sTitle = $this->_prepareArticleTitle( $oArticle );
00423 
00424                 // create uri for all categories
00425                 $sSeoUri = oxRegistry::get("oxSeoEncoderVendor")->getVendorUri( $oVendor, $iLang );
00426                 $sSeoUri = $this->_processSeoUrl( $sSeoUri . $sTitle, $oArticle->getId(), $iLang );
00427 
00428                 $aStdParams = array( 'cnid' => "v_".$oVendor->getId(), 'listtype' => $this->_getListType() );
00429                 $this->_saveToDb(
00430                         'oxarticle',
00431                         $oArticle->getId(),
00432                         oxRegistry::get("oxUtilsUrl")->appendUrl(
00433                                 $oArticle->getBaseStdLink( $iLang ),
00434                                 $aStdParams
00435                         ),
00436                         $sSeoUri,
00437                         $iLang,
00438                         null,
00439                         0,
00440                         $oVendor->getId()
00441                     );
00442             }
00443 
00444             stopProfile(__FUNCTION__);
00445         }
00446         return $sSeoUri;
00447     }
00448 
00457     protected function _getVendor( $oArticle, $iLang )
00458     {
00459         $oView = $this->getConfig()->getActiveView();
00460 
00461         $oVendor = null;
00462         if ( $sActVendorId = $oArticle->oxarticles__oxvendorid->value ) {
00463             if ( $oView instanceof oxView && ( $oActVendor = $oView->getActVendor() ) ) {
00464                 $oVendor = $oActVendor;
00465             } else {
00466                 $oVendor = oxNew( "oxVendor" );
00467             }
00468             if ( $oVendor->getId() !== $sActVendorId ) {
00469                 $oVendor = oxNew( "oxVendor" );
00470                 if ( !$oVendor->loadInLang( $iLang, $sActVendorId ) ) {
00471                     $oVendor = null;
00472                 }
00473             }
00474         }
00475 
00476         return $oVendor;
00477     }
00478 
00488     public function getArticleManufacturerUri( $oArticle, $iLang, $blRegenerate = false )
00489     {
00490         $sSeoUri = null;
00491         startProfile(__FUNCTION__);
00492         if ( $oManufacturer = $this->_getManufacturer( $oArticle, $iLang ) ) {
00493             //load details link from DB
00494             if ( $blRegenerate || !( $sSeoUri = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $oManufacturer->getId(), true ) ) ) {
00495 
00496                 $oArticle = $this->_getProductForLang( $oArticle, $iLang );
00497 
00498                 // create title part for uri
00499                 $sTitle = $this->_prepareArticleTitle( $oArticle );
00500 
00501                 // create uri for all categories
00502                 $sSeoUri = oxRegistry::get("oxSeoEncoderManufacturer")->getManufacturerUri( $oManufacturer, $iLang );
00503                 $sSeoUri = $this->_processSeoUrl( $sSeoUri . $sTitle, $oArticle->getId(), $iLang );
00504 
00505                 $aStdParams = array( 'mnid' => $oManufacturer->getId(), 'listtype' => $this->_getListType() );
00506                 $this->_saveToDb(
00507                         'oxarticle',
00508                         $oArticle->getId(),
00509                         oxRegistry::get("oxUtilsUrl")->appendUrl(
00510                                 $oArticle->getBaseStdLink( $iLang ),
00511                                 $aStdParams
00512                         ),
00513                         $sSeoUri,
00514                         $iLang,
00515                         null,
00516                         0,
00517                         $oManufacturer->getId()
00518                     );
00519             }
00520 
00521             stopProfile(__FUNCTION__);
00522         }
00523         return $sSeoUri;
00524     }
00525 
00534     protected function _getManufacturer( $oArticle, $iLang )
00535     {
00536         $oManufacturer = null;
00537         if ( $sActManufacturerId = $oArticle->oxarticles__oxmanufacturerid->value ) {
00538             $oView = $this->getConfig()->getActiveView();
00539 
00540             if ( $oView instanceof oxView && ( $oActManufacturer = $oView->getActManufacturer() ) ) {
00541                 $oManufacturer = $oActManufacturer;
00542             } else {
00543                 $oManufacturer = oxNew( "oxManufacturer" );
00544             }
00545 
00546             if ( $oManufacturer->getId() !== $sActManufacturerId || $oManufacturer->getLanguage() != $iLang ) {
00547                 $oManufacturer = oxNew( "oxManufacturer" );
00548                 if ( !$oManufacturer->loadInLang( $iLang, $sActManufacturerId ) ) {
00549                     $oManufacturer = null;
00550                 }
00551             }
00552         }
00553 
00554         return $oManufacturer;
00555     }
00556 
00565     public function getArticleMainUrl( $oArticle, $iLang = null )
00566     {
00567         if ( !isset( $iLang ) ) {
00568             $iLang = $oArticle->getLanguage();
00569         }
00570 
00571         return $this->_getFullUrl( $this->getArticleMainUri( $oArticle, $iLang ), $iLang );
00572     }
00573 
00583     public function getArticleUrl( $oArticle, $iLang = null, $iType = 0 )
00584     {
00585         if ( !isset( $iLang ) ) {
00586             $iLang = $oArticle->getLanguage();
00587         }
00588 
00589         $sUri = null;
00590         switch ( $iType ) {
00591             case OXARTICLE_LINKTYPE_VENDOR :
00592                 $sUri = $this->getArticleVendorUri( $oArticle, $iLang );
00593                 break;
00594             case OXARTICLE_LINKTYPE_MANUFACTURER :
00595                 $sUri = $this->getArticleManufacturerUri( $oArticle, $iLang );
00596                 break;
00597             case OXARTICLE_LINKTYPE_TAG :
00598                 $sUri = $this->getArticleTagUri( $oArticle, $iLang );
00599                 break;
00600             case OXARTICLE_LINKTYPE_RECOMM :
00601                 $sUri = $this->getArticleRecommUri( $oArticle, $iLang );
00602                 break;
00603             case OXARTICLE_LINKTYPE_PRICECATEGORY : // goes price category urls to default (category urls)
00604             default:
00605                 $sUri = $this->getArticleUri( $oArticle, $iLang );
00606                 break;
00607         }
00608 
00609         // if was unable to fetch type uri - returning main
00610         if ( !$sUri ) {
00611             $sUri = $this->getArticleMainUri( $oArticle, $iLang );
00612         }
00613 
00614         return $this->_getFullUrl( $sUri, $iLang );
00615     }
00616 
00624     public function onDeleteArticle( $oArticle )
00625     {
00626         $oDb = oxDb::getDb();
00627         $sIdQuoted = $oDb->quote( $oArticle->getId() );
00628         $oDb->execute("delete from oxseo where oxobjectid = $sIdQuoted and oxtype = 'oxarticle'");
00629         $oDb->execute("delete from oxobject2seodata where oxobjectid = $sIdQuoted");
00630     }
00631 
00640     protected function _getAltUri( $sObjectId, $iLang )
00641     {
00642         $sSeoUrl = null;
00643         $oArticle = oxNew( "oxarticle" );
00644         $oArticle->setSkipAssign( true );
00645         if ( $oArticle->loadInLang( $iLang, $sObjectId ) ) {
00646             // choosing URI type to generate
00647             switch( $this->_getListType() ) {
00648                 case 'vendor':
00649                     $sSeoUrl = $this->getArticleVendorUri( $oArticle, $iLang, true );
00650                     break;
00651                 case 'manufacturer':
00652                     $sSeoUrl = $this->getArticleManufacturerUri( $oArticle, $iLang, true );
00653                     break;
00654                 case 'tag':
00655                     $sSeoUrl = $this->getArticleTagUri( $oArticle, $iLang, true );
00656                     break;
00657                 default:
00658                     $sSeoUrl = $this->getArticleUri( $oArticle, $iLang, true );
00659                     break;
00660             }
00661         }
00662         return $sSeoUrl;
00663     }
00664 }