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

/**
 * Class that converts Database Values to Screen Format
 * Could be static Class
 *
 * @author Gaetan Lauff	<glauff@plansoft.de>
 * @version XP $Id: class.ek_screenconverter.php 1 2006-01-31 14:55:23Z glauff $
 * @package dataspace
 */
class EK_ScreenConverter
{
	/**
	 * Constructor
	 */
	function EK_ScreenConverter()
	{
		;
	}
	
	/**
	 * Returns a numeric value based on output format
	 *
	 * @param 	string	$value
	 * @param 	string	$format
	 *
	 * @return 	string
	 *
	 * @access public
	 */
	function getNumeric( $value, $format )
	{
		$conv = localeconv();
		
		if (empty($format)) {
			$format = 2;
		}
			
		return number_format( $value, $format, $conv['decimal_point'], $conv['thousands_sep'] );
	}
	
	function getLogical( $value ) 
	{
		switch( $value ) {
			case 0:
			case '0':
			case 'false':
			case false:
				$value = 'false';
				break;
			default:
				$value = 'true';
				break;
		}
		
		return $value;
	}
	
	/**
	 * Returns an integer
	 *
	 * @param	string	value
	 *
	 * @return 	integer
	 *
	 * @access 	public
	 */
	function getInteger( $value )
	{
		return intval( $value );
	}
	
	/**
	 * Returns a formatted String from a database value
	 *
	 * @param 	string
	 *
	 * @return 	string
	 *
	 * @access 	public
	 */
	function getTime( $value, $format) {
		$time = explode( ":", $value);
			
		if (!isset($time[0]) || !isset($time[1])) {
			return '';
		}
		
		if (empty($format)) {
			 $format = '%H:%M';
		}
			 
		return adodb_strftime( $format, adodb_mktime( $time[0], $time[1], $time[2], 18, 05, 1972) );
	}
	
	/**
	 * Returns a formatted Date from Database Value
	 *
	 * @param 	string	Date
	 * @param 	string	Format
	 *
	 * @return 	string	Date
	 *
	 * @access 	public
	 */
	function getDate( $value, $format )
	{
		if (empty($value)) {
			return '';
		}
			
		$date = explode("-", $value);
		
		if ($date[0] == '0000') {
			return '';
		}
		
		if (empty($format)) {
			$format = '%d.%m.%Y';
		}
		
		return adodb_strftime( $format, adodb_mktime(0,0,0, $date[1], $date[2], $date[0] ) );
	}
	
	/**
	 * Returns a Date Time Format for display from Database Value
	 * @param string	value
	 * @return string
	 * @todo localize function and format
	 */
	function getDatetime( $value, $format )
	{
		if (empty($value)) {
			return;
		}

		if (empty($format)) {
			$format = '%c';
		}
			
		$datetime = explode(" ", $value);
		
		$date = explode("-", $datetime[0]);
		$time = explode(":", $datetime[1]);
		
		return adodb_strftime( $format, adodb_mktime( $time[0], $time[1], $time[2], $date[1], $date[2], $date[0] ) );
	}
	
	/**
	 * Returns a string formatted according to the given format string
	 * using the given integer timestamp or the current local time if no timestamp is given. 
	 * In other words, timestamp is optional and defaults to the value of 
	 * The second Parameter defines the format
	 *
	 * @param 	string	value	The Unix Timestamp
	 * @param 	string	format	
	 *
	 * @return 	string
	 *
	 * @access 	public
	 */
	function getTimestamp( $value, $format )
	{
		if (empty($format)) {
			$format = 'd.m.Y H:i:s';
		}
			
		return adodb_strftime( $format, adodb_date( $value ) );
	}
	
	/**
	 * Returns the passed string 
	 * with all special characters replaced by their html entities in the right encoding
	 *
	 * @param 	string	$value	String to convert
	 *
	 * @return  string
	 *
	 * @access 	public
	 */
	function getString( $value )
	{
		return htmlentities( htmlentities( $value, ENT_QUOTES, EK_ENCODING ), ENT_QUOTES, EK_ENCODING );
	}
}
?>