<?PHP

/* ====================
Seditio - Website engine
Copyright Neocrome
http://www.neocrome.net
[BEGIN_SED]
File=plugins/forumstats/forumstats.php
Version=101
Updated=2006-mar-15
Type=Plugin
Author=Neocrome
Description=
[END_SED]

[BEGIN_SED_EXTPLUGIN]
Code=forumstats
Part=main
File=forumstats
Hooks=standalone
Tags=
Order=10
[END_SED_EXTPLUGIN]
==================== */


if (!defined('SED_CODE') || !defined('SED_PLUG')) { die('Wrong URL.'); }

$plugin_title = $L['plu_title'];

if (!$forumstats)
	{
	$totalsections = sed_sql_rowcount($db_forum_sections);
	$totaltopics = sed_sql_rowcount($db_forum_topics);
	$totalposts = sed_sql_rowcount($db_forum_posts);

	$sql = sed_sql_query("SELECT SUM(fs_viewcount) FROM $db_forum_sections");
	$totalviews = sed_sql_result($sql, 0, "SUM(fs_viewcount)");

	$sql = sed_sql_query("SELECT SUM(fs_topiccount_pruned) FROM $db_forum_sections");
	$totaltopics += sed_sql_result($sql, 0, "SUM(fs_topiccount_pruned)");

	$sql = sed_sql_query("SELECT SUM(fs_postcount_pruned) FROM $db_forum_sections");
	$totalposts += sed_sql_result($sql, 0, "SUM(fs_postcount_pruned)");

	$plugin_body = "<table><tr><td>".$L['plu_sections']." :</td><td style=\"text-align:right;\">".$totalsections."</td></tr>";
	$plugin_body .= "<tr><td>".$L['plu_topics']." : </td><td style=\"text-align:right;\">".$totaltopics."</td></tr>";
	$plugin_body .= "<tr><td>".$L['plu_posts']." : </td><td style=\"text-align:right;\">".$totalposts."</td></tr>";
	$plugin_body .= "<tr><td>".$L['plu_views']." : </td><td style=\"text-align:right;\">".$totalviews."</td></tr></table>";

	$plugin_body .= "<h4>".$L['plu_repliedtop10']." :</h4>";

		$sql = sed_sql_query("SELECT t.ft_id, t.ft_title, t.ft_postcount, t.ft_sticky, t.ft_state, s.fs_id, s.fs_title FROM $db_forum_topics t, $db_forum_sections s WHERE t.ft_sectionid=s.fs_id ORDER BY ft_postcount DESC LIMIT 10");

	$ii=0;

	while ($row = sed_sql_fetcharray($sql))
		{
		$ii++;
		$ft_title=$row['ft_title'];
		$ft_title .= ($row['ft_sticky']) ? " (*)" : '';
		$ft_title .= ($row['ft_state']) ? " (x)" : '';

		$plugin_body .= "#".$ii." : <a href=\"forums.php?m=topics&amp;s=".$row['fs_id']."\"".$cfg['exttarget'].">".sed_cc($row['fs_title'])."</a> ".$sed_separator." ";
		$plugin_body .= (sed_auth('forums', $row['fs_id'], 'R')) ? "<a href=\"forums.php?m=posts&amp;q=".$row['ft_id']."\">".sed_cc($ft_title)."</a> (".$row['ft_postcount'].")" : $L['plu_hidden'];
		$plugin_body .= "<br />";
		}

	$plugin_body .= "<h4>".$L['plu_viewedtop10']." :</h4>";

	$sql = sed_sql_query("SELECT t.ft_id, t.ft_title, t.ft_viewcount, t.ft_sticky, t.ft_state, s.fs_id, s.fs_title FROM $db_forum_topics t, $db_forum_sections s WHERE t.ft_sectionid=s.fs_id ORDER BY ft_viewcount DESC LIMIT 10");

	$ii=0;

	while ($row = sed_sql_fetcharray($sql))
		{
		$ii++;
		$ft_title = $row['ft_title'];
		$ft_title .= ($row['ft_sticky'] && $row['ft_state']) ? " (!)" : '';
		$ft_title .= ($row['ft_sticky'] && !$row['ft_state']) ? " (*)" : '';
		$ft_title .= ($row['ft_state'] && !$row['ft_sticky']) ? " (x)" : '';

		$plugin_body .= "#".$ii." : <a href=\"forums.php?m=topics&amp;s=".$row['fs_id']."\"".$cfg['exttarget'].">".sed_cc($row['fs_title'])."</a> ".$cfg['separator']." ";
		$plugin_body .= (sed_auth('forums', $row['fs_id'], 'R')) ? "<a href=\"forums.php?m=posts&amp;q=".$row['ft_id']."\">".sed_cc($ft_title)."</a> (".$row['ft_viewcount'].")"  : $L['plu_hidden'];
		$plugin_body .= "<br />";
		}

	$plugin_body .= "<h4>".$L['plu_posterstop10']." :</h4>";

	$ii=0;
	$tmpstats = '';
	$sql1 = sed_sql_query("DROP TEMPORARY TABLE IF EXISTS tmp1");
	$sql = sed_sql_query("CREATE TEMPORARY TABLE tmp1 SELECT fp_posterid, fp_postername, COUNT(*) AS tmp_postcount FROM $db_forum_posts WHERE fp_posterid>0 GROUP BY fp_postername");
	$sql = sed_sql_query("SELECT * FROM tmp1 WHERE 1 ORDER by tmp_postcount DESC LIMIT 10");
	$sql1 = sed_sql_query("DROP TEMPORARY TABLE IF EXISTS tmp1");

	while ($row = sed_sql_fetcharray($sql))
		{
		$ii++;
		$plugin_body .= "#".$ii." : ".sed_build_user($row['fp_posterid'], $row['fp_postername'])."</a> (".$row["tmp_postcount"].")<br />";
		}

	$sql = sed_sql_query("DROP TEMPORARY TABLE IF EXISTS tmp1");
	sed_cache_store('forumstats', $plugin_body, 600);
	}
else
   	{ $plugin_body = $forumstats; }

?>
