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

    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.

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


#ifndef DDWEBGROUP_H
#define DDWEBGROUP_H

#include <dddatatable.h>
#include <dddataobject.h>
#include <ddwebrenderer.h>
#include <dditem.h>
#include <ddwebgroup.h>



template<class T>
class ddwebgroup : public dddataobject<T>
{
	
public: 
	
	enum dd_group_fields
	{
		grp_id=0,
		grp_id_state,
		grp_name,
 		grp_description,
		grp_keys,   		
		grp_icon_big,   		
		grp_icon_small,   		
		//grp_count,   	
		grp_field_size
	};
	
	enum dd_group_flags
	{
		grp_is_public = 1,
   	grp_is_trash  = 2,
		grp_is_hidden = 4,
		grp_is_closed = 8
  };

	ddwebgroup()
	{
	}
	
	ddwebgroup( T* parent )
	:	dddataobject<T>( parent ), _count( -1 )
	{		
		//resize( ddwebgroup::grp_field_size );
	}
	
	
	inline bool is_public() const 
	{
		return ( this->_flags & grp_is_public );
	}
	
	
	inline bool is_trash() const 
	{
		return ( this->_flags & grp_is_trash );
	}
	
	
	inline bool is_hidden() const 
	{
		return ( this->_flags & grp_is_hidden );
	}
	
	
	inline bool is_closed() const 
	{
		return ( this->_flags & grp_is_closed );
	}
	  
	
	inline const ddstring& get_id() const
	{
		return (*this)[ddwebgroup::grp_id];
	}
	
	
	inline const ddstring& get_name() const
	{
		return (*this)[ddwebgroup::grp_name];
	}
	
	
	inline const ddstring& get_description() const
	{
		return (*this)[ddwebgroup::grp_description];
	}
	
	
	inline const ddstring& get_keys() const
	{
		return (*this)[ddwebgroup::grp_keys];
	}
	
	
	inline const ddstring& get_icon_big() const
	{
		return (*this)[ddwebgroup::grp_icon_big];
	}	
	
	inline const ddstring& get_icon_small() const
	{
		return (*this)[ddwebgroup::grp_icon_small];
	}	
	
	/**
		FIX! Einen Gruppenlink erzeugen.
	*/
	
	void create_group_link( dditem& entry,  bool usestyle=false, const ddstring& linkclass="sn_info", const ddstring& linkbase="/groups/" )
	{
		// MÖRDERPFUSCH
		if( get_id() == "70" )
		{
			entry.set_link( "news", "/news" );
			return;
		}
		if( get_id() == "71" )
		{
			entry.set_link( "An & Verkauf", "/sale" );
			return;
		}
		ddstring txt = "&nbsp;" + get_name();
		if( usestyle )
			txt = "<li>"+txt+"</li>";
		entry.set_link( txt, linkbase + get_id(), linkclass );	
	}

	
	int get_count()
	{
		if( _count<0 )
			this->get_parent().execute( "count_one", get_id() ).read( _count );
		return _count;
	}
	
	inline void set_count( int count )
	{
		_count = count;
	}
	
	inline void set_cache_entry( const ddstring& key, const ddstring& value )
	{
		_cache[key] = value;
	}
	
	inline const ddstring& get_cache_entry( const ddstring& key )
	{
		return _cache[key];
	}
	
	void invalidate_cache()
	{
		_cache.clear();
		// den artikelzähler für diese Gruppe
		// auch zurücksetzen
		_count = -1;
	}
	
	
protected:

	int					_count; 
	ddstringmap _cache;

};


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

    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.

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


/// ddwebgroup : ex groupmanager
/**
*/

class ddwebgroupmgr : public dddatatable< ddwebgroup<ddwebgroupmgr> >
{


public: 
	
	ddwebgroupmgr();
	virtual ~ddwebgroupmgr();

	void init( ddconfig& config );
	
	void 			add_entry( size_t num_fields, MYSQL_ROW row );	
	ddstring	create_group_selector( const ddstring& grpid, bool is_admin );
	
protected:

	ddstring _groupselector;
	
};



#endif
