<?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.
*/

/**
 * Base Class of EeasyGUI Handlers
 *
 * @author Gaetan Lauff <glauff@plansoft.de>
 * @version	XP $Id: class.ek_base.php 44 2006-02-28 13:40:01Z glauff $
 * @package base
 */
class EK_Base
{
	/**
	 * DOM XML Object
	 * @see EK_DOMXML
	 *
	 * @var object
	 */
	var $userSettings;
	
	/**
	 * DOM XML Object
	 * @see EK_DOMXML
	 *
	 * @var	object
	 */
	var $settings;
	
	/**
	 * @var	array
	 */
	var $templates;
	
	/**
	 * Template Engine that's used
	 *
	 * @var object
	 */
	var $templateEngine;
	
	/**
	 * EasyGUI Templates
	 * @var string
	 */
	var $tplPath;
	
	/**
	 * status setting for webapp.
	 * if the webapp has the status abort
	 * The execution is stopped in the next level.
	 * So we have three levels.
	 * Modify the Model
	 * Get The View
	 * Modify the view (Dlg Updates etc)
	 * Output the new View
	 */
	var $status;
	
	/**
	 * Constructor
	 *
	 * @param 	array	&$config	Reference to the Configuration array
	 *
	 * @return 	void
	 *
	 * @access 	private
	 */
	function EK_Base( &$config )
	{
		$this->config =& $config;
		
		if ( !is_array( $config )) {
			$this->settings = $this->config->getElementsByTagName( 'Settings' );
		} else  {
			$this->settings =& $config;
		}
			
		$this->templates	= array();
		$this->tplPath		= EK_ROOT . '/tpl/';
	}
	
	/**
	 * Initializes the Controller
	 *
	 * In the base class this method tries to detect if the framework is called
	 * from the commandline. When this is true then a usage message is shown.
	 *
	 * There are different options to call easyguiXP from the commandline.
	 *
	 * -i=<input_file>
	 * -o=<output_file>
	 * -ifi=<iframe_input_file>
	 * -ifo=<iframe_output_file>
	 * Substitute the value in <> to match your needs.
	 *
	 * Defaults are:
	 * output_file=easygui.html.
	 * iframe_output_file=easygui_iframe.html.
	 *
	 * @return void
	 *
	 * @access public
	 */
	function init() 
	{
		//print_r($_SERVER);
		if (empty($_SERVER['DOCUMENT_ROOT'])) {
			if (
				(isset($_SERVER['argc']) && isset($_SERVER['argv'])) && 
				($_SERVER['argc'] == 1 || 
				 $_SERVER['argc'] == 2 && 
				 in_array($_SERVER['argv'][1], array('--version', '-version', '-v')))
				) {

				print "\r\nEasyGUI " . EK_VERSION . " Build " . EK_BUILD . " " . EK_REVISION . "\r\n";
				print "(c) " . EK_COPYRIGHT_YEAR . " Gaetan Lauff <glauff@plansoft.de>\r\n\r\n";
				exit(0);
			}
			
			if ((isset($_SERVER['argc']) && isset($_SERVER['argv']) && $_SERVER['argc'] == 2)) {
				if (in_array($_SERVER['argv'][1], array('--help', '-help', '-h', '?'))) {
					print "\r\n\r\nUsage: easygui.php <options>\r\n\r\n";
					print "Options:\r\n";
					print "--------\r\n\r\n";
					print "-i=<input_file>\r\n";
					print "-o=<output_file>\r\n";
					print "-ifi=<iframe_input_file>\r\n";
					print "-ifo=<iframe_output_file>\r\n";
					print "Substitute the values in <> to match your needs.\r\n\r\n";
					print "Defaults:\r\n";
					print "---------\r\n";
					print "output_file=easygui.html\r\n";
					print "iframe_output_file=easygui_iframe.html\r\n\r\n";
					exit(0);
				}
			}
		}
	}
	
	/**
	 * Initializes the View
	 * @todo
	 * @return void
	 *
	 * @access public
	 */
	function initView()
	{
		$this->setTemplates();
		$size = sizeof($this->templates);
		
		for ($i=0; $i < $size; $i++) {
			$tpl 			=& $this->templates[$i]->getObject();
			$engine 		= $tpl->getEngine();
			$transformer 	= $tpl->getTransformer();
			
			$templateEngine =& EK_TemplateEngineFactory::getEngine( $this, $engine, $tpl );
			$template = $templateEngine->fetch();

			$templateTransformer =& EK_TransformerFactory::getTransformer($this, $transformer, $tpl );
			$template = $templateTransformer->transform( $template );

			$tpl->setTemplate($template);
		}
	}
	
	/**
	 * Outputs the generated Content
	 *
	 * The Method iterates through each template and outputs
	 * it with the associated output handler.
	 *
	 * @param 	$outputHandler	Output Handler
	 *
	 * @return 	void
	 *
	 * @access 	public
	 */
	function output( $outputHandler = null ) 
	{
		foreach($this->templates as $template) {
			$stream = $template->getStream();
			$outputHandler =& EK_OutputHandlerFactory::getOutputHandler( $stream );
						
			switch( $stream ) {
				case 'download':
				case 'file':
				case 'zipfile':
					$outputHandler->setFilename( $template->getOutfile() );
					break;
					
				default:
					break;
			}
			
			$outputHandler->setOutput( $template->getTemplate() );
			$outputHandler->output();
		}
	}
	
	/**
	 * Sets About Dialog for Direct Call
	 * 
	 * This Method is overwritten in each Special Handler.
	 * In this case the about.eks Template is set if EasyGUI XP called native.
	 * So here the Version Informations are returned to the client.
	 *
	 * @return void
	 *
	 * @access public
	 */
	function setTemplates()
	{
		$tpl = new EK_Template();
		$tpl->setTemplate( file_get_contents( $this->tplPath . '/about.tpl' ) );
		
		$this->addTemplate( $tpl );
	}
	
	/**
	 * Adds a Template to the Dialog Template Collection
	 *
	 * @param 	object	$tpl	New Template Object
	 *
	 * @return 	void
	 *
	 * @access 	public
	 */
	function addTemplate( &$tpl )
	{
		if ( is_a( $this, 'ek_cdrom' ) ) {
			array_push( $this->templates, $tpl );
		} else {
			$this->templates[0] = $tpl;
		}
	}
	
	/**
	 * Return the internal templates array
	 *
	 * @return array
	 *
	 * @access public
	 */
	function getTemplates()
	{
		return $this->templates;
	}
	
	/**
	 * Returns the current configuration
	 *
	 * @return array
	 *
	 * @access public
	 */
	function getConfig() 
	{
		return $this->config;
	}
	
	/**
	 * Returns the current Settings
	 *
	 * @return array
	 *
	 * @access public
	 */
	function getSettings() 
	{
		return $this->settings;
	}
	
	/**
	 * Returns a constant value
	 *
	 * @return string
	 *
	 * @access public
	 */
	function getValue( $id )
	{
		if (defined( $id ) ) {
			return htmlspecialchars( constant( $id ) );
		}
	}
	
	/**
	 * Returns a value from a query
	 *
	 * In the base class the give id is returned without modification
	 *
	 * @param 	string	$id
	 *
	 * @return string
	 * @abstract 
	 */
	function getValueFromQuery( $id )
	{
		return $id;
	}
	
	/**
	 * Returns a component
	 *
	 * In the base class the id is modified to ~id~ and then returned
	 *
	 * @param string	$id
	 *
	 * @return string
	 *
	 * @access public
	 */
	function getComponent( $id )
	{
		return '~'.$id.'~';
	}
}
?>