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 return self::$_instance;
00034 }
00035
00044 protected function _getArticleUri( $oArticle, $iLang = null)
00045 {
00046 startProfile(__FUNCTION__);
00047 if (!isset($iLang)) {
00048 $iLang = $oArticle->getLanguage();
00049 }
00050
00051 $sActCatId = '';
00052 $oView = $this->getConfig()->getActiveView();
00053 $oActCat = null;
00054
00055 if ( $oView instanceof oxUBase ) {
00056 $oActCat = $oView->getActCategory();
00057 }
00058
00059 if ( $oActCat ) {
00060 $sActCatId = $oActCat->oxcategories__oxrootid->value;
00061 }
00062
00063
00064 if ( !( $sSeoUrl = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $sActCatId, false ) ) ) {
00065
00066 if ($iLang != $oArticle->getLanguage()) {
00067 $sId = $oArticle->getId();
00068 $oArticle = oxNew('oxarticle');
00069 $oArticle->loadInLang($iLang, $sId);
00070 }
00071
00072
00073 $sTitle = $this->_prepareArticleTitle( $oArticle );
00074
00075
00076 $oCategorys = $this->getSeoCategories( $oArticle, $iLang );
00077 if (!$oCategorys->count()) {
00078 $sSeoUrl = $this->_getUniqueSeoUrl( $sTitle, '.html', $oArticle->getId(), $iLang );
00079 $this->_saveToDb( 'oxarticle', $oArticle->getId(), $oArticle->getStdLink(), $sSeoUrl, $iLang );
00080 } else {
00081 $sTmpSeoUrl = '';
00082 $oEncoder = oxSeoEncoderCategory::getInstance();
00083 foreach ($oCategorys as $oCategory) {
00084
00085 $sTmpSeoUrl = $oEncoder->getCategoryUri( $oCategory );
00086 $sTmpSeoUrl .= $sTitle;
00087 $sTmpSeoUrl = $this->_getUniqueSeoUrl( $sTmpSeoUrl, '.html', $oArticle->getId(), $iLang );
00088
00089 $this->_saveToDb( 'oxarticle', $oArticle->getId(), $oArticle->getStdLink(), $sTmpSeoUrl, $iLang, null, 0, '', '', $oCategory->oxcategories__oxrootid->value);
00090 if ($oCategory->oxcategories__oxrootid->value == $sActCatId) {
00091 $sSeoUrl = $sTmpSeoUrl;
00092 }
00093 }
00094 if (!$sSeoUrl) {
00095
00096 $sSeoUrl = $sTmpSeoUrl;
00097 }
00098 }
00099 }
00100
00101 stopProfile(__FUNCTION__);
00102
00103 return $sSeoUrl;
00104 }
00105
00114 protected function _prepareArticleTitle( $oArticle )
00115 {
00116 $sTitle = '';
00117
00118
00119 if ( !( $sTitle = $oArticle->oxarticles__oxtitle->value ) ) {
00120
00121 if ( ( $sParentId = $oArticle->oxarticles__oxparentid->value ) ) {
00122
00123
00124 if ( !isset( self::$_aTitleCache[$sParentId] ) ) {
00125 $sQ = "select oxtitle from oxarticles where oxid = '{$sParentId}'";
00126 self::$_aTitleCache[$sParentId] = oxDb::getDb()->getOne( $sQ );
00127 }
00128 $sTitle = self::$_aTitleCache[$sParentId];
00129 }
00130 }
00131
00132
00133 if ( $oArticle->oxarticles__oxvarselect->value ) {
00134 $sTitle .= ( $sTitle ? ' ' : '' ).$oArticle->oxarticles__oxvarselect->value . ' ';
00135 }
00136
00137
00138 if ( !$sTitle ) {
00139 $sTitle .= $oArticle->oxarticles__oxartnum->value;
00140 }
00141
00142 return $this->_prepareTitle( $sTitle . '.html' );
00143 }
00144
00153 protected function _getArticleVendorUri( $oArticle, $iLang = null )
00154 {
00155 startProfile(__FUNCTION__);
00156 if ( !isset( $iLang ) ) {
00157 $iLang = $oArticle->getLanguage();
00158 }
00159
00160 $sActVendorId = $oArticle->oxarticles__oxvendorid->value;
00161 $oVendor = oxNew( 'oxvendor' );
00162 if ( !$sActVendorId || !$oVendor->load( $sActVendorId ) ) {
00163 $oVendor = null;
00164 }
00165
00166
00167 if ( !( $sSeoUrl = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $sActVendorId, true ) ) ) {
00168
00169 if ( $iLang != $oArticle->getLanguage() ) {
00170 $sId = $oArticle->getId();
00171 $oArticle = oxNew('oxarticle');
00172 $oArticle->loadInLang( $iLang, $sId );
00173 }
00174
00175
00176 $sTitle = $this->_prepareArticleTitle( $oArticle );
00177
00178
00179 if ( !$sActVendorId || !$oVendor ) {
00180 $sSeoUrl = $this->_getUniqueSeoUrl( $sTitle, '.html', $oArticle->getId(), $iLang );
00181 $this->_saveToDb( 'oxarticle', $oArticle->getId(), $oArticle->getStdLink(), $sSeoUrl, $iLang );
00182 } else {
00183 $sSeoUrl = oxSeoEncoderVendor::getInstance()->getVendorUri( $oVendor, $iLang );
00184 $sSeoUrl = $this->_getUniqueSeoUrl( $sSeoUrl . $sTitle, '.html', $oArticle->getId(), $iLang );
00185
00186 $this->_saveToDb( 'oxarticle', $oArticle->getId(), $oArticle->getStdLink(), $sSeoUrl, $iLang, null, 0, '', '', $sActVendorId );
00187 }
00188 }
00189
00190 stopProfile(__FUNCTION__);
00191
00192 return $sSeoUrl;
00193 }
00194
00203 protected function _getArticleManufacturerUri( $oArticle, $iLang = null )
00204 {
00205 startProfile(__FUNCTION__);
00206 if ( !isset( $iLang ) ) {
00207 $iLang = $oArticle->getLanguage();
00208 }
00209
00210 $sActManufacturerId = $oArticle->oxarticles__oxmanufacturerid->value;
00211 $oManufacturer = oxNew( 'oxmanufacturer' );
00212 if ( !$sActManufacturerId || !$oManufacturer->load( $sActManufacturerId ) ) {
00213 $oManufacturer = null;
00214 }
00215
00216
00217 if ( !( $sSeoUrl = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $sActManufacturerId, true ) ) ) {
00218
00219 if ( $iLang != $oArticle->getLanguage() ) {
00220 $sId = $oArticle->getId();
00221 $oArticle = oxNew('oxarticle');
00222 $oArticle->loadInLang( $iLang, $sId );
00223 }
00224
00225
00226 $sTitle = $this->_prepareArticleTitle( $oArticle );
00227
00228
00229 if ( !$sActManufacturerId || !$oManufacturer ) {
00230 $sSeoUrl = $this->_getUniqueSeoUrl( $sTitle, '.html', $oArticle->getId(), $iLang );
00231 $this->_saveToDb( 'oxarticle', $oArticle->getId(), $oArticle->getStdLink(), $sSeoUrl, $iLang );
00232 } else {
00233 $sSeoUrl = oxSeoEncoderManufacturer::getInstance()->getManufacturerUri( $oManufacturer, $iLang );
00234 $sSeoUrl = $this->_getUniqueSeoUrl( $sSeoUrl . $sTitle, '.html', $oArticle->getId(), $iLang );
00235
00236 $this->_saveToDb( 'oxarticle', $oArticle->getId(), $oArticle->getStdLink(), $sSeoUrl, $iLang, null, 0, '', '', $sActManufacturerId );
00237 }
00238 }
00239
00240 stopProfile(__FUNCTION__);
00241
00242 return $sSeoUrl;
00243 }
00253 public function getArticleUrl( $oArticle, $iLang = null, $iType = 0 )
00254 {
00255 if (!isset($iLang)) {
00256 $iLang = $oArticle->getLanguage();
00257 }
00258
00259 $sUri = '';
00260 switch ( $iType ) {
00261 case 1 :
00262 $sUri = $this->_getArticleVendorUri( $oArticle, $iLang );
00263 break;
00264 case 2 :
00265 $sUri = $this->_getArticleManufacturerUri( $oArticle, $iLang );
00266 break;
00267 default:
00268 $sUri = $this->_getArticleUri( $oArticle, $iLang );
00269 break;
00270 }
00271
00272 return $this->_getFullUrl( $sUri, $iLang );
00273
00274 }
00275
00284 public function getSeoCategories( $oArticle, $iLang = null)
00285 {
00286 if (!isset($iLang)) {
00287 $iLang = $oArticle->getLanguage();
00288 }
00289 $sArtId = $oArticle->getId();
00290 if ( isset( $oArticle->oxarticles__oxparentid->value ) && $oArticle->oxarticles__oxparentid->value ) {
00291 $sArtId = $oArticle->oxarticles__oxparentid->value;
00292 }
00293
00294
00295 $oDb = oxDb::getDb( false );
00296 $sCatTable = getViewName('oxcategories');
00297
00298 $sQ = "select distinct catroots.oxrootid
00299 from oxobject2category as o2c
00300 left join {$sCatTable} as catroots
00301 on o2c.oxcatnid=catroots.oxid
00302 where o2c.oxobjectid = '$sArtId'";
00303
00304 $aRoots = $oDb->getAll($sQ);
00305
00306 $oList = oxNew('oxList', 'oxcategory');
00307 foreach ($aRoots as $aRootId) {
00308 $sQ = "select node.* _depth from
00309 ( select oxcatnid from oxobject2category
00310 where oxobjectid = '$sArtId' order by oxtime
00311 ) as sub
00312 left join {$sCatTable} as node
00313 on sub.oxcatnid=node.oxid
00314 join {$sCatTable} as parent
00315 on node.oxrootid = parent.oxrootid
00316 where node.oxrootid = '{$aRootId[0]}'
00317 and node.oxleft between parent.oxleft and parent.oxright
00318 group by node.oxid order by (count( parent.oxid ) ) desc limit 1";
00319
00320 $oCat = oxNew('oxcategory');
00321 $oCat->setLanguage($iLang);
00322 if ($oCat->assignRecord($sQ)) {
00323 $oList[] = $oCat;
00324 }
00325 }
00326 return $oList;
00327 }
00328
00336 public function onDeleteArticle($oArticle)
00337 {
00338 $sId = oxDb::getDb()->quote($oArticle->getId());
00339 oxDb::getDb()->execute("delete from oxseo where oxobjectid = $sId and oxtype = 'oxarticle'");
00340 }
00341 }