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

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


/**
	Constructor.
*/

sxpagehomebase::sxpagehomebase( const ddstring& name, ddwebsite* site, const ddstring& type ) 
: sxpage( name, site, type ), _itemsperhome( 20 )
{	
	// die Tables sind in den Unterklassen nicht unbedingt vorhanden
	
	ddstring viewname = get_name()+".overview";
	if( get_site().get_view_map().contains_key( viewname ) )
		get_view( viewname ).set_bg_color( context.bg1, context.bg2 );
	viewname = get_name()+".groups";
	if( get_site().get_view_map().contains_key( viewname ) )
		get_view( viewname ).set_bg_color( context.bg1, context.bg2 );
		
	// Anzahl der Einträge, die Gästen angezeigt wird. 
	_itemsperhome = site->get_config()["main"].get_int( "items_per_page", 20 );
	_use_overlay_image = site->get_config()["main"].get_bool( "use_forum_overlay_images", false );
	_use_overlay_links = site->get_config()["main"].get_bool( "use_forum_overlay_links", false );
}


/**
	Alte Version von on_create, in sxpage ist eine andere variante am start.
*/

dd_result sxpagehomebase::on_create( dditem& item )
{
	
	
	//ddbasetime timer;
	//timer.start();
	
	ddwebcgi& cgi = get_cgi();
	dditem& block = get_body()["sn_block_create"]["sn_subbody"];
	ddformmanager& mgr = get_site().get_form_manager();
	
	// noch nichts belegt?
	if( cgi["id_user"].empty() )
		return rs_ok;
	
	// ab hier: es wird versucht, einen thread anzulegen.
	
	// daten erst testen...
	if( !mgr.check_form( "check."+get_name(), block, get_user_id(), cgi ) )
		return rs_ok;
		
	// nur speichern, wenn ok
	if(	!mgr.contains_blocked_words() )
	{
		
		// .. ip nachreichen ...
		cgi["sn_senderip"]	= get_connection().get_host_ip();
		
		// ... overlay die links einbauen...
		if( _use_overlay_links )	
			get_site().get_overlay_links().replace_keys( cgi["sn_msgbody"] );
		// ... eventuell die bilder einbauen...
		if( _use_overlay_image )	
			get_site().get_overlay_images().replace_keys( cgi["sn_msgbody"], cgi["sn_msgsubject"] );
	
		// ...dann speichern...
		get_view().execute( cgi );
			
		// ..den fragen-counter hochzählen..	
		if( get_user_id().get_id() != "1" )
			get_user_manager().usr_update_single( get_user_id().get_id(), "sn_numquestions", "sn_numquestions+1" );
	}
	
	// ... die Übersicht ungültig machen ...
	// FIX! das sollte über die abhängikeiten gehen!
	//get_view( get_name()+".overview" ).invalidate();
	
	
	// ...und zur Übersicht
	return create_redirect( get_full_path().trim_pattern( "/docreate" ) );
	(void) item;

}


/**
	Eine Antwort erzeugen, überladene Version für threads, welche korrekt ans
	Ende des threads springt, damit man seine frisch geschriebene Antwort sehen
	kann.
*/

