/***************************************************************************

    supportware
    Copyright (C) 2001-2008 by c.holzheuer
    c.holzheuer@sourceworx.org

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

***************************************************************************/


#include <dditem.h>
#include <stdexcept>
#include <ddiourl.h>
#include <ddwebpage.h>
#include <dduserid.h>
#include <ddwebsite.h>

using namespace std;



/**
	Default constructor.
*/

dditem::dditem()
: _access( 3 )
{

}


/**
	Copy constructor.
*/

dditem::dditem( const dditem& src )
: ddbasenamedobject(), _access( 3 )
{
	*this = src;
}


/**
	Assignment operator.
*/

dditem& dditem::operator=( const dditem& src )
{
	if( this == &src )
		return *this;

  _access = src._access;
 	_name = src._name;
	_value = src._value;
	_param = src._param;

	return *this;
}


/**
	Default constructor.
*/

dditem::dditem( const ddstring& name )
: ddbasenamedobject( name ), _access( 3 )
{


}

 
/**
	Default constructor.
*/

dditem::dditem( const ddstring& name, const ddstring& value, const ddstring& call, size_t access )
: ddbasenamedobject( name ), _access( access ), _value( value ), _param( call )
{

}


/**
	Destructor.
*/

dditem::~dditem()
{

}
	

/**
	@return a deep copy of this item.
*/

dditem* dditem::clone() const
{
 	return new dditem( _name, _value, _param, _access );
}


/**
	@return the accessrights for this item.
*/

size_t dditem::get_access()
{
	return _access;
}


/**
	set the accessrights for this item.
*/

void dditem:: set_access( size_t access )
{
	_access = access;	
}
 

/**
	@return the number of children, in this case 0.
*/

size_t dditem::size()
{
	return 0;
}


/**
	Access by key, prototype only, throws runtime_error.
*/

dditem& dditem::operator[]( const ddstring& key )
{
	throw runtime_error("dditem::operator[]( const ddstring& key ): base call: " + key );
	// avoid warnings
	(void)	key;
}


/**
	Access by index,prototype only, throws runtime_error.
*/

dditem& dditem::operator[]( size_t index )
{
	throw runtime_error("dditem::operator[]( size_t index ): base call!");
	// avoid warnings
	(void)	index;
}


/**
	Acces by key, const version, prototype only, throws runtime_error.
*/

const dditem& dditem::operator[]( const ddstring& key ) const
{
	throw runtime_error("dditem::operator[]( const ddstring& key ): base call;"  + key );
	// avoid warnings
	(void)	key;
}


/**
	Access by index, const version, prototype only, throws runtime_error.
*/

const dditem& dditem::operator[]( size_t index ) const
{
	throw runtime_error("dditem::operator[]( size_t index ): base call!");
	// avoid warnings
	(void) index;
}


/**
	Add a child. Prototype only, throws runtime_error.
*/

void dditem::add_child( dditem* item )
{
	throw runtime_error("dditem::add_child: base call!");
	(void) item;
}


/**
	Set value from bool.
*/

dditem& dditem::from_bool( bool value )
{
	_value.from_bool( value );
	return *this;
}


/**
	Set value from int.
*/

dditem& dditem::from_int( int value )
{
	_value.from_int( value );
	return *this;
}


/**
	Set value from size_t.
*/

dditem& dditem::from_size_t( size_t value )
{
	_value.from_size_t( value );
	return *this;
}


/**
	Set value from double.
*/

dditem& dditem::from_double( double value )
{
	_value.from_double( value );
	return *this;
}


/**
	Set value from string.
*/

dditem& dditem::from_string( const ddstring& value, size_t asize )
{
	_value = value;
	return *this;
	// FIX!
	(void) asize;
}


/**
	Setzt den Feldwert an der Position key, falls key vorhanden.
*/
	
dditem& dditem::set_field_value( const ddstring& key, const ddstring& value )
{
	if( contains_key( key ) )
		(*this)[ key ].from_string( value );
	return *this;
}


/**
	Setzt den Feldwert an der Position key, falls key vorhanden.
*/
	
dditem& dditem::set_field_value( const ddstring& key, int value )
{
	if( contains_key( key ) )
		(*this)[ key ].from_int( value );
	return *this;
}


/**
	Set link data.
	@prototype only. Calls @see from_string( value, clear_style )
	@see dditemlink
*/

dditem& dditem::set_link( const ddstring& value, const ddstring& link, const ddstring& lclass, const ddstring& title )
{
	return from_string( value );
	(void) link;
	(void) lclass;
	(void) title;
}


/**
	Clear this items value.
	@see get_value();
	@see from_bool() et al.
*/

dditem& dditem::clear_value()
{
	_value = "";
	return *this;
}


/**
	Sichtbarkeit ein- und ausschalten.
*/

dditem& dditem::set_visible( bool visible )
{
	_value = visible ? "" : " ";
	return *this;
}


/**
	Clear all child items: call @see clear_value() for all childitem that have
	a name. This is usually used to clear input forms.
	Prototype only, throws base call exception.
*/

