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

/**
 * Build Maps for Applications
 *
 * The Maps play a vital role inside the EasyGUI XP Framework.
 * So if you want to have mor informations on how to implement your own mapping
 * read the EasyGUI XP User's Guide. 
 *
 * @author Gaetan Lauff <glauff@plansoft.de>
 * @version XP $Id: class.ek_map.php 1 2006-01-31 14:55:23Z glauff $
 * @package components
 */
class EK_Map
{	
	/**
	* Holds the mapping of each Element
	* @var array
	*/
	var $map;
	
	/**
	* Constructor
	*
	* @param 	string	$map	This is used to manage more than one mapset.
	*							You can have more than one template transformer. and for each of them you can write your own maps
	*
	* @return 	void
	*
	* @access 	public
	*/
	function EK_Map( )
	{
		$dir = EK_ROOT . '/lib/template/transformer/maps/';
		$hdl = dir($dir);
		
		while ( $namespace = $hdl->read( ) ) {
			if( $namespace != "." && $namespace != ".." ) {
				if ( preg_match( "/(\w+).php$/i", $namespace ) ) {
					include(  EK_ROOT . "/lib/template/transformer/maps/" . $namespace );
					$map = str_replace( ".php", "", $namespace );
					
					$this->map['global'][$map] = $$map;
				} else {
					$dir = EK_ROOT . '/lib/template/transformer/maps/' . $namespace . '/';
						
					$nsHdl = dir( $dir );
					
					while ( $map = $nsHdl->read() ) {
						if ($map != '.' && $map != ".." ) {
							if ( preg_match( "/(\w+).php$/i", $map ) ) {
								include( $dir.$map );
								$map = str_replace( ".php", "", $map );
						
								$this->map[$namespace][$map] = $$map;
								unset( $$map );
							}
						}
					}
				}
			}
		}
	}
	
	/**
	* Returns a list of Replacements from Map
	*
	* @param	string	Map Index
	* @param	array	All Attributes
	*
	* @return	mixed	Is false when no replacement is found
	*
	* @access 	public
	*/
	function getReplacements( $map, &$attributes, $namespace, $list = null, $default = true )
	{
		$namespace = (empty($namespace)) ? 'global' : $namespace;
		
		if (!array_key_exists( $map, $this->map[$namespace])) {
			return 'NOMAP';
		}
		
		$expression = array();
		
		foreach( $attributes as $key => $value ) {
			if ( $value != '' ) {
				if ( EK_COLOR_BUG ) {
					if ( preg_match("/color|colour/i", $key ) ) {
						$value = "#".preg_replace( "/([0-9A-Fa-f][0-9A-Fa-f])([0-9A-Fa-f][0-9A-Fa-f])([0-9A-Fa-f][0-9A-Fa-f])/", "\$3\$2\$1", $value );
					}
				}
				
				$attribute = null;
				$value = str_replace( "\\r\\n", "\r\n", $value );
				$value = str_replace( "\\n", "\r\n", $value );
				$value = trim( $value );
				
				if ( array_key_exists( $key, $this->map[$namespace][$map] ) ) {
					if ( $value != 'false' ) {
						if ( empty( $list ) || in_array( $key, $list ) ) {
							$attribute = stripslashes($this->map[$namespace][$map][$key]);
							
							if ( $key == 'width' || $key == 'height' ) {
								if (!preg_match("/(\%|px|em|auto)/", $value) ) {
									$attribute = str_replace(';', 'px;', $attribute);
								}
							}
							
							if ( $key == 'verticalalign' && $value == 'center' ) {
								$value = 'middle';
							}
						}
					}
				} else {
					if ( !$this->mapEntryExists( $map, $key, $namespace ) ) {
						if ( $default && isset( $this->map[$namespace][$map]['default'] ) && ( empty( $list ) || in_array( $key, $list ) ) ) {
							$attribute = stripslashes( $this->map[$namespace][$map]['default'] );
							$attribute = str_replace( "%k%", $key, $attribute );
						} 
						/*
						else {
							$attribute = $key . '="' . $value . '"';
						}
						*/
					}
				}
				
				if ( !empty( $attribute ) ) {
					$attribute = str_replace( "%v%", $value, $attribute );
					array_push( $expression, $attribute );
					unset( $attributes[$key] );
				}
			}
		}
		
		$separator = ( isset( $this->map[$namespace][$map]['separator'] ) ) ? $this->map[$namespace][$map]['separator'] : ' ';

		return implode( $separator, $expression );
	}
	
	/**
	* Returns a default entry for an attribute
	*
	* @param	String	$entry
	*
	* @return	mixed
	*
	* @access 	public
	*/
	function getDefaultEntryFor( $entry, $namespace = 'global' )
	{
		$namespace = (empty($namespace)) ? 'global' : $namespace;
		
		if ( array_key_exists( $entry, $this->map[$namespace]['defaults'] ) ) {
			return stripslashes( $this->map[$namespace]['defaults'][$entry] );
		}
	}
	
	/**
	* Returns Map Entry For A Component
	*
	* @param 	string	$entry
	*
	* @return 	string	
	*
	* @access 	public
	*/
	function getMapEntry( $entry, $namespace = 'global' )
	{
		$namespace = (empty($namespace)) ? 'global' : $namespace;
		
		if ( array_key_exists( $entry, $this->map[$namespace]['components'] ) ) {
			return stripslashes( $this->map[$namespace]['components'][$entry] );
		}
			
		if ( array_key_exists( $entry, $this->map[$namespace]['compat'] ) ) {
			return stripslashes( $this->map[$namespace]['compat'][$entry] );
		}
		
		if ( array_key_exists( 'default', $this->map[$namespace]['components'] ) ) {
			return $this->map[$namespace]['components']['default'];
		}
	}
	
	/**
	* Checks if a Map Entry exists
	*
	* @param	string
	*
	* @return	bool	TRUE if the Map Entry exists, FALSE otherwise.
	*
	* @access 	public
	*/
	function mapEntryExists( $map, $entry, $namespace = 'global' )
	{
		$namespace = (empty($namespace)) ? 'global' : $namespace;
		
		if ( array_key_exists( $entry, $this->map[$namespace][$map] ) ) {
			return true;
		}
		
		return false;
	}
} // end class Map
?>