dd_result sxpagehomebase::on_reply_create( dditem& item )
{
	
	
	// um einen eintrag zu erzeugen, müssen die automatikfelder
	// geladen sein, dazu muss der entsprechende thread anzeigt gewesen
	// sein.
	
	ddwebcgi& cgi = get_cgi();
	dduserid& uid = get_user_id();
	dditem& block = get_body()["sn_block_comments"]["sn_comcreate"]["sn_subbody"];
	ddformmanager& mgr = get_site().get_form_manager();
	
	// testen, ob der thread überhaupt offen und gültig ist.
	ddwebview& table = get_view( get_name()+".comments" );
	int flag;
	table.execute( "on_close_check", context.id ).read( flag );
	if( flag )
		return create_error( "Thread geschlossen oder ung&uuml;ltig!" );
	
	// im fehlerfall:
	if( !mgr.check_form( _replychecker, block, get_user_id(), cgi ) || mgr.contains_blocked_words() )
		return on_show_entry( item );

	// alles ok: die fehlenden Daten nachreichen...	
	cgi["id"]						= context.id;
	cgi["sn_senderip"]	= get_connection().get_host_ip();
	cgi["id_user"]			= uid.get_id();
	cgi["sn_nickmail"]	= uid.get_nick_mail();

	// speichern
	table.execute( cgi );
		
	/*
		BUG SNSPII-156:
		Aufgrund der langen uptime von 10 tagen wurde die cache-table nicht neu geladen und die betreffenden threads sind da rausgegplumst.
		Im Detail: post von 30.8, letzte antwort 31.8, Serverstart am 22.9 -> thread ist zu alt, kommt nicht in die cache-table
		neue antwort am 29.09 -> thread müsste in die Übersicht, ist aber nicht in der cache table ...
	
		Lösung: Die query
		"update tr_msgcache set sn_childcount = sn_childcount + 1, sn_datemodified = now() where id_parent = '2259573'" schlägt im diesem Fall fehl,
		also die query vereinzeln, das ergebnis testen (get_affected_rows ) und ggf. dann ein "insert into tr_msgcache select ... " hinterherschicken.
	
	*/
	
	int affrows = table.execute( "on_cache_update", cgi ).read();
	// fehlgeschlagen: dann den datensatz in die cache-table laden
	if( !affrows )
		table.execute( "on_cache_insert", cgi );

	// mailalerts verschicken
	ddmailmanager& mm = get_site().get_mail_manager();
	mm.create_alert_mails( context.id, uid.get_nick_mail(), uid.get_nick_name(), cgi["sn_msgsubject"], get_site().get_name() );	
	
	// den antwort-counter hochzählen	
	if( uid.get_id() != "1" )
		get_user_manager().usr_update_single( uid.get_id(), "sn_numanswers", "sn_numanswers+1" );

	// alles ok, wir machen einen redirect auf die letzte seite des threads, damit
	// man die neue Antwort sehen kann.
	
	table.execute( "on_count", context.id, DDADMFILTERFORUM ).read( context.numrows );
	
	//return create_redirect( get_full_path().trim_pattern( "/docreate" ) + "#jumplast" );
	return create_redirect( get_jump_last_url() + "#jumplast" );
	
	
}


/**
 	Die Supportnet Cache-Übersicht Seite rendern.
*/

dd_result sxpagehomebase::on_overview( dditem& item )
{

	// filter für schon beantwortete sachen
	ddstring skip = "100000";
	const ddstring& ac = get_user_id().get_property( DDANSWERCOUNT );
	if( !ac.empty() && ac != "x" )
		skip = ac;
	
	context.pagetitle = get_site().get_default_page_title();
	
	// ausschnitt der tabelle ermitteln
	dduserid& uid = get_user_id();

	// hier abweichend, da konfigurierbar (siehe ddwebpage::handle_content
	context.xcount = uid.is_guest() ? _itemsperhome : uid.get_items_per_page();
 	context.navifrom = context.navipage * context.xcount;
	context.navito   = context.navifrom + context.xcount;
 	
 	// daten holen
	ddstring sfrom   = ddstrutil::from_int( context.navifrom );
	ddstring sxcount = ddstrutil::from_int( context.xcount );	
	
	ddwebview& table = get_view( get_name()+".overview" );
	context.numrows = table.fill( "on_overview", skip, DDADMFILTER, sfrom, sxcount );
	
	// für die navigation innerhalb der cache-table
	ddstring count;	
	table.execute( "on_count", skip, DDADMFILTER ).read( count );	
	table.on_render( *this, get_body()["sn_subbody"]["sn_msgentry"], 0, context.numrows );	
	
	// ...navi basteln ...
	dditem& navi = get_body()["sn_navigator"]; 		
	create_navigator( navi, context.navilinkval, context.navifrom, context.navito, count.as_int() );
 	
	// ...und gut
	return rs_ok;
	(void) item;
	
}


