00001 <?php
00002
00008 class oxSeoEncoderArticle extends oxSeoEncoder
00009 {
00013 protected static $_instance = null;
00014
00020 public static function getInstance()
00021 {
00022 if (!self::$_instance) {
00023 self::$_instance = oxNew("oxSeoEncoderArticle");
00024 }
00025 return self::$_instance;
00026 }
00027
00036 protected function _getArticleUri( $oArticle, $iLang = null)
00037 {
00038 startProfile(__FUNCTION__);
00039 if (!isset($iLang)) {
00040 $iLang = $oArticle->getLanguage();
00041 }
00042
00043 $sActCatId = '';
00044 $oView = $this->getConfig()->getActiveView();
00045 $oActCat = null;
00046
00047 if ( $oView instanceof oxUBase ) {
00048 $oActCat = $oView->getActCategory();
00049 }
00050
00051 if ( $oActCat ) {
00052 $sActCatId = $oActCat->oxcategories__oxrootid->value;
00053 }
00054
00055
00056 if ( !( $sSeoUrl = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, $iShopId, $sActCatId, false ) ) ) {
00057
00058 if ($iLang != $oArticle->getLanguage()) {
00059 $sId = $oArticle->getId();
00060 $oArticle = oxNew('oxarticle');
00061 $oArticle->loadInLang($iLang, $sId);
00062 }
00063
00064
00065 $sTitle = $this->_prepareArticleTitle( $oArticle );
00066
00067
00068 $oCategorys = $this->getSeoCategories( $oArticle, $iLang );
00069 if (!$oCategorys->count()) {
00070 $sSeoUrl = $this->_getUniqueSeoUrl( $sTitle, '.html', $oArticle->getId() );
00071 $this->_saveToDb( 'oxarticle', $oArticle->getId(), $oArticle->getStdLink(), $sSeoUrl, $iLang );
00072 } else {
00073 $sTmpSeoUrl = '';
00074 foreach ($oCategorys as $oCategory) {
00075
00076 $sTmpSeoUrl = oxSeoEncoderCategory::getInstance()->getCategoryUri( $oCategory );
00077 $sTmpSeoUrl .= $sTitle;
00078 $sTmpSeoUrl = $this->_getUniqueSeoUrl( $sTmpSeoUrl, '.html', $oArticle->getId() );
00079 $this->_saveToDb( 'oxarticle', $oArticle->getId(), $oArticle->getStdLink(), $sTmpSeoUrl, $iLang, null, 0, '', '', $oCategory->oxcategories__oxrootid->value);
00080 if ($oCategory->oxcategories__oxrootid->value == $sActCatId) {
00081 $sSeoUrl = $sTmpSeoUrl;
00082 }
00083 }
00084 if (!$sSeoUrl) {
00085
00086 $sSeoUrl = $sTmpSeoUrl;
00087 }
00088 }
00089 }
00090
00091 stopProfile(__FUNCTION__);
00092
00093 return $sSeoUrl;
00094 }
00095
00104 protected function _prepareArticleTitle( $oArticle )
00105 {
00106
00107 if ( !( $sTitle = $oArticle->oxarticles__oxtitle->value ) ) {
00108 $sTitle = $oArticle->oxarticles__oxartnum->value;
00109 }
00110
00111
00112 if ( $oArticle->oxarticles__oxvarselect->value ) {
00113 $sTitle .= ' ' . $oArticle->oxarticles__oxvarselect->value;
00114 }
00115 return $this->_prepareTitle( $sTitle . '.html' );
00116 }
00117
00126 protected function _getArticleVendorUri( $oArticle, $iLang = null )
00127 {
00128 startProfile(__FUNCTION__);
00129 if ( !isset( $iLang ) ) {
00130 $iLang = $oArticle->getLanguage();
00131 }
00132
00133 $sActVendorId = $oArticle->oxarticles__oxvendorid->value;
00134 $oVendor = oxNew( 'oxvendor' );
00135 if ( !$sActVendorId || !$oVendor->load( $sActVendorId ) ) {
00136 $oVendor = null;
00137 }
00138
00139
00140 if ( !( $sSeoUrl = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, $iShopId, $sActVendorId, true ) ) ) {
00141
00142 if ( $iLang != $oArticle->getLanguage() ) {
00143 $sId = $oArticle->getId();
00144 $oArticle = oxNew('oxarticle');
00145 $oArticle->loadInLang( $iLang, $sId );
00146 }
00147
00148
00149 $sTitle = $this->_prepareArticleTitle( $oArticle );
00150
00151
00152 if ( !$sActVendorId || !$oVendor ) {
00153 $sSeoUrl = $this->_getUniqueSeoUrl( $sTitle, '.html', $oArticle->getId() );
00154 $this->_saveToDb( 'oxarticle', $oArticle->getId(), $oArticle->getStdLink(), $sSeoUrl, $iLang );
00155 } else {
00156 $sSeoUrl = oxSeoEncoderVendor::getInstance()->getVendorUri( $oVendor, $iLang );
00157 $sSeoUrl = $this->_getUniqueSeoUrl( $sSeoUrl . $sTitle, '.html', $oArticle->getId() );
00158
00159 $this->_saveToDb( 'oxarticle', $oArticle->getId(), $oArticle->getStdLink(), $sSeoUrl, $iLang, null, 0, '', '', $sActVendorId );
00160 }
00161 }
00162
00163 stopProfile(__FUNCTION__);
00164
00165 return $sSeoUrl;
00166 }
00167
00177 public function getArticleUrl( $oArticle, $iLang = null, $iType = 0 )
00178 {
00179 if (!isset($iLang)) {
00180 $iLang = $oArticle->getLanguage();
00181 }
00182
00183 switch ( $iType ) {
00184 case 1 :
00185 $sUri = $this->_getArticleVendorUri( $oArticle, $iLang );
00186 break;
00187 default:
00188 $sUri = $this->_getArticleUri( $oArticle, $iLang );
00189 }
00190
00191 return $this->_getFullUrl( $sUri, $iLang );
00192
00193 }
00194
00203 public function getSeoCategories( $oArticle, $iLang = null)
00204 {
00205 if (!isset($iLang)) {
00206 $iLang = $oArticle->getLanguage();
00207 }
00208 $sArtId = $oArticle->getId();
00209 if ( isset( $oArticle->oxarticles__oxparentid->value ) && $oArticle->oxarticles__oxparentid->value ) {
00210 $sArtId = $oArticle->oxarticles__oxparentid->value;
00211 }
00212
00213
00214 $oDb = oxDb::getDb( false );
00215 $sCatTable = getViewName('oxcategories');
00216
00217 $sQ = "select distinct catroots.oxrootid
00218 from oxobject2category as o2c
00219 left join {$sCatTable} as catroots
00220 on o2c.oxcatnid=catroots.oxid
00221 where o2c.oxobjectid = '$sArtId'";
00222
00223 $aRoots = $oDb->getAll($sQ);
00224
00225 $oList = oxNew('oxList', 'oxcategory');
00226 foreach ($aRoots as $aRootId) {
00227 $sQ = "select node.* _depth from
00228 ( select oxcatnid from oxobject2category
00229 where oxobjectid = '$sArtId' order by oxtime
00230 ) as sub
00231 left join {$sCatTable} as node
00232 on sub.oxcatnid=node.oxid
00233 join {$sCatTable} as parent
00234 on node.oxrootid = parent.oxrootid
00235 where node.oxrootid = '{$aRootId[0]}'
00236 and node.oxleft between parent.oxleft and parent.oxright
00237 group by node.oxid order by (count( parent.oxid ) ) desc limit 1";
00238
00239 $oCat = oxNew('oxcategory');
00240 $oCat->setLanguage($iLang);
00241 if ($oCat->assignRecord($sQ)) {
00242 $oList[] = $oCat;
00243 }
00244 }
00245 return $oList;
00246 }
00247 }