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 ( defined( 'OXID_PHP_UNIT' ) ) {
00030             self::$_instance = modInstances::getMod( __CLASS__ );
00031         }
00032 
00033         if (!self::$_instance) {
00034             self::$_instance = oxNew("oxSeoEncoderCategory");
00035             if ( defined( 'OXID_PHP_UNIT' ) ) {
00036                 modInstances::addMod( __CLASS__, self::$_instance);
00037             }
00038         }
00039 
00040         if ( defined( 'OXID_PHP_UNIT' ) ) {
00041             // resetting cache
00042             self::$_instance->_aSeoCache = array();
00043         }
00044 
00045         return self::$_instance;
00046     }
00047 
00053     protected function _getUrlExtension()
00054     {
00055         return '/';
00056     }
00057 
00069     protected function _categoryUrlLoader( $oCat, $iLang )
00070     {
00071         $sSeoUrl = false;
00072 
00073         $sCacheId = $this->_getCategoryCacheId( $oCat, $iLang );
00074         if ( isset( $this->_aCatCache[$sCacheId] ) ) {
00075             $sSeoUrl = $this->_aCatCache[ $sCacheId ];
00076         } elseif ( ( $sSeoUrl = $this->_loadFromDb( 'oxcategory', $oCat->getId(), $iLang ) ) ) {
00077             // caching
00078             $this->_aCatCache[ $sCacheId ] = $sSeoUrl;
00079         }
00080 
00081         return $sSeoUrl;
00082     }
00083 
00094     private function _getCategoryCacheId( $oCat, $iLang )
00095     {
00096         return $oCat->getId() . '_' . ( (int) $iLang );
00097     }
00098 
00107     public function getCategoryUri( $oCat, $iLang = null )
00108     {
00109         startProfile(__FUNCTION__);
00110         $sCatId = $oCat->getId();
00111 
00112         // skipping external category URLs
00113         if ( $oCat->oxcategories__oxextlink->value ) {
00114             $sSeoUrl = null;
00115         } else {
00116             // not found in cache, process it from the top
00117             if (!isset($iLang)) {
00118                 $iLang = $oCat->getLanguage();
00119             }
00120 
00121             $aCacheMap = array();
00122             $aStdLinks = array();
00123 
00124             while ( $oCat && !($sSeoUrl = $this->_categoryUrlLoader( $oCat, $iLang ) )) {
00125 
00126                 if ($iLang != $oCat->getLanguage()) {
00127                     $sId = $oCat->getId();
00128                     $oCat = oxNew('oxcategory');
00129                     $oCat->loadInLang($iLang, $sId);
00130                 }
00131 
00132                 // prepare oCat title part
00133                 $sTitle = $this->_prepareTitle( $oCat->oxcategories__oxtitle->value );
00134 
00135                 foreach ( array_keys( $aCacheMap ) as $id ) {
00136                     $aCacheMap[$id] = $sTitle . '/' . $aCacheMap[$id];
00137                 }
00138 
00139                 $aCacheMap[$oCat->getId()] = $sTitle;
00140                 $aStdLinks[$oCat->getId()] = $oCat->getStdLink();
00141 
00142                 // load parent
00143                 $oCat = $oCat->getParentCategory();
00144             }
00145 
00146             foreach ( $aCacheMap as $sId => $sUri ) {
00147                 $this->_aCatCache[$sId.'_'.$iLang] = $this->_processSeoUrl( $sSeoUrl.$sUri.'/', $sId, $iLang );
00148                 $this->_saveToDb( 'oxcategory', $sId, $aStdLinks[$sId], $this->_aCatCache[$sId.'_'.$iLang], $iLang );
00149             }
00150 
00151             $sSeoUrl = $this->_aCatCache[$sCatId.'_'.$iLang];
00152         }
00153 
00154         stopProfile(__FUNCTION__);
00155 
00156         return $sSeoUrl;
00157     }
00158 
00159 
00170     public function getCategoryPageUrl( $oCategory, $iPage, $iLang = null, $blFixed = null )
00171     {
00172         if (!isset($iLang)) {
00173             $iLang = $oCategory->getLanguage();
00174         }
00175         $sStdUrl = $oCategory->getStdLink() . '&amp;pgNr=' . $iPage;
00176         $sParams = (int) ($iPage + 1);
00177 
00178         $sStdUrl = $this->_trimUrl( $sStdUrl, $iLang );
00179         $sSeoUrl = $this->getCategoryUri( $oCategory, $iLang ) . $sParams . "/";
00180 
00181         if ( $blFixed === null ) {
00182             $blFixed = $this->_isFixed( 'oxcategory', $oCategory->getId(), $iLang );
00183         }
00184         return $this->_getFullUrl( $this->_getPageUri( $oCategory, 'oxcategory', $sStdUrl, $sSeoUrl, $sParams, $iLang, $blFixed ), $iLang );
00185     }
00186 
00198     public function getCategoryUrl( $oCategory, $iLang = null )
00199     {
00200         $sUrl = '';
00201         if (!isset($iLang)) {
00202             $iLang = $oCategory->getLanguage();
00203         }
00204         // category may have specified url
00205         if ( ( $sSeoUrl = $this->getCategoryUri( $oCategory, $iLang ) ) ) {
00206             $sUrl = $this->_getFullUrl( $sSeoUrl, $iLang );
00207         }
00208         return $sUrl;
00209     }
00210 
00218     public function markRelatedAsExpired( $oCategory )
00219     {
00220         $oDb = oxDb::getDb();
00221         $sIdQuoted = oxDb::getDb()->quote($oCategory->getId());
00222 
00223         // select it from table instead of using object carrying value
00224         // this is because this method is usually called inside update,
00225         // where object may already be carrying changed id
00226         $aCatInfo = $oDb->getAll("select oxrootid, oxleft, oxright from oxcategories where oxid = $sIdQuoted limit 1");
00227         $sCatRootIdQuoted = $oDb->quote( $aCatInfo[0][0] );
00228 
00229         // update sub cats
00230         $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";
00231         $oDb->execute( $sQ );
00232 
00233         // update subarticles
00234         $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";
00235         $oDb->execute( $sQ );
00236     }
00237 
00238 
00246     public function onDeleteCategory($oCategory)
00247     {
00248         $sIdQuoted = oxDb::getDb()->quote($oCategory->getId());
00249         oxDb::getDb()->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')");
00250         oxDb::getDb()->execute("delete from oxseo where oxobjectid = $sIdQuoted and oxtype = 'oxcategory'");
00251     }
00252 
00253 }

Generated by  doxygen 1.6.2