<?php
/**
 * AgileBill - Open Billing Software
 *
 * This body of work is free software; you can redistribute it and/or
 * modify it under the terms of the Open AgileBill License
 * License as published at http://www.agileco.com/agilebill/license1-4.txt
 *
 * Originally authored by Tony Landis, AgileBill LLC
 *
 * Recent modifications by Deon George
 *
 * @author Deon George <deonATleenooksDOTnet>
 * @copyright 2009 Deon George
 * @link http://osb.leenooks.net
 *
 * @link http://www.agileco.com/
 * @copyright 2004-2008 Agileco, LLC.
 * @license http://www.agileco.com/agilebill/license1-4.txt
 * @author Tony Landis <tony@agileco.com>
 * @package AgileBill
 * @subpackage Core
 */

/**
 * The main AgileBill CORE Theme Class
 *
 * @package AgileBill
 * @subpackage Core
 * @uses CORE_block
 */
class CORE_theme {
	public $id;

	function __construct() {
		global $VAR,$C_debug,$C_translate,$smarty;

		# Get the cuurent session theme:
		if(defined('SESS_THEME') && file_exists(PATH_THEMES.SESS_THEME.'/template.tpl')) {
			if (SESS_THEME == 'default_admin' && SESS_LOGGED != true )
				define ('THEME_NAME',DEF_THEME_N);
			elseif (defined('ADMIN_FORCE'))
				define ('THEME_NAME','default_admin');
			elseif (! defined('ADMIN_FORCE') && SESS_THEME != 'default_admin')
				define ('THEME_NAME',SESS_THEME);
			else
				define ('THEME_NAME',DEF_THEME_N);

		} elseif(file_exists(PATH_THEMES.DEFAULT_THEME.'/template.tpl')) {
			define ('THEME_NAME',DEFAULT_THEME);

		} else {
			define ('THEME_NAME',DEF_THEME_N);
		}

		# load the block class
		$block = new CORE_block;

		# Set smarty vars
		if (isset($smarty->template_dir))
			unset($smarty->template_dir);

		$smarty->use_sub_dirs = false;
		$smarty->template_dir = PATH_THEMES.THEME_NAME.'/';
		$smarty->compile_dir = PATH_SMARTY.'templates/';
		$smarty->config_dir = PATH_SMARTY.'configs/';
		$smarty->cache_dir = PATH_SMARTY.'cache/';
		$this->caching = false;
		$this->compile_check = true;
		$smarty->assign('THEME_NAME',THEME_NAME);

		# Frame Theme Escape
		if (THEME_NAME == 'default_admin') {
			if (! empty($VAR['tid']) && empty($VAR['_escape'])) {
				# Get URL string to pass to mainFrame
				$url = '';
				$i = 0;

				while (list($key,$val) = each($VAR)) {
					if ($key != 'tid') {
						if ($i==0)
							$url .= '?';
						else
							$url .= '&';

						$url .= sprintf('%s=%s',$key,$val);
						$i++;
					}
				}

				$url = preg_replace('/tid=default_admin/','',$url);
				$smarty->assign('mainFrameUrl',$url);
				$this_template = sprintf('file:%s%s/template.tpl',PATH_THEMES,THEME_NAME);
				$smarty->display($this_template);

				exit;
			}

			if (empty($VAR['_escape']))
				$block->display('core:top_frame');

			# Force or define page set?
			if (defined('FORCE_PAGE')) {
				$block->display(FORCE_PAGE);

			} elseif (@$VAR['_page']) {
				$block->display($VAR['_page']);

			} else {
				$block->display('core:admin');
			}

			if (empty($VAR['_escape']))
				$block->display('core:bottom_frame');

			exit();
		}

		# Standard themes
		if (isset($VAR['_escape'])) {
			if (isset($VAR['_print'])) {
				# Load printer friendly version
				$block->display('core:top_frame');
				$block->display($VAR['_page']);
				$block->display('core:bottom_print');
				exit();

			} else {
				# Check for force page:
				if(defined('FORCE_PAGE'))
					$block->display(FORCE_PAGE);
				else
					$block->display($VAR['_page']);

				exit();
			}

		} else {
			if (defined('FORCE_PAGE')) {
				define('THEME_PAGE',FORCE_PAGE);

			} else {
				if (isset($VAR['_page']))
					define('THEME_PAGE',$VAR['_page']);
				else
					define('THEME_PAGE','core:main');
			}

		   # Load the block normally
		   $this_template = sprintf('file:%s%s/template.tpl',PATH_THEMES,THEME_NAME);
		   $smarty->display($this_template);
		}
	}
}

class CORE_block {
	public function display($block_r) {
		global $smarty;

		$savedir = '';

		# If we are in a module, we need to preserve our template path
		if (isset($smarty->_tpl_vars['meth'][0]) && $smarty->_tpl_vars['meth'][0] != 'core')
			$savedir = $smarty->template_dir;

		@$resource = explode(':',$block_r);
		@$module = $resource[0];
		@$block = $resource[1];

		$displayBlock = false;
		if ($module == 'TEMPLATE') {
			$smarty->template_dir = PATH_THEMES.THEME_NAME.'/';
			$displayBlock = true;

		} else {
			if (is_file(sprintf('%s%s/blocks/%s/%s.tpl',PATH_THEMES,THEME_NAME,$module,$block))) {
				$smarty->template_dir = sprintf('%s%s/blocks/%s/',PATH_THEMES,THEME_NAME,$module);
				$displayBlock = true;

			} elseif (is_file(sprintf('%s%s/blocks/%s/%s.tpl',PATH_THEMES,DEF_THEME_N,$module,$block))) {
				$smarty->template_dir = sprintf('%s%s/blocks/%s/',PATH_THEMES,DEF_THEME_N,$module);
				$displayBlock = true;

			} elseif (is_file(sprintf('%s%s/blocks/%s/%s.tpl',PATH_THEMES,'default',$module,$block))) {
				$smarty->template_dir = sprintf('%s%s/blocks/%s/',PATH_THEMES,'default',$module);
				$displayBlock = true;

			} else {
				$smarty->display(sprintf('file:%s%s/blocks/core/invalid_page.tpl',PATH_THEMES,DEF_THEME_N));
			}
		}

		if ($displayBlock) {
			if (! isset($smarty->_tpl_vars['VAR']['_page']) || ! $smarty->_tpl_vars['VAR']['_page'])
				$smarty->_tpl_vars['VAR']['_page'] = $block_r;

			$smarty->display(sprintf('file:%s%s.tpl',$smarty->template_dir,$block));
		}

		if ($savedir)
			$smarty->template_dir = $savedir;
	}
}
?>