Skip to content

Commit

Permalink
GH-224 Move username change screen code to Screen
Browse files Browse the repository at this point in the history
  • Loading branch information
mdziekon committed Jun 23, 2022
1 parent 25fb4b9 commit cc9dd39
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 66 deletions.
2 changes: 2 additions & 0 deletions modules/settings/_includes.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

include($includePath . './screens/InVacationMode/InVacationMode.screen.php');
include($includePath . './screens/InVacationMode/InVacationMode.utils.php');
include($includePath . './screens/UsernameChange/UsernameChange.screen.php');
include($includePath . './screens/UsernameChange/UsernameChange.utils.php');

include($includePath . './utils/content/prepareChangeProcessEmails.content.php');

Expand Down
73 changes: 73 additions & 0 deletions modules/settings/screens/UsernameChange/UsernameChange.screen.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace UniEngine\Engine\Modules\Settings\Screens\UsernameChange;

use UniEngine\Engine\Modules\Settings;
use UniEngine\Engine\Modules\Settings\Screens\UsernameChange;

/**
* @param array $params
* @param arrayRef $params['input']
* @param arrayRef $params['user']
*/
function render($props) {
global $_Lang, $_SkinPath;

$input = &$props['input'];
$user = &$props['user'];

$screenTitle = $_Lang['NickChange_Title'];

$inputHandlingResult = UsernameChange\Utils\handleScreenInput([
'input' => &$input,
'user' => &$user,
]);

if ($inputHandlingResult) {
if (!$inputHandlingResult['isSuccess']) {
$errorMessage = Settings\Utils\ErrorMappers\mapValidateUsernameChangeErrorToReadableMessage(
$inputHandlingResult['error']
);

return message($errorMessage, $screenTitle, 'settings.php?mode=nickchange');
}

return message($_Lang['NewNick_saved'], $screenTitle, 'login.php');
}

$localTemplateLoader = createLocalTemplateLoader(__DIR__);
$tplBodyCache = [
'body' => $localTemplateLoader('body'),
];

$userDarkEnergy = $user['darkEnergy'];

$_Lang['skinpath'] = $_SkinPath;
$_Lang['DarkEnergy_Counter'] = $userDarkEnergy;
if ($userDarkEnergy >= 15) {
$_Lang['DarkEnergy_Color'] = 'lime';
} else if ($userDarkEnergy > 0) {
$_Lang['DarkEnergy_Color'] = 'orange';
} else {
$_Lang['DarkEnergy_Color'] = 'red';
}

$_Lang['NickChange_Info'] = parsetemplate(
$_Lang['NickChange_Info'],
[
'data_changeCost' => Settings\Utils\Helpers\getUsernameChangeCost(),
]
);
$_Lang['AreYouSure'] = parsetemplate(
$_Lang['AreYouSure'],
[
'data_changeCost' => Settings\Utils\Helpers\getUsernameChangeCost(),
]
);

$screenHTML = parsetemplate($tplBodyCache['body'], $_Lang);

display($screenHTML, $screenTitle, false);
}

?>
54 changes: 54 additions & 0 deletions modules/settings/screens/UsernameChange/UsernameChange.utils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace UniEngine\Engine\Modules\Settings\Screens\UsernameChange\Utils;

use UniEngine\Engine\Modules\Session;
use UniEngine\Engine\Modules\Settings;

/**
* @param array $params
* @param arrayRef $params['input']
* @param arrayRef $params['user']
*/
function handleScreenInput($params) {
$input = &$params['input'];
$user = &$params['user'];

if (empty($input['newnick'])) {
return;
}

$normalizedNewUsername = trim($input['newnick']);

$usernameChangeValidationResult = Settings\Utils\Validators\validateUsernameChange([
'input' => [
'newUsername' => $normalizedNewUsername,
],
'currentUser' => &$user,
]);

if (!$usernameChangeValidationResult['isSuccess']) {
return [
'isSuccess' => false,
'error' => $usernameChangeValidationResult['error'],
];
}

Settings\Utils\Queries\updateUserOnUsernameChange([
'newUsername' => $normalizedNewUsername,
'currentUser' => &$user,
]);
Settings\Utils\Queries\createUsernameChangeEntry([
'newUsername' => $normalizedNewUsername,
'currentUser' => &$user,
]);

Session\Utils\Cookie\clearSessionCookie();

return [
'isSuccess' => true,
'payload' => [],
];
}

?>
5 changes: 5 additions & 0 deletions modules/settings/screens/UsernameChange/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

header("Location: ../index.php");

?>
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace UniEngine\Engine\Modules\Settings\Utils\Helpers;

function getUsernameChangeCost() {
return 50;
return 5;
}

?>
69 changes: 4 additions & 65 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -1170,71 +1170,10 @@ function isInputKeyChecked($input, $key) {
}
else if($Mode == 'nickchange')
{
// User is trying to change his nickname
if (!empty($_POST['newnick'])) {
$NewNick = trim($_POST['newnick']);

$usernameChangeValidationResult = Settings\Utils\Validators\validateUsernameChange([
'input' => [
'newUsername' => $NewNick,
],
'currentUser' => &$_User,
]);

if (!$usernameChangeValidationResult['isSuccess']) {
$errorMessage = Settings\Utils\ErrorMappers\mapValidateUsernameChangeErrorToReadableMessage(
$usernameChangeValidationResult['error']
);

message($errorMessage, $_Lang['NickChange_Title'], 'settings.php?mode=nickchange');
} else {
Settings\Utils\Queries\updateUserOnUsernameChange([
'newUsername' => $NewNick,
'currentUser' => &$_User,
]);
Settings\Utils\Queries\createUsernameChangeEntry([
'newUsername' => $NewNick,
'currentUser' => &$_User,
]);

Session\Utils\Cookie\clearSessionCookie();

message($_Lang['NewNick_saved'], $_Lang['NickChange_Title'], 'login.php');
}
}
else
{
$_Lang['skinpath'] = $_SkinPath;
$_Lang['DarkEnergy_Counter'] = $_User['darkEnergy'];
if($_User['darkEnergy'] >= 15)
{
$_Lang['DarkEnergy_Color'] = 'lime';
}
else if($_User['darkEnergy'] > 0)
{
$_Lang['DarkEnergy_Color'] = 'orange';
}
else
{
$_Lang['DarkEnergy_Color'] = 'red';
}

$_Lang['NickChange_Info'] = parsetemplate(
$_Lang['NickChange_Info'],
[
'data_changeCost' => Settings\Utils\Helpers\getUsernameChangeCost(),
]
);
$_Lang['AreYouSure'] = parsetemplate(
$_Lang['AreYouSure'],
[
'data_changeCost' => Settings\Utils\Helpers\getUsernameChangeCost(),
]
);

// Informations box
display(parsetemplate(gettemplate('settings_changenick'), $_Lang), $_Lang['NickChange_Title'], false);
}
Settings\Screens\UsernameChange\render([
'input' => &$_POST,
'user' => &$_User,
]);
}

?>

0 comments on commit cc9dd39

Please sign in to comment.