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->setSkipAssign( true );
00074             $oArticle->loadInLang( $iLang, $sId );
00075         }
00076 
00077         return $oArticle;
00078     }
00079 
00091     protected function _getArticleRecommUri( $oArticle, $iLang )
00092     {
00093         return $this->getArticleRecommUri( $oArticle, $iLang );
00094     }
00095 
00104     public function getArticleRecommUri( $oArticle, $iLang )
00105     {
00106         $sSeoUri = null;
00107         if ( $oRecomm = $this->_getRecomm( $oArticle, $iLang ) ) {
00108             //load details link from DB
00109             if ( !( $sSeoUri = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $oRecomm->getId(), true ) ) ) {
00110 
00111                 $oArticle = $this->_getProductForLang( $oArticle, $iLang );
00112 
00113                 // create title part for uri
00114                 $sTitle = $this->_prepareArticleTitle( $oArticle );
00115 
00116                 // create uri for all categories
00117                 $sSeoUri = oxSeoEncoderRecomm::getInstance()->getRecommUri( $oRecomm, $iLang );
00118                 $sSeoUri = $this->_processSeoUrl( $sSeoUri . $sTitle, $oArticle->getId(), $iLang );
00119 
00120                 $aStdParams = array( 'recommid' => $oRecomm->getId(), 'listtype' => $this->_getListType() );
00121                 $this->_saveToDb(
00122                             'oxarticle',
00123                             $oArticle->getId(),
00124                             oxUtilsUrl::getInstance()->appendUrl(
00125                                     $oArticle->getBaseStdLink( $iLang ),
00126                                     $aStdParams
00127                             ),
00128                             $sSeoUri,
00129                             $iLang,
00130                             null,
00131                             0,
00132                             $oRecomm->getId()
00133                         );
00134             }
00135         }
00136         return $sSeoUri;
00137     }
00138 
00147     protected function _getRecomm( $oArticle, $iLang )
00148     {
00149         $oList = null;
00150         $oView = $this->getConfig()->getActiveView();
00151         if ( $oView instanceof oxView ) {
00152             $oList = $oView->getActiveRecommList();
00153         }
00154         return $oList;
00155     }
00156 
00162     protected function _getListType()
00163     {
00164         return $this->getConfig()->getActiveView()->getListType();
00165     }
00166 
00179     protected function _getArticleTagUri( $oArticle, $iLang, $blRegenerate = false )
00180     {
00181         return $this->getArticleTagUri( $oArticle, $iLang, $blRegenerate );
00182     }
00192     public function getArticleTagUri( $oArticle, $iLang, $blRegenerate = false )
00193     {
00194         $sSeoUri = null;
00195         if ( $sTag = $this->_getTag( $oArticle, $iLang ) ) {
00196             $iShopId = $this->getConfig()->getShopId();
00197             $sStdUrl = $oArticle->getStdTagLink( $sTag );
00198             if ( $blRegenerate || !( $sSeoUri = $this->_loadFromDb( 'dynamic', $this->getDynamicObjectId( $iShopId, $sStdUrl ), $iLang ) ) ) {
00199                 // generating new if not found
00200                 if ( $sSeoUri = oxSeoEncoderTag::getInstance()->getTagUri( $sTag, $iLang, $oArticle->getId() ) ) {
00201                     $sSeoUri .= $this->_prepareArticleTitle( $oArticle );
00202                     $sSeoUri  = $this->_processSeoUrl( $sSeoUri, $this->_getStaticObjectId( $iShopId, $sStdUrl ), $iLang );
00203                     $sSeoUri  = $this->_getDynamicUri( $sStdUrl, $sSeoUri, $iLang );
00204                 }
00205             }
00206         }
00207         return $sSeoUri;
00208     }
00209 
00218     protected function _getTag( $oArticle, $iLang )
00219     {
00220         $sTag = null;
00221         $oView = $this->getConfig()->getActiveView();
00222         if ( $oView instanceof oxView ) {
00223             $sTag = $oView->getTag();
00224         }
00225         return $sTag;
00226     }
00227 
00237     protected function _createArticleCategoryUri( $oArticle, $oCategory, $iLang )
00238     {
00239         startProfile(__FUNCTION__);
00240         $oArticle = $this->_getProductForLang( $oArticle, $iLang );
00241 
00242         // create title part for uri
00243         $sTitle = $this->_prepareArticleTitle( $oArticle );
00244 
00245         // writing category path
00246         $sSeoUri = $this->_processSeoUrl(
00247                             oxSeoEncoderCategory::getInstance()->getCategoryUri( $oCategory, $iLang ).$sTitle,
00248                             $oArticle->getId(), $iLang
00249                         );
00250         $sCatId = $oCategory->getId();
00251         $this->_saveToDb(
00252                     'oxarticle',
00253                     $oArticle->getId(),
00254                     oxUtilsUrl::getInstance()->appendUrl(
00255                             $oArticle->getBaseStdLink( $iLang ),
00256                             array( 'cnid' => $sCatId )
00257                     ),
00258                     $sSeoUri,
00259                     $iLang,
00260                     null,
00261                     0,
00262                     $sCatId
00263                 );
00264 
00265         stopProfile(__FUNCTION__);
00266 
00267         return $sSeoUri;
00268     }
00269 
00282     protected function _getArticleUri( $oArticle, $iLang, $blRegenerate = false )
00283     {
00284         return $this->getArticleUri( $oArticle, $iLang, $blRegenerate );
00285     }
00286 
00296     public function getArticleUri( $oArticle, $iLang, $blRegenerate = false )
00297     {
00298         startProfile(__FUNCTION__);
00299 
00300         $sActCatId = '';
00301         if ( $oActCat = $this->_getCategory( $oArticle, $iLang ) ) {
00302             $sActCatId = $oActCat->getId();
00303         } elseif ( $oActCat = $this->_getMainCategory( $oArticle ) ) {
00304             $sActCatId = $oActCat->getId();
00305         }
00306 
00307         //load details link from DB
00308         if ( $blRegenerate || !( $sSeoUri = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $sActCatId, true ) ) ) {
00309             if ( $oActCat ) {
00310                 $blInCat = false;
00311                 if ( $oActCat->isPriceCategory() ) {
00312                     $blInCat = $oArticle->inPriceCategory( $sActCatId );
00313                 } else {
00314                     $blInCat = $oArticle->inCategory( $sActCatId );
00315                 }
00316                 if ( $blInCat ) {
00317                     $sSeoUri = $this->_createArticleCategoryUri( $oArticle, $oActCat, $iLang );
00318                 }
00319             }
00320         }
00321 
00322         stopProfile(__FUNCTION__);
00323 
00324         return $sSeoUri;
00325     }
00326 
00335     protected function _getCategory( $oArticle, $iLang )
00336     {
00337         $oCat = null;
00338         $oView = $this->getConfig()->getActiveView();
00339         if ( $oView instanceof oxview ) {
00340             $oCat = $oView->getActCategory();
00341         }
00342         return $oCat;
00343     }
00344 
00352     protected function _getMainCategory( $oArticle )
00353     {
00354         $oMainCat = null;
00355 
00356         // if variant parent id must be used
00357         $sArtId = $oArticle->getId();
00358         if ( isset( $oArticle->oxarticles__oxparentid->value ) && $oArticle->oxarticles__oxparentid->value ) {
00359             $sArtId = $oArticle->oxarticles__oxparentid->value;
00360         }
00361 
00362         $oDb = oxDb::getDb();
00363         // add manin category chaching;
00364         $sQ = "select oxcatnid from ".getViewName( "oxobject2category" )." where oxobjectid = ".$oDb->quote( $sArtId )." order by oxtime";
00365         $sIdent = md5( $sQ );
00366 
00367         if ( ( $sMainCatId = $this->_loadFromCache( $sIdent ) ) === false ) {
00368             $sMainCatId = $oDb->getOne( $sQ );
00369             // storing in cache
00370             $this->_saveInCache( $sIdent, $sMainCatId );
00371         }
00372 
00373         if ( $sMainCatId ) {
00374             $oMainCat = oxNew( "oxcategory" );
00375             if ( ! $oMainCat->load( $sMainCatId )) {
00376                 $oMainCat = null;
00377             }
00378         }
00379 
00380         return $oMainCat;
00381     }
00382 
00394     protected function _getArticleMainUri( $oArticle, $iLang )
00395     {
00396         return $this->getArticleMainUri( $oArticle, $iLang );
00397     }
00398 
00407     public function getArticleMainUri( $oArticle, $iLang )
00408     {
00409         startProfile(__FUNCTION__);
00410 
00411         $oMainCat   = $this->_getMainCategory( $oArticle );
00412         $sMainCatId = $oMainCat ? $oMainCat->getId() : '';
00413 
00414         //load default article url from DB
00415         if ( !( $sSeoUri = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $sMainCatId, true ) ) ) {
00416             // save for main category
00417             if ( $oMainCat ) {
00418                 $sSeoUri = $this->_createArticleCategoryUri( $oArticle, $oMainCat, $iLang );
00419             } else {
00420                 // get default article url
00421                 $oArticle = $this->_getProductForLang( $oArticle, $iLang );
00422                 $sSeoUri = $this->_processSeoUrl( $this->_prepareArticleTitle( $oArticle ), $oArticle->getId(), $iLang );
00423 
00424                 // save default article url
00425                 $this->_saveToDb(
00426                         'oxarticle',
00427                         $oArticle->getId(),
00428                         $oArticle->getBaseStdLink( $iLang ),
00429                         $sSeoUri,
00430                         $iLang,
00431                         null,
00432                         0,
00433                         ''
00434                     );
00435             }
00436         }
00437 
00438         stopProfile(__FUNCTION__);
00439         return $sSeoUri;
00440     }
00441 
00450     protected function _prepareArticleTitle( $oArticle )
00451     {
00452         $sTitle = '';
00453 
00454         // create title part for uri
00455         if ( !( $sTitle = $oArticle->oxarticles__oxtitle->value ) ) {
00456             // taking parent article title
00457             if ( ( $sParentId = $oArticle->oxarticles__oxparentid->value ) ) {
00458 
00459                 // looking in cache ..
00460                 if ( !isset( self::$_aTitleCache[$sParentId] ) ) {
00461                     $oDb = oxDb::getDb();
00462                     $sQ = "select oxtitle from ".$oArticle->getViewName()." where oxid = ".$oDb->quote( $sParentId );
00463                     self::$_aTitleCache[$sParentId] = $oDb->getOne( $sQ );
00464                 }
00465                 $sTitle = self::$_aTitleCache[$sParentId];
00466             }
00467         }
00468 
00469         // variant has varselect value
00470         if ( $oArticle->oxarticles__oxvarselect->value ) {
00471             $sTitle .= ( $sTitle ? ' ' : '' ) . $oArticle->oxarticles__oxvarselect->value . ' ';
00472         } elseif ( !$sTitle || ( $oArticle->oxarticles__oxparentid->value ) ) {
00473             // in case nothing was found - looking for number
00474             $sTitle .= ( $sTitle ? ' ' : '' ) . $oArticle->oxarticles__oxartnum->value;
00475         }
00476 
00477         return $this->_prepareTitle( $sTitle ) . '.html';
00478     }
00479 
00492     protected function _getArticleVendorUri( $oArticle, $iLang, $blRegenerate = false )
00493     {
00494         return $this->getArticleVendorUri( $oArticle, $iLang, $blRegenerate );
00495     }
00496 
00506     public function getArticleVendorUri( $oArticle, $iLang, $blRegenerate = false )
00507     {
00508         startProfile(__FUNCTION__);
00509 
00510         $sSeoUri = null;
00511         if ( $oVendor = $this->_getVendor( $oArticle, $iLang ) ) {
00512             //load details link from DB
00513             if ( $blRegenerate || !( $sSeoUri = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $oVendor->getId(), true ) ) ) {
00514 
00515                 $oArticle = $this->_getProductForLang( $oArticle, $iLang );
00516 
00517                 // create title part for uri
00518                 $sTitle = $this->_prepareArticleTitle( $oArticle );
00519 
00520                 // create uri for all categories
00521                 $sSeoUri = oxSeoEncoderVendor::getInstance()->getVendorUri( $oVendor, $iLang );
00522                 $sSeoUri = $this->_processSeoUrl( $sSeoUri . $sTitle, $oArticle->getId(), $iLang );
00523 
00524                 $aStdParams = array( 'cnid' => "v_".$oVendor->getId(), 'listtype' => $this->_getListType() );
00525                 $this->_saveToDb(
00526                         'oxarticle',
00527                         $oArticle->getId(),
00528                         oxUtilsUrl::getInstance()->appendUrl(
00529                                 $oArticle->getBaseStdLink( $iLang ),
00530                                 $aStdParams
00531                         ),
00532                         $sSeoUri,
00533                         $iLang,
00534                         null,
00535                         0,
00536                         $oVendor->getId()
00537                     );
00538             }
00539 
00540             stopProfile(__FUNCTION__);
00541         }
00542         return $sSeoUri;
00543     }
00544 
00553     protected function _getVendor( $oArticle, $iLang )
00554     {
00555         $oView = $this->getConfig()->getActiveView();
00556 
00557         $oVendor = null;
00558         if ( $sActVendorId = $oArticle->oxarticles__oxvendorid->value ) {
00559             if ( $oView instanceof oxView && ( $oActVendor = $oView->getActVendor() ) ) {
00560                 $oVendor = $oActVendor;
00561             } else {
00562                 $oVendor = oxNew( "oxVendor" );
00563             }
00564             if ( $oVendor->getId() !== $sActVendorId ) {
00565                 $oVendor = oxNew( "oxVendor" );
00566                 if ( !$oVendor->loadInLang( $iLang, $sActVendorId ) ) {
00567                     $oVendor = null;
00568                 }
00569             }
00570         }
00571 
00572         return $oVendor;
00573     }
00574 
00587     protected function _getArticleManufacturerUri( $oArticle, $iLang, $blRegenerate = false )
00588     {
00589         return $this->getArticleManufacturerUri( $oArticle, $iLang, $blRegenerate );
00590     }
00591 
00601     public function getArticleManufacturerUri( $oArticle, $iLang, $blRegenerate = false )
00602     {
00603         $sSeoUri = null;
00604         startProfile(__FUNCTION__);
00605         if ( $oManufacturer = $this->_getManufacturer( $oArticle, $iLang ) ) {
00606             //load details link from DB
00607             if ( $blRegenerate || !( $sSeoUri = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $oManufacturer->getId(), true ) ) ) {
00608 
00609                 $oArticle = $this->_getProductForLang( $oArticle, $iLang );
00610 
00611                 // create title part for uri
00612                 $sTitle = $this->_prepareArticleTitle( $oArticle );
00613 
00614                 // create uri for all categories
00615                 $sSeoUri = oxSeoEncoderManufacturer::getInstance()->getManufacturerUri( $oManufacturer, $iLang );
00616                 $sSeoUri = $this->_processSeoUrl( $sSeoUri . $sTitle, $oArticle->getId(), $iLang );
00617 
00618                 $aStdParams = array( 'mnid' => $oManufacturer->getId(), 'listtype' => $this->_getListType() );
00619                 $this->_saveToDb(
00620                         'oxarticle',
00621                         $oArticle->getId(),
00622                         oxUtilsUrl::getInstance()->appendUrl(
00623                                 $oArticle->getBaseStdLink( $iLang ),
00624                                 $aStdParams
00625                         ),
00626                         $sSeoUri,
00627                         $iLang,
00628                         null,
00629                         0,
00630                         $oManufacturer->getId()
00631                     );
00632             }
00633 
00634             stopProfile(__FUNCTION__);
00635         }
00636         return $sSeoUri;
00637     }
00638 
00647     protected function _getManufacturer( $oArticle, $iLang )
00648     {
00649         $oManufacturer = null;
00650         if ( $sActManufacturerId = $oArticle->oxarticles__oxmanufacturerid->value ) {
00651             $oView = $this->getConfig()->getActiveView();
00652 
00653             if ( $oView instanceof oxView && ( $oActManufacturer = $oView->getActManufacturer() ) ) {
00654                 $oManufacturer = $oActManufacturer;
00655             } else {
00656                 $oManufacturer = oxNew( "oxManufacturer" );
00657             }
00658 
00659             if ( $oManufacturer->getId() !== $sActManufacturerId || $oManufacturer->getLanguage() != $iLang ) {
00660                 $oManufacturer = oxNew( "oxManufacturer" );
00661                 if ( !$oManufacturer->loadInLang( $iLang, $sActManufacturerId ) ) {
00662                     $oManufacturer = null;
00663                 }
00664             }
00665         }
00666 
00667         return $oManufacturer;
00668     }
00669 
00678     public function getArticleMainUrl( $oArticle, $iLang = null )
00679     {
00680         if ( !isset( $iLang ) ) {
00681             $iLang = $oArticle->getLanguage();
00682         }
00683 
00684         return $this->_getFullUrl( $this->_getArticleMainUri( $oArticle, $iLang ), $iLang );
00685     }
00686 
00696     public function getArticleUrl( $oArticle, $iLang = null, $iType = 0 )
00697     {
00698         if ( !isset( $iLang ) ) {
00699             $iLang = $oArticle->getLanguage();
00700         }
00701 
00702         $sUri = null;
00703         switch ( $iType ) {
00704             case OXARTICLE_LINKTYPE_VENDOR :
00705                 $sUri = $this->_getArticleVendorUri( $oArticle, $iLang );
00706                 break;
00707             case OXARTICLE_LINKTYPE_MANUFACTURER :
00708                 $sUri = $this->_getArticleManufacturerUri( $oArticle, $iLang );
00709                 break;
00710             case OXARTICLE_LINKTYPE_TAG :
00711                 $sUri = $this->_getArticleTagUri( $oArticle, $iLang );
00712                 break;
00713             case OXARTICLE_LINKTYPE_RECOMM :
00714                 $sUri = $this->_getArticleRecommUri( $oArticle, $iLang );
00715                 break;
00716             case OXARTICLE_LINKTYPE_PRICECATEGORY : // goes price category urls to default (category urls)
00717             default:
00718                 $sUri = $this->_getArticleUri( $oArticle, $iLang );
00719                 break;
00720         }
00721 
00722         // if was unable to fetch type uri - returning main
00723         if ( !$sUri ) {
00724             $sUri = $this->_getArticleMainUri( $oArticle, $iLang );
00725         }
00726 
00727         return $this->_getFullUrl( $sUri, $iLang );
00728     }
00729 
00737     public function onDeleteArticle( $oArticle )
00738     {
00739         $oDb = oxDb::getDb();
00740         $sIdQuoted = $oDb->quote( $oArticle->getId() );
00741         $oDb->execute("delete from oxseo where oxobjectid = $sIdQuoted and oxtype = 'oxarticle'");
00742         $oDb->execute("delete from oxobject2seodata where oxobjectid = $sIdQuoted");
00743     }
00744 
00753     protected function _getAltUri( $sObjectId, $iLang )
00754     {
00755         $sSeoUrl = null;
00756         $oArticle = oxNew( "oxarticle" );
00757         $oArticle->setSkipAssign( true );
00758         if ( $oArticle->loadInLang( $iLang, $sObjectId ) ) {
00759             // choosing URI type to generate
00760             switch( $this->_getListType() ) {
00761                 case 'vendor':
00762                     $sSeoUrl = $this->_getArticleVendorUri( $oArticle, $iLang, true );
00763                     break;
00764                 case 'manufacturer':
00765                     $sSeoUrl = $this->_getArticleManufacturerUri( $oArticle, $iLang, true );
00766                     break;
00767                 case 'tag':
00768                     $sSeoUrl = $this->_getArticleTagUri( $oArticle, $iLang, true );
00769                     break;
00770                 default:
00771                     $sSeoUrl = $this->_getArticleUri( $oArticle, $iLang, true );
00772                     break;
00773             }
00774         }
00775         return $sSeoUrl;
00776     }
00777 }