<?php
/*
EasyGUI XP is free software released under the terms of the following BSD license.

Copyright (c) 2005,2006 Gaetan Lauff (glauff@plansoft.de)
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

- Redistributions of source code must retain the above copyright notice, 
this list of conditions and the following disclaimer. 

- Redistributions in binary form must reproduce the above copyright notice, 
this list of conditions and the following disclaimer in the documentation 
and/or other materials provided with the distribution.

- Neither the name of the developer nor the names of its contributors may 
be used to endorse or promote products derived from this software without 
specific prior written permission. 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

require_once('adodb/adodb.inc.php');
require_once('adodb/adodb-errorhandler.inc.php'); 
require_once('base/class.ek_view.php');
require_once('base/class.ek_response.php');
require_once('dataspace/class.ek_datahandler.php');
require_once('dataspace/class.ek_applicationhandler.php');
require_once('dataspace/class.ek_valuehandler.php');
require_once('dataspace/class.ek_validator.php');
require_once('dataspace/class.ek_valueconverter.php');

/**
 * EasyKat Web Application Base Class
 *
 * The EasyGUI XP WebApp Class provides the basic functionality
 * for the Web Applications
 *
 * A Web Application based on EasyKat has to extend this Class
 * Please read the EasyGUI XP Users Guide for more information about
 * how to write Web Applications with the EasyGUI XP Framework.
 * 
 * @author Gaetan Lauff <glauff@plansoft.de>
 * @version	XP $Id: class.ek_webapp.php 51 2006-03-15 15:57:21Z glauff $
 * @package base
 */
class EK_Webapp extends EK_Base
{
	/**
	 * Application Language
	 * @var string
	 */
	var $appLang;
	
	/**
	 * Country Information
	 * @var string
	 */
	var $appCountry;
	
	/**
	 * Locale Info
	 * @var string
	 */
	var $appLocale;
	
	/**
	 * Client Prefered Languages
	 * @var	string
	 */
	var $preferedLanguages;
	
	/**
	 * Available Languages
	 * @var array
	 */
	var $availableLanguages;
	
	/**
	 * Supported Languages
	 * @var array
	 */
	var $supportedLanguages;
	
	/**
	 * A Collection of all Database Handlers used in the Webapp
	 * @var	array
	 */
	var $dbHandlers;
	
	/**
	 * @see EK_Session
	 * @var	object
	 */
	var $session;
	
	/**
	 * The Application Controller
	 * @var	object
	 */
	var $applicationHandler;
	
	/**
	 * Authentication Handler
	 * @var object
	 */
	var $authHandler;
	
	/**
	 * I18N Support
	 * @var	object
	 */
	var $dictionary;
	
	/**
	 * All Values
	 * @var	array
	 */ 
	var $values;
	
	/**
	 * Dialog Event
	 * @var	string
	 */
	var $dlgEvt;
	
	/**
	 * Dialog Id
	 * @var	string
	 */
	var $dlgId;
	
	/**
	 * Target Dialog Id
	 * @var	string
	 */
	var $tgtId;
	
	/**
	 * Dialog Event Handling Class
	 * @see EK_DlgHandler
	 * @var object
	 */
	var $dlgHandler;
	
	/**
	 * Dialog Controller Class
	 * @see EK_DlgController
	 * @var object
	 */
	var $dlgController;
	
	/**
	 * @see EK_ActionHandler
	 * @var object
	 */
	var $actionHandler;
	
	/**
	 * Data Handler for insert, update, delete
	 * @see EK_DataHandler
	 * @var object
	 */
	var $dataHandler;
	
	/**
	 * Collection of all Dialog Vars
	 * @var Array
	 */ 
	var $dlgVars;
	
	/**
	 * Collection of all Dialog Values
	 * @var Array
	 */
	var $dlgValues;
	
	/**
	 * Collection of all Database Values
	 * @var	array
	 */
	var $dbValues;
			
	/**
	 * Collection of all Target Dialog Variables
	 * @var Array
	 */
	var $targetDlgVars;
	
	/**
	 * Database Handler
	 * @var	object
	 */
	var $dbHandler;
	
	/**
	 * Default Database Handler
	 * @var object
	 */
	var $defHandler;
	
	/**
	 * Server Side Validator
	 * @see EK_Validator
	 * @var object
	 */
	var $validator;
	
	/**
	 * Value Converter
	 * @see EK_ValueConcerter
	 * @var object
	 */
	var $converter;
	
	/**
	 * Localizer
	 * @see EK_Localizer
	 * @var object
	 */
	var $localizer;
	
	/**
	 * Enter description here...
	 *
	 * @var unknown_type
	 */
	var $currentView;
	
	/**
	 * Enter description here...
	 *
	 * @var unknown_type
	 */ 
	var $nextView;
	
	/**
	 * Enter description here...
	 *
	 * @var unknown_type
	 */ 
	var $srcId;
	
	/**
	 * @var Response Object
	 */
	var $response;
	
	/**
	 * Array of Components IDs that are not rendered
	 *
	 * @var array
	 */
	var $preventOutput;
	
	var $ajax;
	
	var $handled;
	
	/**
	 * Constructor
	 *
	 * The EK Webapp ist instantiated by the EK_Factory
	 *
	 * @param 	array	&$config	Reference to global Configuration Array
	 * 
	 * @return 	void
	 *
	 * @access 	private
	 * @see EK_Factory
	 */
	function EK_Webapp( &$config )
	{
		parent::EK_Base( $config );
		$this->session 	= &new EK_Session();
		
		@define('EK_SESSNAME', session_name() );
		$this->session->set( 'EK_SESSID', session_id() );
	}

	/**
	 * Initializes the Application Controller
	 *
	 * First call this method in your application class to initialize the whole application
	 *
	 * @return	void
	 *
	 * @access 	private
	 */
	function init()
	{
		$this->dlgVars 			= array();
		$this->dlgValues 		= array();
		$this->dbValues 		= array();
		
		$this->targetDlgVars 	= array();
		
		$dlgId			= EK_Common::getSubmitValue( 'DLG_ID' );
		$dlgEvt			= EK_Common::getSubmitValue( 'EKSUBMITEVENT' );
		
		$this->ajax		= EK_Common::getSubmitValue( 'EK_REQUEST');
		
		$this->dlgId  	= ( empty( $dlgId ) ) ? '*' : $dlgId;
		$this->dlgEvt	= ( empty( $dlgEvt ) ) ?  '*' : $dlgEvt;
	
		$this->setTemplatePath();
		$this->setEngines();
		$this->initHandlers();
		
		$this->setLanguage();
		$this->setCountry();
		$this->setLocale();

		$this->updateDialog();
		
		$this->handled = $this->currentView->actionHandler->handleActions( );
		
		$this->setAppData();
		$this->prepareDialog();
	}
	
