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 (!self::$_instance) {
00031             self::$_instance = oxNew("oxSeoEncoderArticle");
00032         }
00033 
00034         if ( defined( 'OXID_PHP_UNIT' ) ) {
00035             // resetting cache
00036             self::$_instance->_aSeoCache = array();
00037         }
00038 
00039         return self::$_instance;
00040     }
00041 
00047     protected function _getUrlExtension()
00048     {
00049         return '.html';
00050     }
00051 
00061     protected function _getProductForLang( $oArticle, $iLang )
00062     {
00063         if ( isset( $iLang ) && $iLang != $oArticle->getLanguage() ) {
00064             $sId = $oArticle->getId();
00065             $oArticle = oxNew( 'oxarticle' );
00066             $oArticle->loadInLang( $iLang, $sId );
00067         }
00068 
00069         return $oArticle;
00070     }
00071 
00080     protected function _getArticleTagUri( $oArticle, $iLang = null )
00081     {
00082         $oView = $this->getConfig()->getActiveView();
00083 
00084         $sTag = null;
00085         if ( $oView instanceof oxView ) {
00086             $sTag = $oView->getTag();
00087         }
00088 
00089         $iShopId = $this->getConfig()->getShopId();
00090         $sStdUrl = $oArticle->getStdTagLink( $sTag );
00091         $sSeoUrl = $this->_loadFromDb( 'dynamic', $this->getDynamicObjectId( $iShopId, $sStdUrl ), $iLang );
00092         if ( !$sSeoUrl ) {
00093 
00094             // generating new if not found
00095             $sSeoUrl  = oxSeoEncoderTag::getInstance()->getTagUri( $sTag, $iLang );
00096             $sSeoUrl .= $this->_prepareArticleTitle( $oArticle );
00097             $sSeoUrl  = $this->_processSeoUrl( $sSeoUrl, $this->_getStaticObjectId( $iShopId, $sStdUrl ), $iLang );
00098 
00099             $sSeoUrl = $this->_getDynamicUri( $sStdUrl, $sSeoUrl, $iLang );
00100         }
00101 
00102         return $sSeoUrl;
00103     }
00104 
00114     protected function _createArticleCategoryUri( $oArticle, $oCategory, $iLang = null)
00115     {
00116         startProfile(__FUNCTION__);
00117         if (!isset($iLang)) {
00118             $iLang = $oArticle->getLanguage();
00119         }
00120 
00121         $oArticle = $this->_getProductForLang( $oArticle, $iLang );
00122 
00123         // create title part for uri
00124         $sTitle = $this->_prepareArticleTitle( $oArticle );
00125 
00126         // writing category path
00127         $sSeoUrl = $this->_processSeoUrl(
00128                             oxSeoEncoderCategory::getInstance()->getCategoryUri( $oCategory, $iLang ).$sTitle,
00129                             $oArticle->getId(),
00130                             $iLang
00131                         );
00132         $sCatId = $oCategory->getId();
00133         $this->_saveToDb( 'oxarticle', $oArticle->getId(), $oArticle->getStdLink($iLang, array('cnid'=>$sCatId)), $sSeoUrl, $iLang, null, 0, false, false, $sCatId);
00134 
00135         stopProfile(__FUNCTION__);
00136 
00137         return $sSeoUrl;
00138     }
00139 
00148     protected function _getArticleUri( $oArticle, $iLang = null)
00149     {
00150         startProfile(__FUNCTION__);
00151         if (!isset($iLang)) {
00152             $iLang = $oArticle->getLanguage();
00153         }
00154 
00155         $sActCatId = '';
00156         $oView = $this->getConfig()->getActiveView();
00157         $oActCat = null;
00158 
00159         if ( $oView instanceof oxview ) {
00160             $oActCat = $oView->getActCategory();
00161         }
00162 
00163         if ( $oActCat ) {
00164             $sActCatId = $oActCat->getId();
00165         }
00166 
00167         //load details link from DB
00168         if ( !( $sSeoUrl = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $sActCatId, true ) ) ) {
00169             $blInCat  = false;
00170             if ( $sActCatId ) {
00171                 if ( $oActCat->isPriceCategory() ) {
00172                     $blInCat = $oArticle->inPriceCategory( $sActCatId );
00173                 } else {
00174                     $blInCat = $oArticle->inCategory( $sActCatId );
00175                 }
00176             }
00177             if ( $blInCat ) {
00178                 $sSeoUrl = $this->_createArticleCategoryUri( $oArticle, $oActCat, $iLang );
00179             } else {
00180                 $sSeoUrl = $this->_getArticleMainUri( $oArticle, $iLang );
00181             }
00182         }
00183 
00184         stopProfile(__FUNCTION__);
00185 
00186         return $sSeoUrl;
00187     }
00188 
00197     protected function _getArticleMainUri( $oArticle, $iLang = null)
00198     {
00199         startProfile(__FUNCTION__);
00200         if (!isset($iLang)) {
00201             $iLang = $oArticle->getLanguage();
00202         }
00203 
00204         // if variant parent id must be used
00205         $sArtId = $oArticle->getId();
00206         if ( isset( $oArticle->oxarticles__oxparentid->value ) && $oArticle->oxarticles__oxparentid->value ) {
00207             $sArtId = $oArticle->oxarticles__oxparentid->value;
00208         }
00209 
00210         if ( !( $sMainCatId = oxDb::getDb()->getOne( "select oxcatnid from ".getViewName( "oxobject2category" )." where oxobjectid = '{$sArtId}' order by oxtime" ) ) ) {
00211             $sMainCatId = '';
00212         }
00213 
00214         //load default article url from DB
00215         if ( !( $sSeoUrl = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $sMainCatId, true ) ) ) {
00216             if ( $sMainCatId ) {
00217                 $oMainCat = oxNew( "oxcategory" );
00218                 $oMainCat->load( $sMainCatId );
00219                 // save for main category
00220                 $sSeoUrl = $this->_createArticleCategoryUri( $oArticle, $oMainCat, $iLang );
00221             } else {
00222                 // get default article url
00223                 $oArticle = $this->_getProductForLang( $oArticle, $iLang );
00224                 $sSeoUrl = $this->_processSeoUrl( $this->_prepareArticleTitle( $oArticle ), $oArticle->getId(), $iLang );
00225 
00226                 // save default article url
00227                 $this->_saveToDb( 'oxarticle', $oArticle->getId(), $oArticle->getStdLink($iLang, array( 'cnid' => '' ) ), $sSeoUrl, $iLang, null, 0, false, false, '' );
00228             }
00229         }
00230 
00231         stopProfile(__FUNCTION__);
00232         return $sSeoUrl;
00233     }
00234 
00243     protected function _prepareArticleTitle( $oArticle )
00244     {
00245         $sTitle = '';
00246 
00247         // create title part for uri
00248         if ( !( $sTitle = $oArticle->oxarticles__oxtitle->value ) ) {
00249             // taking parent article title
00250             if ( ( $sParentId = $oArticle->oxarticles__oxparentid->value ) ) {
00251 
00252                 // looking in cache ..
00253                 if ( !isset( self::$_aTitleCache[$sParentId] ) ) {
00254                     $oDb = oxDb::getDb();
00255                     $sQ = "select oxtitle from oxarticles where oxid = ".$oDb->quote( $sParentId );
00256                     self::$_aTitleCache[$sParentId] = $oDb->getOne( $sQ );
00257                 }
00258                 $sTitle = self::$_aTitleCache[$sParentId];
00259             }
00260         }
00261 
00262         // variant has varselect value
00263         if ( $oArticle->oxarticles__oxvarselect->value ) {
00264             $sTitle .= ( $sTitle ? ' ' : '' ).$oArticle->oxarticles__oxvarselect->value . ' ';
00265         }
00266 
00267         // in case nothing was found - looking for number
00268         if ( !$sTitle ) {
00269             $sTitle .= $oArticle->oxarticles__oxartnum->value;
00270         }
00271 
00272         return $this->_prepareTitle( $sTitle ) . '.html';
00273     }
00274 
00283     protected function _getArticleVendorUri( $oArticle, $iLang = null )
00284     {
00285         startProfile(__FUNCTION__);
00286         if ( !isset( $iLang ) ) {
00287             $iLang = $oArticle->getLanguage();
00288         }
00289 
00290         $sActVendorId = $oArticle->oxarticles__oxvendorid->value;
00291         $oVendor = oxNew( 'oxvendor' );
00292         if ( !$sActVendorId || !$oVendor->load( $sActVendorId ) ) {
00293             $oVendor = null;
00294         }
00295 
00296         //load details link from DB
00297         if ( !( $sSeoUrl = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $sActVendorId, true ) ) ) {
00298 
00299             $oArticle = $this->_getProductForLang( $oArticle, $iLang );
00300 
00301             // create title part for uri
00302             $sTitle = $this->_prepareArticleTitle( $oArticle );
00303 
00304             // create uri for all categories
00305             if ( !$sActVendorId || !$oVendor ) {
00306                 $sSeoUrl = $this->_processSeoUrl( $sTitle, $oArticle->getId(), $iLang );
00307                 $this->_saveToDb( 'oxarticle', $oArticle->getId(), $oArticle->getStdLink($iLang, array('cnid'=>'')), $sSeoUrl, $iLang );
00308             } else {
00309                 $sSeoUrl = oxSeoEncoderVendor::getInstance()->getVendorUri( $oVendor, $iLang );
00310                 $sSeoUrl = $this->_processSeoUrl( $sSeoUrl . $sTitle, $oArticle->getId(), $iLang );
00311 
00312                 $this->_saveToDb( 'oxarticle', $oArticle->getId(), $oArticle->getStdLink($iLang, array('cnid'=>'')), $sSeoUrl, $iLang, null, 0, false, false, $sActVendorId );
00313             }
00314         }
00315 
00316         stopProfile(__FUNCTION__);
00317 
00318         return $sSeoUrl;
00319     }
00320 
00329     protected function _getArticleManufacturerUri( $oArticle, $iLang = null )
00330     {
00331         startProfile(__FUNCTION__);
00332         if ( !isset( $iLang ) ) {
00333             $iLang = $oArticle->getLanguage();
00334         }
00335 
00336         $sActManufacturerId = $oArticle->oxarticles__oxmanufacturerid->value;
00337         $oManufacturer = oxNew( 'oxmanufacturer' );
00338         if ( !$sActManufacturerId || !$oManufacturer->load( $sActManufacturerId ) ) {
00339             $oManufacturer = null;
00340         }
00341 
00342         //load details link from DB
00343         if ( !( $sSeoUrl = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $sActManufacturerId, true ) ) ) {
00344 
00345             $oArticle = $this->_getProductForLang( $oArticle, $iLang );
00346 
00347             // create title part for uri
00348             $sTitle = $this->_prepareArticleTitle( $oArticle );
00349 
00350             // create uri for all categories
00351             if ( !$sActManufacturerId || !$oManufacturer ) {
00352                 $sSeoUrl = $this->_processSeoUrl( $sTitle, $oArticle->getId(), $iLang );
00353                 $this->_saveToDb( 'oxarticle', $oArticle->getId(), $oArticle->getStdLink($iLang, array('cnid'=>'')), $sSeoUrl, $iLang );
00354             } else {
00355                 $sSeoUrl = oxSeoEncoderManufacturer::getInstance()->getManufacturerUri( $oManufacturer, $iLang );
00356                 $sSeoUrl = $this->_processSeoUrl( $sSeoUrl . $sTitle, $oArticle->getId(), $iLang );
00357 
00358                 $this->_saveToDb( 'oxarticle', $oArticle->getId(), $oArticle->getStdLink($iLang, array('cnid'=>'')), $sSeoUrl, $iLang, null, 0, false, false, $sActManufacturerId );
00359             }
00360         }
00361 
00362         stopProfile(__FUNCTION__);
00363 
00364         return $sSeoUrl;
00365     }
00366 
00374     public function getArticleMainUrl( $oArticle, $iLang = null )
00375     {
00376         if (!isset($iLang)) {
00377             $iLang = $oArticle->getLanguage();
00378         }
00379         return $this->_getFullUrl( $this->_getArticleMainUri( $oArticle, $iLang ), $iLang );
00380     }
00381 
00391     public function getArticleUrl( $oArticle, $iLang = null, $iType = 0 )
00392     {
00393         if (!isset($iLang)) {
00394             $iLang = $oArticle->getLanguage();
00395         }
00396 
00397         $sUri = '';
00398         switch ( $iType ) {
00399             case 1 :
00400                 $sUri = $this->_getArticleVendorUri( $oArticle, $iLang );
00401                 break;
00402             case 2 :
00403                 $sUri = $this->_getArticleManufacturerUri( $oArticle, $iLang );
00404                 break;
00405             case 4 :
00406                 $sUri = $this->_getArticleTagUri( $oArticle, $iLang );
00407                 break;
00408             case 3 : // goes price category urls to default (category urls)
00409             default:
00410                 $sUri = $this->_getArticleUri( $oArticle, $iLang );
00411                 break;
00412         }
00413 
00414         return $this->_getFullUrl( $sUri, $iLang );
00415     }
00416 
00424     public function onDeleteArticle($oArticle)
00425     {
00426         $sIdQuoted = oxDb::getDb()->quote($oArticle->getId());
00427         oxDb::getDb()->execute("delete from oxseo where oxobjectid = $sIdQuoted and oxtype = 'oxarticle'");
00428     }
00429 }

Generated on Mon Oct 26 20:07:17 2009 for OXID eShop CE by  doxygen 1.5.5