From 449dd10302a27a690b127a60b54de9793b53c22f Mon Sep 17 00:00:00 2001 From: pbbfa23abi Date: Wed, 9 Jul 2025 11:11:30 +0200 Subject: [PATCH] AdminForm in UserAccountPage gemergt --- Controller/AdminController.php | 66 --------------- Controller/UserController.php | 43 +++++++++- Model/AdminModel.php | 81 ------------------- Model/UserModel.php | 72 +++++++++++++++++ Views/{Admin => User}/showAdminForm.phtml | 18 ++--- .../showNewKursConfirmation.phtml} | 0 Views/User/showUserAccountPage.phtml | 10 ++- Views/header.phtml | 9 --- 8 files changed, 129 insertions(+), 170 deletions(-) delete mode 100644 Controller/AdminController.php delete mode 100644 Model/AdminModel.php rename Views/{Admin => User}/showAdminForm.phtml (90%) rename Views/{Admin/showConfirmation.phtml => User/showNewKursConfirmation.phtml} (100%) diff --git a/Controller/AdminController.php b/Controller/AdminController.php deleted file mode 100644 index aeb17f4..0000000 --- a/Controller/AdminController.php +++ /dev/null @@ -1,66 +0,0 @@ - "Name*", - "preis" => "€ Preis*", - "dauer" => "Dauer* (Stunden)", - "rabatt" => "Rabatt", - "kategorie" => "Kategorie", - "|" => "", - "stadt" => "Stadt*", - "strasse" => "Straße und Nummer*", - "plz" => "PLZ*", - "|" => "", - "beschreibung" => "Beschreibung"); - - - public function __construct($view) - { - $this->db = new AdminModel(); - $this->view = $view; - } - - public function showAdminForm() - { - $this->view->setVars([ - 'labels' => $this->labels, - 'validData' => $this->validData, - 'errors' => $this->errors - ]); - } - - public function showConfirmation() - { - - } - - public function validateForm(){ - foreach ($this->labels as $index => $value) { - if (strpos($value, "*") !== false && (!isset($_POST[$index]) || empty($_POST[$index]))) { - $this->errors[$index] = "Bitte " . $value . " eingeben"; - } else { - $this->validData[$index] = $_POST[$index] === '' ? null : $_POST[$index]; - } - } - if (count($this->errors) > 0) { - $this->view->setDoMethodName("showAdminForm"); - $this->showAdminForm(); - } else { - if ($this->db->writeNewCourse($this->validData, $_SESSION["user_id"])) { - $this->view->setDoMethodName("showConfirmation"); - $this->showConfirmation(); - } - } - } -} -?> \ No newline at end of file diff --git a/Controller/UserController.php b/Controller/UserController.php index 92aaadd..b0d5004 100644 --- a/Controller/UserController.php +++ b/Controller/UserController.php @@ -32,6 +32,21 @@ class UserController{ 'password' => 'Passwort*', ]; + private $kursValidData = array(); + private $kursErrors = array(); + private $kursLabels = array( + "name" => "Name*", + "preis" => "€ Preis*", + "dauer" => "Dauer* (Stunden)", + "rabatt" => "Rabatt", + "kategorie" => "Kategorie", + "1" => "|", + "stadt" => "Stadt*", + "strasse" => "Straße und Nummer*", + "plz" => "PLZ*", + "2" => "|", + "beschreibung" => "Beschreibung"); + public function __construct($view){ $this->db = new UserModel(); $this->view = $view; @@ -99,6 +114,28 @@ class UserController{ } } + public function validateKursForm(){ + foreach ($this->kursLabels as $index => $value) { + if($value === "|") continue; + if (strpos($value, "*") !== false && (!isset($_POST[$index]) || empty($_POST[$index]))) { + $this->kursErrors[$index] = "Bitte " . $value . " eingeben"; + } else { + $this->kursValidData[$index] = $_POST[$index] === '' ? null : $_POST[$index]; + } + } + if (count($this->errors) > 0) { + $this->view->setDoMethodName("showUserAccountPage"); + $this->showUserAccountPage(); + } else { + if ($this->db->writeNewCourse($this->kursValidData, $_SESSION["user_id"])) { + $this->view->setDoMethodName("showNewKursConfirmation"); + $this->showConfirmation(); + } + } + } + + public function showConfirmation(){} + public function showUserLoginConfirmation(){ $userId = $this->getCurrentUserId(); $user = null; @@ -172,7 +209,11 @@ class UserController{ } public function showUserAccountPage (){ - + $this->view->setVars([ + 'labels' => $this->kursLabels, + 'errors' => $this->kursErrors, + 'validData' => $this->kursValidData + ]); } public function showUserDeleteConfirmation(){ diff --git a/Model/AdminModel.php b/Model/AdminModel.php deleted file mode 100644 index 98b771d..0000000 --- a/Model/AdminModel.php +++ /dev/null @@ -1,81 +0,0 @@ -createUUID(); - - $sql = "INSERT INTO ort (`id`, `stadt`, `strasse`, `plz`) VALUES ( - :guid, :stadt, :strasse, :plz);"; - - $pdo = $this->linkDB(); - - try { - $sth = $pdo->prepare($sql); - $sth->execute(array(":guid" => $guid, - ":stadt" => $values["stadt"], - ":strasse" => $values["strasse"], - ":plz" => $values["plz"], - )); - return $guid; - } catch (PDOException $e) { - new \Blog\Library\ErrorMsg("Fehler beim Schreiben der Daten.", $e); - die; - } - } - - public function writeNewCourse($values, $kursleiterId) - { - // Bleibt übrig wenn Adresse erstellt wird aber Kurs nicht weil Error - $addressId = $this->writeNewAddress($values); - - $guid = $this->createUUID(); - - $sql = "INSERT INTO kurs (`id`, `name`, `preis`, `dauer`, `rabatt`, `kategorie`, `beschreibung`, `kurseleiter`, `ort_id`) VALUES ( - :guid, :name, :preis, :dauer, :rabatt, :kategorie, :beschreibung, :kurseleiter, :ort_id);"; - - $pdo = $this->linkDB(); - - try { - $sth = $pdo->prepare($sql); - $sth->execute(array(":guid" => $guid, - ":name" => $values["name"], - ":preis" => $values["preis"], - ":dauer" => $values["dauer"], - ":rabatt" => $values["rabatt"], - ":kategorie" => $values["kategorie"], - ":beschreibung" => $values["beschreibung"], - ":kurseleiter" => $kursleiterId, - "ort_id" => $addressId - )); - } catch (PDOException $e) { - new \Blog\Library\ErrorMsg("Fehler beim Schreiben der Daten.", $e); - die; - } - - return true; - } - - public function getMyCourses() { - $sql = "SELECT k.id, k.name, k.preis, k.dauer, k.rabatt, k.kategorie, k.beschreibung, o.stadt, o.strasse, o.plz, b.note, b.kommentar - FROM kurs AS k - JOIN ort AS o ON o.id = k.ort_id - LEFT JOIN bewertungen AS b ON b.kurs_id = k.id - ORDER BY k.name;"; - - $pdo = $this->linkDB(); - - try { - $sth = $pdo->prepare($sql); - $sth->execute(); - return $sth->fetchAll(\PDO::FETCH_ASSOC); - } catch (PDOException $e) { - new \Blog\Library\ErrorMsg("Fehler beim Lesen der Daten.", $e); - die; - } - } -} \ No newline at end of file diff --git a/Model/UserModel.php b/Model/UserModel.php index 8e940ad..4dd93a7 100644 --- a/Model/UserModel.php +++ b/Model/UserModel.php @@ -102,4 +102,76 @@ class UserModel extends Database } + private function writeNewAddress($values) { + $guid = $this->createUUID(); + + $sql = "INSERT INTO ort (`id`, `stadt`, `strasse`, `plz`) VALUES ( + :guid, :stadt, :strasse, :plz);"; + + $pdo = $this->linkDB(); + + try { + $sth = $pdo->prepare($sql); + $sth->execute(array(":guid" => $guid, + ":stadt" => $values["stadt"], + ":strasse" => $values["strasse"], + ":plz" => $values["plz"], + )); + return $guid; + } catch (PDOException $e) { + new \Blog\Library\ErrorMsg("Fehler beim Schreiben der Daten.", $e); + die; + } + } + + public function writeNewCourse($values, $kursleiterId) + { + // Bleibt übrig wenn Adresse erstellt wird aber Kurs nicht weil Error + $addressId = $this->writeNewAddress($values); + + $guid = $this->createUUID(); + + $sql = "INSERT INTO kurs (`id`, `name`, `preis`, `dauer`, `rabatt`, `kategorie`, `beschreibung`, `kurseleiter`, `ort_id`) VALUES ( + :guid, :name, :preis, :dauer, :rabatt, :kategorie, :beschreibung, :kurseleiter, :ort_id);"; + + $pdo = $this->linkDB(); + + try { + $sth = $pdo->prepare($sql); + $sth->execute(array(":guid" => $guid, + ":name" => $values["name"], + ":preis" => $values["preis"], + ":dauer" => $values["dauer"], + ":rabatt" => $values["rabatt"], + ":kategorie" => $values["kategorie"], + ":beschreibung" => $values["beschreibung"], + ":kurseleiter" => $kursleiterId, + "ort_id" => $addressId + )); + } catch (PDOException $e) { + new \Blog\Library\ErrorMsg("Fehler beim Schreiben der Daten.", $e); + die; + } + + return true; + } + + public function getMyCourses() { + $sql = "SELECT k.id, k.name, k.preis, k.dauer, k.rabatt, k.kategorie, k.beschreibung, o.stadt, o.strasse, o.plz, b.note, b.kommentar + FROM kurs AS k + JOIN ort AS o ON o.id = k.ort_id + LEFT JOIN bewertungen AS b ON b.kurs_id = k.id + ORDER BY k.name;"; + + $pdo = $this->linkDB(); + + try { + $sth = $pdo->prepare($sql); + $sth->execute(); + return $sth->fetchAll(\PDO::FETCH_ASSOC); + } catch (PDOException $e) { + new \Blog\Library\ErrorMsg("Fehler beim Lesen der Daten.", $e); + die; + } + } } \ No newline at end of file diff --git a/Views/Admin/showAdminForm.phtml b/Views/User/showAdminForm.phtml similarity index 90% rename from Views/Admin/showAdminForm.phtml rename to Views/User/showAdminForm.phtml index 138f254..4929a03 100644 --- a/Views/Admin/showAdminForm.phtml +++ b/Views/User/showAdminForm.phtml @@ -1,7 +1,3 @@ - -

Neuer Kurs

'; foreach ($labels as $name => $label) { - if($name === "|") { + if($label === "|") { echo ''; echo '
'; } else { echo createInputField($label, $name, $errors, $validData, $type = $name === 'beschreibung' ? 'textarea' : 'input'); } - } + } echo '
'; ?> - - + +
getMyCourses(); +$userModel = new \Blog\Model\UserModel(); +$courses = $userModel->getMyCourses(); $doc = new DOMDocument('1.0', 'UTF-8'); if (!empty($courses)) { @@ -95,5 +91,3 @@ if (!empty($courses)) { echo '

Keine Kurse gefunden.

'; } ?> - - \ No newline at end of file diff --git a/Views/Admin/showConfirmation.phtml b/Views/User/showNewKursConfirmation.phtml similarity index 100% rename from Views/Admin/showConfirmation.phtml rename to Views/User/showNewKursConfirmation.phtml diff --git a/Views/User/showUserAccountPage.phtml b/Views/User/showUserAccountPage.phtml index 41bd45b..cacb05b 100644 --- a/Views/User/showUserAccountPage.phtml +++ b/Views/User/showUserAccountPage.phtml @@ -30,5 +30,13 @@ + - \ No newline at end of file + + + + \ No newline at end of file diff --git a/Views/header.phtml b/Views/header.phtml index 0b8a8aa..4ef8ad2 100644 --- a/Views/header.phtml +++ b/Views/header.phtml @@ -20,21 +20,12 @@