/**
 Die Gruppenseiten im Forum rendern, Unterschied zu sxpage::on_show_group:
 Es wird versucht, die Übersichten aus einem Cache zu holen.
*/

dd_result sxpagehomebase::on_show_group( dditem& item )
{
 	
	// müll gruppe nicht anzeigen
	const ddstring& idgrp = context.idgrp;
	ddstring link = _grouplink + idgrp + "/";

	get_body().clear_value();
	
	//if( !get_user_id().is_admin() && !get_site().is_group_valid( idgrp.as_int() ) )
	if( !get_user_id().is_sub_admin() && !get_site().is_group_valid( idgrp.as_int() ) )				
		return create_hint_box( get_body(), DDHINTNONPUBLICGROUP );
 	
 	// header krempel anzeigen	
	ddwebgroup<ddwebgroupmgr>& group = get_groups()[ idgrp ];
	
	// Gruppen-Schrempel  
	context.pagetitle = "Supportnet - Gruppe " + group.get_name();
	get_item()["sn_meta"]["sn_meta_header"].from_string( group.get_description() );
	get_item()["sn_meta"]["sn_meta_keyword"].from_string( group.get_keys() );
	
	// Ist der Block im Cache?
	ddstring cachekey = ddstrutil::from_int( context.xcount ) + "." + get_raw_path();
	const ddstring& pagedata = group.get_cache_entry( cachekey );
	
	// page ist im cache: ab dafür..
	if( !pagedata.empty() )
	{
		get_body().from_string( pagedata );
		return rs_ok;
	}
	
	// Nicht im cache: erzeugen
	
	// ausschnitt der tabelle ermitteln
	ddstring sxcount  = ddstrutil::from_size_t( context.xcount );
	ddstring sfrom    = ddstrutil::from_size_t( context.navifrom );
	
	// daten holen...
	ddwebview& table = get_view( get_name() + ".groups" );
	context.numrows = table.fill( "on_show_group", idgrp, DDADMFILTER, sfrom, sxcount );	
	
	// ...testen...
	dditem& list = get_body()["sn_list"];
	if( context.numrows <= 0)
		return create_hint_box( list, DDHINTNOGROUPENTRY );	
	
	// ...alles fein...
	list.set_visible( true );
	
	// ...anzeigen...
	dditem& entry = list["sn_subbody"]["sn_msgentry"];
	table.on_render( *this, entry, 0, context.numrows );	
	
	// für die navigation innerhalb der cache-table
	ddstring count;	
	table.execute( "on_count", idgrp, DDADMFILTER ).read( count );	

	// ...navi basteln ...
	dditem& navi = list["sn_navigator"]; 		
		
	create_navigator( navi, link, context.navifrom, context.navito,  count.as_int() );
 
	// ...und gut
	return rs_ok;
	(void) item;

	
}




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

    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.

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


/**
	Constructor.
*/

sxpagehome::sxpagehome( const ddstring& name, ddwebsite* site ) 
: sxpagehomebase( name, site, "home" )
{	
	
	add_request_handler( "www;home;index.html;default.asp", "sx_home_overview", (ddmemptr) &sxpage::on_overview );	
	add_request_handler( "homegroups",											"sx_home_groups", 	(ddmemptr) &sxpage::on_show_group, 2 );
	add_request_handler( "on_create",		 										"sx_forum_create", 	(ddmemptr) &sxpage::on_create );  
	add_request_handler( "on_preview", 											"sx_forum_preview", (ddmemptr) &sxpage::on_preview );  
	
	context.navilinkval = "/home/";
	_grouplink = "/homegroups/";
	// rest
	//init_requests();
	
	//context.breadcrumpstatic = "<a href='/home' class='box_title_bc'>Home</a>";
	//context.breadcrumprow.create("<a href='/forum' class='box_title_bc'>Forum</a>/<a class='box_title_bc' href='/groups/|1|'>|3|</a>/<a class='box_title_bc' href='/threads/|5|'>|7|</a>", "|" );
	try
	{
		// die caches füllen
		get_view( "sxpagehome.overview" ).execute( "create" );	
	}
	catch( exception& error )
	{
		cout << "---ERROR: " << error.what() << endl;
		throw( error );
	}
	
}


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

    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.

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


