Improving resource.db.php by hooking XoopsTpl on XCL2.1

Date 2008-01-10 04:53:50 | Category: XOOPS

in englishin japanese
resource.(type).php is a smarty plugin controlling:
- which template source is adopted
- how new is the template

Since default template resource of XOOPS is "db:", resource.db.php is the most important file.

Default resource.db.php of XCL2.1 has problems like:
(1) With "default" theme, DB templates will be broken easily
(2) Templates under theme will stay alive even after the theme is changed
(3) Paths of templates under theme should be flat because "db:" is flat.
(4) Directory name of "template under theme" is not unified. (templates or modules)
(5) A template file under theme is not recognized its deletion
(6) A template file under theme is not recognized its addition if the compile cache is newer than the file

I've made a new resource.db.php solving the problems (1),(2),(3), and (4).

Of course, it is not a hack.
Just using "XoopsTpl Hooking" by preload, we can put the file under XOOPS_TRUST_PATH/libs/smartyplugins/

You can test it right now.
Just try hodajuku distribution of nightly build (not a preview version).
The theme of "skel_flex" has many templates.
The tempaltes will be adopted with the theme, hence they will never adopted after switching the theme like "chic_black".

<?php
/*
 * Smarty plugin
 * ------------------------------------------------------------- 
 * File:     resource.db.php
 * Type:     resource
 * Name:     db
 * Purpose:  Fetches templates from a database
 * -------------------------------------------------------------
 */

function smarty_resource_db_systemTpl($tpl_name)
{
    // Replace Legacy System Template name to Legacy Module Template name
    static $patterns = null;
    static $replacements = null;
    if (!$patterns) {
        $root=&XCube_Root::getSingleton();
        $systemTemplates = explode(',',$root->getSiteConfig('Legacy_RenderSystem','SystemTemplate',''));
        $prefix = $root->getSiteConfig('Legacy_RenderSystem','SystemTemplatePrefix','legacy');
        $patterns = preg_replace('/^\s*([^\s]*)\s*$/e', '"/".preg_quote("\1","/")."/"', $systemTemplates);
        $replacements = preg_replace('/^\s*system_([^\s]*)\s*/', $prefix.'_\1', $systemTemplates);
    }
    if ($patterns) {
        $tpl_name = preg_replace($patterns, $replacements,$tpl_name);
    }
    return $tpl_name;
}

function smarty_resource_db_source($tpl_name, &$tpl_source, &$smarty)
{
	$tpl_name = smarty_resource_db_systemTpl($tpl_name);
	if ( !$tpl = smarty_resource_db_tplinfo( $tpl_name ) ) {
		return false;
	}
	if ( is_object( $tpl ) ) {
		$tpl_source = $tpl->getVar( 'tpl_source', 'n' );
	} else {
		$tpl_source = file_get_contents( $tpl ) ;
	}
	return true;
}

function smarty_resource_db_timestamp($tpl_name, &$tpl_timestamp, &$smarty)
{
	$tpl_name = smarty_resource_db_systemTpl($tpl_name);
	if ( !$tpl = smarty_resource_db_tplinfo( $tpl_name ) ) {
		return false;
	}
	if ( is_object( $tpl ) ) {
		$tpl_timestamp = $tpl->getVar( 'tpl_lastmodified', 'n' );
	} else {
		$tpl_timestamp = filemtime( $tpl );
	}
	return true;
}

function smarty_resource_db_secure($tpl_name, &$smarty)
{
    // assume all templates are secure
    return true;
}

function smarty_resource_db_trusted($tpl_name, &$smarty)
{
    // not used for templates
}

// return object(XoopsTplfile) or string(filepath)
function smarty_resource_db_tplinfo( $tpl_name )
{
	static $cache = array();
	global $xoopsConfig;

	// First, check the cache
	if ( isset( $cache[$tpl_name] ) ) {
		return $cache[$tpl_name];
	}

	$tplset = isset( $xoopsConfig['template_set'] ) ? $xoopsConfig['template_set']: 'default' ;
	$theme = isset( $xoopsConfig['theme_set'] ) ? $xoopsConfig['theme_set'] : 'default';

	// Second, check templates under themes/(theme)/templates/ (file template)
	$filepath = XOOPS_THEME_PATH . '/' . $theme . '/templates/' . $tpl_name ;
	if ( file_exists( $filepath ) ) {
		return $cache[$tpl_name] = $filepath ;
	}

	// Third, find a DB template of the selected tplset
	$tplfile_handler =& xoops_gethandler('tplfile');
	$tplobj = $tplfile_handler->find( $tplset, null, null, null, $tpl_name, true);	if ( empty( $tplobj ) ) {
		// Forth, find a DB template in default tplset
		$tplobj = $tplfile_handler->find( 'default', null, null, null, $tpl_name, true);
		if( empty( $tplobj ) ) return false ;
	}
	return $cache[$tpl_name] = $tplobj[0];
}
?>




You can read more news at PEAK XOOPS.
http://xoops.peak.ne.jp

The URL for this story is:
http://xoops.peak.ne.jp/md/news/index.php?page=article&storyid=450