<?PHP

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

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

list($usr['auth_read'], $usr['auth_write'], $usr['isadmin']) = sed_auth('forums', 'any');
sed_block($usr['auth_read']);

$id = sed_import('id','G','INT');
$s = sed_import('s','G','ALP');
$q = sed_import('q','G','INT');
$p = sed_import('p','G','INT');
$d = sed_import('d','G','INT');
$o = sed_import('o','G','ALP');
$w = sed_import('w','G','ALP',4);
$c = sed_import('c','G','ALP');
$quote = sed_import('quote','G','INT');
$poll = sed_import('poll','G','INT');
$vote = sed_import('vote','G','INT');
$unread_done = FALSE;
$filter_cats = FALSE;
$ce = explode('_', $s);

if (count($ce)>1)
	{
	$c = 0;
	$query = array();

	foreach($ce as $i => $line)
		{
		$cat_code = trim(sed_import($line,'D','INT'));

		if ($cat_code>0)
			{ $query[] = $cat_code; }
		else
			{ sed_die(); }
		}

	if (count($query>1))
		{
		$query = "(".implode(",", $query).")";
		$filter_cats = TRUE;
		}
	else
		{ sed_die(); }
	}
else
	{ $c = ($c=='fold') ? 9999 : sed_import($c,'D','INT'); }


/* === Hook === */
$extp = sed_getextplugins('forums.sections.first');
if (is_array($extp))
	{ foreach($extp as $k => $pl) { include('plugins/'.$pl['pl_code'].'/'.$pl['pl_file'].'.php'); } }
/* ===== */

if ($n=='markall' && $usr['id']>0)
	{
	$sql = sed_sql_query("UPDATE $db_users set user_lastvisit='".$sys['now_offset']."' WHERE user_id='".$usr['id']."'");
	$usr['lastvisit'] = $sys['now_offset'];
	}

if ($filter_cats)
	{ $sql = sed_sql_query("SELECT * FROM $db_forum_sections WHERE fs_id IN $query ORDER by fs_order, fs_title"); }
else
	{ $sql = sed_sql_query("SELECT * FROM $db_forum_sections ORDER by fs_order, fs_title"); }


if (!$sed_sections_act)
	{
	$timeback = $sys['now'] - 604800;
	$sql_acttmp = sed_sql_query("SELECT fs_id FROM $db_forum_sections");

	while ($tmprow = sed_sql_fetcharray($sql_acttmp))
		{
		$section = $tmprow['fs_id'];
		$sqltmp = sed_sql_query("SELECT COUNT(*) FROM $db_forum_posts WHERE fp_creation>'$timeback' AND fp_sectionid='$section'");
		$sed_sections_act[$section] = sed_sql_result($sqltmp,0,"COUNT(*)");
		}
		sed_cache_store('sed_sections_act', $sed_sections_act, 3600);
	}

unset($pcat);
$secact_max = max($sed_sections_act);
$out['markall'] = ($usr['id']>0) ? "<a href=\"forums.php?n=markall\">".$L['for_markallasread']."</a>" : '';

$out['subtitle'] = $L['Forums'];

/* === Hook === */
$extp = sed_getextplugins('forums.sections.main');
if (is_array($extp))
	{ foreach($extp as $k => $pl) { include('plugins/'.$pl['pl_code'].'/'.$pl['pl_file'].'.php'); } }
/* ===== */

require("system/header.php");

$mskin = "skins/".$skin."/forums.sections.tpl";
$t = new XTemplate($mskin);

$t->assign(array(
	"FORUMS_SECTIONS_PAGETITLE" => "<a href=\"forums.php\">".$L['Forums']."</a>",
	"FORUMS_SECTIONS_MARKALL" =>  $out['markall'],
	"FORUMS_SECTIONS_GMTTIME" => $L['Alltimesare']." ".$usr['timetext'],
	"FORUMS_SECTIONS_WHOSONLINE" => $out['whosonline']." : ".$out['whosonline_reg_list']
		));

/* === Hook - Part1 : Set === */
$extp = sed_getextplugins('forums.sections.loop');
/* ===== */

$catnum = 1;

