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 
00108     public function getCategoryUri( $oCat, $iLang = null, $blRegenerate = false  )
00109     {
00110         startProfile(__FUNCTION__);
00111         $sCatId = $oCat->getId();
00112 
00113         // skipping external category URLs
00114         if ( $oCat->oxcategories__oxextlink->value ) {
00115             $sSeoUrl = null;
00116         } else {
00117             // not found in cache, process it from the top
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                 // prepare oCat title part
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->getBaseStdLink($iLang);
00142 
00143                 // load parent
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 = null )
00172     {
00173         if (!isset($iLang)) {
00174             $iLang = $oCategory->getLanguage();
00175         }
00176         $sStdUrl = $oCategory->getBaseStdLink($iLang) . '&amp;pgNr=' . $iPage;
00177         $sParams = (int) ($iPage + 1);
00178 
00179         $sStdUrl = $this->_trimUrl( $sStdUrl, $iLang );
00180         $sSeoUrl = $this->getCategoryUri( $oCategory, $iLang ) . $sParams . "/";
00181 
00182         if ( $blFixed === null ) {
00183             $blFixed = $this->_isFixed( 'oxcategory', $oCategory->getId(), $iLang );
00184         }
00185         return $this->_getFullUrl( $this->_getPageUri( $oCategory, 'oxcategory', $sStdUrl, $sSeoUrl, $sParams, $iLang, $blFixed ), $iLang );
00186     }
00187 
00199     public function getCategoryUrl( $oCategory, $iLang = null )
00200     {
00201         $sUrl = '';
00202         if (!isset($iLang)) {
00203             $iLang = $oCategory->getLanguage();
00204         }
00205         // category may have specified url
00206         if ( ( $sSeoUrl = $this->getCategoryUri( $oCategory, $iLang ) ) ) {
00207             $sUrl = $this->_getFullUrl( $sSeoUrl, $iLang );
00208         }
00209         return $sUrl;
00210     }
00211 
00219     public function markRelatedAsExpired( $oCategory )
00220     {
00221         $oDb = oxDb::getDb();
00222         $sIdQuoted = $oDb->quote($oCategory->getId());
00223 
00224         // select it from table instead of using object carrying value
00225         // this is because this method is usually called inside update,
00226         // where object may already be carrying changed id
00227         $aCatInfo = $oDb->getAll("select oxrootid, oxleft, oxright from oxcategories where oxid = $sIdQuoted limit 1");
00228         $sCatRootIdQuoted = $oDb->quote( $aCatInfo[0][0] );
00229 
00230         // update sub cats
00231         $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";
00232         $oDb->execute( $sQ );
00233 
00234         // update subarticles
00235         $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";
00236         $oDb->execute( $sQ );
00237     }
00238 
00239 
00247     public function onDeleteCategory( $oCategory )
00248     {
00249         $oDb = oxDb::getDb();
00250         $sIdQuoted = $oDb->quote($oCategory->getId());
00251         $oDb->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')");
00252         $oDb->execute("delete from oxseo where oxseo.oxtype = 'oxarticle' and oxseo.oxparams = $sIdQuoted" );
00253         $oDb->execute("delete from oxseo where oxobjectid = $sIdQuoted and oxtype = 'oxcategory'");
00254         $oDb->execute("delete from oxobject2seodata where oxobjectid = $sIdQuoted");
00255     }
00256 
00265     protected function _getAltUri( $sObjectId, $iLang )
00266     {
00267         $sSeoUrl = null;
00268         $oCat = oxNew( "oxcategory" );
00269         if ( $oCat->loadInLang( $iLang, $sObjectId ) ) {
00270             $sSeoUrl = $this->getCategoryUri( $oCat, $iLang );
00271         }
00272         return $sSeoUrl;
00273     }
00274 }