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

    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 <sxblockmenu.h>
#include <ddwebsite.h>
#include <dditemlink.h>





/**
	Default Constructor
*/

sxblockmenu::sxblockmenu( const ddstring& name, ddwebsite* site )
: ddwebblock( name, site )
{

}


/**
	Destructor.
*/

sxblockmenu::~sxblockmenu()
{

}


/**
	Stellt den aktuellen Menu-Eintrag hervorgehoben dar.
*/
	
dd_result sxblockmenu::create_content( ddwebpage& pagelet, dditem& item )
{

	// hack: bei forum steht noch class="first" davor,
	// also ist eine Fallunterscheidung nötig
	
	item.clear_children();
	const ddstring& ptype = pagelet.context.pagetype; 
	if( item.contains_key( ptype ) )
	
		// hack: bei forum steht noch class="first" davor,
		// also ist eine Fallunterscheidung nötig
		
		item[ ptype ].from_string( ptype == "forum" ? " active" :  " class='active'" );
	
	_content =  item.to_string();

	return rs_ok;
}


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

    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
*/

sxblockrelated::sxblockrelated( const ddstring& name, ddwebsite* site )
: ddwebblock( name, site )
{

}


/**
	Destructor.
*/

sxblockrelated::~sxblockrelated()
{

}


/**
	Erzeugt die 'Related Links'
*/
	
dd_result sxblockrelated::create_content( ddwebpage& pagelet, dditem& item )
{
	
	// FIX: select * from tr_msgmain where id in ( 5000, 5001, 5059)
	// das ist müll!
	// FIX! blockcache einbauen!
	
	_content = "";
	
	ddwebcontext& context = pagelet.context;
	// irgendwas ist schiefgegangen ...
	if( context.subject.empty() )
		return rs_ok;
	
	dditem& msgentry = item["sn_msgentry"];
	// mal wieder der gute alte referenz-hack :-)
	ddstring& result = _linkcache[ context.id ];
	if( result.empty() )
	{
		// alle views sind global erreichbar
		ddwebview& table = pagelet.get_view( "site.searchrelated" );
		ddstring entry;
		table.execute( "fetch", context.id ).read( entry );
		if( entry.empty() )
		{
			// ... aber im hintergrund die daten anfordern
			pagelet.get_site().get_related_manager().enqueue_related_query( context.id, context.subject, context.idgrp );
			return rs_ok;
		}
	
		// das geht nicht in einem Statement !?
		// STORED PROC verwenden !?
		msgentry.clear_value();
		table.fill( "select", entry );
		table.on_render( pagelet, msgentry, 0, table.size() );	
		result = msgentry.get_value();
	}

	// DAS IST MÜLL!
	
	// inhalte setzen ...
	msgentry.from_string( result );		
	// ... und raus damit ...
	item.flush_children( pagelet, _content );
	
	return rs_ok;
	
	
}




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

    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
*/

sxblockgroups::sxblockgroups( const ddstring& name, ddwebsite* site )
: ddwebblock( name, site )
{

	
}


/**
	Destructor.
*/

sxblockgroups::~sxblockgroups()
{

}


/**
	Prototype only.	
*/
	
dd_result sxblockgroups::create_content( ddwebpage& pagelet, dditem& item )
{
	
	static const char* elem = " class='active'";
	
	item.clear_children();
	
	
	// Teil eins: die aktuelle Gruppe 'highlighten'
	
	const ddstring& ptype = pagelet.context.pagetype;
	const ddstring& idgrp = pagelet.context.idgrp;
	
	// PFUSCH! sales ist keine gruppe, muss aber trotzdem gehighlightet
	// werden
	if( item.contains_key( ptype ) )
		item[ ptype ].from_string( elem );
	else if( item.contains_key( idgrp ) )
		item[ idgrp ].from_string( elem );
	
	// hack: auch die toppgruppen highlighten
	ddstring gkey = idgrp + ".top";
	if( item.contains_key( gkey ) )
		item[ gkey ].from_string( "active" );

	// Teil zwei: die Anzahl neuer Artikel pro Gruppe anzeigen
	if( pagelet.get_user_id().is_member() )
	{
		ddusermanager& uma = pagelet.get_user_manager();
		ddstringmap& newposts = uma.get_new_postings_count( pagelet.get_user_id(), pagelet.get_site().get_db_name() );
		
		ddstringmap::const_iterator pos = newposts.begin();
		for( ; pos != newposts.end(); ++pos )
		{
			ddstring key = "sn_entries_" + (*pos).first;
			if( item.contains_key( key ) )
				item[ key ].from_string( (*pos).second );			

		}
	}
	_content =  item.to_string();

	return rs_ok;
}


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

    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
