summaryrefslogtreecommitdiff
path: root/smartyplugins/function.moduleinc.php
blob: 54196f7b7802e23ae41f603308f9d00be0e1ad15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
namespace Bitweaver\Plugins;

use Bitweaver\KernelTools;
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */

/**
 * moduleinc 
 * 
 * Usage: add to the body of any .tpl file
 * Example: {inlinemodule file="_custom:custom/my_custom_module" cache_time=600}
 *
 * Note: currently only supports custom modules generated in themes package, 
 * could support any module with more work
 *
 * @param array $pParams 
 * @param string $pParams['module_rsrc'] the full name of the template, example: _custom:custom/my_custom_module 
 * @param integer $pParams['cache_time'] seconds the template will be cached
 */
function smarty_function_moduleinc($pParams, &$gBitSmarty) {
	global $gBitSystem, $gBitThemes, $gBitSmarty;

	// go through some hassle here in consideration of a future day when this handles any module
	list( $package, $template ) = split(  '/', $pParams['module_rsrc'] );

	if( $package == '_custom:custom' ) {
		global $gBitLanguage;

		// We're gonna run our own cache mechanism for user_modules
		// the cache is here to avoid calls to consumming queries,
		// each module is different for each language because of the strings
		$cacheDir = TEMP_PKG_PATH.'modules/cache/';
		if( !is_dir( $cacheDir )) {
			KernelTools::mkdir_p( $cacheDir );
		}
		$cachefile = $cacheDir.'_custom.'.$gBitLanguage->mLanguage.'.'.$template.'.tpl.cache';

		if( !empty( $pParams["cache_time"] ) && file_exists( $cachefile ) && !(( $gBitSystem->getUTCTime() - filemtime( $cachefile )) > $pParams["cache_time"] )) {
			$fp = fopen( $cachefile, "r" );
			$data = fread( $fp, filesize( $cachefile ));
			fclose( $fp );
			print $data;
		} else {
			if( $moduleParams = $gBitThemes->getCustomModule( $template )) {
				$moduleParams = array_merge( $pParams, $moduleParams );
				$gBitSmarty->assign( 'moduleParams', $moduleParams );
				$data = $gBitSmarty->fetch( 'bitpackage:themes/custom_module.tpl' );

				if( !empty( $pParams["cache_time"] ) ) {
					// write to chache file
					$fp = fopen( $cachefile, "w+" );
					fwrite( $fp, $data, strlen( $data ));
					fclose( $fp );
				}
				print $data;
			}
		}
		unset( $data );
	}
}