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                 if ( $sSeoUri = oxSeoEncoderTag::getInstance()->getTagUri( $sTag, $iLang, $oArticle->getId() ) ) {
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         }
00174         return $sSeoUri;
00175     }
00176 
00185     protected function _getTag( $oArticle, $iLang )
00186     {
00187         $sTag = null;
00188         $oView = $this->getConfig()->getActiveView();
00189         if ( $oView instanceof oxView ) {
00190             $sTag = $oView->getTag();
00191         }
00192         return $sTag;
00193     }
00194 
00204     protected function _createArticleCategoryUri( $oArticle, $oCategory, $iLang )
00205     {
00206         startProfile(__FUNCTION__);
00207         $oArticle = $this->_getProductForLang( $oArticle, $iLang );
00208 
00209         // create title part for uri
00210         $sTitle = $this->_prepareArticleTitle( $oArticle );
00211 
00212         // writing category path
00213         $sSeoUri = $this->_processSeoUrl(
00214                             oxSeoEncoderCategory::getInstance()->getCategoryUri( $oCategory, $iLang ).$sTitle,
00215                             $oArticle->getId(), $iLang
00216                         );
00217         $sCatId = $oCategory->getId();
00218         $this->_saveToDb(
00219                     'oxarticle',
00220                     $oArticle->getId(),
00221                     oxUtilsUrl::getInstance()->appendUrl(
00222                             $oArticle->getBaseStdLink( $iLang ),
00223                             array( 'cnid' => $sCatId )
00224                     ),
00225                     $sSeoUri,
00226                     $iLang,
00227                     null,
00228                     0,
00229                     $sCatId
00230                 );
00231 
00232         stopProfile(__FUNCTION__);
00233 
00234         return $sSeoUri;
00235     }
00236 
00246     protected function _getArticleUri( $oArticle, $iLang, $blRegenerate = false )
00247     {
00248         startProfile(__FUNCTION__);
00249 
00250         $sActCatId = '';
00251         if ( $oActCat = $this->_getCategory( $oArticle, $iLang ) ) {
00252             $sActCatId = $oActCat->getId();
00253         }
00254 
00255         //load details link from DB
00256         if ( $blRegenerate || !( $sSeoUri = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $sActCatId, true ) ) ) {
00257             if ( $oActCat ) {
00258                 $blInCat = false;
00259                 if ( $oActCat->isPriceCategory() ) {
00260                     $blInCat = $oArticle->inPriceCategory( $sActCatId );
00261                 } else {
00262                     $blInCat = $oArticle->inCategory( $sActCatId );
00263                 }
00264                 if ( $blInCat ) {
00265                     $sSeoUri = $this->_createArticleCategoryUri( $oArticle, $oActCat, $iLang );
00266                 }
00267             }
00268         }
00269 
00270         stopProfile(__FUNCTION__);
00271 
00272         return $sSeoUri;
00273     }
00274 
00283     protected function _getCategory( $oArticle, $iLang )
00284     {
00285         $oCat = null;
00286         $oView = $this->getConfig()->getActiveView();
00287         if ( $oView instanceof oxview ) {
00288             $oCat = $oView->getActCategory();
00289         }
00290         return $oCat;
00291     }
00292 
00301     protected function _getArticleMainUri( $oArticle, $iLang )
00302     {
00303         startProfile(__FUNCTION__);
00304 
00305         // if variant parent id must be used
00306         $sArtId = $oArticle->getId();
00307         if ( isset( $oArticle->oxarticles__oxparentid->value ) && $oArticle->oxarticles__oxparentid->value ) {
00308             $sArtId = $oArticle->oxarticles__oxparentid->value;
00309         }
00310 
00311         if ( !( $sMainCatId = oxDb::getDb()->getOne( "select oxcatnid from ".getViewName( "oxobject2category" )." where oxobjectid = '{$sArtId}' order by oxtime" ) ) ) {
00312             $sMainCatId = '';
00313         }
00314 
00315         //load default article url from DB
00316         if ( !( $sSeoUri = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $sMainCatId, true ) ) ) {
00317             if ( $sMainCatId ) {
00318                 $oMainCat = oxNew( "oxcategory" );
00319                 $oMainCat->load( $sMainCatId );
00320                 // save for main category
00321                 $sSeoUri = $this->_createArticleCategoryUri( $oArticle, $oMainCat, $iLang );
00322             } else {
00323                 // get default article url
00324                 $oArticle = $this->_getProductForLang( $oArticle, $iLang );
00325                 $sSeoUri = $this->_processSeoUrl( $this->_prepareArticleTitle( $oArticle ), $oArticle->getId(), $iLang );
00326 
00327                 // save default article url
00328                 $this->_saveToDb(
00329                         'oxarticle',
00330                         $oArticle->getId(),
00331                         $oArticle->getBaseStdLink( $iLang ),
00332                         $sSeoUri,
00333                         $iLang,
00334                         null,
00335                         0,
00336                         ''
00337                     );
00338             }
00339         }
00340 
00341         stopProfile(__FUNCTION__);
00342         return $sSeoUri;
00343     }
00344 
00353     protected function _prepareArticleTitle( $oArticle )
00354     {
00355         $sTitle = '';
00356 
00357         // create title part for uri
00358         if ( !( $sTitle = $oArticle->oxarticles__oxtitle->value ) ) {
00359             // taking parent article title
00360             if ( ( $sParentId = $oArticle->oxarticles__oxparentid->value ) ) {
00361 
00362                 // looking in cache ..
00363                 if ( !isset( self::$_aTitleCache[$sParentId] ) ) {
00364                     $oDb = oxDb::getDb();
00365                     $sQ = "select oxtitle from oxarticles where oxid = ".$oDb->quote( $sParentId );
00366                     self::$_aTitleCache[$sParentId] = $oDb->getOne( $sQ );
00367                 }
00368                 $sTitle = self::$_aTitleCache[$sParentId];
00369             }
00370         }
00371 
00372         // variant has varselect value
00373         if ( $oArticle->oxarticles__oxvarselect->value ) {
00374             $sTitle .= ( $sTitle ? ' ' : '' ).$oArticle->oxarticles__oxvarselect->value . ' ';
00375         }
00376 
00377         // in case nothing was found - looking for number
00378         if ( !$sTitle ) {
00379             $sTitle .= $oArticle->oxarticles__oxartnum->value;
00380         }
00381 
00382         return $this->_prepareTitle( $sTitle ) . '.html';
00383     }
00384 
00394     protected function _getArticleVendorUri( $oArticle, $iLang, $blRegenerate = false )
00395     {
00396         startProfile(__FUNCTION__);
00397 
00398         $sSeoUri = null;
00399         if ( $oVendor = $this->_getVendor( $oArticle, $iLang ) ) {
00400             //load details link from DB
00401             if ( $blRegenerate || !( $sSeoUri = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $oVendor->getId(), true ) ) ) {
00402 
00403                 $oArticle = $this->_getProductForLang( $oArticle, $iLang );
00404 
00405                 // create title part for uri
00406                 $sTitle = $this->_prepareArticleTitle( $oArticle );
00407 
00408                 // create uri for all categories
00409                 $sSeoUri = oxSeoEncoderVendor::getInstance()->getVendorUri( $oVendor, $iLang );
00410                 $sSeoUri = $this->_processSeoUrl( $sSeoUri . $sTitle, $oArticle->getId(), $iLang );
00411 
00412                 $aStdParams = array( 'cnid' => "v_".$oVendor->getId(), 'listtype' => $this->_getListType() );
00413                 $this->_saveToDb(
00414                         'oxarticle',
00415                         $oArticle->getId(),
00416                         oxUtilsUrl::getInstance()->appendUrl(
00417                                 $oArticle->getBaseStdLink( $iLang ),
00418                                 $aStdParams
00419                         ),
00420                         $sSeoUri,
00421                         $iLang,
00422                         null,
00423                         0,
00424                         $oVendor->getId()
00425                     );
00426             }
00427 
00428             stopProfile(__FUNCTION__);
00429         }
00430         return $sSeoUri;
00431     }
00432 
00441     protected function _getVendor( $oArticle, $iLang )
00442     {
00443         $oView = $this->getConfig()->getActiveView();
00444 
00445         $oVendor = null;
00446         if ( $sActVendorId = $oArticle->oxarticles__oxvendorid->value ) {
00447             if ( $oView instanceof oxView && ( $oActVendor = $oView->getActVendor() ) ) {
00448                 $oVendor = $oActVendor;
00449             } else {
00450                 $oVendor = oxNew( "oxVendor" );
00451             }
00452             if ( $oVendor->getId() !== $sActVendorId ) {
00453                 $oVendor = oxNew( "oxVendor" );
00454                 if ( !$oVendor->loadInLang( $iLang, $sActVendorId ) ) {
00455                     $oVendor = null;
00456                 }
00457             }
00458         }
00459 
00460         return $oVendor;
00461     }
00462 
00472     protected function _getArticleManufacturerUri( $oArticle, $iLang, $blRegenerate = false )
00473     {
00474         $sSeoUri = null;
00475         startProfile(__FUNCTION__);
00476         if ( $oManufacturer = $this->_getManufacturer( $oArticle, $iLang ) ) {
00477             //load details link from DB
00478             if ( $blRegenerate || !( $sSeoUri = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $oManufacturer->getId(), true ) ) ) {
00479 
00480                 $oArticle = $this->_getProductForLang( $oArticle, $iLang );
00481 
00482                 // create title part for uri
00483                 $sTitle = $this->_prepareArticleTitle( $oArticle );
00484 
00485                 // create uri for all categories
00486                 $sSeoUri = oxSeoEncoderManufacturer::getInstance()->getManufacturerUri( $oManufacturer, $iLang );
00487                 $sSeoUri = $this->_processSeoUrl( $sSeoUri . $sTitle, $oArticle->getId(), $iLang );
00488 
00489                 $aStdParams = array( 'mnid' => $oManufacturer->getId(), 'listtype' => $this->_getListType() );
00490                 $this->_saveToDb(
00491                         'oxarticle',
00492                         $oArticle->getId(),
00493                         oxUtilsUrl::getInstance()->appendUrl(
00494                                 $oArticle->getBaseStdLink( $iLang ),
00495                                 $aStdParams
00496                         ),
00497                         $sSeoUri,
00498                         $iLang,
00499                         null,
00500                         0,
00501                         $oManufacturer->getId()
00502                     );
00503             }
00504 
00505             stopProfile(__FUNCTION__);
00506         }
00507         return $sSeoUri;
00508     }
00509 
00518     protected function _getManufacturer( $oArticle, $iLang )
00519     {
00520         $oManufacturer = null;
00521         if ( $sActManufacturerId = $oArticle->oxarticles__oxmanufacturerid->value ) {
00522             $oView = $this->getConfig()->getActiveView();
00523 
00524             if ( $oView instanceof oxView && ( $oActManufacturer = $oView->getActManufacturer() ) ) {
00525                 $oManufacturer = $oActManufacturer;
00526             } else {
00527                 $oManufacturer = oxNew( "oxManufacturer" );
00528             }
00529 
00530             if ( $oManufacturer->getId() !== $sActManufacturerId || $oManufacturer->getLanguage() != $iLang ) {
00531                 $oManufacturer = oxNew( "oxManufacturer" );
00532                 if ( !$oManufacturer->loadInLang( $iLang, $sActManufacturerId ) ) {
00533                     $oManufacturer = null;
00534                 }
00535             }
00536         }
00537 
00538         return $oManufacturer;
00539     }
00540 
00549     public function getArticleMainUrl( $oArticle, $iLang = null )
00550     {
00551         if ( !isset( $iLang ) ) {
00552             $iLang = $oArticle->getLanguage();
00553         }
00554 
00555         return $this->_getFullUrl( $this->_getArticleMainUri( $oArticle, $iLang ), $iLang );
00556     }
00557 
00567     public function getArticleUrl( $oArticle, $iLang = null, $iType = 0 )
00568     {
00569         if ( !isset( $iLang ) ) {
00570             $iLang = $oArticle->getLanguage();
00571         }
00572 
00573         $sUri = null;
00574         switch ( $iType ) {
00575             case OXARTICLE_LINKTYPE_VENDOR :
00576                 $sUri = $this->_getArticleVendorUri( $oArticle, $iLang );
00577                 break;
00578             case OXARTICLE_LINKTYPE_MANUFACTURER :
00579                 $sUri = $this->_getArticleManufacturerUri( $oArticle, $iLang );
00580                 break;
00581             case OXARTICLE_LINKTYPE_TAG :
00582                 $sUri = $this->_getArticleTagUri( $oArticle, $iLang );
00583                 break;
00584             case OXARTICLE_LINKTYPE_RECOMM :
00585                 $sUri = $this->_getArticleRecommUri( $oArticle, $iLang );
00586                 break;
00587             case OXARTICLE_LINKTYPE_PRICECATEGORY : // goes price category urls to default (category urls)
00588             default:
00589                 $sUri = $this->_getArticleUri( $oArticle, $iLang );
00590                 break;
00591         }
00592 
00593         // if was unable to fetch type uri - returning main
00594         if ( !$sUri ) {
00595             $sUri = $this->_getArticleMainUri( $oArticle, $iLang );
00596         }
00597 
00598         return $this->_getFullUrl( $sUri, $iLang );
00599     }
00600 
00608     public function onDeleteArticle( $oArticle )
00609     {
00610         $oDb = oxDb::getDb();
00611         $sIdQuoted = $oDb->quote( $oArticle->getId() );
00612         $oDb->execute("delete from oxseo where oxobjectid = $sIdQuoted and oxtype = 'oxarticle'");
00613         $oDb->execute("delete from oxobject2seodata where oxobjectid = $sIdQuoted");
00614     }
00615 
00624     protected function _getAltUri( $sObjectId, $iLang )
00625     {
00626         $sSeoUrl = null;
00627         $oArticle = oxNew( "oxarticle" );
00628         if ( $oArticle->loadInLang( $iLang, $sObjectId ) ) {
00629             // choosing URI type to generate
00630             switch( $this->_getListType() ) {
00631                 case 'vendor':
00632                     $sSeoUrl = $this->_getArticleVendorUri( $oArticle, $iLang, true );
00633                     break;
00634                 case 'manufacturer':
00635                     $sSeoUrl = $this->_getArticleManufacturerUri( $oArticle, $iLang, true );
00636                     break;
00637                 case 'tag':
00638                     $sSeoUrl = $this->_getArticleTagUri( $oArticle, $iLang, true );
00639                     break;
00640                 default:
00641                     $sSeoUrl = $this->_getArticleUri( $oArticle, $iLang, true );
00642                     break;
00643             }
00644         }
00645         return $sSeoUrl;
00646     }
00647 }