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 
00087     protected function _getArticleRecommUri( $oArticle, $iLang )
00088     {
00089         $sSeoUri = null;
00090         if ( $oRecomm = $this->_getRecomm( $oArticle, $iLang ) ) {
00091             //load details link from DB
00092             if ( !( $sSeoUri = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $oRecomm->getId(), true ) ) ) {
00093 
00094                 $oArticle = $this->_getProductForLang( $oArticle, $iLang );
00095 
00096                 // create title part for uri
00097                 $sTitle = $this->_prepareArticleTitle( $oArticle );
00098 
00099                 // create uri for all categories
00100                 $sSeoUri = oxSeoEncoderRecomm::getInstance()->getRecommUri( $oRecomm, $iLang );
00101                 $sSeoUri = $this->_processSeoUrl( $sSeoUri . $sTitle, $oArticle->getId(), $iLang );
00102 
00103                 $aStdParams = array( 'recommid' => $oRecomm->getId(), 'listtype' => $this->_getListType() );
00104                 $this->_saveToDb( 
00105                             'oxarticle',
00106                             $oArticle->getId(),
00107                             oxUtilsUrl::getInstance()->appendUrl(
00108                                     $oArticle->getBaseStdLink( $iLang ),
00109                                     $aStdParams
00110                             ),
00111                             $sSeoUri,
00112                             $iLang,
00113                             null,
00114                             0,
00115                             $oRecomm->getId()
00116                         );
00117             }
00118         }
00119         return $sSeoUri;
00120     }
00121 
00130     protected function _getRecomm( $oArticle, $iLang )
00131     {
00132         $oList = null;
00133         $oView = $this->getConfig()->getActiveView();
00134         if ( $oView instanceof oxView ) {
00135             $oList = $oView->getActiveRecommList();
00136         }
00137         return $oList;
00138     }
00139 
00145     protected function _getListType()
00146     {
00147         return $this->getConfig()->getActiveView()->getListType();
00148     }
00149 
00159     protected function _getArticleTagUri( $oArticle, $iLang, $blRegenerate = false )
00160     {
00161         $sSeoUri = null;
00162         if ( $sTag = $this->_getTag( $oArticle, $iLang ) ) {
00163             $iShopId = $this->getConfig()->getShopId();
00164             $sStdUrl = $oArticle->getStdTagLink( $sTag );
00165             if ( $blRegenerate || !( $sSeoUri = $this->_loadFromDb( 'dynamic', $this->getDynamicObjectId( $iShopId, $sStdUrl ), $iLang ) ) ) {
00166                 // generating new if not found
00167                 $sSeoUri  = oxSeoEncoderTag::getInstance()->getTagUri( $sTag, $iLang );
00168                 $sSeoUri .= $this->_prepareArticleTitle( $oArticle );
00169                 $sSeoUri  = $this->_processSeoUrl( $sSeoUri, $this->_getStaticObjectId( $iShopId, $sStdUrl ), $iLang );
00170                 $sSeoUri  = $this->_getDynamicUri( $sStdUrl, $sSeoUri, $iLang );
00171             }
00172         }
00173         return $sSeoUri;
00174     }
00175 
00184     protected function _getTag( $oArticle, $iLang )
00185     {
00186         $sTag = null;
00187         $oView = $this->getConfig()->getActiveView();
00188         if ( $oView instanceof oxView ) {
00189             $sTag = $oView->getTag();
00190         }
00191         return $sTag;
00192     }
00193 
00203     protected function _createArticleCategoryUri( $oArticle, $oCategory, $iLang )
00204     {
00205         startProfile(__FUNCTION__);
00206         $oArticle = $this->_getProductForLang( $oArticle, $iLang );
00207 
00208         // create title part for uri
00209         $sTitle = $this->_prepareArticleTitle( $oArticle );
00210 
00211         // writing category path
00212         $sSeoUri = $this->_processSeoUrl(
00213                             oxSeoEncoderCategory::getInstance()->getCategoryUri( $oCategory, $iLang ).$sTitle,
00214                             $oArticle->getId(), $iLang
00215                         );
00216         $sCatId = $oCategory->getId();
00217         $this->_saveToDb(
00218                     'oxarticle',
00219                     $oArticle->getId(),
00220                     oxUtilsUrl::getInstance()->appendUrl(
00221                             $oArticle->getBaseStdLink( $iLang ),
00222                             array( 'cnid' => $sCatId )
00223                     ),
00224                     $sSeoUri,
00225                     $iLang,
00226                     null,
00227                     0,
00228                     $sCatId
00229                 );
00230 
00231         stopProfile(__FUNCTION__);
00232 
00233         return $sSeoUri;
00234     }
00235 
00245     protected function _getArticleUri( $oArticle, $iLang, $blRegenerate = false )
00246     {
00247         startProfile(__FUNCTION__);
00248 
00249         $sActCatId = '';
00250         if ( $oActCat = $this->_getCategory( $oArticle, $iLang ) ) {
00251             $sActCatId = $oActCat->getId();
00252         }
00253 
00254         //load details link from DB
00255         if ( $blRegenerate || !( $sSeoUri = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $sActCatId, true ) ) ) {
00256             if ( $oActCat ) {
00257                 $blInCat = false;
00258                 if ( $oActCat->isPriceCategory() ) {
00259                     $blInCat = $oArticle->inPriceCategory( $sActCatId );
00260                 } else {
00261                     $blInCat = $oArticle->inCategory( $sActCatId );
00262                 }
00263                 if ( $blInCat ) {
00264                     $sSeoUri = $this->_createArticleCategoryUri( $oArticle, $oActCat, $iLang );
00265                 }
00266             }
00267         }
00268 
00269         stopProfile(__FUNCTION__);
00270 
00271         return $sSeoUri;
00272     }
00273 
00282     protected function _getCategory( $oArticle, $iLang )
00283     {
00284         $oCat = null;
00285         $oView = $this->getConfig()->getActiveView();
00286         if ( $oView instanceof oxview ) {
00287             $oCat = $oView->getActCategory();
00288         }
00289         return $oCat;
00290     }
00291 
00300     protected function _getArticleMainUri( $oArticle, $iLang )
00301     {
00302         startProfile(__FUNCTION__);
00303 
00304         // if variant parent id must be used
00305         $sArtId = $oArticle->getId();
00306         if ( isset( $oArticle->oxarticles__oxparentid->value ) && $oArticle->oxarticles__oxparentid->value ) {
00307             $sArtId = $oArticle->oxarticles__oxparentid->value;
00308         }
00309 
00310         if ( !( $sMainCatId = oxDb::getDb()->getOne( "select oxcatnid from ".getViewName( "oxobject2category" )." where oxobjectid = '{$sArtId}' order by oxtime" ) ) ) {
00311             $sMainCatId = '';
00312         }
00313 
00314         //load default article url from DB
00315         if ( !( $sSeoUri = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $sMainCatId, true ) ) ) {
00316             if ( $sMainCatId ) {
00317                 $oMainCat = oxNew( "oxcategory" );
00318                 $oMainCat->load( $sMainCatId );
00319                 // save for main category
00320                 $sSeoUri = $this->_createArticleCategoryUri( $oArticle, $oMainCat, $iLang );
00321             } else {
00322                 // get default article url
00323                 $oArticle = $this->_getProductForLang( $oArticle, $iLang );
00324                 $sSeoUri = $this->_processSeoUrl( $this->_prepareArticleTitle( $oArticle ), $oArticle->getId(), $iLang );
00325 
00326                 // save default article url
00327                 $this->_saveToDb(
00328                         'oxarticle',
00329                         $oArticle->getId(),
00330                         $oArticle->getBaseStdLink( $iLang ),
00331                         $sSeoUri,
00332                         $iLang,
00333                         null,
00334                         0,
00335                         ''
00336                     );
00337             }
00338         }
00339 
00340         stopProfile(__FUNCTION__);
00341         return $sSeoUri;
00342     }
00343 
00352     protected function _prepareArticleTitle( $oArticle )
00353     {
00354         $sTitle = '';
00355 
00356         // create title part for uri
00357         if ( !( $sTitle = $oArticle->oxarticles__oxtitle->value ) ) {
00358             // taking parent article title
00359             if ( ( $sParentId = $oArticle->oxarticles__oxparentid->value ) ) {
00360 
00361                 // looking in cache ..
00362                 if ( !isset( self::$_aTitleCache[$sParentId] ) ) {
00363                     $oDb = oxDb::getDb();
00364                     $sQ = "select oxtitle from oxarticles where oxid = ".$oDb->quote( $sParentId );
00365                     self::$_aTitleCache[$sParentId] = $oDb->getOne( $sQ );
00366                 }
00367                 $sTitle = self::$_aTitleCache[$sParentId];
00368             }
00369         }
00370 
00371         // variant has varselect value
00372         if ( $oArticle->oxarticles__oxvarselect->value ) {
00373             $sTitle .= ( $sTitle ? ' ' : '' ).$oArticle->oxarticles__oxvarselect->value . ' ';
00374         }
00375 
00376         // in case nothing was found - looking for number
00377         if ( !$sTitle ) {
00378             $sTitle .= $oArticle->oxarticles__oxartnum->value;
00379         }
00380 
00381         return $this->_prepareTitle( $sTitle ) . '.html';
00382     }
00383 
00393     protected function _getArticleVendorUri( $oArticle, $iLang, $blRegenerate = false )
00394     {
00395         startProfile(__FUNCTION__);
00396 
00397         $sSeoUri = null;
00398         if ( $oVendor = $this->_getVendor( $oArticle, $iLang ) ) {
00399             //load details link from DB
00400             if ( $blRegenerate || !( $sSeoUri = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $oVendor->getId(), true ) ) ) {
00401 
00402                 $oArticle = $this->_getProductForLang( $oArticle, $iLang );
00403 
00404                 // create title part for uri
00405                 $sTitle = $this->_prepareArticleTitle( $oArticle );
00406 
00407                 // create uri for all categories
00408                 $sSeoUri = oxSeoEncoderVendor::getInstance()->getVendorUri( $oVendor, $iLang );
00409                 $sSeoUri = $this->_processSeoUrl( $sSeoUri . $sTitle, $oArticle->getId(), $iLang );
00410 
00411                 $aStdParams = array( 'cnid' => "v_".$oVendor->getId(), 'listtype' => $this->_getListType() );
00412                 $this->_saveToDb(
00413                         'oxarticle',
00414                         $oArticle->getId(),
00415                         oxUtilsUrl::getInstance()->appendUrl(
00416                                 $oArticle->getBaseStdLink( $iLang ),
00417                                 $aStdParams
00418                         ),
00419                         $sSeoUri,
00420                         $iLang,
00421                         null,
00422                         0,
00423                         $oVendor->getId()
00424                     );
00425             }
00426 
00427             stopProfile(__FUNCTION__);
00428         }
00429         return $sSeoUri;
00430     }
00431 
00440     protected function _getVendor( $oArticle, $iLang )
00441     {
00442         $oView = $this->getConfig()->getActiveView();
00443 
00444         $oVendor = null;
00445         if ( $sActVendorId = $oArticle->oxarticles__oxvendorid->value ) {
00446             if ( $oView instanceof oxView && ( $oActVendor = $oView->getActVendor() ) ) {
00447                 $oVendor = $oActVendor;
00448             } else {
00449                 $oVendor = oxNew( "oxVendor" );
00450             }
00451             if ( $oVendor->getId() !== $sActVendorId ) {
00452                 if ( !$oVendor->loadInLang( $iLang, $sActVendorId ) ) {
00453                     $oVendor = null;
00454                 }
00455             }
00456         }
00457 
00458         return $oVendor;
00459     }
00460 
00470     protected function _getArticleManufacturerUri( $oArticle, $iLang, $blRegenerate = false )
00471     {
00472         $sSeoUri = null;
00473         startProfile(__FUNCTION__);
00474         if ( $oManufacturer = $this->_getManufacturer( $oArticle, $iLang ) ) {
00475             //load details link from DB
00476             if ( $blRegenerate || !( $sSeoUri = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $oManufacturer->getId(), true ) ) ) {
00477 
00478                 $oArticle = $this->_getProductForLang( $oArticle, $iLang );
00479 
00480                 // create title part for uri
00481                 $sTitle = $this->_prepareArticleTitle( $oArticle );
00482 
00483                 // create uri for all categories
00484                 $sSeoUri = oxSeoEncoderManufacturer::getInstance()->getManufacturerUri( $oManufacturer, $iLang );
00485                 $sSeoUri = $this->_processSeoUrl( $sSeoUri . $sTitle, $oArticle->getId(), $iLang );
00486 
00487                 $aStdParams = array( 'mnid' => $oManufacturer->getId(), 'listtype' => $this->_getListType() );
00488                 $this->_saveToDb(
00489                         'oxarticle',
00490                         $oArticle->getId(),
00491                         oxUtilsUrl::getInstance()->appendUrl(
00492                                 $oArticle->getBaseStdLink( $iLang ),
00493                                 $aStdParams
00494                         ),
00495                         $sSeoUri,
00496                         $iLang,
00497                         null,
00498                         0,
00499                         $oManufacturer->getId()
00500                     );
00501             }
00502 
00503             stopProfile(__FUNCTION__);
00504         }
00505         return $sSeoUri;
00506     }
00507 
00516     protected function _getManufacturer( $oArticle, $iLang )
00517     {
00518         $oManufacturer = null;
00519         if ( $sActManufacturerId = $oArticle->oxarticles__oxmanufacturerid->value ) {
00520             $oView = $this->getConfig()->getActiveView();
00521             if ( $oView instanceof oxView && ( $oActManufacturer = $oView->getActManufacturer() ) ) {
00522                 $oManufacturer = $oActManufacturer;
00523             } else {
00524                 $oManufacturer = oxNew( "oxManufacturer" );
00525             }
00526             if ( $oManufacturer->getId() !== $sActManufacturerId || $oManufacturer->getLanguage() != $iLang ) {
00527                 if ( !$oManufacturer->loadInLang( $iLang, $sActManufacturerId ) ) {
00528                     $oManufacturer = null;
00529                 }
00530             }
00531         }
00532 
00533         return $oManufacturer;
00534     }
00535 
00544     public function getArticleMainUrl( $oArticle, $iLang = null )
00545     {
00546         if ( !isset( $iLang ) ) {
00547             $iLang = $oArticle->getLanguage();
00548         }
00549 
00550         return $this->_getFullUrl( $this->_getArticleMainUri( $oArticle, $iLang ), $iLang );
00551     }
00552 
00562     public function getArticleUrl( $oArticle, $iLang = null, $iType = 0 )
00563     {
00564         if ( !isset( $iLang ) ) {
00565             $iLang = $oArticle->getLanguage();
00566         }
00567 
00568         $sUri = null;
00569         switch ( $iType ) {
00570             case OXARTICLE_LINKTYPE_VENDOR :
00571                 $sUri = $this->_getArticleVendorUri( $oArticle, $iLang );
00572                 break;
00573             case OXARTICLE_LINKTYPE_MANUFACTURER :
00574                 $sUri = $this->_getArticleManufacturerUri( $oArticle, $iLang );
00575                 break;
00576             case OXARTICLE_LINKTYPE_TAG :
00577                 $sUri = $this->_getArticleTagUri( $oArticle, $iLang );
00578                 break;
00579             case OXARTICLE_LINKTYPE_RECOMM :
00580                 $sUri = $this->_getArticleRecommUri( $oArticle, $iLang );
00581                 break;
00582             case OXARTICLE_LINKTYPE_PRICECATEGORY : // goes price category urls to default (category urls)
00583             default:
00584                 $sUri = $this->_getArticleUri( $oArticle, $iLang );
00585                 break;
00586         }
00587 
00588         // if was unable to fetch type uri - returning main
00589         if ( !$sUri ) {
00590             $sUri = $this->_getArticleMainUri( $oArticle, $iLang );
00591         }
00592 
00593         return $this->_getFullUrl( $sUri, $iLang );
00594     }
00595 
00603     public function onDeleteArticle( $oArticle )
00604     {
00605         $oDb = oxDb::getDb();
00606         $sIdQuoted = $oDb->quote( $oArticle->getId() );
00607         $oDb->execute("delete from oxseo where oxobjectid = $sIdQuoted and oxtype = 'oxarticle'");
00608         $oDb->execute("delete from oxobject2seodata where oxobjectid = $sIdQuoted");
00609     }
00610 
00619     protected function _getAltUri( $sObjectId, $iLang )
00620     {
00621         $sSeoUrl = null;
00622         $oArticle = oxNew( "oxarticle" );
00623         if ( $oArticle->loadInLang( $iLang, $sObjectId ) ) {
00624             // choosing URI type to generate
00625             switch( $this->_getListType() ) {
00626                 case 'vendor':
00627                     $sSeoUrl = $this->_getArticleVendorUri( $oArticle, $iLang, true );
00628                     break;
00629                 case 'manufacturer':
00630                     $sSeoUrl = $this->_getArticleManufacturerUri( $oArticle, $iLang, true );
00631                     break;
00632                 case 'tag':
00633                     $sSeoUrl = $this->_getArticleTagUri( $oArticle, $iLang, true );
00634                     break;
00635                 default:
00636                     $sSeoUrl = $this->_getArticleUri( $oArticle, $iLang, true );
00637                     break;
00638             }
00639         }
00640         return $sSeoUrl;
00641     }
00642 }