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

    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 <sxpageguestbook.h>
#include <dditemset.h>
#include <dditemlink.h>
#include <ddwebconnection.h>



/**
	Constructor.
*/

sxpageguestbook::sxpageguestbook( const ddstring& name, ddwebsite* site )
: sxpage( name, site, "guestbook" )
{
	
	add_request_handler( "guestbook", 			"sx_guestbook_overview", (ddmemptr) &sxpage::on_overview );
	add_request_handler( "on_reply_create",	"sx_guestbook_overview", (ddmemptr) &sxpage::on_reply_create );
	add_request_handler( "on_preview",			"sx_forum_preview",	  	 (ddmemptr) &sxpage::on_preview );  
	add_request_handler( "on_alert", 				"sx_admin_alert", 			 (ddmemptr) &sxpage::on_alert ); 
	add_request_handler( "on_alert_send",		"sx_admin_alert", 			 (ddmemptr) &sxpage::on_alert_send ); 
	
	add_request_handler( "on_reply_edit", 	"sx_comment_update",		 (ddmemptr) &sxpage::on_reply_edit, 	1, dduserid::usr_can_edit_news );  
	add_request_handler( "on_reply_update",	"sx_comment_update",		 (ddmemptr) &sxpage::on_reply_update, 1, dduserid::usr_can_edit_news );  
	add_request_handler( "on_reply_remove", "sx_guestbook_overview", (ddmemptr) &sxpage::on_reply_remove, 1, dduserid::usr_can_edit_news );  
	

	
	
	
	// Die blockliste wird nur hier gefüllt, in den anderen Unterseiten wird sie nicht
	// benutzt
	ddstring blocker;
	get_site().fetch_data_entry( DDKEYGUESTBLOCKER, blocker );
	_guestblocker.from_string( blocker );
	/*
	// bei bösen worten wird der kommentar still ignoriert
	if( messman.contains_blocked_words() || _guestblocker.find_entry_in( cgi["sn_msgbody"] )  )
	{
		block.clear_children();
		return create_redirect( get_user_id().get_property("sn_comment_path") );
	}
	*/
}


/**
	Eine Antwort erzeugen.
*/

dd_result sxpageguestbook::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_comform"]["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" );
		
	// im fehlerfall:
	if( !mgr.check_form( _replychecker, block, get_user_id(), cgi ) || mgr.contains_blocked_words() )
		return create_redirect( "/guestbook" );

	// 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 );
	
	return create_redirect( "/guestbook" );

}


