From 33279cea034d436dfcf2066435c8c8e824f306ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Dzieko=C5=84ski?= Date: Thu, 30 Jun 2022 23:54:11 +0200 Subject: [PATCH] GH-224 Move user settings updater to an util --- modules/settings/_includes.php | 1 + .../queries/updateUserSettings.query.php | 46 +++++++++++++++++++ settings.php | 44 ++++++------------ 3 files changed, 62 insertions(+), 29 deletions(-) create mode 100644 modules/settings/utils/queries/updateUserSettings.query.php diff --git a/modules/settings/_includes.php b/modules/settings/_includes.php index cc50c91f..9adfa0e3 100644 --- a/modules/settings/_includes.php +++ b/modules/settings/_includes.php @@ -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'); diff --git a/modules/settings/utils/queries/updateUserSettings.query.php b/modules/settings/utils/queries/updateUserSettings.query.php new file mode 100644 index 00000000..512441a7 --- /dev/null +++ b/modules/settings/utils/queries/updateUserSettings.query.php @@ -0,0 +1,46 @@ + $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'); +} + +?> diff --git a/settings.php b/settings.php index 1e0e71e4..ac6210f3 100644 --- a/settings.php +++ b/settings.php @@ -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']; } }