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         if ( $oRecomm = $this->_getRecomm( $oArticle, $iLang ) ) {
00090             //load details link from DB
00091             if ( !( $sSeoUri = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $oRecomm->getId(), true ) ) ) {
00092 
00093                 $oArticle = $this->_getProductForLang( $oArticle, $iLang );
00094 
00095                 // create title part for uri
00096                 $sTitle = $this->_prepareArticleTitle( $oArticle );
00097 
00098                 // create uri for all categories
00099                 $sSeoUri = oxSeoEncoderRecomm::getInstance()->getRecommUri( $oRecomm, $iLang );
00100                 $sSeoUri = $this->_processSeoUrl( $sSeoUri . $sTitle, $oArticle->getId(), $iLang );
00101 
00102                 $aStdParams = array( 'recommid' => $oRecomm->getId(), 'listtype' => $this->_getListType() );
00103                 $this->_saveToDb( 'oxarticle', $oArticle->getId(), $oArticle->getStdLink( $iLang, $aStdParams ), $sSeoUri, $iLang, null, 0, false, false, $oRecomm->getId() );
00104             }
00105 
00106             return $sSeoUri;
00107         }
00108     }
00109 
00118     protected function _getRecomm( $oArticle, $iLang )
00119     {
00120         $oView = $this->getConfig()->getActiveView();
00121         if ( $oView instanceof oxView ) {
00122             return $oView->getActiveRecommList();
00123         }
00124     }
00125 
00131     protected function _getListType()
00132     {
00133         return $this->getConfig()->getActiveView()->getListType();
00134     }
00135 
00144     protected function _getArticleTagUri( $oArticle, $iLang )
00145     {
00146         if ( $sTag = $this->_getTag( $oArticle, $iLang ) ) {
00147             $iShopId = $this->getConfig()->getShopId();
00148             $sStdUrl = $oArticle->getStdTagLink( $sTag );
00149             if ( !( $sSeoUri = $this->_loadFromDb( 'dynamic', $this->getDynamicObjectId( $iShopId, $sStdUrl ), $iLang ) ) ) {
00150 
00151                 // generating new if not found
00152                 $sSeoUri  = oxSeoEncoderTag::getInstance()->getTagUri( $sTag, $iLang );
00153                 $sSeoUri .= $this->_prepareArticleTitle( $oArticle );
00154                 $sSeoUri  = $this->_processSeoUrl( $sSeoUri, $this->_getStaticObjectId( $iShopId, $sStdUrl ), $iLang );
00155                 $sSeoUri  = $this->_getDynamicUri( $sStdUrl, $sSeoUri, $iLang );
00156             }
00157             return $sSeoUri;
00158         }
00159     }
00160 
00169     protected function _getTag( $oArticle, $iLang )
00170     {
00171         $oView = $this->getConfig()->getActiveView();
00172         if ( $oView instanceof oxView ) {
00173             return $oView->getTag();
00174         }
00175     }
00176 
00186     protected function _createArticleCategoryUri( $oArticle, $oCategory, $iLang )
00187     {
00188         startProfile(__FUNCTION__);
00189         $oArticle = $this->_getProductForLang( $oArticle, $iLang );
00190 
00191         // create title part for uri
00192         $sTitle = $this->_prepareArticleTitle( $oArticle );
00193 
00194         // writing category path
00195         $sSeoUri = $this->_processSeoUrl(
00196                             oxSeoEncoderCategory::getInstance()->getCategoryUri( $oCategory, $iLang ).$sTitle,
00197                             $oArticle->getId(), $iLang
00198                         );
00199         $sCatId = $oCategory->getId();
00200         $this->_saveToDb( 'oxarticle', $oArticle->getId(), $oArticle->getStdLink( $iLang, array( 'cnid' => $sCatId ) ), $sSeoUri, $iLang, null, 0, false, false, $sCatId);
00201 
00202         stopProfile(__FUNCTION__);
00203 
00204         return $sSeoUri;
00205     }
00206 
00215     protected function _getArticleUri( $oArticle, $iLang )
00216     {
00217         startProfile(__FUNCTION__);
00218 
00219         $sActCatId = '';
00220         if ( $oActCat = $this->_getCategory( $oArticle, $iLang ) ) {
00221             $sActCatId = $oActCat->getId();
00222         }
00223 
00224         //load details link from DB
00225         if ( !( $sSeoUri = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $sActCatId, true ) ) ) {
00226             if ( $oActCat ) {
00227                 $blInCat = false;
00228                 if ( $oActCat->isPriceCategory() ) {
00229                     $blInCat = $oArticle->inPriceCategory( $sActCatId );
00230                 } else {
00231                     $blInCat = $oArticle->inCategory( $sActCatId );
00232                 }
00233                 if ( $blInCat ) {
00234                     $sSeoUri = $this->_createArticleCategoryUri( $oArticle, $oActCat, $iLang );
00235                 }
00236             }
00237         }
00238 
00239         stopProfile(__FUNCTION__);
00240 
00241         return $sSeoUri;
00242     }
00243 
00252     protected function _getCategory( $oArticle, $iLang )
00253     {
00254         $oView = $this->getConfig()->getActiveView();
00255         if ( $oView instanceof oxview ) {
00256             return $oView->getActCategory();
00257         }
00258     }
00259 
00268     protected function _getArticleMainUri( $oArticle, $iLang )
00269     {
00270         startProfile(__FUNCTION__);
00271 
00272         // if variant parent id must be used
00273         $sArtId = $oArticle->getId();
00274         if ( isset( $oArticle->oxarticles__oxparentid->value ) && $oArticle->oxarticles__oxparentid->value ) {
00275             $sArtId = $oArticle->oxarticles__oxparentid->value;
00276         }
00277 
00278         if ( !( $sMainCatId = oxDb::getDb()->getOne( "select oxcatnid from ".getViewName( "oxobject2category" )." where oxobjectid = '{$sArtId}' order by oxtime" ) ) ) {
00279             $sMainCatId = '';
00280         }
00281 
00282         //load default article url from DB
00283         if ( !( $sSeoUri = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $sMainCatId, true ) ) ) {
00284             if ( $sMainCatId ) {
00285                 $oMainCat = oxNew( "oxcategory" );
00286                 $oMainCat->load( $sMainCatId );
00287                 // save for main category
00288                 $sSeoUri = $this->_createArticleCategoryUri( $oArticle, $oMainCat, $iLang );
00289             } else {
00290                 // get default article url
00291                 $oArticle = $this->_getProductForLang( $oArticle, $iLang );
00292                 $sSeoUri = $this->_processSeoUrl( $this->_prepareArticleTitle( $oArticle ), $oArticle->getId(), $iLang );
00293 
00294                 // save default article url
00295                 $this->_saveToDb( 'oxarticle', $oArticle->getId(), $oArticle->getStdLink( $iLang ), $sSeoUri, $iLang, null, 0, false, false, '' );
00296             }
00297         }
00298 
00299         stopProfile(__FUNCTION__);
00300         return $sSeoUri;
00301     }
00302 
00311     protected function _prepareArticleTitle( $oArticle )
00312     {
00313         $sTitle = '';
00314 
00315         // create title part for uri
00316         if ( !( $sTitle = $oArticle->oxarticles__oxtitle->value ) ) {
00317             // taking parent article title
00318             if ( ( $sParentId = $oArticle->oxarticles__oxparentid->value ) ) {
00319 
00320                 // looking in cache ..
00321                 if ( !isset( self::$_aTitleCache[$sParentId] ) ) {
00322                     $oDb = oxDb::getDb();
00323                     $sQ = "select oxtitle from oxarticles where oxid = ".$oDb->quote( $sParentId );
00324                     self::$_aTitleCache[$sParentId] = $oDb->getOne( $sQ );
00325                 }
00326                 $sTitle = self::$_aTitleCache[$sParentId];
00327             }
00328         }
00329 
00330         // variant has varselect value
00331         if ( $oArticle->oxarticles__oxvarselect->value ) {
00332             $sTitle .= ( $sTitle ? ' ' : '' ).$oArticle->oxarticles__oxvarselect->value . ' ';
00333         }
00334 
00335         // in case nothing was found - looking for number
00336         if ( !$sTitle ) {
00337             $sTitle .= $oArticle->oxarticles__oxartnum->value;
00338         }
00339 
00340         return $this->_prepareTitle( $sTitle ) . '.html';
00341     }
00342 
00351     protected function _getArticleVendorUri( $oArticle, $iLang )
00352     {
00353         startProfile(__FUNCTION__);
00354 
00355         if ( $oVendor = $this->_getVendor( $oArticle, $iLang ) ) {
00356             //load details link from DB
00357             if ( !( $sSeoUri = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $oVendor->getId(), true ) ) ) {
00358 
00359                 $oArticle = $this->_getProductForLang( $oArticle, $iLang );
00360 
00361                 // create title part for uri
00362                 $sTitle = $this->_prepareArticleTitle( $oArticle );
00363 
00364                 // create uri for all categories
00365                 $sSeoUri = oxSeoEncoderVendor::getInstance()->getVendorUri( $oVendor, $iLang );
00366                 $sSeoUri = $this->_processSeoUrl( $sSeoUri . $sTitle, $oArticle->getId(), $iLang );
00367 
00368                 $aStdParams = array( 'cnid' => "v_".$oVendor->getId(), 'listtype' => $this->_getListType() );
00369                 $this->_saveToDb( 'oxarticle', $oArticle->getId(), $oArticle->getStdLink( $iLang, $aStdParams ), $sSeoUri, $iLang, null, 0, false, false, $oVendor->getId() );
00370             }
00371 
00372             stopProfile(__FUNCTION__);
00373             return $sSeoUri;
00374         }
00375     }
00376 
00385     protected function _getVendor( $oArticle, $iLang )
00386     {
00387         $oView = $this->getConfig()->getActiveView();
00388 
00389         $oVendor = null;
00390         if ( $sActVendorId = $oArticle->oxarticles__oxvendorid->value ) {
00391             if ( $oView instanceof oxView && ( $oActVendor = $oView->getActVendor() ) ) {
00392                 $oVendor = $oActVendor;
00393             } else {
00394                 $oVendor = oxNew( "oxVendor" );
00395             }
00396             if ( $oVendor->getId() !== $sActVendorId ) {
00397                 if ( !$oVendor->loadInLang( $iLang, $sActVendorId ) ) {
00398                     $oVendor = null;
00399                 }
00400             }
00401         }
00402 
00403         return $oVendor;
00404     }
00405 
00414     protected function _getArticleManufacturerUri( $oArticle, $iLang )
00415     {
00416         startProfile(__FUNCTION__);
00417         if ( $oManufacturer = $this->_getManufacturer( $oArticle, $iLang ) ) {
00418             //load details link from DB
00419             if ( !( $sSeoUri = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $oManufacturer->getId(), true ) ) ) {
00420 
00421                 $oArticle = $this->_getProductForLang( $oArticle, $iLang );
00422 
00423                 // create title part for uri
00424                 $sTitle = $this->_prepareArticleTitle( $oArticle );
00425 
00426                 // create uri for all categories
00427                 $sSeoUri = oxSeoEncoderManufacturer::getInstance()->getManufacturerUri( $oManufacturer, $iLang );
00428                 $sSeoUri = $this->_processSeoUrl( $sSeoUri . $sTitle, $oArticle->getId(), $iLang );
00429 
00430                 $aStdParams = array( 'mnid' => $oManufacturer->getId(), 'listtype' => $this->_getListType() );
00431                 $this->_saveToDb( 'oxarticle', $oArticle->getId(), $oArticle->getStdLink( $iLang, $aStdParams ), $sSeoUri, $iLang, null, 0, false, false, $oManufacturer->getId() );
00432             }
00433 
00434             stopProfile(__FUNCTION__);
00435             return $sSeoUri;
00436         }
00437     }
00438 
00447     protected function _getManufacturer( $oArticle, $iLang )
00448     {
00449         $oManufacturer = null;
00450         if ( $sActManufacturerId = $oArticle->oxarticles__oxmanufacturerid->value ) {
00451             $oView = $this->getConfig()->getActiveView();
00452             if ( $oView instanceof oxView && ( $oActManufacturer = $oView->getActManufacturer() ) ) {
00453                 $oManufacturer = $oActManufacturer;
00454             } else {
00455                 $oManufacturer = oxNew( "oxManufacturer" );
00456             }
00457             if ( $oManufacturer->getId() !== $sActManufacturerId || $oManufacturer->getLanguage() != $iLang ) {
00458                 if ( !$oManufacturer->loadInLang( $iLang, $sActManufacturerId ) ) {
00459                     $oManufacturer = null;
00460                 }
00461             }
00462         }
00463 
00464         return $oManufacturer;
00465     }
00466 
00475     public function getArticleMainUrl( $oArticle, $iLang = null )
00476     {
00477         if ( !isset( $iLang ) ) {
00478             $iLang = $oArticle->getLanguage();
00479         }
00480 
00481         return $this->_getFullUrl( $this->_getArticleMainUri( $oArticle, $iLang ), $iLang );
00482     }
00483 
00493     public function getArticleUrl( $oArticle, $iLang = null, $iType = 0 )
00494     {
00495         if ( !isset( $iLang ) ) {
00496             $iLang = $oArticle->getLanguage();
00497         }
00498 
00499         $sUri = null;
00500         switch ( $iType ) {
00501             case OXARTICLE_LINKTYPE_VENDOR :
00502                 $sUri = $this->_getArticleVendorUri( $oArticle, $iLang );
00503                 break;
00504             case OXARTICLE_LINKTYPE_MANUFACTURER :
00505                 $sUri = $this->_getArticleManufacturerUri( $oArticle, $iLang );
00506                 break;
00507             case OXARTICLE_LINKTYPE_TAG :
00508                 $sUri = $this->_getArticleTagUri( $oArticle, $iLang );
00509                 break;
00510             case OXARTICLE_LINKTYPE_RECOMM :
00511                 $sUri = $this->_getArticleRecommUri( $oArticle, $iLang );
00512                 break;
00513             case OXARTICLE_LINKTYPE_PRICECATEGORY : // goes price category urls to default (category urls)
00514             default:
00515                 $sUri = $this->_getArticleUri( $oArticle, $iLang );
00516                 break;
00517         }
00518 
00519         // if was unable to fetch type uri - returning main
00520         if ( !$sUri ) {
00521             $sUri = $this->_getArticleMainUri( $oArticle, $iLang );
00522         }
00523 
00524         return $this->_getFullUrl( $sUri, $iLang );
00525     }
00526 
00534     public function onDeleteArticle( $oArticle )
00535     {
00536         $sIdQuoted = oxDb::getDb()->quote($oArticle->getId());
00537         oxDb::getDb()->execute("delete from oxseo where oxobjectid = $sIdQuoted and oxtype = 'oxarticle'");
00538     }
00539 }

Generated by  doxygen 1.6.2