Skip to content

Commit

Permalink
Merge pull request #228 from mdziekon/gh-224-simplify-nickchange-screen
Browse files Browse the repository at this point in the history
GH-224 | Settings - Username change screen refactor
  • Loading branch information
mdziekon authored Jun 24, 2022
2 parents 25fb4b9 + 9040ac9 commit 44ec67b
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 79 deletions.
19 changes: 13 additions & 6 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,20 @@ function getUserMinimalVacationTime(&$user) {
return getUserMinimalNormalVacationTime($user);
}

function getUserMinimalNormalVacationTime(&$user) {
$hadProAccountWhenVacationStarted = ($user['pro_time'] > $user['vacation_starttime']);
function getUserMinimalNormalVacationDuration(&$user, $atTimestamp) {
$hasProAccount = ($user['pro_time'] > $atTimestamp);

return (
$hasProAccount ?
MINURLOP_PRO :
MINURLOP_FREE
);
}

$vacationMinimalDuration = (
$hadProAccountWhenVacationStarted ?
MINURLOP_PRO :
MINURLOP_FREE
function getUserMinimalNormalVacationTime(&$user) {
$vacationMinimalDuration = getUserMinimalNormalVacationDuration(
$user,
$user['vacation_starttime']
);

return ($vacationMinimalDuration + $user['vacation_starttime']);
Expand Down
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
74 changes: 74 additions & 0 deletions modules/settings/screens/UsernameChange/UsernameChange.screen.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?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'];
$usernameChangeCost = Settings\Utils\Helpers\getUsernameChangeCost();

$_Lang['skinpath'] = $_SkinPath;
$_Lang['DarkEnergy_Counter'] = $userDarkEnergy;
if ($userDarkEnergy >= $usernameChangeCost) {
$_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' => $usernameChangeCost,
]
);
$_Lang['AreYouSure'] = parsetemplate(
$_Lang['AreYouSure'],
[
'data_changeCost' => $usernameChangeCost,
]
);

$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");

?>
84 changes: 13 additions & 71 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
$LogonLIMIT = 20;
$MaxEspionageProbesCount = 9999;

$vacationMinSeconds = getUserMinimalNormalVacationDuration($_User, $Now);

$_Lang['ServerSkins'] = '';
$_Lang['QuickRes_PlanetList'] = '';
$_Lang['CreateResSortList'] = '';
Expand All @@ -30,9 +32,9 @@
$_Lang['ShowOldEmail'] = $_User['email'];
$_User['skinpath'] = $_SkinPath;
$_Lang['skinpath'] = $_User['skinpath'];
$_Lang['VacationDays'] = 3;
$_Lang['PHP_Insert_VacationComeback'] = $Now + ($_Lang['VacationDays'] * TIME_DAY);
$_Lang['PHP_Insert_VacationComeback'] = date('d.m.Y', $_Lang['PHP_Insert_VacationComeback'])." {$_Lang['atHour']} ".date('h:i:s', $_Lang['PHP_Insert_VacationComeback']);
$_Lang['PHP_Insert_VacationMinDuration'] = $vacationMinSeconds;
$_Lang['PHP_Insert_VacationComeback'] = $Now + $vacationMinSeconds;
$_Lang['PHP_Insert_VacationComeback'] = date('d.m.Y', $_Lang['PHP_Insert_VacationComeback'])." {$_Lang['atHour']} ".date('H:i:s', $_Lang['PHP_Insert_VacationComeback']);
$_Lang['PHP_Insert_LanguageOptions'] = [];

foreach ($_Lang['LanguagesAvailable'] as $langKey => $langData) {
Expand Down Expand Up @@ -109,6 +111,14 @@ function isInputKeyChecked($input, $key) {

die();
}
if ($Mode === 'nickchange') {
Settings\Screens\UsernameChange\render([
'input' => &$_POST,
'user' => &$_User,
]);

die();
}

if(empty($Mode) OR $Mode == 'general')
{
Expand Down Expand Up @@ -1168,73 +1178,5 @@ function isInputKeyChecked($input, $key) {
$Page = parsetemplate($BodyTPL, $_Lang);
display($Page, $_Lang['Page_Title'], false);
}
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);
}
}

?>
3 changes: 1 addition & 2 deletions templates/default_template/settings_body.tpl
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<script>
var OldPass = '{MD5OldPass}';
var VacationTime = {VacationDays} * 86400;
var VacationMiliseconds = VacationTime * 1000;
var VacationMiliseconds = {PHP_Insert_VacationMinDuration} * 1000;
var OldResSort = '{OldResSort_ArrayString}';
var OverrideTab = '{SetActiveMarker}';
var TemplateData = {'SaveOnlyThis': '{SaveOnlyThis}', 'SaveAll': '{SaveAll}', 'use_skin_check': '{use_skin_check}', 'AYS_WantNoSkin': '{AYS_WantNoSkin}', 'skin_path': '{skin_path}', 'SetSkin_BadNetSkin': '{SetSkin_BadNetSkin}', 'SetSkin_BadLocSkin': '{SetSkin_BadLocSkin}', 'SetSkin_AjaxError': '{SetSkin_AjaxError}', 'IgnoreUserNow': '{IgnoreUserNow}', 'DeleteFromIgnoreList': '{DeleteFromIgnoreList}', 'atHour': '{atHour}'};
Expand Down

0 comments on commit 44ec67b

Please sign in to comment.