/**
	Constructor.
*/

sxpageforumbase::sxpageforumbase( const ddstring& name, ddwebsite* site ) 
: sxpagehomebase( name, site, "forum" )
{	
	
}

/**
	Initialisiert alle gängigen callbacks.
	ACHTUNG! die entsprechenden Items müssen vorhanden sein
*/

void sxpageforumbase::init_requests()
{
	// gilt für 'thread', 'listthread' und 't'
	
	add_request_handler( "on_create",		 					"sx_forum_create", 				(ddmemptr) &sxpageforumbase::on_create );  
	add_request_handler( "on_preview", 						"sx_forum_preview",  			(ddmemptr) &sxpageforumbase::on_preview );  
	add_request_handler( "on_edit",		 						"sx_forum_update", 				(ddmemptr) &sxpageforumbase::on_edit, 			2, dduserid::usr_can_edit_news  );  
	add_request_handler( "on_update", 						"sx_forum_update", 				(ddmemptr) &sxpageforumbase::on_update, 		2, dduserid::usr_can_edit_news  );  
	add_request_handler( "on_remove", 						"sx_forum_update", 				(ddmemptr) &sxpageforumbase::on_remove, 		2, dduserid::usr_can_edit_news  );  
	add_request_handler( "on_remove_block",				"sx_forum_update", 				(ddmemptr) &sxpageforumbase::on_remove, 		2, dduserid::usr_can_edit_news  );  
	
	add_request_handler( "on_close", 							"sx_forum_update", 				(ddmemptr) &sxpageforumbase::on_set_state, 	2, dduserid::usr_can_edit_news  );  
	add_request_handler( "on_open", 							"sx_forum_update", 				(ddmemptr) &sxpageforumbase::on_set_state, 	2, dduserid::usr_can_edit_news  ); 
	add_request_handler( "on_solve", 							"sx_forum_update", 				(ddmemptr) &sxpageforumbase::on_solve,			2, dduserid::usr_can_edit_news  );  	
	add_request_handler( "on_set_solved", 				"sx_forum_solved",				(ddmemptr) &sxpageforumbase::on_set_solved,	2  );
	
	add_request_handler( "on_alert", 							"sx_admin_alert", 				(ddmemptr) &sxpageforumbase::on_alert ); 
	add_request_handler( "on_alert_send",					"sx_admin_alert", 				(ddmemptr) &sxpageforumbase::on_alert_send ); 

	add_request_handler( "on_reply_create",				"sx_forum_show",					(ddmemptr) &sxpageforumbase::on_reply_create, 2 );
	
	add_request_handler( "on_reply_preview",			"sx_forum_preview_reply",	(ddmemptr) &sxpageforumbase::on_preview );      
	add_request_handler( "on_reply_vote", 				"sx_forum_vote",					(ddmemptr) &sxpageforumbase::on_reply_vote );
	
	add_request_handler( "on_reply_edit", 				"sx_forum_update_reply",	(ddmemptr) &sxpageforumbase::on_reply_edit, 	2, dduserid::usr_can_edit_news );  
	add_request_handler( "on_reply_remove", 			"sx_forum_show",					(ddmemptr) &sxpageforumbase::on_reply_remove, 2, dduserid::usr_can_edit_news );  
	add_request_handler( "on_reply_remove_block", "sx_forum_show",					(ddmemptr) &sxpageforumbase::on_reply_remove, 2, dduserid::usr_can_edit_news );  

	add_request_handler( "on_reply_update", 			"sx_forum_update_reply",	(ddmemptr) &sxpageforumbase::on_reply_update,	2, dduserid::usr_can_edit_news );  
	
	add_request_handler( "on_drop_subscription",	"sx_forum_vote",					(ddmemptr) &sxpageforumbase::on_drop_subscription );
	
	add_request_handler( "on_bookmark",						"sx_forum_vote",					(ddmemptr) &sxpageforumbase::on_bookmark );
	add_request_handler( "on_bookmark_drop",			"sx_forum_vote",					(ddmemptr) &sxpageforumbase::on_bookmark );
	add_request_handler( "on_bookmark_update",		"sx_forum_vote",					(ddmemptr) &sxpageforumbase::on_bookmark );
	
	
	// Der Names des Comment-Checkers. wird in pagehome überschrieben
	_replychecker = "check.reply";	
	context.navilinkval = "/forum/";

}