	/**
	 * Enter description here...
	 *
	 */
	function initView()
	{
		if ( $this->ajax )
			return;
		
		$this->nextView =& $this->getCurrentView();
		$this->nextView->initView();
		parent::initView();
	}
	
	/**
	 * Resets the Dialog
	 *
	 * @param string	optional Dlg ID Default is the current Dialog
	 *
	 * @access public
	 */
	function resetDialog( $dlgId = null )
	{
		if (! is_null( $dlgId ) ) {
			if ( ! $this->session->idxExists( $dlgId ) ) {
				trigger_error( 'Cannot reset Dialog: '.$dlgId.' Session idx does not exist ! ', E_USER_WARNING );
			}
		} else {
			$dlgId = $this->dlgId;
		}
		
		$sql = "SELECT * FROM dlg_vars WHERE dlg_id='" . $dlgId . "' AND global=1";
		$result =& $this->dbHandler->Execute( $sql );
		
		while( $row = $result->FetchRow() ) {
			$this->unsetGlobalValue( $row['ctrl_id'] );
		}
			
		$this->session->del( $dlgId );
	}
	
	/**
	 * Tries to set the default language, set in the webapp.ekw file.
	 * If the default language is not set and no language information is submit
	 * this defaults to the preferred language set in the users browser
	 * If the Language is not set in the Session
	 *
	 * @return void
	 *
	 * @access private
	 * @internal 
	 */
	function setLanguage()
	{
		$this->setPreferedLanguages();
		$this->availableLanguages = $this->dictionary->getLanguages();
		
		if ( !$this->session->get('APP_LANG') ) {
			$lang = EK_Common::getSubmitValue( 'APP_LANG', $this->config->getAttribute( $this->settings, 'defaultlanguage' ) );
			$this->appLang = $this->getLanguage( $lang );
			$this->session->set('APP_LANG', $this->appLang );
		} else {
			$this->appLang = $this->session->get('APP_LANG');
		}
	}
	
	/**
	 * Tries to set the country information, set in the webapp.ekw file.
	 * If the defaultcountry is not set and no country information is submit
	 * this defaults to the preferred country set in the users browser
	 * 
	 * @todo implement the right way
	 *
	 * @return void
	 *
	 * @access private
	 * @internal 
	 */
	function setCountry()
	{
		if ( !$this->session->get('APP_COUNTRY') ) {
			$country = EK_Common::getSubmitValue('APP_COUNTRY', $this->config->getAttribute( $this->settings, 'defaultcountry') );
			$this->appCountry = $this->getCountry( $country );
			$this->session->set('APP_COUNTRY', $this->appCountry );
		} else {
			$this->appCountry = $this->session->get('APP_COUNTRY');
		}
	}

	/**
	 * Sets the current locale information based on the settings in the workspace file
	 * or the presets in the users browser
	 * 
	 * @todo implement the right way
	 * 
	 * @return void
	 *
	 * @access private
	 * @internal 
	 */
	function setLocale()
	{
		$this->appLocale = strtolower( $this->appLang ) . "_" . strtoupper( $this->appCountry );
		$this->session->set( 'APP_LOCALE', $this->appLocale );
	}
	
	/**
	 * Tries to set the template path, set in the webapp.ekw file.
	 * The default is a directory called 'tpl' under your webapp directory
	 *
	 * @return void
	 *
	 * @access private
	 * @internal 
	 */
	function setTemplatePath()
	{
		$template_dir = $this->config->getAttribute( $this->settings, 'template_dir');
		define('TPL_DIR', empty($template_dir) ? './tpl' : $template_dir );
		$this->tplPath = APPPATH . '/' . TPL_DIR;
	}
	
	/**
	 * Set up any external Template Engines
	 * 
	 * You can use external template engines to design your application
	 * without sacrificing the power of easyguiXP.
	 * You can define the external Template Engines in the Workspace File.
	 * Please read the EasyGUI XP Users Guide for more information on external
	 * Template Engines and their usage
	 *
	 * @return 	void
	 *
	 * @access 	private 
	 */
	function setEngines()
	{
		$this->engines = array();
		
		$templateEngines 	= $this->config->getElementsByTagName( 'TemplateEngines' );
		$engines			= EK_SimpleXML::childNodes( $templateEngines );
		
		foreach( $engines as $node ) {
			$id = $this->config->getAttribute( $node, 'id' );
			
			if (!$id) {
				trigger_error('Missing ID for Template Engine in Workspace File', E_USER_ERROR);
			}
				
			$className = $this->config->getAttribute( $node, 'class' );
			
			if (!$className) {
				trigger_error('No ClassName set for Template Engine\r\nYou must manually include the Template Engine !', E_USER_NOTICE);
			} else {
				require_once( $className );
			}
			
			$params = EK_SimpleXML::childNodes( $node );
			$settings = array();
			
			foreach ( $params as $node ) {
				$name = $this->config->getAttribute( $node, 'name' );
				$settings[$name] = $this->config->getAttribute( $node, 'value' );
			}
			
			$this->engines[$id] =  $settings;
		}
	}
	
