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 static $inst = array();
00031 self::$_instance = $inst[oxClassCacheKey()];
00032 }
00033
00034 if (!self::$_instance) {
00035 self::$_instance = oxNew("oxSeoEncoderCategory");
00036 if ( defined( 'OXID_PHP_UNIT' ) ) {
00037 $inst[oxClassCacheKey()] = self::$_instance;
00038 }
00039 }
00040
00041 if ( defined( 'OXID_PHP_UNIT' ) ) {
00042
00043 self::$_instance->_aSeoCache = array();
00044 }
00045
00046 return self::$_instance;
00047 }
00048
00054 protected function _getUrlExtension()
00055 {
00056 return '/';
00057 }
00058
00070 protected function _categoryUrlLoader( $oCat, $iLang )
00071 {
00072 $sSeoUrl = false;
00073
00074 $sCacheId = $this->_getCategoryCacheId( $oCat, $iLang );
00075 if ( isset( $this->_aCatCache[$sCacheId] ) ) {
00076 $sSeoUrl = $this->_aCatCache[ $sCacheId ];
00077 } elseif ( ( $sSeoUrl = $this->_loadFromDb( 'oxcategory', $oCat->getId(), $iLang ) ) ) {
00078
00079 $this->_aCatCache[ $sCacheId ] = $sSeoUrl;
00080 }
00081
00082 return $sSeoUrl;
00083 }
00084
00095 private function _getCategoryCacheId( $oCat, $iLang )
00096 {
00097 return $oCat->getId() . '_' . ( (int) $iLang );
00098 }
00099
00108 public function getCategoryUri( $oCat, $iLang = null )
00109 {
00110 startProfile(__FUNCTION__);
00111 $sCatId = $oCat->getId();
00112
00113
00114 if ( $oCat->oxcategories__oxextlink->value ) {
00115 $sSeoUrl = null;
00116 } else {
00117
00118 if (!isset($iLang)) {
00119 $iLang = $oCat->getLanguage();
00120 }
00121
00122 $aCacheMap = array();
00123 $aStdLinks = array();
00124
00125 while ( $oCat && !($sSeoUrl = $this->_categoryUrlLoader( $oCat, $iLang ) )) {
00126
00127 if ($iLang != $oCat->getLanguage()) {
00128 $sId = $oCat->getId();
00129 $oCat = oxNew('oxcategory');
00130 $oCat->loadInLang($iLang, $sId);
00131 }
00132
00133
00134 $sTitle = $this->_prepareTitle( $oCat->oxcategories__oxtitle->value );
00135
00136 foreach ( array_keys( $aCacheMap ) as $id ) {
00137 $aCacheMap[$id] = $sTitle . '/' . $aCacheMap[$id];
00138 }
00139
00140 $aCacheMap[$oCat->getId()] = $sTitle;
00141 $aStdLinks[$oCat->getId()] = $oCat->getStdLink();
00142
00143
00144 $oCat = $oCat->getParentCategory();
00145 }
00146
00147 foreach ( $aCacheMap as $sId => $sUri ) {
00148 $this->_aCatCache[$sId.'_'.$iLang] = $this->_processSeoUrl( $sSeoUrl.$sUri.'/', $sId, $iLang );
00149 $this->_saveToDb( 'oxcategory', $sId, $aStdLinks[$sId], $this->_aCatCache[$sId.'_'.$iLang], $iLang );
00150 }
00151
00152 $sSeoUrl = $this->_aCatCache[$sCatId.'_'.$iLang];
00153 }
00154
00155 stopProfile(__FUNCTION__);
00156
00157 return $sSeoUrl;
00158 }
00159
00160
00171 public function getCategoryPageUrl( $oCategory, $iPage, $iLang = null, $blFixed = false )
00172 {
00173 if (!isset($iLang)) {
00174 $iLang = $oCategory->getLanguage();
00175 }
00176 $sStdUrl = $oCategory->getStdLink() . '&pgNr=' . $iPage;
00177 $sParams = sprintf( "%0" . ceil( $this->_iCntPages / 10 + 1 ) . "d", $iPage + 1 );
00178
00179 $sStdUrl = $this->_trimUrl( $sStdUrl, $iLang );
00180 $sSeoUrl = $this->getCategoryUri( $oCategory, $iLang ) . $sParams . "/";
00181
00182 return $this->_getFullUrl( $this->_getPageUri( $oCategory, 'oxcategory', $sStdUrl, $sSeoUrl, $sParams, $iLang, $blFixed ), $iLang );
00183 }
00184
00196 public function getCategoryUrl( $oCategory, $iLang = null )
00197 {
00198 if (!isset($iLang)) {
00199 $iLang = $oCategory->getLanguage();
00200 }
00201
00202 if ( ( $sSeoUrl = $this->getCategoryUri( $oCategory, $iLang ) ) ) {
00203 return $this->_getFullUrl( $sSeoUrl, $iLang );
00204 }
00205 return '';
00206 }
00207
00215 public function markRelatedAsExpired( $oCategory )
00216 {
00217 $oDb = oxDb::getDb();
00218
00219
00220
00221 $aCatInfo = $oDb->getAll("select oxrootid, oxleft, oxright from oxcategories where oxid = '".$oCategory->getId()."' limit 1");
00222 $sCatRootIdQuoted = $oDb->quote( $aCatInfo[0][0] );
00223
00224 $sQ = "update oxseo as seo1, (select oxobjectid from oxseo where oxtype = 'oxarticle' and oxparams = {$sCatRootIdQuoted}) as seo2 set seo1.oxexpired = '1' where seo1.oxtype = 'oxarticle' and seo1.oxobjectid = seo2.oxobjectid";
00225 $oDb->execute( $sQ );
00226
00227
00228 $sQ = "update oxseo as seo1, (select oxid from oxcategories where oxrootid={$sCatRootIdQuoted} and oxleft > ".$oDb->quote( $aCatInfo[0][1] )." and oxright < ".$oDb->quote( $aCatInfo[0][2] ).") as seo2 set seo1.oxexpired = '1' where seo1.oxtype = 'oxcategory' and seo1.oxobjectid = seo2.oxid";
00229 $oDb->execute( $sQ );
00230 }
00231
00232
00240 public function onDeleteCategory($oCategory)
00241 {
00242 $sIdQuoted = oxDb::getDb()->quote($oCategory->getId());
00243 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')");
00244 oxDb::getDb()->execute("delete from oxseo where oxobjectid = $sIdQuoted and oxtype = 'oxcategory'");
00245 }
00246
00247 }