/**
 	Einen Thread anzeigen.
*/

dd_result sxpageforumbase::on_show_entry( dditem& item )
{
	
	
	dduserid& uid = get_user_id();
	
	// thread holen ...
	ddwebview& table = get_view( get_name()+".show" );
	// param 2 steuert den müllfilter: admins kriegen den müll zu sehen
  context.numrows = table.fill( "on_show", context.id, DDADMFILTERFORUM );
	
	
	// alt: 404 erzeugen
	//if( context.numrows <= 0 )
	//	//return create_hint_box( get_body(), DDHINTNOENTRY );
	//	return create_error( DDHINTNOENTRY, ddwebresponse::ret_404_not_found );
	
	// neu: exceptions vermeiden und dummy page anzeigen
	if( !context.numrows )
		return create_redirect( "/linknotfound?sn_key=" + get_full_path().dup().escape(), ret_301_moved_permanently );

	
	if( table[0]["id_type"] != "posting" )
		return create_error( "Falscher Artikeltyp: " +  table[0]["id_type"] );
	
	// Posting anzeigen
	get_body().clear_value();
	table.on_render_entry( *this, get_body()["sn_message"]["sn_subbody"], 0, true );
		
	create_header_entries( table, item );
	// hack: hier den title wieder ändern
	context.pagetitle	= get_groups()[context.idgrp].get_name()+ ": " + context.subject;
		
	dditem& block = get_body()["sn_block_comments"];
	// thread veraltet?
	if ( get_user_id().is_guest() && table[0]["sn_age"].as_int() > 90 )
		create_hint_box( block, DDHINTDEPRECATED );
	else
		// thread geschlossen?
		create_hint_box( block, DDHINTCLOSED, table[0].is_closed() );
	
	dditem& thread = get_body()["sn_thread"];			
  thread.set_visible( context.numrows > 1 );
 
 	// mailbenachrichtigungssperre löschen
	ddmailmanager& mm = get_site().get_mail_manager();
	mm.clear_alert_flag( uid.get_nick_name(), context.id );
 	
 	if( context.numrows > 1 )
  	table.on_render( *this, thread["sn_subbody"], context.navifrom+1, context.navito+1 );	
 
 	// ...navi basteln ...
	dditem& navi = get_body()["sn_navigator"]; 		
	ddstring base = _threadlinkbase + context.id + "/";
	create_navigator( navi, base, context.navifrom, context.navito, context.numrows-1 );
 
	dditem& subnavi = get_body()["sn_message"]["sn_subbody"]["sn_subnavi"];
	create_navigator_slim( subnavi, base, context.navifrom, context.navito, context.numrows-1 );
 	
 		// hack>
	item.set_field_value( "sn_gtraq", context.id == "1437277" ? "" : " " );
	
	// ...und gut
	return rs_ok;
	(void) item;
	

}


/**
 	Einen Thread als Lösung markieren, admin-version
*/

