<?php
/***************************************************************************
 *                              admin_users_list.php
 *                              -------------------
 *   begin                : Wednesday, January 29, 2003
 *   copyright            : (C) 2003 Smartor
 *   email                : smartor_xp@hotmail.com
 *
 *   $Id: admin_users_list.php,v 1.1.0 1/29/2003, 11:02:14 ngoctu Exp $
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

define('IN_PHPBB', true);

if( !empty($setmodules) )
{
	$filename = basename(__FILE__);
	$module['Users']['Users List'] = $filename;
	return;
}

//
// Let's set the root dir for phpBB
//
$phpbb_root_path = '../';
require('./pagestart.php');

//
// Set variables
//
$users_per_page = 25;

$start = (isset($HTTP_GET_VARS['start'])) ? intval($HTTP_GET_VARS['start']) : 0;

if( isset($HTTP_POST_VARS['sort']) )
{
	$sort_method = $HTTP_POST_VARS['sort'];
}
else if( isset($HTTP_GET_VARS['sort']) )
{
	$sort_method = $HTTP_GET_VARS['sort'];
}
else
{
	$sort_method = 'u.uid';
}

if( isset($HTTP_POST_VARS['order']) )
{
	$sort_order = $HTTP_POST_VARS['order'];
}
else if( isset($HTTP_GET_VARS['order']) )
{
	$sort_order = $HTTP_GET_VARS['order'];
}
else
{
	$sort_order = '';
}


$template->set_filenames(array(
	'body' => 'admin/admin_users_list_body.tpl')
);

// Count users
$sql = "SELECT count(uid) as total FROM ".USERS_TABLE." WHERE uid > 0";
if(!$result = $db->sql_query($sql))
{
	message_die(GENERAL_ERROR, "Could not count users", "", __LINE__, __FILE__, $sql);
}
$row = $db->sql_fetchrow($result);
$total_users = $row['total'];

$template->assign_vars(array(
	'L_SELECT_SORT_METHOD' => $lang['Select_sort_method'],
	'U_LIST_ACTION' => append_sid("admin_users_list.php"),
	'L_SORT' => $lang['Sort'],
	'L_ORDER' => $lang['Order'],
	'L_SORT_DESCENDING' => $lang['Sort_Descending'],
	'L_SORT_ASCENDING' => $lang['Sort_Ascending'],
	'ID_SELECTED' => ($sort_method == 'u.uid') ? 'selected="selected"' : '',
	'USERNAME_SELECTED' => ($sort_method == 'u.uname') ? 'selected="selected"' : '',
	'POSTS_SELECTED' => ($sort_method == 'u.posts') ? 'selected="selected"' : '',
	'LASTVISIT_SELECTED' => ($sort_method == 'ue.user_lastvisit') ? 'selected="selected"' : '',
	'ASC_SELECTED' => ($sort_order != 'DESC') ? 'selected="selected"' : '',
	'DESC_SELECTED' => ($sort_order == 'DESC') ? 'selected="selected"' : '',
	'TOTAL_USERS' => $total_users
	)
);

// Query users info...
$sql = "SELECT u.uid, u.uname, u.email, user_regdate, ue.user_lastvisit, u.posts, u.level
		FROM (".USERS_TABLE." u 
		LEFT JOIN ".USERS_TABLE_EXT." ue ON ue.uid=u.uid)
		WHERE u.uid > 0
		ORDER BY " . $sort_method . " " . $sort_order . "
		LIMIT ".$start.",".$users_per_page;
if(!$result = $db->sql_query($sql))
{
	message_die(GENERAL_ERROR, "Could not query Users information", "", __LINE__, __FILE__, $sql);
}

while( $row = $db->sql_fetchrow($result) )
{
	$userrow[] = $row;
}

for ($i = 0; $i < $users_per_page; $i++)
{
	if (empty($userrow[$i]))
	{
		break;
	}

	$row_color = (($i % 2) == 0) ? "row1" : "row2";
	
	$template->assign_block_vars('userrow', array(
		'COLOR' => $row_color,
		'NUMBER' => $userrow[$i]['uid'],
		'USERNAME' => $userrow[$i]['uname'],
		'U_ADMIN_USER' => append_sid("admin_users.php?mode=edit&amp;" . POST_USERS_URL . "=" . $userrow[$i]['uid']),
		'U_ADMIN_USER_AUTH' => append_sid("admin_ug_auth.php?mode=user&amp;" . POST_USERS_URL . "=" . $userrow[$i]['uid']),
		'EMAIL' => $userrow[$i]['email'],
		'JOINED' => create_date($lang['DATE_FORMAT'], $userrow[$i]['user_regdate'], $board_config['board_timezone']),
		'LAST_VISIT' => (!$userrow[$i]['user_lastvisit']) ? '' : create_date($lang['DATE_FORMAT'], $userrow[$i]['user_lastvisit'], $board_config['board_timezone']),
		'POSTS' => $userrow[$i]['posts'],
		'ACTIVE' => ( $userrow[$i]['level']>0 ) ? $lang['Yes'] : $lang['No']
		) //end array
	);
} // end for

$template->assign_vars(array(
	'PAGINATION' => generate_pagination("admin_users_list.php?sort=$sort_method&amp;order=$sort_order", $total_users, $users_per_page, $start),
	'PAGE_NUMBER' => sprintf($lang['Page_of'], ( floor( $start / $users_per_page ) + 1 ), ceil( $total_users / $users_per_page ))
	) // end array
);

// Finally...
$template->pparse('body');

include('./page_footer_admin.php');

?>