This commit is contained in:
2025-07-03 11:51:34 +02:00
12 changed files with 398 additions and 69 deletions

View File

@@ -10,13 +10,32 @@ class UserController{
private $validData = array();
private $errors = array();
private $labels = [
"name" => "Vorname*",
"lastname" => "Nachname*",
"email" => "E-Mail*",
"password" => "Passwort*",
"role" => "Rolle*"
];
private $validLoginData = array();
private $loginErrors = array();
private $loginLabels = [
"email" => "E-Mail*",
"password" => "Passwort*",
];
public function __construct($view){
$this->db = new UserModel();
$this->view = $view;
}
public function showUserRegisterForm(){
$this->view->setVars([
'labels' => $this->labels,
'errors' => $this->errors,
'validData' => $this->validData
]);
}
public function showUserRegisterConfirmation(){
@@ -24,23 +43,63 @@ class UserController{
}
public function showUserLoginForm(){
$this->view->setVars([
'labels' => $this->loginLabels,
'errors' => $this->loginErrors,
'validData' => $this->validLoginData
]);
}
private function validateForm() {
foreach ($this->labels as $key => $label) {
if (!isset($_POST[$key]) || trim($_POST[$key]) === '') {
$this->errors[$key] = "Bitte $label angeben";
} else {
$this->validData[$key] = trim($_POST[$key]);
}
}
if (isset($this->validData['password'])) {
if (strlen($this->validData['password']) < 6) {
$this->errors['password'] = "Das Passwort muss mindestens 6 Zeichen lang sein.";
}
}
if (isset($this->validData['email']) && !filter_var($this->validData['email'], FILTER_VALIDATE_EMAIL)) {
$this->errors['email'] = "Bitte eine gültige E-Mail-Adresse eingeben.";
}
}
public function validateLoginForm(){
foreach ($this->loginLabels as $key => $label) {
if (isset($this->validData['password'])) {
if (strlen($this->validData['password']) < 6) {
$this->errors['password'] = "Das Passwort muss mindestens 6 Zeichen lang sein.";
}
}
if (isset($this->validData['email']) && !filter_var($this->validData['email'], FILTER_VALIDATE_EMAIL)) {
$this->errors['email'] = "Bitte eine gültige E-Mail-Adresse eingeben.";
}
}
}
public function showUserLoginConfirmation(){
$userId = $this->getCurrentUserId();
$user = null;
var_dump($user . "user");
if($userId){
$user = $this->db->getUserById($userId);
}
include 'Views/User/showUserLoginConfirmation.phtml';
$path = "Views/User/showUserLoginConfirmation.phtml";
if(file_exists($path)){
include $path;
}
}
public function register(){
$this->db->createUser($_POST);
$this->view->setDoMethodName("showUserRegisterConfirmation");
$this->showUserRegisterConfirmation();
$this->login();
}
public function login(){
@@ -73,7 +132,8 @@ class UserController{
public function logout(){
$this->clearUserSession();
echo "Erfolgreich ausgeloggt";
header("Location: index.php?controller=user&do=showUserLoginForm");
exit();
}
public function isUserLoggenIn(){
@@ -83,4 +143,8 @@ class UserController{
public function getCurrentUserId(){
return $_SESSION["user_id"] ?? null;
}
public function showUserAccountPage (){
}
}