	/**
	 * Init all Handlers for Application to function
	 *
	 * Here we initialize all Handlers needed by the Application
	 * What's initialized is defined in the Workspace File.
	 * Please read the EasyGUI XP Users Guide for more Informations on the
	 * Workspace File and the possibilities you have to configure your
	 * WebApplication
	 * 
	 * @return	void
	 *
	 * @access 	private
	 */
	function initHandlers()
	{
		$this->dbHandlers 	= array();
		$this->preventOutput = array();

		$auth	= $this->config->getAttribute( $this->settings, 'authentication');
		$signup	= $this->config->getAttribute( $this->settings, 'signup' );
		$i18n	= $this->config->getAttribute( $this->settings, 'i18n');
		$l10n	= $this->config->getAttribute( $this->settings, 'l10n');
		
		/* Authorization not implemented yet *
		$authorization		= EK_Common::getArrayValue( $this->settings, 'authorization');
		*/
		
		/**
		 * Handler Initialization
		 */
		$this->valueHandler			=& new EK_ValueHandler( $this );
		$this->applicationHandler 	=& new EK_ApplicationHandler( $this );
		
		$connections = $this->config->getElementsByTagName( 'Connections' );
		$connections = EK_SimpleXML::childNodes( $connections );
		
		foreach( $connections as $node ) {
			$connectstr = $this->config->getAttribute( $node, 'connectstring');
			$dsn		= $this->config->getAttribute( $node, 'dsn');
			$appctl		= $this->config->getAttribute( $node, 'appctl');
			$default	= $this->config->getAttribute( $node, 'default');
			
			$connect = parse_url( $connectstr );
			
			if (isset($connect['port']) && !empty($connect['port'])) {
				$connect['host'] .= ':'.$connect['port'];
			}
			
			//$db->debug = true;
			
			if ( preg_match("/^mysql/", $connect['scheme'] ) ) {
				$db = ADONewConnection($connect['scheme']);
    			$db->NConnect($connect['host'], $connect['user'], $connect['pass'], str_replace('/', '', $connect['path'])); 
    		
    			if (!$db->isConnected()) {
					trigger_error( $db->ErrorMsg(), E_USER_ERROR );
				}
				
    			if (EK_ENCODING == 'UTF-8') {
    				$info =  $db->ServerInfo();
    				
    				$version = (float) $info['version'];
    				
    				if  ($version >= 4.1) {
						$sql = "SET NAMES 'utf8';";
						$db->Execute( $sql );
    				}
    			}
    			
			} else {
				$db = ADONewConnection( $connectstr );
			}
						
    		$this->dbHandlers[$dsn] = $db;
    		
			if ( $appctl ) {
				$this->dbHandler = $this->dbHandlers[$dsn];
				$this->dbHandlers['appctl'] =& $this->dbHandler;
			}

			if ( $default ) {
				$this->defHandler = $this->dbHandlers[$dsn];
				$this->dbHandlers['default'] =& $this->defHandler;
			}
		}
		
		if ( sizeof( $this->dbHandlers ) == 0 ) {
			if ( file_exists( APPPATH . '/webapp.db' ) ) {
				$this->dbHandler = ADONewConnection( 'sqlite://webapp.db' );
				$this->dbHandlers['default'] = $this->dbHandler;
			} else {
				trigger_error('No Database Connection set in Workspace file. Please check your settings !', E_USER_ERROR );
			}
		}

		if (!isset($this->dbHandlers['default'])) {
			trigger_error('You have no default database handler set. Setting appctl Connection as default !', E_USER_NOTICE );
			
			$this->defHandler =& $this->dbHandler;
			$this->dbHandlers['default'] =& $this->defHandler;
		}
		
		if ( $auth ) {		
			$node	= $this->config->getElementsByTagName( 'Authentication');
			$dsn 	= $this->config->getAttribute( $node, 'dsn' );
			
			if ( !empty( $dsn ) ) {
				require_once( 'auth/class.ek_auth.php' );
				$this->authHandler = &new EK_Auth($this->getDatabaseHandler( $dsn ), $this->session, $node );
			} else {
				trigger_error('Authentication DSN not set !', E_USER_ERROR);
			}
		} else {
			$this->authHandler = null;
		}
		
		/*
		if ( $signup ) {
			$node 	= $this->config->getElementsByTagName( 'Signup');
			$dsn 	= $this->config->getAttribute( $node, 'dsn' );
			
			if (!empty( $dsn )) {
				require_once('auth/class.ek_signup.php');
				$this->signupHandler = &new EK_SignUp( $this->getDatabaseHandler($dsn), $this->session, $node );
			} else {
				trigger_error('Signup DSN not set !', E_USER_ERROR);
			}
		}
		*/
		
		if ( $i18n ) {		
			$node 	= $this->config->getElementsByTagName( 'Dictionary');
			$dsn 	= $this->config->getAttribute( $node, 'dsn' );
			
			require_once('i18n/class.ek_dictionary.php');
				
			if ( !empty( $dsn ) ) {
				$dbHandler = $this->getDatabaseHandler( $dsn );
			} else {
				$dbHandler = $this->getDatabaseHandler('default');
			}
			
			$this->dictionary = &new EK_Dictionary( $dbHandler, $node );
		} else {
			$this->dictionary = null;
		}
		
		if ( $l10n ) {
			if (!setlocale( LC_ALL, $this->appLocale )) {
				trigger_error('Your locale ' . $this->appLocale . ' is not supported on this system ! Switching to default !');
			}
		}

		$this->currentView			=& $this->getCurrentView();
		
		$this->validator 			=& new EK_Validator( $this->session );
		$this->converter 			=& new EK_ValueConverter( $this->session );
		$this->dataHandler 	 		=& new EK_DataHandler( $this );
		//$this->response				=& new EK_Response( $this );
	}
	
	/**
	 * Returns the Current View Object
	 *
	 * @return object
	 * @access private
	 */
	function &getCurrentView()
	{	
		$className = 'EK_View';
		$dlgId = strtolower( $this->dlgId );
		
		if ( file_exists( CLASSPATH . '/class.' . $dlgId . '.php' )) {
			require_once( 'class.' . $dlgId . '.php' );
		} elseif ( file_exists( CLASSPATH . '/' . $dlgId . '/class.' . $dlgId . '.php' ) ) {
			require_once( $dlgId . '/class.' . $dlgId . '.php' );
		}
		
		if ( file_exists( CLASSPATH . '/class.view.' .$dlgId . '.php' ) ) {
			require_once( 'class.view.' . $dlgId . '.php' );
		} elseif ( file_exists( CLASSPATH . '/' . $dlgId . '/class.view.' . $dlgId . '.php') ) {
			require_once( $dlgId . '/class.view.' . $dlgId . '.php' );
		}
			
		if ( class_exists( $dlgId . '_View' ) ) {
			$className = $dlgId . '_View';
		} else {
			//$dlgId = strtolower( $this->dlgId );
			
			if ( file_exists( CLASSPATH . '/class.' . $dlgId . '.php' )) {
				require_once( 'class.' . $dlgId . '.php' );
			} elseif ( file_exists( CLASSPATH . '/' . $dlgId . '/class.' . $dlgId . '.php' ) ) {
				require_once( $dlgId . '/class.' . $dlgId . '.php' );
			}
			
			if ( file_exists( CLASSPATH . '/class.view.' . $dlgId . '.php' ) ) {
				require_once( 'class.view.' . $dlgId . '.php' );
			} elseif ( file_exists( CLASSPATH . '/' . $dlgId . '/class.view.' . $dlgId . '.php') ) {
				require_once( $dlgId . '/class.view.' . $dlgId . '.php' );
			}
				
			if ( class_exists( $dlgId . '_View' ) ) {
				$className = $dlgId . '_View';
			}
		}
		
		return new $className( $this );
	}
	