*/

sxblockletters::sxblockletters( const ddstring& name, ddwebsite* site )
: ddwebblock( name, site )
{

	
}


/**
	Destructor.
*/

sxblockletters::~sxblockletters()
{

}


/**
	Prototype only.	
*/
	
dd_result sxblockletters::create_content( ddwebpage& pagelet, dditem& item )
{
	item.clear_children();
	_content = "";

	if( item.contains_key( pagelet.context.idgrp ) )
		item[ pagelet.context.idgrp ].from_string( "_active" );
	item.flush_children( pagelet, _content );

	return rs_ok;
}


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

    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
*/

sxblockpics::sxblockpics( const ddstring& name, ddwebsite* site )
: ddwebblock( name, site )
{

	
}


/**
	Destructor.
*/

sxblockpics::~sxblockpics()
{

}


/**
	Prototype only.	
*/
	
dd_result sxblockpics::create_content( ddwebpage& pagelet, dditem& item )
{
	
	item.clear_children();
	_content = "";

	if( item.contains_key( pagelet.context.idgrp ) )
		item[ pagelet.context.idgrp ].from_string( "_a" );
	item.flush_children( pagelet, _content );

	return rs_ok;
}


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

    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
*/

sxblockfaqs::sxblockfaqs( const ddstring& name, ddwebsite* site )
: ddwebblock( name, site ), _initcalled( false )
{

	
}


/**
	Destructor.
*/

sxblockfaqs::~sxblockfaqs()
{

}


/**
	FAQs zum Thema suchen.
*/
	
dd_result sxblockfaqs::create_content( ddwebpage& pagelet, dditem& item )
{
	
	if( !_initcalled )
	{	
		ddstring content;
		ddwebsite& site = pagelet.get_site();
		site.fetch_data_entry( DDKEYSTOPWORDS, content );
		_keygen.init_stop_words( content, site.get_config()["main"]["evilchars"] );
		_initcalled = true;
	}	
	
	// ohne key ist keine suche nötig ...
	// und faqs brauchen nicht gedoppelt werden.
	
	ddwebcgi& cgi = pagelet.get_cgi();
	const ddstring& snkey = cgi["sn_key"];
	if( snkey.empty() || !cgi["sn_check5"].empty() )
		return rs_ok;
	
	ddwebview& table = pagelet.get_view( "sxpagesearch.faqs" );
	int hits = table.fill(	"on_search", _keygen.prepare_key( snkey ) );
	// die suche loggen, aber nur wenn sie neu für diesen user ist
	if( pagelet.get_user_id().get_property( snkey ).empty() ) 
	{
		ddstring msg = "faqs: " + snkey + " hits: " + ddstrutil::from_int( hits );
		pagelet.get_user_id().set_property( snkey, "1" );
		pagelet.get_site().log_message( ddlogdevice::lg_search, msg );
	}
	// kein ergebnis ? den block nicht anzeigen!
	if( hits <= 0 )
		return rs_ok;
	
	//dditem& msgentry = item["sn_subbody"]["sn_msgentry"];
  dditem& msgentry = item["sn_msgentry"];
  table.on_render( pagelet, msgentry, 0, table.size() );	
	
 	// ... und raus damit ...
	pagelet.write( item.to_string() );
	
	return rs_ok;
}


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

    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
*/

sxblockarchive::sxblockarchive( const ddstring& name, ddwebsite* site, size_t idcount )
: ddwebblock( name, site ), _idcount( idcount )
{

	
}


/**
	Destructor.
*/

sxblockarchive::~sxblockarchive()
{

}


/**
	Automatische Archivbox: für alte Artikel soll sie eingeblendet werden.
*/
	
dd_result sxblockarchive::create_content( ddwebpage& pagelet, dditem& item )
{
	item.set_visible( pagelet.context.id.as_size_t() <= _idcount );
	pagelet.write( item.to_string() );
	return rs_ok;
}


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

    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
*/

sxblockchannel::sxblockchannel( const ddstring& name, ddwebsite* site, bool use_first)
: ddwebblock( name, site ), _use_first( use_first )
{

	
}


/**
	Destructor.
*/

sxblockchannel::~sxblockchannel()
{

}


/**
	Automatische Archivbox: für alte Artikel soll sie eingeblendet werden.
*/
	
dd_result sxblockchannel::create_content( ddwebpage& pagelet, dditem& item )
{
	/*
	item.set_visible( pagelet.context.id.as_size_t() <= _idcount );
	pagelet.write( item.to_string() );
	*/
	return rs_ok;
}


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

    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
*/

sxblockmemberbox::sxblockmemberbox( const ddstring& name, ddwebsite* site )
: ddwebblock( name, site )
{

	
}


