oxseoencoderarticle.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class oxSeoEncoderArticle extends oxSeoEncoder
00008 {
00009 
00015     protected static $_aTitleCache = array();
00016 
00022     protected function _getUrlExtension()
00023     {
00024         return '.html';
00025     }
00026 
00036     protected function _getProductForLang($oArticle, $iLang)
00037     {
00038         if (isset($iLang) && $iLang != $oArticle->getLanguage()) {
00039             $sId = $oArticle->getId();
00040             $oArticle = oxNew('oxArticle');
00041             $oArticle->setSkipAssign(true);
00042             $oArticle->loadInLang($iLang, $sId);
00043         }
00044 
00045         return $oArticle;
00046     }
00047 
00056     public function getArticleRecommUri($oArticle, $iLang)
00057     {
00058         $sSeoUri = null;
00059         if ($oRecomm = $this->_getRecomm($oArticle, $iLang)) {
00060             //load details link from DB
00061             if (!($sSeoUri = $this->_loadFromDb('oxarticle', $oArticle->getId(), $iLang, null, $oRecomm->getId(), true))) {
00062 
00063                 $oArticle = $this->_getProductForLang($oArticle, $iLang);
00064 
00065                 // create title part for uri
00066                 $sTitle = $this->_prepareArticleTitle($oArticle);
00067 
00068                 // create uri for all categories
00069                 $sSeoUri = oxRegistry::get("oxSeoEncoderRecomm")->getRecommUri($oRecomm, $iLang);
00070                 $sSeoUri = $this->_processSeoUrl($sSeoUri . $sTitle, $oArticle->getId(), $iLang);
00071 
00072                 $aStdParams = array('recommid' => $oRecomm->getId(), 'listtype' => $this->_getListType());
00073                 $this->_saveToDb(
00074                     'oxarticle',
00075                     $oArticle->getId(),
00076                     oxRegistry::get("oxUtilsUrl")->appendUrl(
00077                         $oArticle->getBaseStdLink($iLang),
00078                         $aStdParams
00079                     ),
00080                     $sSeoUri,
00081                     $iLang,
00082                     null,
00083                     0,
00084                     $oRecomm->getId()
00085                 );
00086             }
00087         }
00088 
00089         return $sSeoUri;
00090     }
00091 
00100     protected function _getRecomm($oArticle, $iLang)
00101     {
00102         $oList = null;
00103         $oView = $this->getConfig()->getActiveView();
00104         if ($oView instanceof oxView) {
00105             $oList = $oView->getActiveRecommList();
00106         }
00107 
00108         return $oList;
00109     }
00110 
00116     protected function _getListType()
00117     {
00118         return $this->getConfig()->getActiveView()->getListType();
00119     }
00120 
00130     public function getArticleTagUri($oArticle, $iLang, $blRegenerate = false)
00131     {
00132         $sSeoUri = null;
00133         if ($sTag = $this->_getTag($oArticle, $iLang)) {
00134             $iShopId = $this->getConfig()->getShopId();
00135 
00136             $oArticleTags = oxNew('oxArticleTagList');
00137             $oArticleTags->setArticleId($oArticle->getId());
00138             $oArticleTags->getStdTagLink($sTag);
00139 
00140             $sStdUrl = $oArticleTags->getStdTagLink($sTag);
00141             if ($blRegenerate || !($sSeoUri = $this->_loadFromDb('dynamic', $this->getDynamicObjectId($iShopId, $sStdUrl), $iLang))) {
00142                 // generating new if not found
00143                 if ($sSeoUri = oxRegistry::get("oxSeoEncoderTag")->getTagUri($sTag, $iLang, $oArticle->getId())) {
00144                     $sSeoUri .= $this->_prepareArticleTitle($oArticle);
00145                     $sSeoUri = $this->_processSeoUrl($sSeoUri, $this->_getStaticObjectId($iShopId, $sStdUrl), $iLang);
00146                     $sSeoUri = $this->_getDynamicUri($sStdUrl, $sSeoUri, $iLang);
00147                 }
00148             }
00149         }
00150 
00151         return $sSeoUri;
00152     }
00153 
00162     protected function _getTag($oArticle, $iLang)
00163     {
00164         $sTag = null;
00165         $oView = $this->getConfig()->getTopActiveView();
00166         if ($oView instanceof oxView) {
00167             $sTag = $oView->getTag();
00168         }
00169 
00170         return $sTag;
00171     }
00172 
00182     protected function _createArticleCategoryUri($oArticle, $oCategory, $iLang)
00183     {
00184         startProfile(__FUNCTION__);
00185         $oArticle = $this->_getProductForLang($oArticle, $iLang);
00186 
00187         // create title part for uri
00188         $sTitle = $this->_prepareArticleTitle($oArticle);
00189 
00190         // writing category path
00191         $sSeoUri = $this->_processSeoUrl(
00192             oxRegistry::get("oxSeoEncoderCategory")->getCategoryUri($oCategory, $iLang) . $sTitle,
00193             $oArticle->getId(), $iLang
00194         );
00195         $sCatId = $oCategory->getId();
00196         $this->_saveToDb(
00197             'oxarticle',
00198             $oArticle->getId(),
00199             oxRegistry::get("oxUtilsUrl")->appendUrl(
00200                 $oArticle->getBaseStdLink($iLang),
00201                 array('cnid' => $sCatId)
00202             ),
00203             $sSeoUri,
00204             $iLang,
00205             null,
00206             0,
00207             $sCatId
00208         );
00209 
00210         stopProfile(__FUNCTION__);
00211 
00212         return $sSeoUri;
00213     }
00214 
00224     public function getArticleUri($oArticle, $iLang, $blRegenerate = false)
00225     {
00226         startProfile(__FUNCTION__);
00227 
00228         $sActCatId = '';
00229 
00230         $oActCat = $this->_getCategory($oArticle, $iLang);
00231 
00232         if ($oActCat instanceof oxCategory) {
00233             $sActCatId = $oActCat->getId();
00234         } elseif ($oActCat = $this->_getMainCategory($oArticle)) {
00235             $sActCatId = $oActCat->getId();
00236         }
00237 
00238         //load details link from DB
00239         if ($blRegenerate || !($sSeoUri = $this->_loadFromDb('oxarticle', $oArticle->getId(), $iLang, null, $sActCatId, true))) {
00240             if ($oActCat) {
00241                 $blInCat = false;
00242                 if ($oActCat->isPriceCategory()) {
00243                     $blInCat = $oArticle->inPriceCategory($sActCatId);
00244                 } else {
00245                     $blInCat = $oArticle->inCategory($sActCatId);
00246                 }
00247                 if ($blInCat) {
00248                     $sSeoUri = $this->_createArticleCategoryUri($oArticle, $oActCat, $iLang);
00249                 }
00250             }
00251         }
00252 
00253         stopProfile(__FUNCTION__);
00254 
00255         return $sSeoUri;
00256     }
00257 
00266     protected function _getCategory($oArticle, $iLang)
00267     {
00268         $oCat = null;
00269         $oView = $this->getConfig()->getActiveView();
00270         if ($oView instanceof oxUBase) {
00271             $oCat = $oView->getActiveCategory();
00272         } elseif ($oView instanceof oxView) {
00273             $oCat = $oView->getActCategory();
00274         }
00275 
00276         return $oCat;
00277     }
00278 
00286     protected function _getMainCategory($oArticle)
00287     {
00288         $oMainCat = null;
00289 
00290         // if variant parent id must be used
00291         $sArtId = $oArticle->getId();
00292         if (isset($oArticle->oxarticles__oxparentid->value) && $oArticle->oxarticles__oxparentid->value) {
00293             $sArtId = $oArticle->oxarticles__oxparentid->value;
00294         }
00295 
00296         $oDb = oxDb::getDb();
00297         // add main category caching;
00298         $sQ = "select oxcatnid from " . getViewName("oxobject2category") . " where oxobjectid = " . $oDb->quote($sArtId) . " order by oxtime";
00299         $sIdent = md5($sQ);
00300 
00301         if (($sMainCatId = $this->_loadFromCache($sIdent, "oxarticle")) === false) {
00302             $sMainCatId = $oDb->getOne($sQ);
00303             // storing in cache
00304             $this->_saveInCache($sIdent, $sMainCatId, "oxarticle");
00305         }
00306 
00307         if ($sMainCatId) {
00308             $oMainCat = oxNew("oxCategory");
00309             if (!$oMainCat->load($sMainCatId)) {
00310                 $oMainCat = null;
00311             }
00312         }
00313 
00314         return $oMainCat;
00315     }
00316 
00325     public function getArticleMainUri($oArticle, $iLang)
00326     {
00327         startProfile(__FUNCTION__);
00328 
00329         $oMainCat = $this->_getMainCategory($oArticle);
00330         $sMainCatId = $oMainCat ? $oMainCat->getId() : '';
00331 
00332         //load default article url from DB
00333         if (!($sSeoUri = $this->_loadFromDb('oxarticle', $oArticle->getId(), $iLang, null, $sMainCatId, true))) {
00334             // save for main category
00335             if ($oMainCat) {
00336                 $sSeoUri = $this->_createArticleCategoryUri($oArticle, $oMainCat, $iLang);
00337             } else {
00338                 // get default article url
00339                 $oArticle = $this->_getProductForLang($oArticle, $iLang);
00340                 $sSeoUri = $this->_processSeoUrl($this->_prepareArticleTitle($oArticle), $oArticle->getId(), $iLang);
00341 
00342                 // save default article url
00343                 $this->_saveToDb(
00344                     'oxarticle',
00345                     $oArticle->getId(),
00346                     $oArticle->getBaseStdLink($iLang),
00347                     $sSeoUri,
00348                     $iLang,
00349                     null,
00350                     0,
00351                     ''
00352                 );
00353             }
00354         }
00355 
00356         stopProfile(__FUNCTION__);
00357 
00358         return $sSeoUri;
00359     }
00360 
00369     protected function _prepareArticleTitle($oArticle)
00370     {
00371         $sTitle = '';
00372 
00373         // create title part for uri
00374         if (!($sTitle = $oArticle->oxarticles__oxtitle->value)) {
00375             // taking parent article title
00376             if (($sParentId = $oArticle->oxarticles__oxparentid->value)) {
00377 
00378                 // looking in cache ..
00379                 if (!isset(self::$_aTitleCache[$sParentId])) {
00380                     $oDb = oxDb::getDb();
00381                     $sQ = "select oxtitle from " . $oArticle->getViewName() . " where oxid = " . $oDb->quote($sParentId);
00382                     self::$_aTitleCache[$sParentId] = $oDb->getOne($sQ);
00383                 }
00384                 $sTitle = self::$_aTitleCache[$sParentId];
00385             }
00386         }
00387 
00388         // variant has varselect value
00389         if ($oArticle->oxarticles__oxvarselect->value) {
00390             $sTitle .= ($sTitle ? ' ' : '') . $oArticle->oxarticles__oxvarselect->value . ' ';
00391         } elseif (!$sTitle || ($oArticle->oxarticles__oxparentid->value)) {
00392             // in case nothing was found - looking for number
00393             $sTitle .= ($sTitle ? ' ' : '') . $oArticle->oxarticles__oxartnum->value;
00394         }
00395 
00396         return $this->_prepareTitle($sTitle, false, $oArticle->getLanguage()) . '.html';
00397     }
00398 
00408     public function getArticleVendorUri($oArticle, $iLang, $blRegenerate = false)
00409     {
00410         startProfile(__FUNCTION__);
00411 
00412         $sSeoUri = null;
00413         if ($oVendor = $this->_getVendor($oArticle, $iLang)) {
00414             //load details link from DB
00415             if ($blRegenerate || !($sSeoUri = $this->_loadFromDb('oxarticle', $oArticle->getId(), $iLang, null, $oVendor->getId(), true))) {
00416 
00417                 $oArticle = $this->_getProductForLang($oArticle, $iLang);
00418 
00419                 // create title part for uri
00420                 $sTitle = $this->_prepareArticleTitle($oArticle);
00421 
00422                 // create uri for all categories
00423                 $sSeoUri = oxRegistry::get("oxSeoEncoderVendor")->getVendorUri($oVendor, $iLang);
00424                 $sSeoUri = $this->_processSeoUrl($sSeoUri . $sTitle, $oArticle->getId(), $iLang);
00425 
00426                 $aStdParams = array('cnid' => "v_" . $oVendor->getId(), 'listtype' => $this->_getListType());
00427                 $this->_saveToDb(
00428                     'oxarticle',
00429                     $oArticle->getId(),
00430                     oxRegistry::get("oxUtilsUrl")->appendUrl(
00431                         $oArticle->getBaseStdLink($iLang),
00432                         $aStdParams
00433                     ),
00434                     $sSeoUri,
00435                     $iLang,
00436                     null,
00437                     0,
00438                     $oVendor->getId()
00439                 );
00440             }
00441 
00442             stopProfile(__FUNCTION__);
00443         }
00444 
00445         return $sSeoUri;
00446     }
00447 
00456     protected function _getVendor($oArticle, $iLang)
00457     {
00458         $oView = $this->getConfig()->getActiveView();
00459 
00460         $oVendor = null;
00461         if ($sActVendorId = $oArticle->oxarticles__oxvendorid->value) {
00462             if ($oView instanceof oxView && ($oActVendor = $oView->getActVendor())) {
00463                 $oVendor = $oActVendor;
00464             } else {
00465                 $oVendor = oxNew("oxVendor");
00466             }
00467             if ($oVendor->getId() !== $sActVendorId) {
00468                 $oVendor = oxNew("oxVendor");
00469                 if (!$oVendor->loadInLang($iLang, $sActVendorId)) {
00470                     $oVendor = null;
00471                 }
00472             }
00473         }
00474 
00475         return $oVendor;
00476     }
00477 
00487     public function getArticleManufacturerUri($oArticle, $iLang, $blRegenerate = false)
00488     {
00489         $sSeoUri = null;
00490         startProfile(__FUNCTION__);
00491         if ($oManufacturer = $this->_getManufacturer($oArticle, $iLang)) {
00492             //load details link from DB
00493             if ($blRegenerate || !($sSeoUri = $this->_loadFromDb('oxarticle', $oArticle->getId(), $iLang, null, $oManufacturer->getId(), true))) {
00494 
00495                 $oArticle = $this->_getProductForLang($oArticle, $iLang);
00496 
00497                 // create title part for uri
00498                 $sTitle = $this->_prepareArticleTitle($oArticle);
00499 
00500                 // create uri for all categories
00501                 $sSeoUri = oxRegistry::get("oxSeoEncoderManufacturer")->getManufacturerUri($oManufacturer, $iLang);
00502                 $sSeoUri = $this->_processSeoUrl($sSeoUri . $sTitle, $oArticle->getId(), $iLang);
00503 
00504                 $aStdParams = array('mnid' => $oManufacturer->getId(), 'listtype' => $this->_getListType());
00505                 $this->_saveToDb(
00506                     'oxarticle',
00507                     $oArticle->getId(),
00508                     oxRegistry::get("oxUtilsUrl")->appendUrl(
00509                         $oArticle->getBaseStdLink($iLang),
00510                         $aStdParams
00511                     ),
00512                     $sSeoUri,
00513                     $iLang,
00514                     null,
00515                     0,
00516                     $oManufacturer->getId()
00517                 );
00518             }
00519 
00520             stopProfile(__FUNCTION__);
00521         }
00522 
00523         return $sSeoUri;
00524     }
00525 
00534     protected function _getManufacturer($oArticle, $iLang)
00535     {
00536         $oManufacturer = null;
00537         if ($sActManufacturerId = $oArticle->oxarticles__oxmanufacturerid->value) {
00538             $oView = $this->getConfig()->getActiveView();
00539 
00540             if ($oView instanceof oxView && ($oActManufacturer = $oView->getActManufacturer())) {
00541                 $oManufacturer = $oActManufacturer;
00542             } else {
00543                 $oManufacturer = oxNew("oxManufacturer");
00544             }
00545 
00546             if ($oManufacturer->getId() !== $sActManufacturerId || $oManufacturer->getLanguage() != $iLang) {
00547                 $oManufacturer = oxNew("oxManufacturer");
00548                 if (!$oManufacturer->loadInLang($iLang, $sActManufacturerId)) {
00549                     $oManufacturer = null;
00550                 }
00551             }
00552         }
00553 
00554         return $oManufacturer;
00555     }
00556 
00565     public function getArticleMainUrl($oArticle, $iLang = null)
00566     {
00567         if (!isset($iLang)) {
00568             $iLang = $oArticle->getLanguage();
00569         }
00570 
00571         return $this->_getFullUrl($this->getArticleMainUri($oArticle, $iLang), $iLang);
00572     }
00573 
00583     public function getArticleUrl($oArticle, $iLang = null, $iType = 0)
00584     {
00585         if (!isset($iLang)) {
00586             $iLang = $oArticle->getLanguage();
00587         }
00588 
00589         $sUri = null;
00590         switch ($iType) {
00591             case OXARTICLE_LINKTYPE_VENDOR:
00592                 $sUri = $this->getArticleVendorUri($oArticle, $iLang);
00593                 break;
00594             case OXARTICLE_LINKTYPE_MANUFACTURER:
00595                 $sUri = $this->getArticleManufacturerUri($oArticle, $iLang);
00596                 break;
00597             case OXARTICLE_LINKTYPE_TAG:
00598                 $sUri = $this->getArticleTagUri($oArticle, $iLang);
00599                 break;
00600             case OXARTICLE_LINKTYPE_RECOMM:
00601                 $sUri = $this->getArticleRecommUri($oArticle, $iLang);
00602                 break;
00603             case OXARTICLE_LINKTYPE_PRICECATEGORY: // goes price category urls to default (category urls)
00604             default:
00605                 $sUri = $this->getArticleUri($oArticle, $iLang);
00606                 break;
00607         }
00608 
00609         // if was unable to fetch type uri - returning main
00610         if (!$sUri) {
00611             $sUri = $this->getArticleMainUri($oArticle, $iLang);
00612         }
00613 
00614         return $this->_getFullUrl($sUri, $iLang);
00615     }
00616 
00622     public function onDeleteArticle($oArticle)
00623     {
00624         $oDb = oxDb::getDb();
00625         $sIdQuoted = $oDb->quote($oArticle->getId());
00626         $oDb->execute("delete from oxseo where oxobjectid = $sIdQuoted and oxtype = 'oxarticle'");
00627         $oDb->execute("delete from oxobject2seodata where oxobjectid = $sIdQuoted");
00628     }
00629 
00638     protected function _getAltUri($sObjectId, $iLang)
00639     {
00640         $sSeoUrl = null;
00641         $oArticle = oxNew("oxArticle");
00642         $oArticle->setSkipAssign(true);
00643         if ($oArticle->loadInLang($iLang, $sObjectId)) {
00644             // choosing URI type to generate
00645             switch ($this->_getListType()) {
00646                 case 'vendor':
00647                     $sSeoUrl = $this->getArticleVendorUri($oArticle, $iLang, true);
00648                     break;
00649                 case 'manufacturer':
00650                     $sSeoUrl = $this->getArticleManufacturerUri($oArticle, $iLang, true);
00651                     break;
00652                 case 'tag':
00653                     $sSeoUrl = $this->getArticleTagUri($oArticle, $iLang, true);
00654                     break;
00655                 default:
00656                     $sSeoUrl = $this->getArticleUri($oArticle, $iLang, true);
00657                     break;
00658             }
00659         }
00660 
00661         return $sSeoUrl;
00662     }
00663 }