<?php
/**
 * cf. Core Definition Constants
 * CakePHP 1.2.* : http://book.cakephp.org/view/122/Core-Definition-Constants
 * CakePHP 1.3.* : http://book.cakephp.org/view/1141/Core-Definition-Constants
 * CakePHP 2.*.* : not found
 */
/**
 * Smarty View for 1.2
 * http://bakery.cakephp.org/articles/icedcheese/2008/01/14/smarty-view-for-1-2
 * modified smarty.php by 708
 */
App::import('Vendor', 'Smarty', array('file' => 'smarty'.DS.'Smarty.class.php'));

/**
 * View class @ ROOT.'/cake/libs/view/view.php'
 */
class SmartyView extends View {
	function __construct (&$controller) {
		parent::__construct($controller);
		//$this->subDir = 'smarty'.DS; // no use !!
		$this->ext= '.tpl';

		$this->Smarty = &new Smarty();
		$this->Smarty->cache_dir     = TMP.'smarty'.DS.'cache'.DS;
		$this->Smarty->compile_dir   = TMP.'smarty'.DS.'compile'.DS;
		$this->Smarty->config_dir    = 'config';
		$this->Smarty->template_dir  = VIEWS.DS;
		//$this->Smarty->plugins_dir[] = VIEWS.'smarty_plugins'.DS;
		//$this->Smarty->caching       = true;
		//$this->Smarty->error_reporting = 'E_ALL & ~E_NOTICE';
		//$this->Smarty->debugging = true; 
	}

	/**
	 * Overrides the View::_render()
	 */
	function _render($___viewFn, $___data_for_view, $___play_safe = true, $loadHelpers = true) {
		/*
		 * Smarty 2.*.* : assign_by_ref
		 * Smarty 3.*.* : assignByRef
		 */
		$assign_by_ref = (isset($this->Smarty->_version) && preg_match('|^2|', $this->Smarty->_version) > 0)
			? 'assign_by_ref'
			: 'assignByRef';

		if ($this->helpers != false && $loadHelpers === true) {
			$loadedHelpers =  array();
			$loadedHelpers = $this->_loadHelpers($loadedHelpers, $this->helpers);

			foreach(array_keys($loadedHelpers) as $helper) {
				$replace = strtolower(substr($helper, 0, 1));
				$camelBackedHelper = preg_replace('/\\w/', $replace, $helper, 1);

				${$camelBackedHelper} =& $loadedHelpers[$helper];
				if (isset(${$camelBackedHelper}->helpers) && is_array(${$camelBackedHelper}->helpers)) {
					foreach(${$camelBackedHelper}->helpers as $subHelper) {
						${$camelBackedHelper}->{$subHelper} =& $loadedHelpers[$subHelper];
					}
				}
				$this->loaded[$camelBackedHelper] = (${$camelBackedHelper});
				$this->Smarty->$assign_by_ref($camelBackedHelper, ${$camelBackedHelper});
			}
		}

		$this->register_functions();

		foreach($___data_for_view as $data => $value) {
			if (!is_object($data)) {
				$this->Smarty->assign($data, $value);
			}
		}

		$this->Smarty->$assign_by_ref('view', $this);
	    ob_start();
		echo $this->Smarty->fetch($___viewFn);
		//echo "<p>smarty.php echo : ". $this->_getLayoutFileName() . "</p>"; // no use !! for debug
		return ob_get_clean();
	}