/**
	Destructor.
*/

sxblockmemberbox::~sxblockmemberbox()
{

}


/**
	Rendert den 'members online' kasten.
*/
	
dd_result sxblockmemberbox::create_content( ddwebpage& pagelet, dditem& item )
{
	ddthreadautomutex tmplock( _listlock );		
	//pagelet.get_session_manager().create_block_memberbox( _content );	
	return rs_ok;
}


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

    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
*/

sxblockteambox::sxblockteambox( const ddstring& name, ddwebsite* site )
: ddwebblock( name, site )
{

}


/**
	Destructor.
*/

sxblockteambox::~sxblockteambox()
{

}


/**
	rendert die teambox.
*/
	
dd_result sxblockteambox::create_content( ddwebpage& pagelet, dditem& item )
{
	//pagelet.get_session_manager().create_block_teambox( _content );
 	return rs_ok;	
}


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

    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
*/

sxblocknavigator::sxblocknavigator( const ddstring& name, ddwebsite* site )
: ddwebblock( name, site )
{
	_navlink.create("<li ||><a href='|3|'>|5|</a></li>\n","|");
}


/**
	Destructor.
*/

sxblocknavigator::~sxblocknavigator()
{

}


/**
	rendert den neuen navi.
*/
	
dd_result sxblocknavigator::create_content( ddwebpage& pagelet, dditem& item )
{
	
	_content = "";

	int numrows = pagelet.context.numrows;
	int from    = pagelet.context.navifrom;
	int to  	  = pagelet.context.navito;
	int itemspp = pagelet.context.itemspp;
	
	_linkbase = pagelet.context.navilinkbase;
	
	
	if( _linkbase.empty() )
	{
		_content = "Navigator: Linkbasis fehlt!";
		return rs_ok;	
	}
	
	// nur anzeigen, falls nötig
	if( numrows <= itemspp )
		return rs_ok;	
	
	// ab hier: es gibt was zu navigieren
	dditem& navi = item["sn_navibody"];
	navi.clear_children();
	
	/*
	
		Die neue navigation soll 'floaten' und entspricht damit einem Nachbau
		der Navigation von fotolia.de:
		
		Grundsätzlich immer erscheinen die Links auf Weiter, Zurück, erste Seite, letzte Seite:
		
		[<<] [1] ... [n] [>>]
		
		Dazwischen kommt der Floating-Block, der zur aktuelle Seite max. 2 Blöcke davor und dahinter anzeigt.
		Wenn sich ein Sprung zwischen erster und letzer Seite und dem Anfang bzw. Ende des Floating-Blocks ergibt,
		werden 'Gaps' eingefügt: [..]
		
		Also etwa so:
		
		[<<] [1] [..] [15] [16]] [[17]] [18] [19] [..] [46] [>>]
	
	*/
	
	ddstring result;
	
	//<li class="active"><a href="#">3</a></li>
	//<li><b><!--#sn_last:dp_link--></b></li>
	
	// eigentlich trivial: die gesamtanzahl der seiten entspricht den
	// 'vollen' Seiten + die eventuell vorhandene Restseite
	_numpages = ( numrows / itemspp ) + ( numrows % itemspp ? 1 : 0 );
	_currentpage = (from/itemspp)+1;
	// mittelteil: max. 5 blocks
	int xfrom = DDMAX( 2, _currentpage - 2 );
	int xto   = DDMIN( _numpages - 1, _currentpage + 2 );
	
	// Es geht zurück
	create_slot( result, _currentpage-1, "<b>&laquo; Zur&uuml;ck</b>" );
	
	// erste Seite
	create_slot( result, 1 );
	
	// ggf. die [..] einfügen: Es gibt eine Lücke vorne:
	// fotolia bug, s.u. eine distance von 1 ist keine :-)
	int distance = xfrom - 2;
	if( distance )
		create_slot( result, 2, distance == 1 ? "" : ".." );	
	
	// mittelteil einfügen: [16]] [[17]] [18] 
	for( int i = xfrom; i <= xto; ++i )
		create_slot( result, i );	
		
	// ggf. die [..] einfügen: Es gibt eine Lücke hinten:
	// fotolia Bug: [96] [97] [..] [99] [Weiter]:
	// der logok halber sollte hier statt des 'Gap' die 98 stehen, d.h. Gap
	// füllen, wenn der Abstand nur 1 ist
	distance = _numpages - xto -1;
	if( distance )
		create_slot( result, _numpages-1, distance == 1 ? "" : ".." );	

	// letzte Seite
	create_slot( result, _numpages );	
	
	// Es geht Weiter
	create_slot( result, _currentpage+1, "<b>Weiter &raquo;</b>" );

	navi["sn_naviblocks"].from_string( result );
	item.flush_children( pagelet, _content );
	
	return rs_ok;	

}


