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 
00027     public static function getInstance()
00028     {
00029         if (!self::$_instance) {
00030             self::$_instance = oxNew("oxSeoEncoderCategory");
00031         }
00032 
00033         if ( defined( 'OXID_PHP_UNIT' ) ) {
00034             // resetting cache
00035             self::$_instance->_aSeoCache = array();
00036         }
00037 
00038         return self::$_instance;
00039     }
00040 
00046     protected function _getUrlExtension()
00047     {
00048         return '/';
00049     }
00050 
00062     protected function _categoryUrlLoader( $oCat, $iLang )
00063     {
00064         $sSeoUrl = false;
00065 
00066         $sCacheId = $this->_getCategoryCacheId( $oCat, $iLang );
00067         if ( isset( $this->_aCatCache[$sCacheId] ) ) {
00068             $sSeoUrl = $this->_aCatCache[ $sCacheId ];
00069         } elseif ( ( $sSeoUrl = $this->_loadFromDb( 'oxcategory', $oCat->getId(), $iLang ) ) ) {
00070             // caching
00071             $this->_aCatCache[ $sCacheId ] = $sSeoUrl;
00072         }
00073 
00074         return $sSeoUrl;
00075     }
00076 
00087     private function _getCategoryCacheId( $oCat, $iLang )
00088     {
00089         return $oCat->getId() . '_' . ( (int) $iLang );
00090     }
00091 
00100     public function getCategoryUri( $oCat, $iLang = null )
00101     {
00102         startProfile(__FUNCTION__);
00103         $sCatId = $oCat->getId();
00104 
00105         // skipping external category URLs
00106         if ( $oCat->oxcategories__oxextlink->value ) {
00107             $sSeoUrl = null;
00108         } else {
00109             // not found in cache, process it from the top
00110             if (!isset($iLang)) {
00111                 $iLang = $oCat->getLanguage();
00112             }
00113 
00114             $aCacheMap = array();
00115             $aStdLinks = array();
00116 
00117             while ( $oCat && !($sSeoUrl = $this->_categoryUrlLoader( $oCat, $iLang ) )) {
00118 
00119                 if ($iLang != $oCat->getLanguage()) {
00120                     $sId = $oCat->getId();
00121                     $oCat = oxNew('oxcategory');
00122                     $oCat->loadInLang($iLang, $sId);
00123                 }
00124 
00125                 // prepare oCat title part
00126                 $sTitle = $this->_prepareTitle( $oCat->oxcategories__oxtitle->value );
00127 
00128                 foreach ( array_keys( $aCacheMap ) as $id ) {
00129                     $aCacheMap[$id] = $sTitle . '/' . $aCacheMap[$id];
00130                 }
00131 
00132                 $aCacheMap[$oCat->getId()] = $sTitle;
00133                 $aStdLinks[$oCat->getId()] = $oCat->getStdLink();
00134 
00135                 // load parent
00136                 $oCat = $oCat->getParentCategory();
00137             }
00138 
00139             foreach ( $aCacheMap as $sId => $sUri ) {
00140                 $this->_aCatCache[$sId.'_'.$iLang] = $this->_processSeoUrl( $sSeoUrl.$sUri.'/', $sId, $iLang );
00141                 $this->_saveToDb( 'oxcategory', $sId, $aStdLinks[$sId], $this->_aCatCache[$sId.'_'.$iLang], $iLang );
00142             }
00143 
00144             $sSeoUrl = $this->_aCatCache[$sCatId.'_'.$iLang];
00145         }
00146 
00147         stopProfile(__FUNCTION__);
00148 
00149         return $sSeoUrl;
00150     }
00151 
00152 
00163     public function getCategoryPageUrl( $oCategory, $iPage, $iLang = null, $blFixed = false )
00164     {
00165         if (!isset($iLang)) {
00166             $iLang = $oCategory->getLanguage();
00167         }
00168         $sStdUrl = $oCategory->getStdLink() . '&amp;pgNr=' . $iPage;
00169         $sParams = sprintf( "%0" . ceil( $this->_iCntPages / 10 + 1 ) . "d", $iPage + 1 );
00170 
00171         $sStdUrl = $this->_trimUrl( $sStdUrl, $iLang );
00172         $sSeoUrl = $this->getCategoryUri( $oCategory, $iLang ) . $sParams . "/";
00173 
00174         return $this->_getFullUrl( $this->_getPageUri( $oCategory, 'oxcategory', $sStdUrl, $sSeoUrl, $sParams, $iLang, $blFixed ), $iLang );
00175     }
00176 
00188     public function getCategoryUrl( $oCategory, $iLang = null )
00189     {
00190         if (!isset($iLang)) {
00191             $iLang = $oCategory->getLanguage();
00192         }
00193         // category may have specified url
00194         if ( ( $sSeoUrl = $this->getCategoryUri( $oCategory, $iLang ) ) ) {
00195             return $this->_getFullUrl( $sSeoUrl, $iLang );
00196         }
00197         return '';
00198     }
00199 
00207     public function markRelatedAsExpired( $oCategory )
00208     {
00209         $oDb = oxDb::getDb();
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 = '".$oCategory->getId()."' limit 1");
00214         $sCatRootId = $aCatInfo[0][0];
00215         // update article for root of this cat
00216         $sQ = "update oxseo as seo1, (select oxobjectid from oxseo where oxtype = 'oxarticle' and oxparams = '{$sCatRootId}') as seo2 set seo1.oxexpired = '1' where seo1.oxtype = 'oxarticle' and seo1.oxobjectid = seo2.oxobjectid";
00217         $oDb->execute( $sQ );
00218 
00219         // update sub cats
00220         $sQ = "update oxseo as seo1, (select oxid from oxcategories where oxrootid='$sCatRootId' and oxleft > {$aCatInfo[0][1]} and oxright < {$aCatInfo[0][2]}) as seo2 set seo1.oxexpired = '1' where seo1.oxtype = 'oxcategory' and seo1.oxobjectid = seo2.oxid";
00221         $oDb->execute( $sQ );
00222     }
00223 
00224 
00232     public function onDeleteCategory($oCategory)
00233     {
00234         $sId = oxDb::getDb()->quote($oCategory->getId());
00235         oxDb::getDb()->execute("update oxseo, (select oxseourl from oxseo where oxobjectid = $sId and oxtype = 'oxcategory') as test set oxseo.oxexpired=1 where oxseo.oxseourl like concat(test.oxseourl, '%') and (oxtype = 'oxcategory' or oxtype = 'oxarticle')");
00236         oxDb::getDb()->execute("delete from oxseo where oxobjectid = $sId and oxtype = 'oxcategory'");
00237     }
00238 
00239 }

Generated on Tue Aug 18 09:21:06 2009 for OXID eShop CE by  doxygen 1.5.5