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