<?php

$plugins->add_hook('index_start', 'dynamicstats_index');
$plugins->add_hook('stats_start', 'dynamicstats_run');


function dynamicstats_info()
{
	return array(
		'name'			=> 'Dynamic Board Statistics',
		'description'	=> 'Causes the board statistics to change, depending on whether users are allowed to view certain sections of the board.',
		'website'		=> 'http://mybbhacks.zingaburga.com/',
		'author'		=> 'ZiNgA BuRgA',
		'authorsite'	=> 'http://zingaburga.com/',
		'version'		=> '1.0',
		'compatibility'	=> '1*'
	);
}

function dynamicstats_index()
{
	global $mybb;
	if($mybb->settings['showindexstats'] == 'yes' || $mybb->settings['showindexstats'] == 1)
		dynamicstats_run();
}

function dynamicstats_run()
{
	$ufs = str_replace('\'', '', get_unviewable_forums());
	if(!$ufs) return;
	
	global $cache, $db, $stats;
	$uf = explode(',', $ufs);
	if(!is_array($stats))
		$stats = $cache->read("stats");
	
	$query = $db->query('SELECT fid,threads,posts FROM '.TABLE_PREFIX.'forums WHERE fid IN ('.$ufs.')');
	$forums = array();
	while($forum = $db->fetch_array($query))
		$forums[$forum['fid']] = $forum;
	
	foreach($uf as $fid)
	{
		$stats['numthreads'] -= $forums[$fid]['threads'];
		$stats['numposts'] -= $forums[$fid]['posts'];
	}
	
	// cache hacks :P
	$cache->cache['stats'] = $stats;
}
?>