void sxblocknavigator::create_slot( ddstring& result, int pageno, const ddstring& ltext )
{
	int blockno = pageno - 1;
	// hack: zurück && weiter ggf. ausgrauen
	if( blockno < 0 || ( (pageno > _numpages) ) )
	{	
		result += "<li class='graydisabled'>" + ltext + "</li>";
		return;

	}
	// den aktuellen Block markieren, aber nicht die zurück/weiter links	
	_navlink[1] = pageno == _currentpage && ltext.empty() ? "class='active'" : "" ;
	ddstring linktext = ltext.empty() ? ddstrutil::from_int( pageno ) : ltext ;
	// block 0: Die Null weglassen, doppelter content
	_navlink[3] = !blockno? _linkbase : _linkbase + ddstrutil::from_int( blockno );
	_navlink[5] = linktext;
	
	result += _navlink.join();

}


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

    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
*/

sxblockstatistics::sxblockstatistics( const ddstring& name, ddwebsite* site )
: ddwebblock( name, site )
{

}


/**
	Destructor.
*/

sxblockstatistics::~sxblockstatistics()
{

}


/**
	rendert den neuen navi.
*/
	
dd_result sxblockstatistics::create_content( ddwebpage& pagelet, dditem& item )
{
	_content = _result = "";
	item.flush_children( pagelet, _content );
	return rs_ok;	
	
	dditem& slide = item["sn_slide"];
	slide.clear_value();
	
	for( int i=1; i<4; ++i )
		render_slide( pagelet, slide, i );
	
	slide.from_string( _result );
	item.flush_children( pagelet, _content );
	
	return rs_ok;	

}


void sxblockstatistics::render_slide( ddwebpage& pagelet, dditem& item, int slideno )
{
	
	item["sn_slideno"].from_int( slideno );
	item["sn_slideno2"].from_int( slideno );
	 
	ddusermanager& mgr = pagelet.get_user_manager();
	dditem& current1 = item["sn_msgentry1"];
	current1.clear_value();
	
	ddstring entry;	
	for( int i=0; i<5; ++i )
	{		
		current1["id"].from_string( mgr[i].get_id() );
		current1["sn_nickname"].from_string( mgr[i].get_nick_name() );
		entry += current1.to_string();
	}
	current1.from_string( entry );

	dditem& current2 = item["sn_msgentry2"];	
	current2.clear_value();
	entry = "";
	
	for( int i=5; i<10; ++i )
	{
		current2["id"].from_string( mgr[i].get_id() );
		current2["sn_nickname"].from_string( mgr[i].get_nick_name() );
		entry += current2.to_string();
	}
	current2.from_string( entry );
	
	_result += item.to_string();
	
	
}


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

    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
*/

sxblocksysmessage::sxblocksysmessage( const ddstring& name, ddwebsite* site )
: ddwebblock( name, site )
{

}


/**
	Destructor.
*/

sxblocksysmessage::~sxblocksysmessage()
{

}


/**
	rendert die aktuelle Systemmeldung
*/
	
dd_result sxblocksysmessage::create_content( ddwebpage& pagelet, dditem& item )
{
	_content = "";
	
	dduserid& uid = pagelet.get_user_id();
	const ddstring& sysmsg1 = uid.get_property( "sn_sysmessage1" );
	
	if( sysmsg1.empty() )
		return rs_ok;	
	
	const ddstring& sysmsg2 = uid.get_property( "sn_sysmessage2" );
	const ddstring& syslnk1 = uid.get_property( "sn_sysmessage3" );
	
	// message zusammenbasteln:
	ddstring sysmsg3 = sysmsg1;
	if( !syslnk1.empty() )
		sysmsg3 = "<a href='" + syslnk1 + "'>" + sysmsg1 + "</a>";
	
	ddstring sysmsg4 = sysmsg2;
	if( !syslnk1.empty() )
		sysmsg4 = "<a href='" + syslnk1 + "'>" + sysmsg2 + "</a>";
	
	item["sn_sysmessage1"].from_string( sysmsg3 );
	item["sn_sysmessage2"].from_string( sysmsg4.empty() ? "" :  "<br />" + sysmsg4 + "<br />" );
	
	/*
	<!--Mail message-->
	<div class="message-top">
     <div class="mt-box">
      	 <span><!--#sn_sysmessage1--></span><br />
     			<!--#sn_sysmessag2-->
      </div>
    </div>
	*/
	item.flush_children( pagelet, _content );
	
	// und schluss mit der message
	uid.clear_system_message();
	
	return rs_ok;	

}