	/**
	 * Returns the Target Dialog
	 *
	 * @return string
	 * @access public
	 */
	function getTargetDlgId() 
	{
		return EK_Common::getSubmitValue( 'EK_TARGET' );
	}
	
	/**
	 * Sets the Target Dialog
	 * Can be called by an Action Handler to set a new Target Dialog
	 *
	 * @param string $dlgId
	 * @access public
	 */
	function setTargetDlgId( $dlgId )
	{
		EK_Common::setSubmitValue( 'EK_TARGET', $dlgId );
	}
	
	/**
	 * Sets Dialog Data
	 *
	 * @return void
	 *
	 * @access private
	 */
	function setAppData()
	{
		$dlgId = $this->getTargetDlgId();
		
		if ( !$this->handled ) {
			$dlgId = $this->dlgId;
		}
			
		if ( strtolower( $dlgId ) == 'back') {
			$dlgId = array_pop( $_SESSION['DLGSTACK'] ) ;
			array_push( $_SESSION[ 'DLGSTACK' ], $dlgId );
		}
					
		$this->dlgId 	= ( empty( $dlgId ) ) ? ( $this->dlgId == '*' ) ? EK_INDEX_DLG : $this->dlgId : $dlgId;
		
		/**
		 * Init Global Variables if they are not set
		 */
		if ( !$this->session->idxExists( '*' ) ) {
			$sql = "SELECT * FROM dlg_vars WHERE (dlg_id='" . $this->dlgId . "' OR dlg_id='*') AND global=1";
		
			$result =& $this->dbHandler->Execute( $sql );
			
			while( $row = $result->FetchRow() ) {
				$this->session->set( $row['ctrl_id'], $this->getDefaultSessionValue( $row['ctrl_default_value'] ), '*' );
			}
		}
		
		$this->session->set( 'EK_APPNAME', APPNAME );
	}
	
	/**
	 * Updates the Dialog Arrays
	 *
	 * Fills Dlg Vars
	 * Fills Dlg Tables
	 * Updates all relevant Session Data
	 *
	 * @return void
	 *
	 * @access private
	 */
	function updateDialog()
	{
		$dlg_id = $this->dlgId;

		$sql = "SELECT * FROM dlg_vars WHERE dlg_id='" . $dlg_id . "' OR dlg_id='*'";// AND ( global <> 1 OR global is null ) )";
		$result =& $this->dbHandler->Execute( $sql ); 
	
		if (! $this->session->idxExists( $dlg_id ) ) {
			$this->session->set( $dlg_id, array() );
		}
	
    	while ($row = $result->FetchRow()) {
			$ctrl_id 	= $row['ctrl_id'];
			$table		= $row['ctrl_db_table'];
			$field		= $row['ctrl_db_field'];
			$format		= $row['ctrl_in_format'];
			
			$global 	= $row['global'];
			
			$dbHandler = ( empty( $row['ctrl_db_dsn'] ) ) ? $this->defHandler : $this->getDatabaseHandler( $row['ctrl_db_dsn'] );
								
			if (EK_Common::issetSubmitVar($ctrl_id)) {
				$value = EK_Common::getSubmitValue( $ctrl_id );
				
				$this->dlgValues[$dlg_id][$ctrl_id] = $value;
			
				if (!empty( $table ) && !empty($field) ) {
					if ( is_array( $value ) ) {
						$this->dbValues[$dlg_id][$ctrl_id] = $value;
					} else {
						$this->dbValues[$dlg_id][$ctrl_id] = $this->converter->getDBValue( $value, $format, $this->getField( $table, $field, $dbHandler), $dbHandler );
					}
				}
			
				if ( $global ) {
					$this->session->set( $ctrl_id, $value, '*' );
				}
				
				$this->session->set( $ctrl_id, $value, $dlg_id );
			} else {
				$value =  $this->getSessionValue( $ctrl_id );
				$this->dbValues[$dlg_id][$ctrl_id] = $value;
			}
			
			$this->dlgVars[$dlg_id][$ctrl_id] = $row;
		}
	}
	
	/**
	 * Sets the target dialog fields
	 *
	 * Fills the targetDlgVars Array with the right values
	 *
	 * @return void
	 *
	 * @access private
	 */
	function prepareDialog() 
	{
		if (empty($this->dlgId)) 
		{
			$this->dlgId = $this->getTargetDlg();
		}
		
		$sql = "SELECT * FROM dlg_vars WHERE dlg_id='" . $this->dlgId . "' OR dlg_id='*'"; //( dlg_id='*' AND ( global <> 1 OR global is null)  )";
		$result =& $this->dbHandler->Execute( $sql );
		
		if (!$this->session->idxExists( $this->dlgId ) ) 
		{
			$this->session->set( $this->dlgId, array() );
		}
		
		while( $row = $result->FetchRow() ) 
		{
			$val 	= null;
			$id 	= $row['ctrl_id'];
			$global = $row['global'];
			
			$this->targetDlgVars[$id] = $row;
			$dlgId = $this->dlgId;
			
			if ( ( ! ($this->session->idxExists( $id, $dlgId )) ) && !$this->session->idxExists( $id, '*' ) )
			{
				if ( !is_null( $row['ctrl_db_field'] ) ) 
				{
					$val = $this->getValue( $row['ctrl_id'] );
				} 
				
				if ( is_null($val) || $val === false )
				{
					$val = $this->getDefaultValue( $row['ctrl_default_value'] );
				}
				
				$this->setDlgValue( $id, $val );
			}
		}
		
		$dlgKeys = $this->getDlgKeys( $this->dlgId );
		
		if ( !empty( $dlgKeys ) ) {
			foreach ( $dlgKeys as $table => $row ) {
				$table 		= $row['key_table'];
				$key 		= explode( '.', $row['key_ctrl'] );
				$sessionKey	= $this->dlgId;
				
				if (empty($key[1])) {
					$dlgKey 	= $key[0];
				} else {
					$sessionKey = $key[0];
					$dlgKey 	= $key[1];
				}
				
				$keyVal = $this->getSessionValue( $dlgKey, $sessionKey );
			
				$_SESSION[$this->dlgId]['DLG_TABLES']['PRIMARY_KEYS'][$table] = $keyVal;
				
				$sql = "SELECT DISTINCT ctrl_db_table FROM dlg_vars WHERE dlg_id='".$this->dlgId."'";
				$result =& $this->dbHandler->Execute( $sql );
				
				while ($row = $result->FetchRow()) {
					$sql = "SELECT * FROM dlg_table_refs WHERE table_name='" . $row['ctrl_db_table'] . "' AND foreign_key is not NULL";
					$res =& $this->dbHandler->Execute( $sql );
				
					while ( $_row = $res->FetchRow() ) {
						if ($_row['reference_table'] == $table) {
							if ( isset( $_SESSION[$this->dlgId]['DLG_TABLES']['PRIMARY_KEYS'][$table] ) && !empty( $_SESSION[$this->dlgId]['DLG_TABLES']['PRIMARY_KEYS'][$table] ) ) {
								$_SESSION[$this->dlgId]['DLG_TABLES']['FOREIGN_KEYS'][$_row['table_name']][$_row['foreign_key']] = $_SESSION[$this->dlgId]['DLG_TABLES']['PRIMARY_KEYS'][$table];
							}
						}
					}
				}
			}
		}
	}
	
