<?php
/*
 * DON'T REMOVE THE FOLLOWING LICENSE
 *           INFORMATION!
 * ----------------------------------
 * Copyright by
 * Dennis Ritz
 * Author: Dennis Ritz
 * dennis.ritz@gmx.net
 * 2007-2008
 * ----------------------------------
 */

class App extends User {
	protected $HTML;
	protected $JS;
	protected $CSS;
	private $_componentFile = null;
	private $_componentPath = null;
	private $_component		= null;
	private $_componentI18NFile = null;
	
	protected function __construct($p_componentFile = null) {
		parent::__construct();
		$this->_componentFile = $p_componentFile;
		$this->_componentPath = dirname($p_componentFile);
		$this->_component		= array_shift(explode(".",basename($p_componentFile)));
		$this->_componentPath .= "/";
		if(file_exists($this->_componentPath.$this->_component.".i18n.xml"))
			$this->componentI18NFile = $this->_componentPath.$this->_component.".i18n.xml";
	}	
	
	/* OLD FUNCTIONS */
	
    protected function Template(){
        if(!isset($this->Template)) {
            $this->Template = new Smarty();
            $this->Template->left_delimiter = "{[";
            $this->Template->right_delimiter = "]}";
            $this->Template->compile_dir = $this->_componentPath."compiled";
            $this->Template->cache_dir = dirname(__FILE__)."/Smarty/cached";
            $this->Template->config_dir = $this->_componentPath."configs";
            $this->Template->template_dir = $this->_componentPath."templates";
            $this->Template->error_reporting = true;
        }
        return $this->Template;
    }

	protected function __COMPONENT() {
		ob_start();
		if(file_exists($this->_componentPath.$this->_component.".js"))
			include($this->_componentPath.$this->_component.".js");
		$js = ob_get_contents();
		ob_clean();
		if(file_exists($this->_componentPath.$this->_component.".css"))
			include($this->_componentPath.$this->_component.".css");
		$css = ob_get_contents();
		ob_end_clean();		
		$eval = "";//CMgr.setCOMPONENT(".$_component.");".$p_eval;
		return array($js,$css,$eval);	
	}
	
	protected function __MODEL() {
		ob_start();
		if(file_exists($this->_componentPath.$this->_component.".js"))
			include($this->_componentPath.$this->_component.".js");
		$js = ob_get_contents();
		ob_clean();
		if(file_exists($this->_componentPath.$this->_component.".css"))		
			include($this->_componentPath.$this->_component.".css");
		$css = ob_get_contents();
		ob_end_clean();		
		$eval = "CMgr.setMODEL(".$this->_component.");";
		return array($js,$css,$eval);		
	}
	
	protected function __AUTH($p_componentFile = null) {
		ob_start();
		if(file_exists($this->_componentPath.$this->_component.".js"))
			include($this->_componentPath.$this->_component.".js");
		$js = ob_get_contents();
		ob_clean();
		if(file_exists($this->_componentPath.$this->_component.".css"))		
			include($this->_componentPath.$this->_component.".css");
		$css = ob_get_contents();
		ob_end_clean();		
		$eval = "CMgr.setAUTH(".$this->_component.");";
		return array($js,$css,$eval);		
	}
	protected function _getI18N($p_node) {
		$MISConfig = new MISConfig();

		if($this->componentI18NFile != null) {
			$i18nDOM = new DOMDocument('1.0', 'utf-8');
			$i18nDOM->load($this->componentI18NFile);
			$i18nXpath = new DOMXpath($i18nDOM);
			$i18nNodeList = $i18nXpath->query("/i18n/".$p_node."/".$this->_getLang());
			$i18nNodeList2 = $i18nXpath->query("/i18n/".$p_node."");
			if($count = $i18nNodeList->length > 0 && $i18nNodeList->item(0)->nodeValue != "") {
				return $i18nNodeList->item(0)->nodeValue;
			}elseif ($i18nNodeList2->length == 0) {
				$MISConfig->setNodeValue($p_node,"",$i18nDOM,$i18nXpath->query("/i18n")->item(0));
				$i18nDOM->save($this->componentI18NFile);
				return "[".$this->_component.":".$this->_getLang().":".$p_node."=!]";
			} else {
				return "[".$this->_component.":".$this->_getLang().":".$p_node."=?]";	
			}
		} else {
			return "[".$this->_component.":".$this->_getLang().":".$p_node.": NO LANGUAGE PACK INSTALLED]";
		}
	}
	
	// USER
	
	
	final protected function _getLang(){
		$p_username = $this->getUsername();
		
		$userDOM = new DOMDocument('1.0', 'utf-8');
		$userDOM->load($this->authFile);
		$userXpath = new DOMXpath($userDOM);
		$UU = $userXpath->query("/auth/users/user[./@username='".utf8_encode($p_username)."']")->item(0);
		if(!$UU) {
	        $MISConfig = new MISConfig();
	        $projectDOM = new DOMDocument('1.0', 'utf-8');
	        $projectDOM->load($MISConfig->getConfigFile());
	        $projectXpath = new DOMXpath($projectDOM);
			return $projectXpath->query("/Configuration/General/Language")->item(0)->nodeValue;
		}
		return $UU->getAttribute("lang");
	}
	
	final protected function _getView($p_username=null){
		if($p_username==null) $p_username = utf8_decode($this->getUsername());
		$userDOM = new DOMDocument('1.0', 'utf-8');
		$userDOM->load($this->authFile);
		$userXpath = new DOMXpath($userDOM);
		$UU = $userXpath->query("/auth/users/user[./@username='".utf8_encode($p_username)."']")->item(0);
		return $UU->getAttribute("view");
	}
}
?>