<?php
//
//	file: includes/acp/acp_attach_userlist.php
//	author: ptirhiik
//	begin: 27/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_userlist';
$cp_title_explain = 'Attach_userlist_explain';

$available_sort = array(
	'author' => 'Sort_Username',
	'attachments' => 'Sort_Attachments',
	'filesize' => 'Sort_Size',
);
$sort_fields = array(
	'author' => 'u.username',
	'attachments' => $db->sql_col_id('COUNT(d.attach_id)', 'count_attach_id'),
	'filesize' => $db->sql_col_id('SUM(d.filesize)', 'sum_filesize'),
);
$available_order = array(
	'' => 'Sort_Ascending',
	'desc' => 'Sort_Descending',
);
$order_fields = array(
	'' => '',
	'desc' => ' DESC',
);
$dft_sort = 'attachments';
$dft_order = 'desc';

$fields = array(
	'author' => array('type' => 'attach_search_username', 'legend' => 'Search_author', 'explain' => 'Search_author_explain', 'length' => 25),
	'sort' => array('type' => 'list', 'legend' => 'Sort_by', 'options' => $available_sort, 'value' => $dft_sort),
	'order' => array('type' => 'list', 'combined' => true, 'options' => $available_order, 'value' => $dft_order),
);

class attach_userlist extends form
{
	var $start;
	var $ppage;

	function attach_userlist($requester, $parms='', &$fields)
	{
		$this->requester = $requester;
		$this->parms = empty($parms) ? array() : $parms;
		parent::form($fields);
	}

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

	function init()
	{
		global $config;

		parent::init();
		$this->start = _read('start', TYPE_INT);
		$this->ppage = _read('ppage', TYPE_INT, intval($config->data['topics_per_page']));
		return true;
	}

	function check()
	{
		global $config, $user;
		global $error, $error_msg;

		if ( _button('submit_form') )
		{
			parent::check();
		}
		if ( $error )
		{
			message_return($error_msg, 'Click_return_attach_userlist', $config->url($this->requester, $this->parms, true));
		}
	}

	function validate()
	{
	}

	function display()
	{
		global $db, $template, $user, $config;
		global $sort_fields, $order_fields;
		global $dft_sort, $dbt_order;

		// display search form
		foreach ( $this->fields as $field_name => $field )
		{
			$this->fields[$field_name]->display();
		}
		display_buttons($this->buttons);
		$template->assign_vars(array('SEARCH_FORM' => $template->include_file('form_fields.tpl', array('field'))));

		// check search on name
		$author = preg_replace('/\*/', '%', $this->fields['author']->value);
		if ( trim(str_replace('%', '', $author)) == '' )
		{
			$author = '';
		}

		// get total rows
		$sql = 'SELECT DISTINCT a.user_id
					FROM ' . ATT_LINKS_TABLE . ' a' . (empty($author) ? '' : ', ' . USERS_TABLE . ' u
					WHERE u.user_id = a.user_id
						AND u.username LIKE \'' . $db->sql_escape_string($author) . '\'');
		$result = $db->sql_query($sql, false, __LINE__, __FILE__);
		$total_users = $db->sql_numrows($result);
		$db->sql_freeresult($result);
		if ( $total_users )
		{
			// get doubled attachments (due to private messages)
			$sql = 'SELECT attach_id, MAX(privmsgs_id) AS max_privmsgs_id, COUNT(privmsgs_id) AS count_privmsgs_id
						FROM ' . ATT_LINKS_TABLE . '
						WHERE privmsgs_id <> 0
						GROUP BY attach_id
						HAVING COUNT(privmsgs_id) > 1';
			$result = $db->sql_query($sql, false, __LINE__, __FILE__);
			$privmsgs_ids = array();
			while ( $row = $db->sql_fetchrow($result) )
			{
				$privmsgs_ids[] = intval($row['max_privmsgs_id']);
			}
			$db->sql_freeresult($result);

			// get sort and order
			$sort_field = $sort_fields[ $this->fields['sort']->value ];
			$order_field = $order_fields[ $this->fields['order']->value ];

			// get users rows
			$sql = 'SELECT a.user_id, u.username, COUNT(a.attach_id) AS count_attach_id, SUM(d.filesize) AS sum_filesize
						FROM ' . USERS_TABLE . ' u, ' . ATT_LINKS_TABLE . ' a
							LEFT JOIN ' . ATT_DESC_TABLE . ' d
								ON d.attach_id = a.attach_id' . (empty($privmsgs_ids) ? '' : '
									AND a.privmsgs_id NOT IN(' . implode(', ', $privmsgs_ids) . ')') . '
						WHERE u.user_id = a.user_id' . (empty($author) ? '' : '
							AND u.username LIKE \'' . $db->sql_escape_string($author) . '\'') . '
						GROUP BY a.user_id
						ORDER BY ' . $sort_field . $order_field . '
						LIMIT ' . $this->start . ', ' . $this->ppage;
			unset($privmsgs_ids);
			$result = $db->sql_query($sql, false, __LINE__, __FILE__);
			unset($sql);
			$i = $this->start;
			$color = false;
			while ( $row = $db->sql_fetchrow($result) )
			{
				$i++;
				$file_size = format_size(intval($row['sum_filesize']));
				$template->assign_block_vars('row', array(
					'ROW_NUMBER' => $i,
					'USERNAME' => !$row['user_id'] || ($row['user_id'] == ANONYMOUS) ? $user->lang('Guest') : $row['username'],
					'U_USERNAME' => $config->url($this->requester, array_merge($this->parms, array('ctx' => 'search', POST_USERS_URL => !$row['user_id'] ? ANONYMOUS : intval($row['user_id']))), true),
					'COUNT_FILES' => intval($row['count_attach_id']),
					'SIZE_FILES' => $file_size['value'],
					'UNIT' => $user->lang($file_size['unit']),
				));
				$color = !$color;
				$template->set_switch('row.light', $color);
			}
			$db->sql_freeresult($result);
		}

		// display userlist
		$template->assign_vars(array(
			'L_USERNAME' => $user->lang('Username'),
			'L_USERNAME_ATT' => $user->lang('View_details'),
			'L_COUNT_FILES' => $user->lang('Attachments'),
			'L_SIZE_FILES' => $user->lang('Total_filesize'),
			'L_NO_MATCHES' => $user->lang('No_users_search_match'),
		));

		$parms = $this->parms;
		if ( $this->fields['sort']->value != $dft_sort )
		{
			$parms['sort'] = $this->fields['sort']->value;
		}
		if ( $this->fields['order']->value != $dft_order )
		{
			$parms['order'] = $this->fields['order']->value;
		}
		if ( trim($this->fields['author']->value) != '' )
		{
			$parms['author'] = rawurlencode($this->fields['author']->value);
		}
		$pagination = new pagination($this->requester, $parms);
		$pagination->display('pagination', $total_users, $this->ppage, $this->start, true, 'Users_count');
		unset($pagination);

		// send all to template
		$template->assign_block_vars('cp_content', array('BOX' => $template->include_file('acp/acp_attach_userlist_box.tpl')));
	}
}

// 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_userlist($cp_requester, $cp_parms + $parms, $fields);
$form->process();
$template->set_filenames(array('body' => 'cp_generic.tpl'));

?>