	function getDefaultValue( $value ) 
	{
		$val = $this->getDefaultSessionValue( $value );
		
		if ( !$val ) {
			$val = $this->getValueFromExpression( $value );
		} 
		
		if ( !$val ) {
			$val = $value;
		}
		
		return $val;
	}
	
	function getDefaultSessionValue( $val )
	{
		if (preg_match('/\$(.*)\$/i', $val, $matches ) ) {
			$sessval = $this->getSessionValue( $matches[1] );
					
			if ( $sessval === false ) {
				return false;
			} 
			
			return str_replace( $matches[0], $sessval, $val );
		}
		
		return false;
	}
	
	function getValueFromExpression( $expression )
	{
		$val = null;
		
		if ( preg_match('/\$\{(.*)\}/i', $expression, $matches ) ) {
			$command = trim( $matches[1], ';' );
			
			eval( '$val = ' . $command . ';' );
			return $val;
		}
		
		return $expression;
	}
	/**
	 * @abstract 
	 */
	function initDialog()
	{
		;// abstract class
	}
		
	/**
	 * Overrides submitted DlgId
	 *
	 * @param	string
	 * 
	 * @return 	void
	 *
	 * @access 	public
	 */
	function setDlgId( $id )
	{
		$this->dlgId = $id;
	}
	
	/**
	 * Returns the current Dialog Id
	 *
	 * @return string
	 *
	 * @access public
	 */
	function getDlgId()
	{
		return $this->dlgId;
	}
	
	/**
	 * Returns the Source Dialog Id
	 *
	 * @return string
	 *
	 * @access public
	 */
	function getSrcId()
	{
		return $this->srcId;
	}

	/**
	 * Overrides submitted DlgEvent
	 *
	 * @param	string
	 * 
	 * @return 	void
	 *
	 * @access 	public
	 */
	function setDlgEvt( $id )
	{
		$this->dlgEvt = $id;
	}
	
	/**
	 * returns current dialog event id
	 *
	 * @return string
	 *
	 * @access public
	 */
	function getDlgEvt()
	{
		return $this->dlgEvt;
	}
	
	/**
	 * Sets a global Value in the Session
	 * The reference is a *
	 *
	 * @param string	$idx
	 * @param string	$default
	 *
	 * @return void
	 *
	 * @access public
	 */
	function setGlobalValue( $idx, $val )
	{
		$this->session->set( $idx, $val, '*' );
	}
	
	/**
	 * Returns a global value from session
	 *
	 * @param string	$idx;
	 * @access public
	 */
	function getGlobalValue( $idx )
	{
		return $this->session->get( $idx, '*' );
	}
	
	/**
	 * Deletes a global value from Session
	 *
	 * @param string $idx
	 * @access public
	 */
	function unsetGlobalValue( $idx )
	{
		$this->session->del( $idx, '*' );
	}
	
	/**
	 * Sets a dialog value according informations found in the dlg_vars table
	 * or sets a new session variable
	 * 
	 * If the variable is defined global, the global variable is updated.
	 *
	 * @param 	string	$idx
	 * @param 	string	$val
	 * @param 	string	$dlgId
	 * @access 	public
	 */
	function setDlgValue( $idx, $val, $dlgId = null )
	{
		if ( is_null( $dlgId ) ) {
			$dlgId = $this->dlgId;
		}
		
		$dlgVar = $this->getDlgVar( $dlgId, $idx );
		
		if (!empty($dlgVar)) {
			if ( $dlgVar['global'] ) {
				$this->setGlobalValue( $idx, $val );
			} else {
				$this->session->set( $idx, $val, $dlgId );
			}
			
			return;
		}
		
		$this->session->set( $idx, $val, $dlgId );
	}
	
	/**
	 * Returns a current dialog value from session
	 * The Function checks wheter the defined Variable is searched in the global
	 * context or in a local context and is just an alias for getSessionValue.
	 *
	 * @param string	$idx;
	 * @access public
	 */
	function getDlgValue( $idx, $dlgId = null )
	{
		if ( is_null( $dlgId ) ) {
			$dlgId = $this->dlgId;
		}
		
		return $this->getSessionValue( $idx, $dlgId );
	}
	
	/**
	 * Deletes a current Dialog value from Session
	 * if the variable is defined to be global the global representation is deleted too.
	 *
	 * @param string	$idx
	 * @access public
	 */
	function unsetDlgValue( $idx, $dlgId )
	{
		if ( is_null( $dlgId ) ) {
			$dlgId = $this->dlgId;
		}
		
		$dlgVar = $this->getDlgVar( $dlgId, $idx );
		
		if (!empty($dlgVar) ) {
			if ($dlgVar['global']) {
				$this->unsetGlobalValue( $idx );
			} else {
				$this->session->del( $idx, $dlgId );
			}
			
			return;
		}
		
		$this->session->del( $idx, $dlgId );
	}
	
	/**
	 * Sets a Program Value
	 * They are just relevant in a program context 
	 */
	function setAppValue( $var, $val )
	{
		$this->targetDlgVars[$var] = $val;
	}
	
