Skip to content

Commit

Permalink
GH-224 Move user settings updater to an util
Browse files Browse the repository at this point in the history
  • Loading branch information
mdziekon committed Jun 30, 2022
1 parent cf2412e commit 33279ce
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 29 deletions.
1 change: 1 addition & 0 deletions modules/settings/_includes.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
include($includePath . './utils/queries/updateUserOnUsernameChange.query.php');
include($includePath . './utils/queries/updateUserOnVacationFinish.query.php');
include($includePath . './utils/queries/updateUserPlanetsOnVacationFinish.query.php');
include($includePath . './utils/queries/updateUserSettings.query.php');

include($includePath . './utils/validators/validatePasswordChange.validator.php');
include($includePath . './utils/validators/validateEmailChange.validator.php');
Expand Down
46 changes: 46 additions & 0 deletions modules/settings/utils/queries/updateUserSettings.query.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace UniEngine\Engine\Modules\Settings\Utils\Queries;

/**
* @param array $params
* @param arrayRef $params['user']
* @param array $params['changedUserParams']
* @param array $params['changedUserParamsTypes']
*/
function updateUserSettings($params) {
$user = &$params['user'];
$userId = $user['id'];
$changedUserParams = $params['changedUserParams'];
$changedUserParamsTypes = $params['changedUserParamsTypes'];

$updateFields = [];

foreach ($changedUserParams as $paramName => $newParamValue) {
$user[$paramName] = $newParamValue;

if (
isset($changedUserParamsTypes[$paramName]) &&
$changedUserParamsTypes[$paramName] == 's'
) {
$newParamValue = "'{$newParamValue}'";
}

$updateFields[] = "`{$paramName}` = {$newParamValue}";
}

$updateFieldsString = implode(', ', $updateFields);
$query = (
"UPDATE {{table}} " .
"SET " .
"{$updateFieldsString} " .
"WHERE " .
"`id` = {$userId} " .
"LIMIT 1 " .
"; -- UniEngine\Engine\Modules\Settings\Utils\Queries\updateUserSettings"
);

doquery($query, 'users');
}

?>
44 changes: 15 additions & 29 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -830,42 +830,28 @@ function isInputKeyChecked($input, $key) {
}
}

if(!empty($ChangeSet))
{
$UpdateQuery = [];

foreach($ChangeSet as $Key => $Value)
{
$_User[$Key] = $Value;

if(isset($ChangeSetTypes[$Key]) && $ChangeSetTypes[$Key] == 's')
{
$Value = "'{$Value}'";
}

$UpdateQuery[] = "`{$Key}` = {$Value}";
}

doquery("UPDATE {{table}} SET ".implode(', ', $UpdateQuery)." WHERE `id` = {$_User['id']};", 'users');
if($ForceGoingOnVacationMsg === TRUE)
{
if (!empty($ChangeSet)) {
Settings\Utils\Queries\updateUserSettings([
'user' => &$_User,
'changedUserParams' => $ChangeSet,
'changedUserParamsTypes' => $ChangeSetTypes,
]);

if ($ForceGoingOnVacationMsg === true) {
message((isset($ShowDeletionInfo) ? $_Lang['Vacation_GoingOnVacationsWithDeletion'] : $_Lang['Vacation_GoingOnVacations']), $_Lang['Vacations_Title'], 'settings.php', 3);
}

$ChangeSetCounted = count($ChangeSet) - $ChangeSetCount;
if($ChangeSetCounted > 0)
{
if ($ChangeSetCounted > 0) {
$InfoMsgs[] = sprintf($_Lang['Info_SaveWellDone'], $ChangeSetCounted);
}
else
{
} else {
$NoticeMsgs[] = $_Lang['Info_NoChanges'];
}
}
else
{
if(!isset($DontShow_NoChanges) || $DontShow_NoChanges !== true)
{
} else {
if (
!isset($DontShow_NoChanges) ||
$DontShow_NoChanges !== true
) {
$NoticeMsgs[] = $_Lang['Info_NoChanges'];
}
}
Expand Down

0 comments on commit 33279ce

Please sign in to comment.