	/**
	 * Overrides the View::_getLayoutFileName()
	 */
	function _getLayoutFileName() {
		if (isset($this->webservices) && !is_null($this->webservices)) {
			$type = strtolower($this->webservices) . DS;
		} else {
			$type = null;
		}

		if (isset($this->plugin) && !is_null($this->plugin)) {
			if (file_exists(APP . 'plugins' . DS . $this->plugin . DS . 'views' . DS . 'layouts' . DS . $this->layout . $this->ext)) {
				$layoutFileName = APP . 'plugins' . DS . $this->plugin . DS . 'views' . DS . 'layouts' . DS . $this->layout . $this->ext;
				return $layoutFileName;
			}
		}

		/*
		 * CakePHP 1.2.* : $tmp = Configure::getInstance(); $paths = $tmp->viewPaths;
		 * CakePHP 1.3.* : $paths = App::path('views');
		 */
		if (preg_match("|^1\.2\.\d+|", Configure::version()) > 0) {
			$tmp = Configure::getInstance();
			$paths = $tmp->viewPaths;
		}elseif (preg_match("|^1\.3\.\d+|", Configure::version()) > 0) {
			$paths = App::path('views');
		}
		foreach($paths as $path) {
			if (file_exists($path . 'layouts' . DS . $this->subDir . $type . $this->layout . $this->ext)) {
				$layoutFileName = $path . 'layouts' . DS . $this->subDir . $type . $this->layout . $this->ext;
				return $layoutFileName;
			}
		}

		// added for .ctp viewPath fallback
		foreach($paths as $path) {
			if (file_exists($path . 'layouts' . DS  . $type . $this->layout . '.ctp')) {
				$layoutFileName = $path . 'layouts' . DS . $type . $this->layout . '.ctp';
				return $layoutFileName;
			}
		}

		if ($layoutFileName = fileExistsInPath(LIBS . 'view' . DS . 'templates' . DS . 'layouts' . DS . $type . $this->layout . '.ctp')) {
		} else {
			$layoutFileName = LAYOUTS . $type . $this->layout.$this->ext;
		}
		return $layoutFileName;
	}

	/**
	 * Overrides the View::_getViewFileName()
	 */
	function _getViewFileName($action) {
		$action = Inflector::underscore($action);

		//if (!is_null($this->webservices)) {
		if (isset($this->webservices) && !is_null($this->webservices)) {
			$type = strtolower($this->webservices) . DS;
		} else {
			$type = null;
		}

		if (empty($action)) {
			$action = $this->action;
		}

		$position = strpos($action, '..');

		if ($position === false) {
		} else {
			$action = explode('/', $action);
			$i = array_search('..', $action);
			unset($action[$i - 1]);
			unset($action[$i]);
			$action='..' . DS . implode(DS, $action);
		}

		/*
		 * CakePHP 1.2.* : $tmp = Configure::getInstance(); $paths = $tmp->viewPaths;
		 * CakePHP 1.3.* : $paths = App::path('views');
		 */
		if (preg_match("|^1\.2\.\d+|", Configure::version()) > 0) {
			$tmp = Configure::getInstance();
			$paths = $tmp->viewPaths;
		}elseif (preg_match("|^1\.3\.\d+|", Configure::version()) > 0) {
			$paths = App::path('views');
		}

		foreach($paths as $path) {
			if (file_exists($path . $this->viewPath . DS . $this->subDir . $type . $action . $this->ext)) {
				$viewFileName = $path . $this->viewPath . DS . $this->subDir . $type . $action . $this->ext;
				return $viewFileName;
			}
		}

		// added for .ctp viewPath fallback
		foreach($paths as $path) {
			if (file_exists($path . $this->viewPath . DS . $type . $action . '.ctp')) {
				$viewFileName = $path . $this->viewPath . DS . $type . $action . '.ctp';
				return $viewFileName;
			}
		}

		if ($viewFileName = fileExistsInPath(LIBS . 'view' . DS . 'templates' . DS . 'errors' . DS . $type . $action . '.ctp')) {
		} elseif ($viewFileName = fileExistsInPath(LIBS . 'view' . DS . 'templates' . DS . $this->viewPath . DS . $type . $action . '.ctp')) {
		} else {
			$viewFileName = VIEWS . $this->viewPath . DS . $this->subDir . $type . $action . $this->ext;
		}

		return $viewFileName;
	}

	/**
	 * checks for existence of special method on loaded helpers, invoking it if it exists
	 * this allows helpers to register smarty functions, modifiers, blocks, etc.
	 */
	function register_functions() {
		foreach(array_keys($this->loaded) as $helper) {
			if (method_exists($this->loaded[$helper], '_register_smarty_functions')) {
				$this->loaded[$helper]->_register_smarty_functions($this->Smarty);
			}
		}
	}
}
?>