	/**
	 * Returns a Program Value from the target Dialog Vars Array
	 *
	 * @param string $var
	 * @return The Value if it's set in the array, FALSE otherwise
	 */ 
	function getAppValue( $var ) 
	{
		if (isset($this->targetDlgVars[$var])) {
			return $this->targetDlgVars[$var];
		}
		
		return false;
	}
	
	/**
	 * Changes Application Language
	 * This can be used as a Callback Handler
	 * 
	 * @param 	string	$lang	The language in which the application should switch
	 *
	 * @return 	void
	 *
	 * @access 	public
	 */
	function changeLanguage( $lang )
	{
		$this->appLang = $lang;
		$this->session->set( 'APP_LANG', $lang );
	}
	
	/**
	 * Changes Application Country
	 * This can be used as a Callback Handler
	 * 
	 * @param 	string	$lang	The country to which the application should switch
	 *
	 * @return 	void
	 *
	 * @access 	public
	 */
	function changeCountry( $country )
	{
		$this->appCountry = $country;
		$this->session->set( 'APP_COUNTRY', $country );
	}
	
	/**
	 * Changes Application Country
	 * This can be used as a Callback Handler
	 * 
	 * @param 	string	$lang	The country to which the application should switch
	 *
	 * @return 	void
	 *
	 * @access 	public
	 */
	function changeLocale( $locale )
	{
		$this->appLocale = $locale;
		$this->session->set( 'APP_LOCALE', $locale );
	}
	
	/**
	 * Returns current Language
	 *
	 * @return 	string	The current language set in the session
	 *
	 * @access 	public
	 */
	function getApplicationLanguage()
	{
		return $this->appLang;
	}
	
	/**
	 * Returns a user Setting from the Workspace File
	 *
	 * @param 	$name	Name of Setting to search for
	 * @return  Value of Setting, FALSE otherwise
	 * @access 	public
	 */
	function getUserSetting( $name )
	{
		$value = null;
		$section = $this->config->getElementsByTagName( 'UserSettings' );
		$settings = EK_SimpleXML::childNodes( $section );
		
		foreach ( $settings as $node ) {
			$setting = $this->config->getAttribute( $node, 'name' );
			
			if ($setting == $name) {
				$value = $this->config->getAttribute( $node, 'value' );
				return $value;
			}
		}
		
		if ( is_null( $value ) ) {
			trigger_error( 'User Setting ' . $name . ' not found !', E_USER_NOTICE );
		}
		
		return $value;
	}

	/**
	 * Returns the preferred Language from Browser
	 *
	 * @return string	The preferred language set in the users browser or 'eng' by default.
	 *
	 * @access public
	 */
	function getLanguage( $_lang )
	{
		if (!empty( $_lang ) ) {
			return $_lang;
		}
		
		foreach( $this->preferedLanguages as $language ) {
			$language = explode( '-', $language );
	   		$language = $language[0];
	   		
	   		if ( isset( $this->availableLanguages[strtoupper( $language )] ) ) {
	   			return $language;
	   		}
		}
		
		return 'en';
	}
	
	/**
	 * Returns the Locale Language
	 */
	function getLocaleLang( $_lang ) 
	{   			
		include( 'i18n/inc/inc.global.languages.php' );
		
		if ( !empty( $_lang ) ) {
			if (PHP_OS == 'WINNT') {
				if ( isset( $languages[$_lang] ) ) {
					$_lang = $languages[$_lang];
				}
			} 
			
			return $_lang;
		}
			
		foreach( $this->preferedLanguages as $language ) {
	   		$language = explode( '-', $language );
	   		$language = $language[0];
	  		
	   		if ( PHP_OS == 'WINNT') {
	   			if ( isset( $languages[$language] ) ) {
	   				$lang = strtoupper( $languages[$language] );
	   			}
	   		} else {
	   			$lang = strtoupper( $language );
	   		}
	   		
	   		if ( isset( $this->availableLanguages[$lang] ) ) {
	   			if (PHP_OS == 'WINNT') {
	   				return $languages[$language];
	   			}
	   			
	   			return $language;
	   		}	
	   	}
	   	
	   	if (empty($_lang)) {
	   		$language = (PHP_OS == 'WINNT') ? 'eng' : 'en';
	   	}
	   	
	   	return $language;
	}
	
	/**
	 * Returns the preferred country (locale) from Browser
	 *
	 * @return string	The preferred locale set in the users browser or 'eng' by default.
	 *
	 * @access public
	 */
	function getCountry( $_country )
	{	
		include( 'i18n/inc/inc.global.countries.php' );
		
		if (empty($_country)) {
			$lang = $this->getPreferedLanguage();
	   		$country = explode('-', $lang);
		} else {
			return $_country;
		}
	   	
		//$country[] = $_country;
		
	  	if (sizeof( $country ) == 1) {
	  		$country = $country[0];
	  	} else {
	  		$country = $country[1];
	  	}
	  	
	  	if (PHP_OS == 'WINNT') {
	  		$country = strtoupper( $country );
	  	
	  		if (isset( $countries[$country] ) ) {
	  			$country = strtoupper( $countries[$country] );
	  		} else {
	  			$country = 'GBR';
	  		}
	  	}
	  	
	  	if ($country == 'en') {
	  		$country = 'GB';
	  	}
	  	
	  	return $country;
	}
	
	/**
	 * Returns the current locale
	 *
	 * @return string	The current locale
	 *
	 * @access public
	 */
	function getLocale()
	{
		return $this->appLocale;
	}
		
	
	/**
	 * Returns the preferred Language
	 *
	 * @return string	The preferred language
	 *
	 * @access public
	 */
	function getPreferedLanguage()
	{
   		return $this->preferedLanguages[0];
	}

	/**
	 * Sets the prefered Languages
	 *
	 * This function tries to detect the preferred languages that the user
	 * has set in his browser
	 * This function is only called if no defaultlanguage is set in the webapp.ekw file
	 *
	 * @return void
	 *
	 * @access private
	 */
	function setPreferedLanguages()
	{
		$this->preferedLanguages = array();
		$languages = explode(",", $_SERVER['HTTP_ACCEPT_LANGUAGE'] );
			
		foreach( $languages as $language ) {
	   		$lang_array = split(";q=", trim( $language ) );
	       	$lang = trim( $lang_array[0] );
	       
	       	if( !isset( $lang_array[1] ) ) {
	           $q = 1;
	       	} else {
	           $q = trim($lang_array[1]);
	       	}
	           
	       	$this->preferedLanguages[$lang] = (float)$q;
	   	}
	   	
	   	arsort($this->preferedLanguages);
	   	
	   	$i = 0;
	   	
		foreach($this->preferedLanguages as $lang => $q) {
		    $this->preferedLanguages[$i++] = $lang;
		}
	}
	
