<?php
if ( !defined( 'SMARTY_DIR' ) ) {
	include_once( '../init.php' );
}
include ( 'sessioninc.php' );

define( 'PAGE_ID', 'admin_mgt' );

if ( !checkAdminPermission( PAGE_ID ) ) {

	header( 'location: not_authorize.php' );
	exit;
}
$psize = getPageSize();

if ( $_GET['edit'] ) {

	$t->assign( 'admin',$osDB->getRow( 'SELECT * from ! where id = ?', array( ADMIN_TABLE, $_GET['edit'] ) ) );

	$t->assign( 'lang', $lang );

	$t->assign( 'error_msg', get_lang('admin_error', $_GET['errid'] ) );

	$t->assign('rendered_page', $t->fetch('admin/adminedit.tpl'));

	$t->display( 'admin/index.tpl' );

	exit;
}

if ( $_POST['deleteadmin'] ) {

   $adminname = $osDB->getOne("SELECT username FROM ".ADMIN_TABLE." WHERE id = '$_POST[deleteadmin]'");

	$osDB->query( 'DELETE FROM ! where id = ?', array( ADMIN_TABLE, $_POST['deleteadmin'] ) );

	$osDB->query( 'DELETE FROM !  where adminid = ?', array( ADMIN_RIGHTS_TABLE, $_POST['deleteadmin'] ) );

	if ($config['forum_installed'] != '' && $config['forum_installed'] != 'None') {

		include(FORUM_DIR.$config['forum_installed'] . '_forum.php');

		forum_manageadmin($adminname);
	}
}

$_SESSION['txtuname'] = $_SESSION['txtfullname'] = '';

$page_size = $psize;

$t->assign('psize', $psize);

$page = (int)$_GET['offset'];

if( $page == 0 ) $page = 1;

$upr = ($page)*$page_size - $page_size;

$sort = findSortBy();

$sql = 'SELECT count(*) as num FROM !';

$t->assign ( 'total_recs',  $osDB->getOne( $sql, array( ADMIN_TABLE) ) );

$t->assign( 'data', $osDB->getAll( 'SELECT * FROM ! ORDER BY ' . $sort . " LIMIT $upr,$page_size ", array( ADMIN_TABLE ) ) );

$t->assign( 'lang', $lang );

$t->assign( 'sort_type', checkSortType( $_GET['type'] ) );

$t->assign( 'querystring', 'sort=' . $_GET['sort'] . '&type=' . $_GET['type']);

$t->assign( 'upr', $upr );

$t->assign('rendered_page', $t->fetch('admin/manageadmin.tpl'));

$t->display( 'admin/index.tpl' );

?>