oxseoencoderarticle.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class oxSeoEncoderArticle extends oxSeoEncoder
00008 {
00012     protected static $_instance = null;
00013 
00019     public static function getInstance()
00020     {
00021         if (!self::$_instance) {
00022             self::$_instance = oxNew("oxSeoEncoderArticle");
00023         }
00024         return self::$_instance;
00025     }
00026 
00035     protected function _getArticleUri( $oArticle, $iLang = null)
00036     {
00037         startProfile(__FUNCTION__);
00038         if (!isset($iLang)) {
00039             $iLang = $oArticle->getLanguage();
00040         }
00041 
00042         $sActCatId = '';
00043         $oView = $this->getConfig()->getActiveView();
00044         $oActCat = null;
00045 
00046         if ( $oView instanceof oxUBase ) {
00047             $oActCat = $oView->getActCategory();
00048         }
00049 
00050         if ( $oActCat ) {
00051             $sActCatId = $oActCat->oxcategories__oxrootid->value;
00052         }
00053 
00054         //load details link from DB
00055         if ( !( $sSeoUrl = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $sActCatId, false ) ) ) {
00056 
00057             if ($iLang != $oArticle->getLanguage()) {
00058                 $sId = $oArticle->getId();
00059                 $oArticle = oxNew('oxarticle');
00060                 $oArticle->loadInLang($iLang, $sId);
00061             }
00062 
00063             // create title part for uri
00064             $sTitle = $this->_prepareArticleTitle( $oArticle );
00065 
00066             // create uri for all categories
00067             $oCategorys = $this->getSeoCategories( $oArticle, $iLang );
00068             if (!$oCategorys->count()) {
00069                 $sSeoUrl = $this->_getUniqueSeoUrl( $sTitle, '.html', $oArticle->getId(), $iLang );
00070                 $this->_saveToDb( 'oxarticle', $oArticle->getId(), $oArticle->getStdLink(), $sSeoUrl, $iLang );
00071             } else {
00072                 $sTmpSeoUrl = '';
00073                 $oEncoder = oxSeoEncoderCategory::getInstance();
00074                 foreach ($oCategorys as $oCategory) {
00075                     // writing category path
00076                     $sTmpSeoUrl = $oEncoder->getCategoryUri( $oCategory );
00077                     $sTmpSeoUrl .= $sTitle;
00078                     $sTmpSeoUrl  = $this->_getUniqueSeoUrl( $sTmpSeoUrl, '.html', $oArticle->getId(), $iLang );
00079 
00080                     $this->_saveToDb( 'oxarticle', $oArticle->getId(), $oArticle->getStdLink(), $sTmpSeoUrl, $iLang, null, 0, '', '', $oCategory->oxcategories__oxrootid->value);
00081                     if ($oCategory->oxcategories__oxrootid->value == $sActCatId) {
00082                         $sSeoUrl = $sTmpSeoUrl;
00083                     }
00084                 }
00085                 if (!$sSeoUrl) {
00086                     // seo url not found, use any
00087                     $sSeoUrl = $sTmpSeoUrl;
00088                 }
00089             }
00090         }
00091 
00092         stopProfile(__FUNCTION__);
00093 
00094         return $sSeoUrl;
00095     }
00096 
00105     protected function _prepareArticleTitle( $oArticle )
00106     {
00107         // create title part for uri
00108         if ( !( $sTitle = $oArticle->oxarticles__oxtitle->value ) ) {
00109             $sTitle = $oArticle->oxarticles__oxartnum->value;
00110         }
00111 
00112         // variant has varselect value
00113         if ( $oArticle->oxarticles__oxvarselect->value ) {
00114             $sTitle .= ' ' . $oArticle->oxarticles__oxvarselect->value;
00115         }
00116         return $this->_prepareTitle( $sTitle . '.html' );
00117     }
00118 
00127     protected function _getArticleVendorUri( $oArticle, $iLang = null )
00128     {
00129         startProfile(__FUNCTION__);
00130         if ( !isset( $iLang ) ) {
00131             $iLang = $oArticle->getLanguage();
00132         }
00133 
00134         $sActVendorId = $oArticle->oxarticles__oxvendorid->value;
00135         $oVendor = oxNew( 'oxvendor' );
00136         if ( !$sActVendorId || !$oVendor->load( $sActVendorId ) ) {
00137             $oVendor = null;
00138         }
00139 
00140         //load details link from DB
00141         if ( !( $sSeoUrl = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $sActVendorId, true ) ) ) {
00142 
00143             if ( $iLang != $oArticle->getLanguage() ) {
00144                 $sId = $oArticle->getId();
00145                 $oArticle = oxNew('oxarticle');
00146                 $oArticle->loadInLang( $iLang, $sId );
00147             }
00148 
00149             // create title part for uri
00150             $sTitle = $this->_prepareArticleTitle( $oArticle );
00151 
00152             // create uri for all categories
00153             if ( !$sActVendorId || !$oVendor ) {
00154                 $sSeoUrl = $this->_getUniqueSeoUrl( $sTitle, '.html', $oArticle->getId(), $iLang );
00155                 $this->_saveToDb( 'oxarticle', $oArticle->getId(), $oArticle->getStdLink(), $sSeoUrl, $iLang );
00156             } else {
00157                 $sSeoUrl = oxSeoEncoderVendor::getInstance()->getVendorUri( $oVendor, $iLang );
00158                 $sSeoUrl = $this->_getUniqueSeoUrl( $sSeoUrl . $sTitle, '.html', $oArticle->getId(), $iLang );
00159 
00160                 $this->_saveToDb( 'oxarticle', $oArticle->getId(), $oArticle->getStdLink(), $sSeoUrl, $iLang, null, 0, '', '', $sActVendorId );
00161             }
00162         }
00163 
00164         stopProfile(__FUNCTION__);
00165 
00166         return $sSeoUrl;
00167     }
00168 
00177     protected function _getArticleManufacturerUri( $oArticle, $iLang = null )
00178     {
00179         startProfile(__FUNCTION__);
00180         if ( !isset( $iLang ) ) {
00181             $iLang = $oArticle->getLanguage();
00182         }
00183 
00184         $sActManufacturerId = $oArticle->oxarticles__oxmanufacturerid->value;
00185         $oManufacturer = oxNew( 'oxmanufacturer' );
00186         if ( !$sActManufacturerId || !$oManufacturer->load( $sActManufacturerId ) ) {
00187             $oManufacturer = null;
00188         }
00189 
00190         //load details link from DB
00191         if ( !( $sSeoUrl = $this->_loadFromDb( 'oxarticle', $oArticle->getId(), $iLang, null, $sActManufacturerId, true ) ) ) {
00192 
00193             if ( $iLang != $oArticle->getLanguage() ) {
00194                 $sId = $oArticle->getId();
00195                 $oArticle = oxNew('oxarticle');
00196                 $oArticle->loadInLang( $iLang, $sId );
00197             }
00198 
00199             // create title part for uri
00200             $sTitle = $this->_prepareArticleTitle( $oArticle );
00201 
00202             // create uri for all categories
00203             if ( !$sActManufacturerId || !$oManufacturer ) {
00204                 $sSeoUrl = $this->_getUniqueSeoUrl( $sTitle, '.html', $oArticle->getId(), $iLang );
00205                 $this->_saveToDb( 'oxarticle', $oArticle->getId(), $oArticle->getStdLink(), $sSeoUrl, $iLang );
00206             } else {
00207                 $sSeoUrl = oxSeoEncoderManufacturer::getInstance()->getManufacturerUri( $oManufacturer, $iLang );
00208                 $sSeoUrl = $this->_getUniqueSeoUrl( $sSeoUrl . $sTitle, '.html', $oArticle->getId(), $iLang );
00209 
00210                 $this->_saveToDb( 'oxarticle', $oArticle->getId(), $oArticle->getStdLink(), $sSeoUrl, $iLang, null, 0, '', '', $sActManufacturerId );
00211             }
00212         }
00213 
00214         stopProfile(__FUNCTION__);
00215 
00216         return $sSeoUrl;
00217     }
00227     public function getArticleUrl( $oArticle, $iLang = null, $iType = 0 )
00228     {
00229         if (!isset($iLang)) {
00230             $iLang = $oArticle->getLanguage();
00231         }
00232 
00233         $sUri = '';
00234         switch ( $iType ) {
00235             case 1 :
00236                 $sUri = $this->_getArticleVendorUri( $oArticle, $iLang );
00237                 break;
00238             case 2 :
00239                 $sUri = $this->_getArticleManufacturerUri( $oArticle, $iLang );
00240                 break;
00241             default:
00242                 $sUri = $this->_getArticleUri( $oArticle, $iLang );
00243         }
00244 
00245         return $this->_getFullUrl( $sUri, $iLang );
00246 
00247     }
00248 
00257     public function getSeoCategories( $oArticle, $iLang = null)
00258     {
00259         if (!isset($iLang)) {
00260             $iLang = $oArticle->getLanguage();
00261         }
00262         $sArtId = $oArticle->getId();
00263         if ( isset( $oArticle->oxarticles__oxparentid->value ) && $oArticle->oxarticles__oxparentid->value ) {
00264             $sArtId = $oArticle->oxarticles__oxparentid->value;
00265         }
00266 
00267         // checking cache
00268         $oDb = oxDb::getDb( false );
00269         $sCatTable = getViewName('oxcategories');
00270 
00271         $sQ = "select distinct catroots.oxrootid
00272                 from oxobject2category as o2c
00273                 left join {$sCatTable} as catroots
00274                     on o2c.oxcatnid=catroots.oxid
00275                 where o2c.oxobjectid = '$sArtId'";
00276 
00277         $aRoots = $oDb->getAll($sQ);
00278 
00279         $oList = oxNew('oxList', 'oxcategory');
00280         foreach ($aRoots as $aRootId) {
00281             $sQ = "select node.* _depth from
00282                     ( select oxcatnid from oxobject2category
00283                             where oxobjectid = '$sArtId' order by oxtime
00284                         ) as sub
00285                         left join {$sCatTable} as node
00286                             on sub.oxcatnid=node.oxid
00287                         join {$sCatTable} as parent
00288                             on node.oxrootid = parent.oxrootid
00289                     where node.oxrootid = '{$aRootId[0]}'
00290                         and node.oxleft between parent.oxleft and parent.oxright
00291                 group by node.oxid order by (count( parent.oxid ) ) desc limit 1";
00292 
00293             $oCat = oxNew('oxcategory');
00294             $oCat->setLanguage($iLang);
00295             if ($oCat->assignRecord($sQ)) {
00296                 $oList[] = $oCat;
00297             }
00298         }
00299         return $oList;
00300     }
00301 
00309     public function onDeleteArticle($oArticle)
00310     {
00311         $sId = oxDb::getDb()->quote($oArticle->getId());
00312         oxDb::getDb()->execute("delete from oxseo where oxobjectid = $sId and oxtype = 'oxarticle'");
00313     }
00314 }

Generated on Tue Apr 21 15:45:45 2009 for OXID eShop CE by  doxygen 1.5.5