<?php
namespace Engine\Smarty;

/**
 * Extend the base smarty object
 * @author Lucian Sirbu
 *
 */
class ExtendedSmarty extends \Smarty\Smarty
{

	/**
	 * Setup this object for use with the default TINA setup
	 * This version of this function creates subfolders for every module.
	 * 	This is useful for old modules, wehre smarty somehow screws up the included template, and requires the same template in a completly different module, where its not even included
	 * 	Ex: load plugins/Prism/reports/, then load reservations/orderForm/
	 * TODO: [nonMVC] remove this function, the simplified one should be used on MVC-only app
	 */
	public function setupInstance()
	{
		$CFG = \Engine\Loader_Cfg::getInstance()->getGlobal();
		$smartyBaseTemplates = 	sprintf("%s/%s", APP_REAL_PATH, $CFG['thirdParties']['smarty']['pathToCompiledTemplates']);
		$templatePermissions = 	0777;

		$cleanModuleDirs = 		substr(str_replace('/', '_', MODULE_PATH), 0, -1);

		// setup internal vars
		$this->setCompileDir($smartyBaseTemplates . $cleanModuleDirs);
		$this->setTemplateDir($CFG['thirdParties']['smarty']['pathToLocalTemplates']);	// TODO: [nonMVC] delete this after mvc is completed.

		// checks if the main directory exists and if not then create them
		if ( !is_dir($smartyBaseTemplates . $cleanModuleDirs) ) {
			if ( !is_dir($smartyBaseTemplates) ) {
				mkdir($smartyBaseTemplates, $templatePermissions);
				chmod($smartyBaseTemplates, $templatePermissions);
			}

			if ($cleanModuleDirs) {
				mkdir($smartyBaseTemplates . $cleanModuleDirs, $templatePermissions);
				chmod($smartyBaseTemplates . $cleanModuleDirs, $templatePermissions);
			}
		}
	}

	/**
	 * Setup this object for use with the default TINA setup
	 * This version of this function doesn't create subfolders for every module
	 * */
	/*
	public function setupInstance()
	{
		$CFG = \Engine\Loader_Cfg::getInstance()->getGlobal();
		$smartyBaseTemplates = 	sprintf("%s/%s", APP_REAL_PATH, $CFG['thirdParties']['smarty']['pathToCompiledTemplates']);
		$templatePermissions = 	0777;

		// setup internal vars
		$this->setCompileDir($smartyBaseTemplates);
		$this->setTemplateDir($CFG['thirdParties']['smarty']['pathToLocalTemplates']);	// TODO: [nonMVC] delete this after mvc is completed.

		// checks if the main directory exists and if not then create them
		if ( !is_dir($smartyBaseTemplates) ) {
			mkdir($smartyBaseTemplates, $templatePermissions);
			chmod($smartyBaseTemplates, $templatePermissions);
		}
	}
	*/

	/* debug-version of the fetch function */

	/*
	public function display($template = null, $cache_id = null, $compile_id = null, $parent = null)
	{
		\Engine\Debug\ParserCallTrace\ParserCallTrace::getInstance()->logCallStart($this, __METHOD__, [$template, $cache_id, $compile_id]);

		$t1 = microtime(true);
		$result = parent::display($template, $cache_id, $compile_id, $parent);
		$t2 = microtime(true);

		\Engine\Debug\ParserCallTrace\ParserCallTrace::getInstance()->logOutput([
			'duration' => $t2 - $t1,
			'size' => strlen($result)/1024,
		]);

		\Engine\Debug\ParserCallTrace\ParserCallTrace::getInstance()->logCallEnd();
		return $result;
	}

	public function fetch($template = null, $cache_id = null, $compile_id = null, $parent = null)
	{
		\Engine\Debug\ParserCallTrace\ParserCallTrace::getInstance()->logCallStart($this, __METHOD__, [$template, $cache_id, $compile_id]);

		$t1 = microtime(true);
		$result = parent::fetch($template, $cache_id, $compile_id, $parent);
		$t2 = microtime(true);

		\Engine\Debug\ParserCallTrace\ParserCallTrace::getInstance()->logOutput([
			'duration' => $t2 - $t1,
			'size' => strlen($result)/1024,
		]);

		\Engine\Debug\ParserCallTrace\ParserCallTrace::getInstance()->logCallEnd();
		return $result;
	}
	*/

}