	/**
	 * Sets the next view
	 *
	 * @return	void
	 *
	 * @access  private
	 */
	function setTemplates()
	{
		if (sizeof( $this->templates ) == 0 ) {
			$tpl = array();
			$tpl = $this->getTemplate();
			
			$this->addTemplate( $tpl );
		}
	}
	
	/**
	 * Returns a column Description from a specific Table
	 * 
	 * This function is used to get the meta fieldtype or the real field type of a database column
	 *
	 *
	 * @param string	$table
	 * @param string	$field
	 * @param object	$dbHandler
	 * @see AdoDB
	 *
	 * @return ADOResultObject
	 *
	 * @access public
	 */
	function getField( $table, $field, &$dbHandler )
	{
		$sql = "SELECT " . $field . " FROM " . $table;
		$result =& $dbHandler->Execute( $sql );

    	return $result;
	}
	
	/**
	 * Returns the Database Handler to a specific DSN from the Database Handler Collection
	 *
	 * If the Handler is not found in the collection an error is raised
	 *
	 * @param	string		$dsn	DSN
	 *
	 * @return	object		DB Handler
	 */
	function & getDatabaseHandler( $dsn = 'default' )
	{
		if ( empty( $dsn ) ) {
			return $this->getDatabaseHandler();
		}
		
		if (!array_key_exists($dsn, $this->dbHandlers)) {
			trigger_error('No Handler registered for DSN: ' . $dsn, E_USER_ERROR);
		}
			
		return $this->dbHandlers[$dsn];
	}
	
	/**
	 * Tries to get definition for a dlg var from dlg_vars table.
	 *
	 * First tries to get the definition from the dlg id,
	 * If it not exists the funtion tries to get the definition from the global context.
	 * If nothing is found an error is thrown.
	 *
	 * @param string	$dlgId	Dialolg Id
	 * @param string	$id		Component ID
	 *
	 * @return array
	 */
	function getDlgVar( $dlgId, $id )
	{
		return $this->applicationHandler->getDlgVar( $dlgId, $id );
	}
	
	/**
	 * Returns all tables that have reference to to the current table
	 *
	 * @param	string	$table
	 *
	 * @return 	mixed	An array with all references to a given table, FALSE if nothing is found
	 *
	 * @access 	private
	 */
	function getReferences( $table ) 
	{
		return $this->applicationHandler->getReferences( $table );
	}
	
	/**
	 * Tries to get the Dlg Keys for a dialog
	 *
	 * @return object	An ADODB Result Object @see ADODB_Result
	 *
	 * @access private
	 */
	function getDlgKeys( $dlgId )
	{
		return $this->applicationHandler->getDlgKeys( $dlgId );
	}
	
	/**
	 * Returns all tables that are affected in the give dialog
	 * 
	 * @param 	string	$dlgId	The Dialog Id
	 *
	 * @return 	array	An ADODB Result Object
	 *
	 * @access 	public
	 */
	function getDlgTables( $dlgId )
	{
		return $this->applicationHandler->getDlgTables( $dlgId );
	}
	
	/**
	 * Tries to get a Session Value
	 *
	 * @param string	$idx	
	 * @param string	$section
	 *
	 * @return string
	 *
	 * @access public
	 */
	function getSessionValue( $idx, $section = null )
	{
		$idx = explode( '.', $idx );
		
		if (empty($idx[1])) {
			$idx = $idx[0];
		} else {
			$section	= $idx[0];
			$idx 		= $idx[1];
		}
		
		if (is_null($section)) {
			$section = $this->dlgId;
		}
		
		if ( $this->session->idxExists( $idx, $section ) ) {
			return $this->session->get( $idx, $section);
		} elseif ( $this->session->idxExists( $idx, '*' ) ) {
			return $this->session->get( $idx, '*' );
		} elseif ( $this->session->idxExists( $idx ) ) {
			return $this->session->get( $idx );
		} else {
			return false;
		}
	}
		
	/**
	 * Returns a Value from a query
	 *
	 * Please refer to EasyGUI XP Users Guide for more Informations
	 * about the power of prepared Queries.
	 *
	 * @param 	string	$id
	 *
	 * @return 	mixed	The Value from the Query, FALSE otherwise
	 *
	 * @access 	private
	 */
	function getValueFromQuery( $id )
	{
		return $this->valueHandler->getValueFromQuery( $id );
	}
	
	/**
	 * Returns the Default Database Handler
	 *
	 * @return object
	 *
	 * @access public
	 */
	function & getDefaultDbHandler()
	{
		return $this->defHandler;
	}
	
	/**
	 * Returns the Dialog Control Handler
	 *
	 * @return object
	 *
	 * @access public
	 */
	function & getDbHandler()
	{
		return $this->dbHandler;
	}
	
	/**
	 * Returns the session Handling object
	 *
	 * @return object
	 *
	 * @access public
	 */
	function & getSessionHandler()
	{
		return $this->session;
	}
	
	
	/**
	 * Returns a row from the dlg_queries table which can then be used to get the
	 * sql statement and the dsn
	 *
	 *
	 * @return array
	 */
	function & getQuery( $id )
	{
		return $this->applicationHandler->getQuery( $id );
	}
	
	/**
	 * Returns a query from the dlgVars Table for a specific control
	 *
	 * @param string $id component Id
	 * @param unknown_type $dlgId Dialog ID
	 * @return array The Query result Array containing ctrl_query and the query_dsn for the Component, FALSE otherwise
	 */ 
	function getCtrlQuery( $id, $dlgId )
	{
		return $this->applicationHandler->getCtrlQuery( $id );
	}
	
	/**
	 * Returns the Target Dialog Id
	 *
	 * @return 	string
	 *
	 * @access 	private
	 */
	function getTargetDlg( )
	{
		return $this->applicationHandler->getTargetDlg();
	}
	
	/**
	 * Returns the current Dlg Id
	 * @return string
	 * @access public
	 */
	function getCurrentDlg()
	{
		return $this->dlgId;
	}
	
