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         return self::$_instance;
00033     }
00034 
00046     protected function _categoryUrlLoader( $oCat, $iLang )
00047     {
00048         $sSeoUrl = false;
00049 
00050         $sCacheId = $this->_getCategoryCacheId( $oCat, $iLang );
00051         if ( isset( $this->_aCatCache[$sCacheId] ) ) {
00052             $sSeoUrl = $this->_aCatCache[ $sCacheId ];
00053         } elseif ( ( $sSeoUrl = $this->_loadFromDb( 'oxcategory', $oCat->getId(), $iLang ) ) ) {
00054             // caching
00055             $this->_aCatCache[ $sCacheId ] = $sSeoUrl;
00056         }
00057 
00058         return $sSeoUrl;
00059     }
00060 
00071     private function _getCategoryCacheId( $oCat, $iLang )
00072     {
00073         return $oCat->getId() . '_' . ( (int) $iLang );
00074     }
00075 
00084     public function getCategoryUri( $oCat, $iLang = null )
00085     {
00086         startProfile(__FUNCTION__);
00087         $sCatId = $oCat->getId();
00088 
00089         // skipping external category URLs
00090         if ( $oCat->oxcategories__oxextlink->value ) {
00091             $sSeoUrl = null;
00092         } else {
00093             // not found in cache, process it from the top
00094             if (!isset($iLang)) {
00095                 $iLang = $oCat->getLanguage();
00096             }
00097 
00098             $aCacheMap = array();
00099             $aStdLinks = array();
00100 
00101             while ( $oCat && !($sSeoUrl = $this->_categoryUrlLoader( $oCat, $iLang ) )) {
00102 
00103                 if ($iLang != $oCat->getLanguage()) {
00104                     $sId = $oCat->getId();
00105                     $oCat = oxNew('oxcategory');
00106                     $oCat->loadInLang($iLang, $sId);
00107                 }
00108 
00109                 // prepare oCat title part
00110                 $sTitle = $this->_prepareTitle( $oCat->oxcategories__oxtitle->value );
00111 
00112                 foreach ( array_keys( $aCacheMap ) as $id ) {
00113                     $aCacheMap[$id] = $sTitle . '/' . $aCacheMap[$id];
00114                 }
00115 
00116                 $aCacheMap[$oCat->getId()] = $sTitle;
00117                 $aStdLinks[$oCat->getId()] = $oCat->getStdLink();
00118 
00119                 // load parent
00120                 $oCat = $oCat->getParentCategory();
00121             }
00122 
00123             foreach ( $aCacheMap as $sId => $sUri ) {
00124                 $this->_aCatCache[$sId.'_'.$iLang] = $this->_getUniqueSeoUrl( $sSeoUrl.$sUri.'/', '/', $sId, $iLang );
00125                 $this->_saveToDb( 'oxcategory', $sId, $aStdLinks[$sId], $this->_aCatCache[$sId.'_'.$iLang], $iLang );
00126             }
00127 
00128             $sSeoUrl = $this->_aCatCache[$sCatId.'_'.$iLang];
00129         }
00130 
00131         stopProfile(__FUNCTION__);
00132 
00133         return $sSeoUrl;
00134     }
00135 
00136 
00147     public function getCategoryPageUrl( $oCategory, $iPage, $iLang = null, $blFixed = false )
00148     {
00149         if (!isset($iLang)) {
00150             $iLang = $oCategory->getLanguage();
00151         }
00152         $sStdUrl = $oCategory->getStdLink() . '&amp;pgNr=' . $iPage;
00153         $sParams = sprintf( "%0" . ceil( $this->_iCntPages / 10 + 1 ) . "d", $iPage + 1 );
00154 
00155         $sStdUrl = $this->_trimUrl( $sStdUrl, $iLang );
00156         $sSeoUrl = $this->getCategoryUri( $oCategory, $iLang ) . $sParams . "/";
00157 
00158         return $this->_getFullUrl( $this->_getPageUri( $oCategory, 'oxcategory', $sStdUrl, $sSeoUrl, $sParams, $iLang, $blFixed ), $iLang );
00159     }
00160 
00172     public function getCategoryUrl( $oCategory, $iLang = null )
00173     {
00174         if (!isset($iLang)) {
00175             $iLang = $oCategory->getLanguage();
00176         }
00177         // category may have specified url
00178         if ( ( $sSeoUrl = $this->getCategoryUri( $oCategory, $iLang ) ) ) {
00179             return $this->_getFullUrl( $sSeoUrl, $iLang );
00180         }
00181         return '';
00182     }
00183 
00191     public function markRelatedAsExpired( $oCategory )
00192     {
00193         $oDb = oxDb::getDb();
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 = '".$oCategory->getId()."' limit 1");
00198         $sCatRootId = $aCatInfo[0][0];
00199         // update article for root of this cat
00200         $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";
00201         $oDb->execute( $sQ );
00202 
00203         // update sub cats
00204         $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";
00205         $oDb->execute( $sQ );
00206     }
00207 
00208 
00216     public function onDeleteCategory($oCategory)
00217     {
00218         $sId = oxDb::getDb()->quote($oCategory->getId());
00219         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')");
00220         oxDb::getDb()->execute("delete from oxseo where oxobjectid = $sId and oxtype = 'oxcategory'");
00221     }
00222 
00223 }

Generated on Wed May 13 13:25:51 2009 for OXID eShop CE by  doxygen 1.5.5