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 
00052     protected function _categoryUrlLoader( $oCat, $iLang )
00053     {
00054         $sSeoUrl = false;
00055 
00056         $sCacheId = $this->_getCategoryCacheId( $oCat, $iLang );
00057         if ( isset( $this->_aCatCache[$sCacheId] ) ) {
00058             $sSeoUrl = $this->_aCatCache[ $sCacheId ];
00059         } elseif ( ( $sSeoUrl = $this->_loadFromDb( 'oxcategory', $oCat->getId(), $iLang ) ) ) {
00060             // caching
00061             $this->_aCatCache[ $sCacheId ] = $sSeoUrl;
00062         }
00063 
00064         return $sSeoUrl;
00065     }
00066 
00077     private function _getCategoryCacheId( $oCat, $iLang )
00078     {
00079         return $oCat->getId() . '_' . ( (int) $iLang );
00080     }
00081 
00090     public function getCategoryUri( $oCat, $iLang = null )
00091     {
00092         startProfile(__FUNCTION__);
00093         $sCatId = $oCat->getId();
00094 
00095         // skipping external category URLs
00096         if ( $oCat->oxcategories__oxextlink->value ) {
00097             $sSeoUrl = null;
00098         } else {
00099             // not found in cache, process it from the top
00100             if (!isset($iLang)) {
00101                 $iLang = $oCat->getLanguage();
00102             }
00103 
00104             $aCacheMap = array();
00105             $aStdLinks = array();
00106 
00107             while ( $oCat && !($sSeoUrl = $this->_categoryUrlLoader( $oCat, $iLang ) )) {
00108 
00109                 if ($iLang != $oCat->getLanguage()) {
00110                     $sId = $oCat->getId();
00111                     $oCat = oxNew('oxcategory');
00112                     $oCat->loadInLang($iLang, $sId);
00113                 }
00114 
00115                 // prepare oCat title part
00116                 $sTitle = $this->_prepareTitle( $oCat->oxcategories__oxtitle->value );
00117 
00118                 foreach ( array_keys( $aCacheMap ) as $id ) {
00119                     $aCacheMap[$id] = $sTitle . '/' . $aCacheMap[$id];
00120                 }
00121 
00122                 $aCacheMap[$oCat->getId()] = $sTitle;
00123                 $aStdLinks[$oCat->getId()] = $oCat->getStdLink();
00124 
00125                 // load parent
00126                 $oCat = $oCat->getParentCategory();
00127             }
00128 
00129             foreach ( $aCacheMap as $sId => $sUri ) {
00130                 $this->_aCatCache[$sId.'_'.$iLang] = $this->_getUniqueSeoUrl( $sSeoUrl.$sUri.'/', '/', $sId, $iLang );
00131                 $this->_saveToDb( 'oxcategory', $sId, $aStdLinks[$sId], $this->_aCatCache[$sId.'_'.$iLang], $iLang );
00132             }
00133 
00134             $sSeoUrl = $this->_aCatCache[$sCatId.'_'.$iLang];
00135         }
00136 
00137         stopProfile(__FUNCTION__);
00138 
00139         return $sSeoUrl;
00140     }
00141 
00142 
00153     public function getCategoryPageUrl( $oCategory, $iPage, $iLang = null, $blFixed = false )
00154     {
00155         if (!isset($iLang)) {
00156             $iLang = $oCategory->getLanguage();
00157         }
00158         $sStdUrl = $oCategory->getStdLink() . '&amp;pgNr=' . $iPage;
00159         $sParams = sprintf( "%0" . ceil( $this->_iCntPages / 10 + 1 ) . "d", $iPage + 1 );
00160 
00161         $sStdUrl = $this->_trimUrl( $sStdUrl, $iLang );
00162         $sSeoUrl = $this->getCategoryUri( $oCategory, $iLang ) . $sParams . "/";
00163 
00164         return $this->_getFullUrl( $this->_getPageUri( $oCategory, 'oxcategory', $sStdUrl, $sSeoUrl, $sParams, $iLang, $blFixed ), $iLang );
00165     }
00166 
00178     public function getCategoryUrl( $oCategory, $iLang = null )
00179     {
00180         if (!isset($iLang)) {
00181             $iLang = $oCategory->getLanguage();
00182         }
00183         // category may have specified url
00184         if ( ( $sSeoUrl = $this->getCategoryUri( $oCategory, $iLang ) ) ) {
00185             return $this->_getFullUrl( $sSeoUrl, $iLang );
00186         }
00187         return '';
00188     }
00189 
00197     public function markRelatedAsExpired( $oCategory )
00198     {
00199         $oDb = oxDb::getDb();
00200         // select it from table instead of using object carrying value
00201         // this is because this method is usually called inside update,
00202         // where object may already be carrying changed id
00203         $aCatInfo = $oDb->getAll("select oxrootid, oxleft, oxright from oxcategories where oxid = '".$oCategory->getId()."' limit 1");
00204         $sCatRootId = $aCatInfo[0][0];
00205         // update article for root of this cat
00206         $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";
00207         $oDb->execute( $sQ );
00208 
00209         // update sub cats
00210         $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";
00211         $oDb->execute( $sQ );
00212     }
00213 
00214 
00222     public function onDeleteCategory($oCategory)
00223     {
00224         $sId = oxDb::getDb()->quote($oCategory->getId());
00225         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')");
00226         oxDb::getDb()->execute("delete from oxseo where oxobjectid = $sId and oxtype = 'oxcategory'");
00227     }
00228 
00229 }

Generated on Wed Jun 17 12:09:02 2009 for OXID eShop CE by  doxygen 1.5.5