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

    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 <ddwebgroup.h>


/**
	Default Constructor
*/

ddwebgroupmgr::ddwebgroupmgr()
{
}


/**
	Destructor.
*/

ddwebgroupmgr::~ddwebgroupmgr()
{
}


void ddwebgroupmgr::add_entry( size_t num_fields, MYSQL_ROW row )	
{
	size_t idx = add( ddwebgroup<ddwebgroupmgr>( this ) );
	ddwebgroup<ddwebgroupmgr>& newobj = (*this)[idx];
	//FIX! PFUSCH!!
	for( size_t i=0; i < num_fields; i++)
		newobj.add( row[i] ? row[i] : "" );		
	// 'id'	speichern für index_of
	if( newobj.contains_key( "id" ) )
		this->_index[ newobj["id"] ] = idx;
	// den object state setzen
	// FIX! das müsste eigentlich das object irgenwie selbermachen...
	if( newobj.contains_key( "id_state" ) )
		newobj.set_state_flags( newobj["id_state"].as_size_t() );
}


/**
	Queries und Caches laden.
*/

void ddwebgroupmgr::init( ddconfig& config )
{
	
	init_queries( config["webgroupmgr"] );
	connect( config["main"]["connectstring"] );
	fill();	
	// den standard group-selector vorrendern.
	int gid = get_field_index().index_of( "id" );
  int gnm = get_field_index().index_of( "sn_groupname" );
  ddstring base = "<option value='";
	ddstring end  = "</option>\n";
	for( size_t i= DDFIRSTGROUPINDEX; i<_data.size(); ++i )
	{
  	const ddstring& grp = (*this)[i][gid];
		// Hack: Admingruppe nicht anzeigen!
		if( grp == "13" || grp == "2" )
			continue;
		_groupselector += (base+(*this)[i][gid]+"'>"+(*this)[i][gnm]+end);
	}

}


ddstring ddwebgroupmgr::create_group_selector( const ddstring& grpid, bool is_admin )
{
	// no selection: use cache
	if( grpid.empty() )	
		return _groupselector;
	
	int gid = get_field_index().index_of( "id" );
	int gnm = get_field_index().index_of( "sn_groupname" );
 
	// fill group jump box options
	ddstring jump;		
	ddstring base = "<option value='";
  ddstring end  = "</option>\n";
	for( int i = DDFIRSTGROUPINDEX; i<size(); ++i )
	{
  	const ddstring& grp = (*this)[i][gid];
    	
  	// Hack: Admingruppe nicht anzeigen!
		if( !is_admin )
		{
			if( grp == "13" || grp == "2" )
				continue;
		}
		jump += (base+grp+"'");
		if( grp == grpid )
			jump += " selected";
		jump += (">"+(*this)[i][gnm]+end);
	}		
	return jump;
}


