oxseoencodercategory.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class oxSeoEncoderCategory extends oxSeoEncoder
00008 {
00009 
00016     protected $_aCatCache = array();
00017 
00023     protected function _getUrlExtension()
00024     {
00025         return '/';
00026     }
00027 
00039     protected function _categoryUrlLoader($oCat, $iLang)
00040     {
00041         $sSeoUrl = false;
00042 
00043         $sCacheId = $this->_getCategoryCacheId($oCat, $iLang);
00044         if (isset($this->_aCatCache[$sCacheId])) {
00045             $sSeoUrl = $this->_aCatCache[$sCacheId];
00046         } elseif (($sSeoUrl = $this->_loadFromDb('oxcategory', $oCat->getId(), $iLang))) {
00047             // caching
00048             $this->_aCatCache[$sCacheId] = $sSeoUrl;
00049         }
00050 
00051         return $sSeoUrl;
00052     }
00053 
00064     private function _getCategoryCacheId($oCat, $iLang)
00065     {
00066         return $oCat->getId() . '_' . ((int) $iLang);
00067     }
00068 
00078     public function getCategoryUri($oCat, $iLang = null, $blRegenerate = false)
00079     {
00080         startProfile(__FUNCTION__);
00081         $sCatId = $oCat->getId();
00082 
00083         // skipping external category URLs
00084         if ($oCat->oxcategories__oxextlink->value) {
00085             $sSeoUrl = null;
00086         } else {
00087             // not found in cache, process it from the top
00088             if (!isset($iLang)) {
00089                 $iLang = $oCat->getLanguage();
00090             }
00091 
00092             $aCacheMap = array();
00093             $aStdLinks = array();
00094 
00095             while ($oCat && !($sSeoUrl = $this->_categoryUrlLoader($oCat, $iLang))) {
00096 
00097                 if ($iLang != $oCat->getLanguage()) {
00098                     $sId = $oCat->getId();
00099                     $oCat = oxNew('oxcategory');
00100                     $oCat->loadInLang($iLang, $sId);
00101                 }
00102 
00103                 // prepare oCat title part
00104                 $sTitle = $this->_prepareTitle($oCat->oxcategories__oxtitle->value, false, $oCat->getLanguage());
00105 
00106                 foreach (array_keys($aCacheMap) as $id) {
00107                     $aCacheMap[$id] = $sTitle . '/' . $aCacheMap[$id];
00108                 }
00109 
00110                 $aCacheMap[$oCat->getId()] = $sTitle;
00111                 $aStdLinks[$oCat->getId()] = $oCat->getBaseStdLink($iLang);
00112 
00113                 // load parent
00114                 $oCat = $oCat->getParentCategory();
00115             }
00116 
00117             foreach ($aCacheMap as $sId => $sUri) {
00118                 $this->_aCatCache[$sId . '_' . $iLang] = $this->_processSeoUrl($sSeoUrl . $sUri . '/', $sId, $iLang);
00119                 $this->_saveToDb('oxcategory', $sId, $aStdLinks[$sId], $this->_aCatCache[$sId . '_' . $iLang], $iLang);
00120             }
00121 
00122             $sSeoUrl = $this->_aCatCache[$sCatId . '_' . $iLang];
00123         }
00124 
00125         stopProfile(__FUNCTION__);
00126 
00127         return $sSeoUrl;
00128     }
00129 
00130 
00141     public function getCategoryPageUrl($oCategory, $iPage, $iLang = null, $blFixed = null)
00142     {
00143         if (!isset($iLang)) {
00144             $iLang = $oCategory->getLanguage();
00145         }
00146         $sStdUrl = $oCategory->getBaseStdLink($iLang) . '&amp;pgNr=' . $iPage;
00147         $sParams = (int) ($iPage + 1);
00148 
00149         $sStdUrl = $this->_trimUrl($sStdUrl, $iLang);
00150         $sSeoUrl = $this->getCategoryUri($oCategory, $iLang) . $sParams . "/";
00151 
00152         if ($blFixed === null) {
00153             $blFixed = $this->_isFixed('oxcategory', $oCategory->getId(), $iLang);
00154         }
00155 
00156         return $this->_getFullUrl($this->_getPageUri($oCategory, 'oxcategory', $sStdUrl, $sSeoUrl, $sParams, $iLang, $blFixed), $iLang);
00157     }
00158 
00170     public function getCategoryUrl($oCategory, $iLang = null)
00171     {
00172         $sUrl = '';
00173         if (!isset($iLang)) {
00174             $iLang = $oCategory->getLanguage();
00175         }
00176         // category may have specified url
00177         if (($sSeoUrl = $this->getCategoryUri($oCategory, $iLang))) {
00178             $sUrl = $this->_getFullUrl($sSeoUrl, $iLang);
00179         }
00180 
00181         return $sUrl;
00182     }
00183 
00189     public function markRelatedAsExpired($oCategory)
00190     {
00191         $oDb = oxDb::getDb();
00192         $sIdQuoted = $oDb->quote($oCategory->getId());
00193 
00194         // select it from table instead of using object carrying value
00195         // this is because this method is usually called inside update,
00196         // where object may already be carrying changed id
00197         $aCatInfo = $oDb->getAll("select oxrootid, oxleft, oxright from oxcategories where oxid = $sIdQuoted limit 1");
00198         $sCatRootIdQuoted = $oDb->quote($aCatInfo[0][0]);
00199 
00200         // update sub cats
00201         $sQ = "update oxseo as seo1, (select oxid from oxcategories where oxrootid={$sCatRootIdQuoted} and oxleft > " . ((int) $aCatInfo[0][1]) . " and oxright < " . ((int) $aCatInfo[0][2]) . ") as seo2 set seo1.oxexpired = '1' where seo1.oxtype = 'oxcategory' and seo1.oxobjectid = seo2.oxid";
00202         $oDb->execute($sQ);
00203 
00204         // update subarticles
00205         $sQ = "update oxseo as seo1, (select o2c.oxobjectid as id from oxcategories as cat left join oxobject2category"
00206               ." as o2c on o2c.oxcatnid=cat.oxid where cat.oxrootid={$sCatRootIdQuoted} and cat.oxleft >= "
00207               .((int) $aCatInfo[0][1] )." and cat.oxright <= ".((int) $aCatInfo[0][2] ).") as seo2 "
00208               ."set seo1.oxexpired = '1' where seo1.oxtype = 'oxarticle' and seo1.oxobjectid = seo2.id "
00209               ."and seo1.oxfixed = 0";
00210 
00211         $oDb->execute($sQ);
00212     }
00213 
00214 
00220     public function onDeleteCategory($oCategory)
00221     {
00222         $oDb = oxDb::getDb();
00223         $sIdQuoted = $oDb->quote($oCategory->getId());
00224         $oDb->execute("update oxseo, (select oxseourl from oxseo where oxobjectid = $sIdQuoted and oxtype = 'oxcategory') as test set oxseo.oxexpired=1 where oxseo.oxseourl like concat(test.oxseourl, '%') and (oxtype = 'oxcategory' or oxtype = 'oxarticle')");
00225         $oDb->execute("delete from oxseo where oxseo.oxtype = 'oxarticle' and oxseo.oxparams = $sIdQuoted");
00226         $oDb->execute("delete from oxseo where oxobjectid = $sIdQuoted and oxtype = 'oxcategory'");
00227         $oDb->execute("delete from oxobject2seodata where oxobjectid = $sIdQuoted");
00228     }
00229 
00238     protected function _getAltUri($sObjectId, $iLang)
00239     {
00240         $sSeoUrl = null;
00241         $oCat = oxNew("oxcategory");
00242         if ($oCat->loadInLang($iLang, $sObjectId)) {
00243             $sSeoUrl = $this->getCategoryUri($oCat, $iLang);
00244         }
00245 
00246         return $sSeoUrl;
00247     }
00248 }