dd_result sxpageforumbase::on_solve( dditem& item )
{	
	ddwebcgi& cgi = get_cgi();
	// cache auch löschen
	// FIX! Der Cache wird eh nicht benutzt!!
	//get_groups()[ cgi["id_group"] ].invalidate_cache();
	
	// ...ausführen....
	ddwebview& table = get_view();
	// flag:
	ddstring op = "id_state | 16";
	// sowohl parent als auch
	table.execute( "on_set_state", op, context.id );	
	// kind als lösung markieren
	table.execute( "on_set_state", op, cgi["id"] );	

	// punkte vergeben: der sender ...
	size_t points = get_user_id().get_rank_score();
	// an den Empänger
	ddstring& iduser = cgi["id_user"];
	// nicht aber bei gästen
	if( iduser != "1" )
		get_user_manager().usr_inc_score( iduser, points );
	
	// ...und zur Übersicht zurück.
	return create_redirect(  get_full_path() );
	
	(void) item;
}	



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

    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.

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


sxpageforum::sxpageforum( const ddstring& name, ddwebsite* site ) 
: sxpageforumbase( name, site )
{	
	add_request_handler( "t",				 							"sx_forum_show_short", 		 			(ddmemptr) &sxpage::on_show, 2 );
	_grouplink = "/groups/";
	_threadlinkbase ="/t/";
	
	// rest
	init_requests();
	
	
}

/**
 	Einen Thread anzeigen, neues Format '/t/'. Dabei werden threads vor 09/2007
 	(id<1612470) aufs die startseite umgeleitet
*/

dd_result sxpageforum::on_show_entry( dditem& item )
{
	//ddbasetime timer;
	//timer.start();
	//log_error( "TRY: forum new: " + get_full_path() );
		//log_error( "OK:  forum new: " + get_full_path() + " : " + ddstrutil::from_double( timer.elapsed() ) );	

	if( context.id.empty() || !context.id.is_number() )
		return create_error( "Fehlerhafte ID" );
	
	if( context.id.as_int() <= DDLASTID )
	//	return create_redirect( "/threads/" + context.id, true );	
	// neu: lieber auf die startseite umleiten
		return create_redirect( "/" );	
	
	dd_result result = sxpageforumbase::on_show_entry( item );
	//log_error( "OK:  forum new: " + get_full_path() + " : " + ddstrutil::from_double( timer.elapsed() ) );	
	
	return result;


}
	
	
/***************************************************************************

    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.

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


sxpageforumoverview::sxpageforumoverview( const ddstring& name, ddwebsite* site ) 
: sxpageforumbase( name, site )
{	
	add_request_handler( "forum", 								"sx_forum_overview", 			(ddmemptr) &sxpage::on_overview );
	_grouplink = "/groups/";
	_threadlinkbase ="/t/";
		
	add_outer_call( "query0", "sx_forum_query1", (ddmemptr) &sxpageforumoverview::query_stage0 );  
  add_outer_call( "query1", "sx_forum_query1", (ddmemptr) &sxpageforumoverview::query_stage1 );  
	add_outer_call( "query2", "sx_forum_query2", (ddmemptr) &sxpageforumoverview::query_stage2 ); 
	add_outer_call( "query3", "sx_forum_query2", (ddmemptr) &sxpageforumoverview::query_stage2 ); 
	add_outer_call( "query4", "sx_forum_query3", (ddmemptr) &sxpageforumoverview::query_stage3 );
	
}


/**
	Das subject-Feld retten und weiter zum 1. Formular.
*/

dd_result sxpageforumoverview::query_stage0( dditem& item )
{
	
   //  die daten müssen irgendwie von form zu form transportiert
	// werden -> zwischenspeicher beim ersten schritt erstmal löschen
	
	/*
	update:
	
	Alle AOL daus kriegen nur eine IP,
	speichern per session geht also nicht mehr
	HACK: session key simulieren
	
	dduserid& uid = get_user_id();
	uid.get_property( "sn_msgbody" ) = "";
	uid.get_property( "id_group" ) = "";
	uid.get_property( "sn_msgsubject" ) = "";
	*/
	
	// erster aufruf, noch nüscht drin -> weiter
	dditem& block = get_item()["sn_header"]["sn_subbody"];
	block.clear_children();
	block["sn_msgsubject"].from_string( get_cgi()["sn_msgsubject"] );
	block["sn_key"].from_string( get_site().create_passwd() );
	
	return rs_ok;
	(void) item;
}