while ($fsn = sed_sql_fetcharray($sql))
	{
	$display_block = FALSE;

	if ($pcat!=$fsn['fs_category'])
		{
		$pcat = $fsn['fs_category'];
		$t-> assign(array(
			"FORUMS_SECTIONS_ROW_CAT_TITLE" => "<a href=\"forums.php?c=".$catnum."\">".sed_cc($fsn['fs_category'])."</a>",
			));
		$t->parse("MAIN.FORUMS_SECTIONS_ROW.FORUMS_SECTIONS_ROW_CAT");
		$display_block = TRUE;
		$catnum++;
		}

	if (($c>0 && $c==$catnum-1) || !$c)
		{
		$fs_num++;
		$fsn['fs_topiccount_all'] = $fsn['fs_topiccount'] + $fsn['fs_topiccount_pruned'];
		$fsn['fs_postcount_all'] = $fsn['fs_postcount'] + $fsn['fs_postcount_pruned'];
		$fsn['fs_newposts'] = '0';
		$fsn['fs_desc'] = sed_bbcode($fsn['fs_desc']);
		$fsn['fs_desc'] .= ($fsn['fs_state']) ? " ".$L['Locked'] : '';

	 	if (!$fsn['fs_lt_id'])
	 		{ sed_forum_sectionsetlast($fsn['fs_id']); }

		if (sed_auth('forums', $fsn['fs_id'], 'R'))
			{
			$fsn['fs_timago'] = sed_build_timegap($fsn['fs_lt_date'], $sys['now_offset']);

			if ($usr['id']>0 && $fsn['fs_lt_date']>$usr['lastvisit'] && $fsn['fs_lt_posterid']!=$usr['id'])
				{
				$fsn['fs_title'] = "+ ".$fsn['fs_title'];
				$fsn['fs_newposts'] = '1';
				}

			if ($fsn['fs_lt_id']>0)
				{
				$fsn['lastpost'] = ($usr['id']>0 && $fsn['fs_lt_date']>$usr['lastvisit'] && $fsn['fs_lt_posterid']!=$usr['id']) ? "<a href=\"forums.php?m=posts&amp;q=".$fsn['fs_lt_id']."&amp;n=unread#unread\">" : "<a href=\"forums.php?m=posts&amp;q=".$fsn['fs_lt_id']."&amp;n=last#bottom\">";
				$fsn['lastpost'] .= sed_cutstring($fsn['fs_lt_title'], 32)."</a>";
				}
			else
				{
				$fsn['lastpost'] = '&nbsp;';
				$fsn['fs_lt_date'] = '&nbsp;';
				$fsn['fs_lt_postername'] = '';
				$fsn['fs_lt_posterid'] = 0;
				}

			$fsn['fs_lt_date'] = ($fsn['fs_lt_date']>0) ? @date($cfg['formatmonthdayhourmin'], $fsn['fs_lt_date'] + $usr['timezone'] * 3600) : '';
    	   	$fsn['fs_viewcount_short'] = ($fsn['fs_viewcount']>9999) ? floor($fsn['fs_viewcount']/1000)."k" : $fsn['fs_viewcount'];
	     	$fsn['fs_lt_postername'] = sed_build_user($fsn['fs_lt_posterid'], sed_cc($fsn['fs_lt_postername']));

			if (!$secact_max)
				{
				$section_activity = '';
				$section_activity_img = '';
				$secact_num = 0;
				}
			else
				{
				$secact_num = round(6.25 * $sed_sections_act[$fsn['fs_id']] / $secact_max);
				if ($secact_num>5) { $secact_num = 5; }
				if (!$secact_num && $sed_sections_act[$fsn['fs_id']]>1) { $secact_num = 1; }
				$section_activity_img = "<img src=\"skins/".$skin."/img/system/activity".$secact_num.".gif\" alt=\"\" />";
				}

			$t-> assign(array(
			"FORUMS_SECTIONS_ROW_ID" => $fsn['fs_id'],
			"FORUMS_SECTIONS_ROW_STATE" => $fsn['fs_state'],
			"FORUMS_SECTIONS_ROW_ORDER" => $fsn['fs_order'],
			"FORUMS_SECTIONS_ROW_TITLE" => $fsn['fs_title'],
			"FORUMS_SECTIONS_ROW_DESC" => $fsn['fs_desc'],
			"FORUMS_SECTIONS_ROW_ICON" => $fsn['fs_icon'],
			"FORUMS_SECTIONS_ROW_TOPICCOUNT" => $fsn['fs_topiccount'],
			"FORUMS_SECTIONS_ROW_POSTCOUNT" => $fsn['fs_postcount'],
			"FORUMS_SECTIONS_ROW_TOPICCOUNT_ALL" => $fsn['fs_topiccount_all'],
			"FORUMS_SECTIONS_ROW_POSTCOUNT_ALL" => $fsn['fs_postcount_all'],
			"FORUMS_SECTIONS_ROW_VIEWCOUNT" => $fsn['fs_viewcount'],
			"FORUMS_SECTIONS_ROW_VIEWCOUNT_SHORT" => $fsn['fs_viewcount_short'],
			"FORUMS_SECTIONS_ROW_URL" => "forums.php?m=topics&amp;s=".$fsn['fs_id'],
			"FORUMS_SECTIONS_ROW_LASTPOSTDATE" => $fsn['fs_lt_date'],
			"FORUMS_SECTIONS_ROW_LASTPOSTER" => $fsn['fs_lt_postername'],
			"FORUMS_SECTIONS_ROW_LASTPOST" => $fsn['lastpost'],
			"FORUMS_SECTIONS_ROW_TIMEAGO" => $fsn['fs_timago'],
			"FORUMS_SECTIONS_ROW_ACTIVITY" => $section_activity_img,
			"FORUMS_SECTIONS_ROW_ACTIVITYVALUE" => $secact_num,
			"FORUMS_SECTIONS_ROW_NEWPOSTS" => $fsn['fs_newposts'],
			"FORUMS_SECTIONS_ROW_ODDEVEN" => sed_build_oddeven($fs_num),
			"FORUMS_SECTIONS_ROW" => $fsn,
				));

			/* === Hook - Part2 : Include === */
			if (is_array($extp))
				{ foreach($extp as $k => $pl) { include('plugins/'.$pl['pl_code'].'/'.$pl['pl_file'].'.php'); } }
			/* ===== */

			$t->parse("MAIN.FORUMS_SECTIONS_ROW.FORUMS_SECTIONS_ROW_SECTION");
			$display_block = TRUE;
			}
		}

	if ($display_block)
		{ $t->parse("MAIN.FORUMS_SECTIONS_ROW"); }
	}

/* === Hook === */
$extp = sed_getextplugins('forums.sections.tags');
if (is_array($extp))
	{ foreach($extp as $k => $pl) { include('plugins/'.$pl['pl_code'].'/'.$pl['pl_file'].'.php'); } }
/* ===== */

$t->parse("MAIN");
$t->out("MAIN");

require("system/footer.php");

?>