dditem& dditem::clear_children()
{
  throw runtime_error("dditem::clear_children(): base call!");
}


/**
	Synchronize the datafields of the cgi object with the fields of
	this item.
	@see ddwebcgi
	(Prototype implementation only. throws runtime_error.
*/

void dditem::sync( ddwebcgi& cgi )
{
	throw runtime_error("dditem::sync: base call!");
	(void) cgi;
}



/**
	
*/

dd_result dditem::flush_content( ddwebpage& pagelet )
{
   // check accessrights
  //if( !( get_access() & pagelet.get_user_id().get_access_rights() ) )
	//	return 1;
  if( !_param.empty() )
		return pagelet.execute_call( _param, *this );
	pagelet.get_buffer().append( to_string() );
  return rs_ok;
}


/**
	dummy.
	@see dditemtreebase
*/

dd_result dditem::flush_children( ddwebpage& pagelet, int from, int to  )
{
  return rs_ok;
  // avoid warnings
	(void) pagelet;
	(void) from;
	(void) to;
}


dd_result dditem::flush_content( ddwebpage& pagelet, ddstring& target )
{
	if( !_param.empty() )
		return pagelet.execute_call( _param, *this );
	target.append( to_string() );
  return rs_ok;
}


dd_result dditem::flush_children( ddwebpage& pagelet, ddstring& target, int from, int to )
{
  return rs_ok;
  // avoid warnings
	(void) target;
	(void) from;
	(void) to;
}


/**
	@return the current value.
*/

const ddstring& dditem::get_value() const
{
	return _value;
}


/**
	@return the call function or value.
*/

const ddstring& dditem::get_param() const
{
	return _param;
}


/**
	@return the call function or value.
*/

void dditem::set_param( const ddstring& param )
{
	// hack: if param contains a parameter,
	// it is stored in _value
	size_t pos = param.find( '#' );
	if( pos != ddstring::npos )
	{
		_param = param.substr( 0, pos );
		_value =  param.substr( pos+1 );
	}	
	else
	{
		_param = param;
	}
	
}


/**
	Create a string representation of this item.
	@return _prefix + get_value() + _suffix.
*/

ddstring dditem::to_string() const
{
	return get_value();
}


/**
	@return the current value as char*
*/

const char* dditem::data() const
{
	return _value.c_str();
}


/**
	@return the index.
	Prototype only.
*/

const ddmapindex& dditem::get_index() const
{
	throw runtime_error(" dditem::get_index(): base call!");
}


/**
	@return the internal index of an entry or -1  when an entry 'key' does not exists.
	Prototype only.
*/

int dditem::index_of( const ddstring& key ) const
{
	throw runtime_error("dditem::index_of: base call!");
	return -1;
	// avoid warnings
	(void) key;
}


/**
	@return true when an entry 'key' exists.
	Prototype only.
*/

bool dditem::contains_key( const ddstring& key ) const
{
	return false;
	// avoid warnings
	(void) key;
}


/**
	
*/

ddstring dditem::join() const
{
	return get_value();
}


/**
	Dump this object to std::out for debugging purposes.
*/

void dditem::dump() const
{
	cout <<  "item: name:" << get_name() << "; value: " << get_value() << endl;
}


/***************************************************************************

    supportware
    Copyright (C) 2001-2008 by c.holzheuer
    c.holzheuer@sourceworx.org

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

***************************************************************************/


/**
	Default constructor.
*/

dditemcheckbox::dditemcheckbox()
{

}


/**
	Copy constructor.
*/

dditemcheckbox::dditemcheckbox( const dditemcheckbox& src )
{
	*this = src;
}


/**
	Default constructor.
*/

dditemcheckbox::dditemcheckbox( const ddstring& name )
: dditem( name )
{


}

 
/**
	Default constructor.
*/

dditemcheckbox::dditemcheckbox( const ddstring& name, const ddstring& value, const ddstring& call, size_t access )
: dditem( name, value, call )
{

}


/**
	Destructor.
*/

dditemcheckbox::~dditemcheckbox()
{

}
	

/**
	@return a deep copy of this itemcheckbox.
*/

dditem* dditemcheckbox::clone() const
{
 	return new dditemcheckbox( _name, _value, _param, _access );
}


/**
	Set value from bool.
*/

dditem& dditemcheckbox::from_bool( bool value )
{
	_value = value ? "checked" : "";
	return *this;
}


/**
	Set value from int.
*/

dditem& dditemcheckbox::from_int( int value )
{
	_value = value ? "checked" : "";
	return *this;
}


/**
	Set value from size_t.
*/

dditem& dditemcheckbox::from_size_t( size_t value )
{
	_value = value ? "checked" : "";
	return *this;
}


/**
	Set value from double.
*/

dditem& dditemcheckbox::from_double( double value )
{
	_value = value ? "checked" : "";
	return *this;
}


/**
	Set value from string.
*/

dditem& dditemcheckbox::from_string( const ddstring& value, size_t asize )
{
	_value = value.empty() ?  "" : "checked";
	return *this;
	(void) asize;
}
