<?
if (!defined('IN_BANDTOPIA'))
{
	exit;
}

$userObj = new UserObj();

if($action == 'userList')
{
	$template -> set_filenames(array(
		'page' => MODULE_PATH.'admin/template/userList.htm'));
	
	$userList = $userObj->UserList();
	$i = 0;
	foreach($userList as $user)
	{
		$template -> assign_block_vars('user', array(
			'USER_ID' => $user->UserId,
			'USERNAME' => $user->UserName,
			'FIRST_NAME' => $user->FirstName,
			'LAST_NAME' => $user->LastName,
			'CELL_ALT' => ($i%2 == 1)?'_alt':''
		));
		$i++;
	}
}
elseif($action == 'userAdmin')
{
	$template -> set_filenames(array(
		'page' => MODULE_PATH.'admin/template/userAdmin.htm'));
	
	if ($_SERVER['REQUEST_METHOD'] == 'POST') {
		$userObj->UserId = $_POST['UserId'];
		$userObj->UserName = $_POST['UserName'];
		$userObj->FirstName = $_POST['FirstName'];
		$userObj->MI = $_POST['MI'];
		$userObj->LastName = $_POST['LastName'];
		$userObj->UserActive = ($_POST['UserActive']!=1)?0:1;
		$userObj->Email = $_POST['Email'];
		$userObj->UserTypeId = ($_POST['UserTypeId']=='')?0:$_POST['UserTypeId'];
		$userObj->AccountId = ($_POST['AccountId']=='')?0:$_POST['AccountId'];
		$userObj->AccountAdmin = ($_POST['AccountAdmin']!=1)?0:1;
		$userObj->ProfileId = ($_POST['ProfileId']=='')?0:$_POST['ProfileId'];
		$userObj->UpdateUser();
		
		if($_POST['Password'] == $_POST['ConfirmPassword'] && $userObj->UserId > 0)
		{
			$cryptObj = new cryptRoutineObj();
			$userObj->Password = $cryptObj->EncryptPass($_POST['Password']);
			$userObj->ChangePassword();
		}
	}
	else
	{
		$userObj -> UserId = $_GET['UserId'];
		$userObj -> GetUser();
	}
	
	if(isset($_GET['AccountId']) && $userObj->AccountId<1)
	{
		$userObj -> AccountId = $_GET['AccountId'];
	}
	
	$template -> assign_vars(array(
		'USER_ID' => $userObj -> UserId,
		'USERNAME' => $userObj -> UserName,
		'FIRST_NAME' => $userObj -> FirstName,
		'MI' => $userObj -> MI,
		'LAST_NAME' => $userObj -> LastName,
		'USER_ACTIVE' => ($userObj -> UserActive)?' CHECKED':'',
		'EMAIL' => $userObj -> Email,
		'USER_TYPE_ID' => $userObj -> UserTypeId,
		'ACCOUNT_ID' => $userObj -> AccountId,
		'ACCOUNT_ADMIN' => ($userObj -> AccountAdmin)?' CHECKED':'',
		'ACCOUNT_NAME' => $userObj -> AccountName,
		'PROFILE_ID' => $userObj -> ProfileId
	));
	
	$userTypeObj = new UserTypeObj();
	$userTypeList = $userTypeObj -> UserTypeList();
	foreach($userTypeList as $userType)
	{
		$template -> assign_block_vars('user_type', array(
			'USER_TYPE_ID' => $userType -> UserTypeId,
			'TYPE_NAME' => $userType -> TypeName,
			'USER_TYPE_SELECTED' => ($userType->UserTypeId == $userObj->UserTypeId)?' SELECTED':''
		));
	}
	
	$accountObj = new AccountObj();
	$accountList = $accountObj->AccountList();
	foreach($accountList as $account)
	{
		$template -> assign_block_vars('account', array(
			'ACCOUNT_ID' => $account->AccountId,
			'ACCOUNT_NAME' => $account->AccountName,
			'ACCOUNT_SELECTED' => ($account->AccountId == $userObj->AccountId)?' SELECTED':''
		));
	}
}

?>