	/**
	 * Returns Array with Temlate Information from dlg_process
	 *
	 * @return	object	EK_Template
	 *
	 * @access 	private
	 */
	function & getTemplate( )
	{
		return $this->applicationHandler->getTemplate();
	}
	
	/**
	 * Returns an array containing the table description
	 *
	 * @param string	$table	Name of table to get description for
	 *
	 * @return mixed	An array with table description, FALSE otherwise	
	 *
	 * @access public
	 */
	function getTableDescription( $table )
	{		
		return $this->applicationHandler->getTableDescription( $table );
	}
	
	/**
	 * @abstract 
	 */
	function handleAction( )
	{
		return -1;
	}
	
	/**
	 * @abstract 
	 */
	/*
	function handleAfterAction( )
	{
		;
	}
	*/
	
	/**
	 * returns a translated Text from the dictionary
	 * 
	 * @param 	string	$id		Id of text to search for
	 * @param 	string	$lang	Language in which to translate
	 *
	 * @return 	A translated text from the dictionary
	 * @access 	public
	 */
	function getText( $id, $lang = null )
	{
		if ( is_null ($lang) ) {
			$lang = $this->appLang;
		}
		
		return $this->dictionary->getText( $id, $lang );
	}
	
	/**
	 * Returns a value from Database or Session
	 *
	 * @param 	string	$id	Id of value to search for
	 *
	 * @return 	mixed	The value if it's found, FALSE otherwise
	 *
	 * @access 	public
	 */
	function getValue( $id )
	{
		return $this->valueHandler->getValue( $id );
	}
	
	/**
	 * Returns a string where all Session Variables are replaced
	 *
	 * You have the possibility to refer to session Values in your queries.
	 * Just enclose the values in $ Signs and they are replace by the current session values
	 * Please refer to the EasyGUI XP Users Guide to get more information on how to design
	 * queries.
	 *
	 * @param string	$query
	 * @param string	$context	Session Context in which you want to search for the replacements
	 *
	 * @return string
	 *
	 * @access public
	 */
	function getReplacedSQL( $query, $context = null )
	{
		return $this->valueHandler->getReplacedSQL( $query, $context );
	}

	/**
	 * Updates or Inserts into the application database
	 *
	 * This Method determines whether to INSERT or UPDATE the Database.
	 *
	 * @return boolean
	 *
	 * @access public
	 */
	function dbUpdate()
	{
		return $this->dataHandler->dbUpdate();
	}
	
	/**
	 * Deletes record from Database with referential integrity
	 *
	 * @return boolean
	 *
	 * @access public
	 */
	function dbDelete()
	{
		return $this->dataHandler->dbDelete();
	}
	
	/**
	 * Sets the Insert Mode
	 *
	 * @return boolean
	 *
	 * @access public
	 */
	function dbInsert()
	{
		return $this->dataHandler->dbInsert();
	}
	
	/**
    * Checks username and password against database
    *
    * @return void
    *
    * @access private
    */
	function login()
	{
		if ( !$this->authHandler->login() ) {
			$this->setTargetDlgId( EK_INDEX_DLG );
			return false;
		}
		
		return true;
	}
	
	/**
    * Logs the user out
    *
    * @return void
    *
    * @access public
    */
	function logout()
	{
		if (isset($this->authHandler)) {
			$this->authHandler->logout();
			$this->setTargetDlgId( EK_INDEX_DLG );
			$this->session->set( 'EK_SESSID', session_id() );
			return true;
		} else {
			if (DEBUG) {
				trigger_error('DEFAULT LOGOUT ACTION DEFINED IN DLG_VARS WITHOUT AUTH_HANDLING SET TO ON !', E_USER_NOTICE);
			}
		}
	}
	
	/**
    * Confirms that an existing login is still valid
    *
    * @return void
    *
    * @access private
    */
	function confirmAuth()
	{
		if ( isset( $this->authHandler ) ) {
			return $this->authHandler->confirmAuth();
		}
		
		return true;
	}
	
	/**
	 * Returns a converted Value for Display
	 *
	 * @param 	mixed		$value
	 * @param 	string		$format
	 * @param 	string		$type
	 * @param 	resource	&$dbHandler	
	 *
	 * @return 	mixed
	 *
	 * @access 	public
	 */
	function getScreenValue( $value, $format, $field, $metatype, &$dbHandler )
	{
		return $this->converter->getScreenValue( $value, $format, $field, $metatype, $dbHandler );
	}
	
	/**
	 * Returns a converted Value for insert in Database
	 *
	 * @param 	string		$value
	 * @param 	string		$format
	 * @param 	string		$type
	 * @param 	resource	&$dbHandler
	 *
	 * @return 	mixed
	 *
	 * @access 	public
	 */
	function getDBValue( $value, $format, $result, &$dbHandler )
	{
		return $this->converter->getDBValue( $value, $format, $result, $dbHandler );
	}
	
	/**
	 * Returns the Primary Key value stored in the Session
	 *
	 * @param 	array	$row
	 *
	 * @return 	The Session Value for the Primary Key
	 *
	 * @access 	public
	 */
	function getPrimaryKeyValue( $row )
	{
		return $this->dataHandler->getPrimaryKeyValue( $row );
	}
	
	/**
	 * Returns the Primary Key Field
	 *
	 * @param 	string	$table	Table Name
	 * @param 	refer
	 *
	 * @return mixed
	 *
	 * @access public
	 */
	function getPrimaryKeyField( $table, &$dbHandler )
	{
		return $this->dataHandler->getPrimaryKeyField( $table, $dbHandler );
	}
	
	/**
	 * Inserts a Component id in the preventOutput Array,
	 * so that the component is not rendered in the resulting Page 
	 *
	 * @param string $id
	 * @return void
	 * @access public
	 */ 
	function preventOutput( $id ) 
	{
		$this->preventOutput[$id] = TRUE;
	}
	
	/**
	 * Outputs the templates or the output buffer
	 *
	 */
	function output() 
	{
		if ( $this->ajax ) {
			$outputHandler = EK_OutputHandlerFactory::getOutputHandler('EK');
			
			$buffer = ob_get_clean();
			
			if ( !empty( $buffer ) ) {	
				$outputHandler->setOutput( $buffer );
			} 
			
			if( $this->handled && $this->getTargetDlgId() ) {
				$outputHandler->setOutput( "ekProceed('".$this->dlgEvt."');");
			}
			
			$outputHandler->output();
		} else {
			parent::output();
		}
	}
}
?>