<?php

include './core.php';

if( !isset( $user ) ) access_error();

if( !isset( $_REQUEST['sort_field'] ) )
	$sf = "name";
else
	$sf = $_REQUEST['sort_field'];

if( !isset( $_REQUEST['sort_order'] ) )
	$so = "ASC";
else
	$so = $_REQUEST['sort_order'];

if( !isset( $_REQUEST['page_num'] ) || $_REQUEST['page_num'] < 1 )
	$page_num = 1;
else
	$page_num = intval( $_REQUEST['page_num'] );

$Smarty->assign( 'page_num', $page_num );

list( $account_count ) = $MySQL->query_first( "SELECT COUNT(*) FROM server.accounts" );
$pages = ceil( $account_count / intval( $settings['accounts_perpage'] ) );

$Smarty->assign( 'pages', $pages );

$page_navigation = make_page_navigation( "accountlist.php?sort_order=".$so."&amp;sort_field=".$sf."&amp;".SID, $page_num, $pages, 2 );
$Smarty->assign( 'page_navigation', $page_navigation );

$accounts_result = $MySQL->query( "SELECT accountid FROM server.accounts ORDER BY ".$sf." ".$so." LIMIT ".( intval( $settings['accounts_perpage'] ) * ( $page_num - 1 ) ).", ".$settings['accounts_perpage'] );
$accounts = array();
while( $row = $MySQL->fetch_array( $accounts_result ) )
{
	$c_account = new Account( (int) $row['accountid'] );
    if( $c_account->ID > 0 && $c_account->activation && !$c_account->block )
    	$accounts[] = $c_account;
}

$Smarty->assign( 'accounts', $accounts );

$sort_fields = Array( 'name' => 'Benutzername', 'email' => 'eMail-Adresse', 'register_time' => 'Registrierdatum' );
$Smarty->assign( 'sort_fields', $sort_fields );
$sort_order = Array( 'ASC' => 'aufsteigender', 'DESC' => 'absteigender' );
$Smarty->assign( 'sort_order', $sort_order );

assign_formvalues( 'sort_field', 'sort_order' );

$page['content'] = $Smarty->fetch( 'accountlist.tpl' );

$Smarty->assign( 'page', $page );
$Smarty->display( 'main.tpl' );

?>