HomeHelpTrac

Changeset 15897 for trunk/templates


Ignore:
Timestamp:
01/18/12 23:41:12 (4 months ago)
Author:
Alexander Trofimov
Message:

Page - refactoring, docs, cache implementation.

Location:
trunk/templates/base
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/templates/base/_header.html

    r15286 r15897  
    77    __page_description__ 
    88    __page_keywords__ 
     9    __page_robots__ 
    910 
    1011    <meta http-equiv="Content-Style-Type" content="text/css" /> 
  • trunk/templates/base/scripts/BxBasePage.php

    r15864 r15897  
    1717 
    1818    protected $_oTemplate; 
     19    protected $_oPageCacheObject = null; 
    1920 
    2021    public function __construct ($aObject, $oTemplate) { 
     
    2728    } 
    2829 
    29  
    30     public function getCode () {                 
     30    /** 
     31     * Get page code with automatic caching, adding necessary css/js files and system template vars.  
     32     * @return string. 
     33     */ 
     34    public function getCode () { 
    3135 
    3236        if (!$this->_isVisiblePage($this->_aObject)) 
    33             return MsgBox(_t('_Access denied')); 
    34  
     37            return $this->_getPageAccessDeniedMsg (); 
     38 
     39        $this->_addJsCss(); 
     40  
     41        $this->_addSysTemplateVars(); 
     42 
     43        // TODO: insert profiler call here  
     44 
     45        if (!getParam('sys_page_cache_enable') || !$this->_aObject['cache_server']) 
     46            return $this->_getPageCode(); 
     47         
     48        $oCache = $this->_getPageCacheObject(); 
     49        $sKey = $this->_getPageCacheKey(); 
     50 
     51        $mixedRet = $oCache->getData($sKey, $this->_aObject['cache_server']); 
     52 
     53        if ($mixedRet !== null) { 
     54            return $mixedRet; 
     55        } else { 
     56            $sPageCode = $this->_getPageCode(); 
     57            $oCache->setData($sKey, $sPageCode, $this->_aObject['cache_server']); 
     58        }         
     59 
     60        return $sPageCode; 
     61    } 
     62     
     63    /** 
     64     * Get page code only. 
     65     * @return string 
     66     */ 
     67    protected function _getPageCode () { 
    3568        $aVars = array (); 
    3669        $aBlocks = $this->_oQuery->getPageBlocks(); 
     
    3871            $sCell = ''; 
    3972            foreach ($aCell as $aBlock) { 
    40  
    41                 if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->beginPageBlock(_t($aBlock['title']), $aBlock['id']); 
    42  
    43                 if (!$this->_isVisibleBlock($aBlock)) 
    44                     continue; 
    45  
    46                 $sFunc = '_getBlock' . ucfirst($aBlock['type']); 
    47                 $mixedContent = $this->$sFunc($aBlock); 
    48  
    49                 if (is_array($mixedContent)) { 
    50                     // TODO: get top, bottom, content code from array 
    51                 } elseif (is_string($mixedContent)) { 
    52                     $sContentWithBox = DesignBoxContent(_t($aBlock['title']), $mixedContent, $aBlock['designbox_id']); 
    53                 } else { 
    54                     $sContentWithBox = false; 
    55                 } 
     73                $sContentWithBox = $this->_getBlockCode($aBlock); 
    5674                if ($sContentWithBox) 
    5775                    $sCell .= '<div class="bx-page-block-container" id="bx-page-block-' . $aBlock['id'] . '">' . $sContentWithBox . '</div>'; 
    58  
    59                 if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->endPageBlock($aBlock['id'], $sContentWithBox ? false : true, false ); 
    6076            } 
    6177            $aVars[$sKey] = $sCell; 
    6278        } 
    63         $this->_addJsCss(); 
    64      
     79 
    6580        return $this->_oTemplate->parseHtmlByName($this->_aObject['template'], $aVars); 
    6681    } 
    67      
     82 
     83    /** 
     84     * Get block code. 
     85     * @return string 
     86     */ 
     87    protected function _getBlockCode($aBlock) { 
     88        $sContentWithBox = ''; 
     89 
     90        if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->beginPageBlock(_t($aBlock['title']), $aBlock['id']); 
     91 
     92        if ($this->_isVisibleBlock($aBlock)) { 
     93            $sFunc = '_getBlock' . ucfirst($aBlock['type']); 
     94            $mixedContent = $this->$sFunc($aBlock); 
     95 
     96            if (is_array($mixedContent)) { 
     97                // TODO: get menu from array 
     98                if (isset($mixedContent['content'])) 
     99                    $sContentWithBox = DesignBoxContent(_t($aBlock['title']), $mixedContent['content'], $aBlock['designbox_id']); 
     100            } elseif (is_string($mixedContent)) { 
     101                $sContentWithBox = DesignBoxContent(_t($aBlock['title']), $mixedContent, $aBlock['designbox_id']); 
     102            } else { 
     103                $sContentWithBox = false; 
     104            } 
     105        } 
     106 
     107        if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->endPageBlock($aBlock['id'], $sContentWithBox ? false : true, false ); 
     108 
     109        return $sContentWithBox; 
     110    } 
     111 
     112    /** 
     113     * Add necessary js and css files. 
     114     */ 
    68115    protected function _addJsCss() { 
    69116        $this->_oTemplate->addCss('page_layouts.css'); 
    70117    } 
    71118 
     119    /** 
     120     * Set system template variables, like page title, meta description, meta keywords and meta robots. 
     121     */ 
     122    protected function _addSysTemplateVars () { 
     123        $sPageTitle = $this->_getPageTitle(); 
     124        if ($sPageTitle) 
     125            $this->_oTemplate->setPageHeader ($sPageTitle); 
     126    
     127        $sMetaDesc = $this->_getPageMetaDesc(); 
     128        if ($sMetaDesc) 
     129            $this->_oTemplate->setPageDescription ($sMetaDesc); 
     130 
     131        $sMetaKeywords = $this->_getPageMetaKeywords(); 
     132        if ($sMetaKeywords) 
     133            $this->_oTemplate->addPageKeywords ($sMetaKeywords); 
     134 
     135        $sMetaRobots = $this->_getPageMetaRobots(); 
     136        if ($sMetaRobots) 
     137            $this->_oTemplate->setPageMetaRobots ($sMetaRobots); 
     138    } 
     139 
     140    /** 
     141     * Get content for 'raw' block type. 
     142     * @return string 
     143     */ 
    72144    protected function _getBlockRaw ($aBlock) { 
    73145        return $aBlock['content']; 
    74146    } 
    75147 
     148    /** 
     149     * Get content for 'html' block type. 
     150     * @return string 
     151     */ 
    76152    protected function _getBlockHtml ($aBlock) { 
    77153        return $this->_getBlockRaw ($aBlock); 
    78154    } 
    79155 
     156    /** 
     157     * Get content for 'lang' block type. 
     158     * @return string 
     159     */ 
    80160    protected function _getBlockLang ($aBlock) { 
    81161        return _t(trim($aBlock['content'])); 
    82162    } 
    83163 
     164    /** 
     165     * Get content for 'image' block type. 
     166     * @return string 
     167     */ 
    84168    protected function _getBlockImage ($aBlock) { 
    85169        // TODO: 
     
    87171    } 
    88172 
     173    /** 
     174     * Get content for 'rss' block type. 
     175     * @return string 
     176     */ 
    89177    protected function _getBlockRss ($aBlock) { 
    90178        list( $sUrl, $iNum ) = explode( '#', $sContent ); 
     
    95183    } 
    96184 
     185    /** 
     186     * Get content for 'menu' block type. 
     187     * @return string 
     188     */ 
    97189    protected function _getBlockMenu ($aBlock) { 
    98190        bx_import('BxTemplMenu'); 
     
    101193    } 
    102194 
     195    /** 
     196     * Get content for 'service' block type. 
     197     * @return string 
     198     */ 
    103199    protected function _getBlockService ($aBlock) { 
    104200        $a = @unserialize($aBlock['content']); 
     
    107203        return BxDolService::call($a['module'], $a['method'], $a['params']); 
    108204    } 
     205 
     206    /** 
     207     * Get page title. 
     208     * @return string 
     209     */ 
     210    protected function _getPageTitle() { 
     211        return _t($this->_aObject['title']); 
     212    } 
     213 
     214    /** 
     215     * Get page meta description. 
     216     * @return string 
     217     */ 
     218    protected function _getPageMetaDesc() { 
     219        return _t($this->_aObject['meta_description']); 
     220    } 
     221 
     222    /** 
     223     * Get page meta keywords. 
     224     * @return string 
     225     */ 
     226    protected function _getPageMetaKeywords() { 
     227        return _t($this->_aObject['meta_keywords']); 
     228    } 
     229 
     230    /** 
     231     * Get page meta robots. 
     232     * @return string 
     233     */ 
     234    protected function _getPageMetaRobots() { 
     235        return _t($this->_aObject['meta_robots']); 
     236    } 
     237 
     238    /** 
     239     * Get access denied message HTML. 
     240     * @return string 
     241     */ 
     242    protected function _getPageAccessDeniedMsg () { 
     243        return MsgBox(_t('_Access denied')); 
     244    } 
     245 
     246 
     247    /** 
     248     * Get page cache object. 
     249     * @return cache object instance 
     250     */ 
     251    protected function _getPageCacheObject () { 
     252        if ($this->_oPageCacheObject != null) { 
     253            return $this->_oPageCacheObject; 
     254        } else { 
     255            $sEngine = getParam('sys_page_cache_engine'); 
     256            $this->_oPageCacheObject = bx_instance ('BxDolCache' . $sEngine); 
     257            if (!$this->_oPageCacheObject->isAvailable()) 
     258                $this->_oPageCacheObject = bx_instance ('BxDolCacheFile'); 
     259            return $this->_oPageCacheObject; 
     260        } 
     261    } 
     262 
     263    /** 
     264     * Get page cache key. 
     265     * @param $isPrefixOnly return key prefix only. 
     266     * @return string 
     267     */ 
     268    protected function _getPageCacheKey ($isPrefixOnly = false) { 
     269        $s = 'page_' . $this->_aObject['object'] . '_'; 
     270        if ($isPrefixOnly) 
     271            return $s; 
     272        $s .= $this->_getPageCacheParams (); 
     273        $s .= md5(BX_DOL_VERSION . BX_DOL_BUILD . BX_DOL_URL_ROOT) . '.php'; 
     274        return $s; 
     275    } 
     276 
     277    /** 
     278     * Additional cache key. In the case of dynamic page.  
     279     * For example - profile page, where each profile has own page. 
     280     * @return string 
     281     */ 
     282    protected function _getPageCacheParams () { 
     283        return ''; 
     284    } 
     285 
     286    /** 
     287     * Clean page cache. 
     288     * @param $isDelAllWithPagePrefix delete cache by prefix, it can be used for dynamic pages, like profile view, where for each profile separate cache is generated. 
     289     * @return string 
     290     */ 
     291    protected function cleanCache ($isDelAllWithPagePrefix = false) { 
     292        $oCache = $this->_getPageCacheObject (); 
     293        $sKey = $this->_getPageCacheKey($isDelAllWithPagePrefix); 
     294 
     295        if ($isDelAllWithPagePrefix) 
     296            return $oCache->removeAllByPrefix($sKey); 
     297        else 
     298            return $oCache->delData($sKey); 
     299    } 
    109300} 
    110301 
Note: See TracChangeset for help on using the changeset viewer.