From 43c21c010e8d3e6e220baef647e91d98c8304648 Mon Sep 17 00:00:00 2001 From: Tim Krehan Date: Tue, 21 May 2019 10:28:07 +0200 Subject: [PATCH] implemented full settings functionality --- bin/settings.js | 72 ++++++++++++++++++++++++++++++++++++++++++-- cont/settings.php | 6 ++-- php/classes.user.php | 23 ++++++++++++++ php/edit-user.php | 9 +++++- 4 files changed, 103 insertions(+), 7 deletions(-) diff --git a/bin/settings.js b/bin/settings.js index 8a8fa12..35a0ac5 100644 --- a/bin/settings.js +++ b/bin/settings.js @@ -1,4 +1,6 @@ $(document).ready(function () { + $(".user-input").on("input", checkUserStrings); + $("#userSaveButton").click(userChange); $(".password-input").on("input", checkPasswordStrings); $("#passwordSaveButton").click(savePassword); $("#export-recipe-button").click(exportRecipe); @@ -46,18 +48,46 @@ $(document).ready(function () { }); }); -function themeChange(){ +function userChange() { + var mail = $('.user-input.is-valid[aria-label="email"]').val(); + var userName = $('.user-input.is-valid[aria-label="Username"]').val(); + if(mail != null){ + $.post( + "/api/user/change-mail", + { + mail: mail + }, + function(){ + window.location.href = window.location.href; + } + ) + } + + if (userName != null) { + $.post( + "/api/user/change-username", + { + username: userName + }, + function () { + window.location.href = window.location.href; + } + ) + } +} + +function themeChange() { $("#themeSaveButton").removeClass("disabled"); $("#themeSaveButton").attr("disabled", false); } function changeTheme() { $.post( - "/api/user/change-theme", + "/api/user/change-theme", { theme: $('#changeThemePrepend option:selected').val() }, - function(){ + function () { window.location.href = window.location.href; } ); @@ -113,6 +143,42 @@ function checkPasswordStrings() { } } +function checkUserStrings() { + switch ($(this).attr("aria-label")) { + case "Username": + if ($(this).val().length >= 3) { + $("#userSaveButton").attr("disabled", false); + $("#userSaveButton").removeClass("disabled"); + $(this).removeClass("is-invalid"); + $(this).addClass("is-valid"); + } + else { + $("#userSaveButton").attr("disabled", true); + $("#userSaveButton").addClass("disabled"); + $(this).addClass("is-invalid"); + $(this).removeClass("is-valid"); + } + break; + case "email": + mailRegEx = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; + if (($(this).val().length > 5) && (mailRegEx.exec($(this).val()) !== null)) { + $("#userSaveButton").attr("disabled", false); + $("#userSaveButton").removeClass("disabled"); + $(this).removeClass("is-invalid"); + $(this).addClass("is-valid"); + } + else { + $("#userSaveButton").attr("disabled", true); + $("#userSaveButton").addClass("disabled"); + $(this).addClass("is-invalid"); + $(this).removeClass("is-valid"); + } + break; + default: + break; + } +} + function downloadObjectAsJson(exportObj, exportName) { var dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(exportObj)); var downloadAnchorNode = document.createElement('a'); diff --git a/cont/settings.php b/cont/settings.php index 1522a11..d934441 100644 --- a/cont/settings.php +++ b/cont/settings.php @@ -12,14 +12,14 @@
Benutzername
- +
Mail
- +
@@ -28,7 +28,7 @@
- + diff --git a/php/classes.user.php b/php/classes.user.php index da0da37..b1ab751 100644 --- a/php/classes.user.php +++ b/php/classes.user.php @@ -34,6 +34,29 @@ } } + function change_mail($mailaddress){ + include $_SESSION["docroot"].'/php/hash.php'; + include $_SESSION["docroot"].'/php/connect.php'; + $this->mail = $mailaddress; + $result = $mysqli->query("UPDATE `users` SET `email` = '$mailaddress' WHERE `users`.`uid` = $this->uid;"); + $mysqli->close(); + } + + function change_username($newname){ + include $_SESSION["docroot"].'/php/hash.php'; + include $_SESSION["docroot"].'/php/connect.php'; + $this->username = $newname; + $result = $mysqli->query("SELECT * WHERE `username` = $this->username;"); + if($result->num_rows==0){ + $result = $mysqli->query("UPDATE `users` SET `username` = '$newname' WHERE `users`.`uid` = $this->uid;"); + print_r("0"); + } + else{ + print_r("1"); + } + $mysqli->close(); + } + function change_theme($theme){ include $_SESSION["docroot"].'/php/connect.php'; $result = $mysqli->query("UPDATE `users` SET `theme` = '$theme' WHERE `users`.`uid` = $this->uid;"); diff --git a/php/edit-user.php b/php/edit-user.php index 3c40033..3c4e04d 100644 --- a/php/edit-user.php +++ b/php/edit-user.php @@ -13,7 +13,14 @@ case 'change-theme': $user->change_theme($_POST["theme"]); break; - + + case 'change-mail': + $user->change_mail($_POST["mail"]); + break; + + case 'change-username': + $user->change_username($_POST["username"]); + break; case 'new': $user->new($_POST["username"], $_POST["passwd"]);