/**
	Eingabeformulare behandeln: Diese Methode wird für beide
	Formularteile benutzt.
*/

dd_result sxpageforumoverview::query_stage1( dditem& item )
{
	
	dditem& block  = get_item()["sn_header"]["sn_subbody"];
 	ddwebcgi& cgi = get_cgi();
	
	if( get_split_path().size() < 2 )
		return create_error( "query_stage1: path to short: " + get_raw_path() );

	const ddstring& key = get_split_path()[1];	
	// testen: dableiben, bis unser form akzeptiert ist ...
	ddformmanager& messman = get_site().get_form_manager();
	//if( !messman.check_form( "checkquery", block, get_user_id(), cgi ) )	
	if( !messman.check_form( "check.sxpagehome", block, get_user_id(), cgi ) )	
	{
		block["sn_key"].from_string( key );
		return rs_ok;
	}
	
	// ... und dann die daten speichern ...
	// FIX ME! auto-sync basteln
	
	dduserid& uid = get_user_id();
	uid.get_property( key + ".sn_msgsubject" ) = cgi["sn_msgsubject"];
	uid.get_property( key + ".sn_msgbody" ) = cgi["sn_msgbody"];
	uid.get_property( key + ".id_group" ) = cgi["id_group"];
	
	// ... und ab zum nächsten schritt
	return create_redirect( "/query2/" + key );
	(void) item;
	
}


dd_result sxpageforumoverview::query_stage2( dditem& item )
{
	
	
	dditem& block  = get_item()["sn_header"]["sn_subbody"];
 	block.clear_children();
 	ddwebcgi& cgi = get_cgi();
	
	if( get_split_path().size() < 2 )
		return create_error( "query_stage1: path to short: " + get_raw_path() );

	const ddstring& key = get_split_path()[1];	
	block["sn_key"].from_string( key );
	
	// noch nix neues -> weiter
	if( get_split_path()[0] == "query2" )	
		return rs_ok;
	
	// testen: dableiben, bis unser form akzeptiert ist ...
	ddformmanager& messman = get_site().get_form_manager();
	if( !messman.check_form( "check.sxpagehome", block, get_user_id(), cgi ) )	
	{	
		block["sn_key"].from_string( key );
		return rs_ok;
	}
	
	// die vorher gespeicherten Sachen ins CGI zurückschreiben
	dduserid& uid = get_user_id();
	cgi["sn_msgsubject"] = uid.get_property( key + ".sn_msgsubject" );
	cgi["sn_msgbody"]    = uid.get_property( key + ".sn_msgbody" );
	cgi["id_group"]      = uid.get_property( key + ".id_group" );
		
	if( cgi["id_group"].empty() || cgi["sn_msgbody"].empty() || cgi["sn_msgsubject"].empty() )
	{
		get_connection().create_log_entry();
		log_debug( "EMPTY query: " + get_connection().get_log_entry() );
		return create_error( "Sorry, Datenbankfehler!" );
	}
	
	// PFUSCH! Das gehört in die VIEW!
	// ab hier: es wird versucht, einen thread anzulegen.
	
	ddformmanager& mgr = get_site().get_form_manager();
	// daten erst testen...
	if( !mgr.check_form( "check."+get_name(), block, get_user_id(), cgi ) )
		return rs_ok;
	
	// .. ip nachreichen ...
	cgi["sn_senderip"]	= get_connection().get_host_ip();
	cgi["sx_action"]	  = "on_create";
	
	// ...dann speichern...
	get_view().execute( cgi );
	ddstring lastid;
	// = get_view().get_last_insert_id();
	get_view().execute( "insert_id" ).read( lastid );
	
	// ..den fragen-counter hochzählen..	
	if( get_user_id().get_id() != "1" )
		get_user_manager().usr_update_single( get_user_id().get_id(), "sn_numquestions", "sn_numquestions+1" );

	// ... die Übersicht ungültig machen ...
	// FIX! das sollte über die abhängikeiten gehen!
	get_view( get_name()+".overview" ).invalidate();
	
	
	return create_redirect( "/query4?id_group=" + cgi["id_group"] + "&id_thread=" + lastid );
	
}


