<?php
/**
 * ****************************************************************
 * Project:      *
 * Copyright:    Copyright (c) 2007
 * Company:      Thomas Niemann
 * ****************************************************************
 * Workfile:     ContentLoader.php
 * ****************************************************************
 * Description:
 *
 * ***************************************************************/
class BasicLoader{
  // change languageSetting
	//private $language= $_GET['language'];
	protected $language = "DE";
  	protected $xmlContent;
	/****************************************************************
	 * private constructor to prevent creation outside
	 * this class
	 ****************************************************************/
	private function __construct()
	{
		$this->language = empty ($_SESSION['language']) ? (empty($_GET['language'])? "DE" : $_GET['language']): $_SESSION['language'];
	}
	
	/*****************************************************************
	 * returns the expected element safety -> by utf_decode & htmlentities
	 ****************************************************************/
	protected function getElement($_elementPath, $_messageId, $_raw = false)
	{
		$itemList = explode("->", $_elementPath);
		$node = $this->xmlContent;
		// iterate XMLnodes
		foreach ($itemList as $item)
		{
			$node = $node-> $item;
		}
		// iterate messages and return if equals	
		foreach ($node as $message)
		{
			if (strcmp($message->attributes()->name, $_messageId) == 0)
			{
				if (!$_raw)
				{
					return $this->convertElement($message->getValue());
				}
				else
				{
					return $message->getValue();
				}
			}
		}
		// if message havent't been found
		//throw new Exception('Message '.$_elementPath.'->'.$_messageId.' does not exist.');
	}
	
	/****************************************************************
	 * setting template variables and display the content
	 ****************************************************************/
	public static function showMessages()
	{
		$messages = null;
		if (!empty ($_SESSION['messages']))
		{
			foreach ($_SESSION['messages'] as $message)
				$messages .= $message . "<br />";
		}
		$smarty = SmartyHelper :: getInstance();
		$smarty->assign('MESSAGE_TEXT', $messages);
		$smarty->display('message.tpl');
		unset ($_SESSION['messages']);
	}
	/*****************************************************************
	 * converts XML Elements by utf_decode & htmlentities etc.
	 ****************************************************************/
	protected function convertElement($_string) {

		// convert all chars with htmlEntites
		$temp = htmlentities(utf8_decode($_string));

		// but if there are some needed htmlchars, include this here:
		$search 	= array('&lt;', '&gt;', '&amp;euro;', '&amp;Sigma;', '&amp;nbsp;', '&amp;rarr;');
		$replace 	= array('<'		, '>'		, '&euro;'		, '&Sigma'     , '&nbsp;'    , '&rarr;');

		return str_replace($search, $replace, $temp);
	}
}