weiter mit change user data

This commit is contained in:
Illia Hromovoi 2025-07-09 11:12:30 +02:00
parent 7ce389d179
commit 3d0fcc42c4
2 changed files with 34 additions and 21 deletions

View File

@ -26,8 +26,8 @@ class UserController{
]; ];
private $changeUserLabels = [ private $changeUserLabels = [
'name' => 'Vorname*', 'vorname' => 'Vorname*',
'lastname' => 'Nachname*', 'name' => 'Nachname*',
'email' => 'E-Mail*', 'email' => 'E-Mail*',
'password' => 'Passwort*', 'password' => 'Passwort*',
]; ];
@ -210,19 +210,19 @@ class UserController{
} }
$validData = [ $validData = [
'name' => $currentUser["name"],
'vorname' => $currentUser["vorname"], 'vorname' => $currentUser["vorname"],
'name' => $currentUser["name"],
'email' => $currentUser["email"], 'email' => $currentUser["email"],
]; ];
$this->view->setVars([ $this->view->setVars([
'labels' => $this->changeUserLabels, 'changeUserLabels' => $this->changeUserLabels,
'validData' => $validData, 'validData' => $validData,
'errors' => $this->errors, 'errors' => $this->errors,
'message' => $this->message ?? null,
]); ]);
$this->view->render('User/showUserChangeAccountSettings'); //$this->view->render('User/showUserChangeAccountSettings');
return;
} }
public function updateAccountData() public function updateAccountData()
@ -259,21 +259,29 @@ class UserController{
$this->errors['password'] = 'Passwort muss mindestens 6 Zeichen haben.'; $this->errors['password'] = 'Passwort muss mindestens 6 Zeichen haben.';
} }
if (count($this->errors) > 0) { if (count($this->errors) > 0) {
$this->view->setVars([ $this->view->setVars([
'labels' => $this->changeUserLabels, 'labels' => $this->changeUserLabels,
'validData' => $submitted, 'validData' => $submitted,
'errors' => $this->errors, 'errors' => $this->errors,
]); ]);
return $this->showUserChangeAccountSettings(); return;
$this->view->render('User/showUserChangeAccountSettings');
} }
$updateData = []; $updateData = [];
foreach (['name','lastname','email'] as $field) { if ($submitted['name'] !== $currentUser['firstname']) {
if ($submitted[$field] !== $currentUser[$field]) { $updateData['firstname'] = $submitted['name']; // PHP name = DB firstname
$updateData[$field] = $submitted[$field];
} }
if ($submitted['lastname'] !== $currentUser['name']) {
$updateData['name'] = $submitted['lastname']; // PHP lastname = DB name
} }
if ($submitted['email'] !== $currentUser['email']) {
$updateData['email'] = $submitted['email'];
}
if ($submitted['password'] !== '') { if ($submitted['password'] !== '') {
$salt = bin2hex(random_bytes(16)); $salt = bin2hex(random_bytes(16));
$hash = hash('sha256', $submitted['password'] . $salt); $hash = hash('sha256', $submitted['password'] . $salt);
@ -283,22 +291,27 @@ class UserController{
if (empty($updateData)) { if (empty($updateData)) {
$this->message = 'Keine Änderungen festgestellt.'; $this->message = 'Keine Änderungen festgestellt.';
return $this->showUserChangeAccountSettings(); $this->view->render('User/showUserChangeAccountSettings');
return;
} }
$ok = $this->db->updateUserData($userId, $updateData); $ok = $this->db->updateUserData($userId, $updateData);
if ($ok) { if ($ok) {
// SessionWerte aktualisieren
$_SESSION['vorname'] = $updateData['name'] ?? $_SESSION['vorname']; $_SESSION['vorname'] = $updateData['name'] ?? $_SESSION['vorname'];
$_SESSION['name'] = $updateData['lastname'] ?? $_SESSION['name']; $_SESSION['name'] = $updateData['lastname'] ?? $_SESSION['name'];
$_SESSION['email'] = $updateData['email'] ?? $_SESSION['email']; $_SESSION['email'] = $updateData['email'] ?? $_SESSION['email'];
$this->message = 'Änderungen erfolgreich gespeichert.'; $this->message = 'Änderungen erfolgreich gespeichert.';
header("Location: index.php?controller=user&do=showUserAccountPage");
exit();
} else { } else {
$this->errors['general'] = 'Beim Speichern ist ein Fehler aufgetreten.'; $this->errors['general'] = 'Beim Speichern ist ein Fehler aufgetreten.';
return;
} }
return $this->showUserChangeAccountSettings(); $this->view->render('User/showUserAccountPage');
return;
} }

View File

@ -14,9 +14,9 @@ include dirname(__DIR__).'/header.phtml';
<?php if ($key === 'password'): ?> <?php if ($key === 'password'): ?>
<input type="password" name="<?= $key ?>" id="reg_<?= $key ?>"> <input type="password" name="<?= $key ?>" id="reg_<?= $key ?>">
<?php elseif($key === 'email'): ?> <?php elseif($key === 'email'): ?>
<input type="email" name="<?= $key ?>" id="reg_<?= $key ?>"> <input type="email" name="<?= $key ?>" id="reg_<?= $key ?>" value="<?= htmlspecialchars($validData[$key] ?? '') ?>">
<?php else: ?> <?php else: ?>
<input type="text" name="<?= $key ?>" id="reg_<?= $key ?>" value="<?= htmlspecialchars($validData[$key] ?? '') ?>"> <input type="text" name="<?= $key ?>" id="reg_<?= $key ?>" value="<?= htmlspecialchars($validData[$key] ?? $_SESSION['vorname']) ?>">
<?php endif; ?> <?php endif; ?>
<?php if (!empty($errors[$key])): ?> <?php if (!empty($errors[$key])): ?>