<?php

$plugins->add_hook('index_start', 'dynamicstats_index');
$plugins->add_hook('stats_start', 'dynamicstats_run');
$plugins->add_hook('member_profile_start', 'dynamicstats_run');
$plugins->add_hook('usercp_start', 'dynamicstats_usercp');


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.2',
		'compatibility'	=> '1*'
	);
}

function dynamicstats_index()
{
	global $mybb;
	if($mybb->settings['showindexstats'] == 'yes' || $mybb->settings['showindexstats'] == 1)
		dynamicstats_run();
}
function dynamicstats_usercp()
{
	global $mybb;
	if($mybb->version_code >= 1600 && !$mybb->input['action']) dynamicstats_run();
}

function dynamicstats_run()
{
	$ufs = str_replace('\'', '', get_unviewable_forums());
	if(!$ufs) return;
	
	global $cache, $db, $stats, $mybb;
	if($mybb->version_code >= 1400) {
		$mybb_no = '0';
		$mybb_yes = '1';
	} else {
		$mybb_no = 'no';
		$mybb_yes = 'yes';
	}
	$uf = explode(',', $ufs);
	
	// if all forums are visible to this user, and he is supermod/admin, escape
	if(empty($uf) && ($mybb->usergroup['issupermod'] == $mybb_yes || $mybb->usergroup['cancp'] == $mybb_yes))
		return;
	
	if(!is_array($stats))
		$stats = $cache->read("stats");
	
	// recount all the threads & posts
	$newstat = $db->fetch_array($db->query('SELECT SUM(threads) AS nt, SUM(posts) as np FROM '.TABLE_PREFIX.'forums WHERE fid NOT IN ('.$ufs.') AND active != "'.$mybb_no.'"'));
	$stats['numthreads'] = $newstat['nt'];
	$stats['numposts'] = $newstat['np'];
	
	
	// cache hacks :P
	$cache->cache['stats'] = $stats;
}
?>