dd_result sxpageforumoverview::query_stage3( dditem& item )
{
	ddwebcgi& cgi = get_cgi();
	dditem& block  = get_item()["sn_header"]["sn_subbody"];
	//create_group_link( block["sn_groupname"], cgi["id_group"], false );
	
	
	get_groups()[ cgi["id_group"] ].create_link( block["sn_groupname"] );	
	
	
	ddstring threadlink = _threadlinkbase + cgi["id_thread"];
	block["sn_l1"].from_string( threadlink );
	block["sn_l2"].from_string( threadlink );
	block["sn_l3"].from_string( threadlink );
	block["sn_l4"].from_string( threadlink );
	
	return rs_ok;	
	(void) item;
}


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

    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.

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


sxpageforumgroups::sxpageforumgroups( const ddstring& name, ddwebsite* site ) 
: sxpageforumbase( name, site )
{	
	//add_request_handler( "forum", 								"sx_forum_overview", 			(ddmemptr) &sxpage::on_overview );
	add_request_handler( "groups",								"sx_forum_groups", 	 			(ddmemptr) &sxpage::on_show_group, 2 );
	
	//add_request_handler( "threads", 							"sx_forum_show", 		 			(ddmemptr) &sxpage::on_show, 2 );
	//add_request_handler( "t",				 							"sx_forum_show", 		 			(ddmemptr) &sxpage::on_show, 2 );
	_grouplink = "/groups/";
	_threadlinkbase ="/t/";
	
	// rest
	init_requests();
	
	
}



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

    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.

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


sxpageforumthread::sxpageforumthread( const ddstring& name, ddwebsite* site ) 
: sxpageforumbase( name, site )
{	
	add_request_handler( "listthread", 							"sx_forum_show", 		 			(ddmemptr) &sxpage::on_show, 2 );
	// rest
	init_requests();
	_threadlinkbase ="/listthread/";
	
}


/**
 	Einen Thread anzeigen, altes Format '/listthread/'
*/

dd_result sxpageforumthread::on_show_entry( dditem& item )
{
	//ddbasetime timer;
	//timer.start();
	//log_error( "TRY: forum thread: " + get_full_path() );	
	dd_result result = sxpageforumbase::on_show_entry( item );
	//log_error( "OK:  forum thread: " + get_full_path() + " : " + ddstrutil::from_double( timer.elapsed() ) );	
	return result;

}


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

    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.

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


sxpageforumold::sxpageforumold( const ddstring& name, ddwebsite* site ) 
: sxpageforumbase( name, site )
{	
	add_request_handler( "threads", 							"sx_forum_show", 		 			(ddmemptr) &sxpage::on_show, 2 );
	// rest
	init_requests();
	
	_threadlinkbase = "/threads/";

}


/**
 	Einen Thread anzeigen, altes Format. Dab werden threads ab 09/2007
 	(id>1612470) aufs neue format umgeleitet
*/

dd_result sxpageforumold::on_show_entry( dditem& item )
{
	//ddbasetime timer;
	//timer.start();
	//log_error( "TRY: forum old: " + get_full_path() );
	
	if( context.id.empty() || !context.id.is_number() )
		return create_error( "Fehlerhafte ID" );
	
	if( context.id.as_int() > DDLASTID )
		return create_redirect( "/t/" + context.id, ret_301_moved_permanently );
	
	dd_result result = sxpageforumbase::on_show_entry( item );
	//log_error( "OK:  forum old: " + get_full_path() + " : " + ddstrutil::from_double( timer.elapsed() ) );	
	
	return result;
	

}
	

	

