<?php
//
//	file: includes/acp/acp_attach_statistics.php
//	author: ptirhiik
//	begin: 26/02/2006
//	version: 1.6.0 - 10/06/2006
//	license: http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
//
//	based on : attachment_mod copyright (c) 2002 Meik Sievertsen (acydburn)
//

if ( !defined('IN_PHPBB') )
{
	die('Hack attempt');
}

$cp_title = 'Attach_statistics';
$cp_title_explain = 'Attach_statistics_explain';

class attach_statistics
{
	var $requester;
	var $parms;

	function attach_statistics($requester, $parms='')
	{
		$this->requester = $requester;
		$this->parms = empty($parms) ? array() : $parms;
	}

	function process()
	{
		if ( $this->init() )
		{
			$this->check();
			$this->validate();
			$this->display();
			return true;
		}
		return false;
	}

	function init()
	{
		return true;
	}

	function check()
	{
	}

	function validate()
	{
	}

	function display()
	{
		global $db, $template, $user, $config;
		global $attach_config;

		// get total size andnumber of files
		$count_files = $sum_filesize = 0;
		$sql = 'SELECT COUNT(physical_filename) AS count_physical_filename, SUM(filesize) AS sum_filesize
					FROM ' . ATT_DESC_TABLE;
		$result = $db->sql_query($sql, false, __LINE__, __FILE__);
		if ( $row = $db->sql_fetchrow($result) )
		{
			$sum_filesize = intval($row['sum_filesize']);
			$count_files = intval($row['count_physical_filename']);
		}
		$db->sql_freeresult($result);

		// quota
		$quota_size = intval($attach_config->data['attachment_quota']);

		// number of posts
		$sql = 'SELECT DISTINCT post_id
					FROM ' . ATT_LINKS_TABLE . '
					WHERE post_id <> 0';
		$result = $db->sql_query($sql, false, __LINE__, __FILE__);
		$count_posts = $db->sql_numrows($result);
		$db->sql_freeresult($result);

		// number of topics
		$sql = 'SELECT p.topic_id
					FROM ' . ATT_LINKS_TABLE . ' a, ' . POSTS_TABLE . ' p
					WHERE p.post_id = a.post_id
					GROUP BY p.topic_id';
		$result = $db->sql_query($sql, false, __LINE__, __FILE__);
		$count_topics = $db->sql_numrows($result);
		$db->sql_freeresult($result);

		// number of private messages
		$sql = 'SELECT DISTINCT privmsgs_id
					FROM ' . ATT_LINKS_TABLE . '
					WHERE privmsgs_id <> 0';
		$result = $db->sql_query($sql, false, __LINE__, __FILE__);
		$count_pms = $db->sql_numrows($result);
		$db->sql_freeresult($result);

		// number of users posting attachments
		$sql = 'SELECT DISTINCT user_id
					FROM ' . ATT_LINKS_TABLE . '
					WHERE post_id <> 0';
		$result = $db->sql_query($sql, false, __LINE__, __FILE__);
		$count_users = $db->sql_numrows($result);
		$db->sql_freeresult($result);

		$stats = array(
			'Number_of_attachments' => array('value' => $count_files),
			'Total_filesize' => format_size($sum_filesize),
			'Attach_quota' => format_size($quota_size),
			'Number_posts_attach' => array('value' => $count_posts),
			'Number_topics_attach' => array('value' => $count_topics),
			'Number_pms_attach' => array('value' => $count_pms),
			'Number_users_attach' => array('value' => $count_users),
		);
		foreach ( $stats as $txt => $data )
		{
			$template->assign_block_vars('row', array(
				'L_STAT' => $user->lang($txt),
				'STAT' => $data['value'],
				'L_UNIT' => $data['unit'] ? $user->lang($data['unit']) : '',
			));
			$template->set_switch('row.unit', $data['unit']);
		}
		$template->assign_vars(array('FORM' => $template->include_file('acp/acp_attach_statistics_box.tpl', 'row')));
	}
}

// instantiate the display
$parms = array(
	'mode' => $menu_id,
	'sub' => $subm_id == $menu_id ? '' : $subm_id,
	'ctx' => $ctx_id == $subm_id ? '' : $ctx_id,
);
$form = new attach_statistics($cp_requester, $cp_parms + $parms);
$form->process();
$template->set_switch('form');
$template->set_filenames(array('body' => 'cp_generic.tpl'));

?>