oxseoencodercategory.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class oxSeoEncoderCategory extends oxSeoEncoder
00008 {
00012     protected static $_instance = null;
00013 
00020     protected $_aCatCache = array();
00021 
00029     public static function getInstance()
00030     {
00031         return oxRegistry::get("oxSeoEncoderCategory");
00032     }
00033 
00039     protected function _getUrlExtension()
00040     {
00041         return '/';
00042     }
00043 
00055     protected function _categoryUrlLoader( $oCat, $iLang )
00056     {
00057         $sSeoUrl = false;
00058 
00059         $sCacheId = $this->_getCategoryCacheId( $oCat, $iLang );
00060         if ( isset( $this->_aCatCache[$sCacheId] ) ) {
00061             $sSeoUrl = $this->_aCatCache[ $sCacheId ];
00062         } elseif ( ( $sSeoUrl = $this->_loadFromDb( 'oxcategory', $oCat->getId(), $iLang ) ) ) {
00063             // caching
00064             $this->_aCatCache[ $sCacheId ] = $sSeoUrl;
00065         }
00066 
00067         return $sSeoUrl;
00068     }
00069 
00080     private function _getCategoryCacheId( $oCat, $iLang )
00081     {
00082         return $oCat->getId() . '_' . ( (int) $iLang );
00083     }
00084 
00094     public function getCategoryUri( $oCat, $iLang = null, $blRegenerate = false  )
00095     {
00096         startProfile(__FUNCTION__);
00097         $sCatId = $oCat->getId();
00098 
00099         // skipping external category URLs
00100         if ( $oCat->oxcategories__oxextlink->value ) {
00101             $sSeoUrl = null;
00102         } else {
00103             // not found in cache, process it from the top
00104             if (!isset($iLang)) {
00105                 $iLang = $oCat->getLanguage();
00106             }
00107 
00108             $aCacheMap = array();
00109             $aStdLinks = array();
00110 
00111             while ( $oCat && !($sSeoUrl = $this->_categoryUrlLoader( $oCat, $iLang ) )) {
00112 
00113                 if ($iLang != $oCat->getLanguage()) {
00114                     $sId = $oCat->getId();
00115                     $oCat = oxNew('oxcategory');
00116                     $oCat->loadInLang($iLang, $sId);
00117                 }
00118 
00119                 // prepare oCat title part
00120                 $sTitle = $this->_prepareTitle( $oCat->oxcategories__oxtitle->value, false, $oCat->getLanguage() );
00121 
00122                 foreach ( array_keys( $aCacheMap ) as $id ) {
00123                     $aCacheMap[$id] = $sTitle . '/' . $aCacheMap[$id];
00124                 }
00125 
00126                 $aCacheMap[$oCat->getId()] = $sTitle;
00127                 $aStdLinks[$oCat->getId()] = $oCat->getBaseStdLink($iLang);
00128 
00129                 // load parent
00130                 $oCat = $oCat->getParentCategory();
00131             }
00132 
00133             foreach ( $aCacheMap as $sId => $sUri ) {
00134                 $this->_aCatCache[$sId.'_'.$iLang] = $this->_processSeoUrl( $sSeoUrl.$sUri.'/', $sId, $iLang );
00135                 $this->_saveToDb( 'oxcategory', $sId, $aStdLinks[$sId], $this->_aCatCache[$sId.'_'.$iLang], $iLang );
00136             }
00137 
00138             $sSeoUrl = $this->_aCatCache[$sCatId.'_'.$iLang];
00139         }
00140 
00141         stopProfile(__FUNCTION__);
00142 
00143         return $sSeoUrl;
00144     }
00145 
00146 
00157     public function getCategoryPageUrl( $oCategory, $iPage, $iLang = null, $blFixed = null )
00158     {
00159         if (!isset($iLang)) {
00160             $iLang = $oCategory->getLanguage();
00161         }
00162         $sStdUrl = $oCategory->getBaseStdLink($iLang) . '&amp;pgNr=' . $iPage;
00163         $sParams = (int) ($iPage + 1);
00164 
00165         $sStdUrl = $this->_trimUrl( $sStdUrl, $iLang );
00166         $sSeoUrl = $this->getCategoryUri( $oCategory, $iLang ) . $sParams . "/";
00167 
00168         if ( $blFixed === null ) {
00169             $blFixed = $this->_isFixed( 'oxcategory', $oCategory->getId(), $iLang );
00170         }
00171         return $this->_getFullUrl( $this->_getPageUri( $oCategory, 'oxcategory', $sStdUrl, $sSeoUrl, $sParams, $iLang, $blFixed ), $iLang );
00172     }
00173 
00185     public function getCategoryUrl( $oCategory, $iLang = null )
00186     {
00187         $sUrl = '';
00188         if (!isset($iLang)) {
00189             $iLang = $oCategory->getLanguage();
00190         }
00191         // category may have specified url
00192         if ( ( $sSeoUrl = $this->getCategoryUri( $oCategory, $iLang ) ) ) {
00193             $sUrl = $this->_getFullUrl( $sSeoUrl, $iLang );
00194         }
00195         return $sUrl;
00196     }
00197 
00205     public function markRelatedAsExpired( $oCategory )
00206     {
00207         $oDb = oxDb::getDb();
00208         $sIdQuoted = $oDb->quote($oCategory->getId());
00209 
00210         // select it from table instead of using object carrying value
00211         // this is because this method is usually called inside update,
00212         // where object may already be carrying changed id
00213         $aCatInfo = $oDb->getAll("select oxrootid, oxleft, oxright from oxcategories where oxid = $sIdQuoted limit 1");
00214         $sCatRootIdQuoted = $oDb->quote( $aCatInfo[0][0] );
00215 
00216         // update sub cats
00217         $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";
00218         $oDb->execute( $sQ );
00219 
00220         // update subarticles
00221         $sQ = "update oxseo as seo1, (select o2c.oxobjectid as id from oxcategories as cat left join oxobject2category as o2c on o2c.oxcatnid=cat.oxid where cat.oxrootid={$sCatRootIdQuoted} and cat.oxleft >= ".((int) $aCatInfo[0][1] )." and cat.oxright <= ".((int) $aCatInfo[0][2] ).") as seo2 set seo1.oxexpired = '1' where seo1.oxtype = 'oxarticle' and seo1.oxobjectid = seo2.id";
00222         $oDb->execute( $sQ );
00223     }
00224 
00225 
00233     public function onDeleteCategory( $oCategory )
00234     {
00235         $oDb = oxDb::getDb();
00236         $sIdQuoted = $oDb->quote($oCategory->getId());
00237         $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')");
00238         $oDb->execute("delete from oxseo where oxseo.oxtype = 'oxarticle' and oxseo.oxparams = $sIdQuoted" );
00239         $oDb->execute("delete from oxseo where oxobjectid = $sIdQuoted and oxtype = 'oxcategory'");
00240         $oDb->execute("delete from oxobject2seodata where oxobjectid = $sIdQuoted");
00241     }
00242 
00251     protected function _getAltUri( $sObjectId, $iLang )
00252     {
00253         $sSeoUrl = null;
00254         $oCat = oxNew( "oxcategory" );
00255         if ( $oCat->loadInLang( $iLang, $sObjectId ) ) {
00256             $sSeoUrl = $this->getCategoryUri( $oCat, $iLang );
00257         }
00258         return $sSeoUrl;
00259     }
00260 }