From af6f6238ae48ebed5befe00ed8ffe37d7fc6ef95 Mon Sep 17 00:00:00 2001 From: pbbfa23abi Date: Thu, 10 Jul 2025 10:57:30 +0200 Subject: [PATCH] kurs bearbeiten --- Controller/UserController.php | 20 ++++++ Model/UserModel.php | 72 ++++++++++++++++++++- Views/User/showAdminForm.phtml | 37 +++++++++-- Views/User/showKursEditedConfirmation.phtml | 12 ++++ Views/User/showNewKursConfirmation.phtml | 2 +- 5 files changed, 134 insertions(+), 9 deletions(-) create mode 100644 Views/User/showKursEditedConfirmation.phtml diff --git a/Controller/UserController.php b/Controller/UserController.php index 5837577..aff9823 100644 --- a/Controller/UserController.php +++ b/Controller/UserController.php @@ -134,6 +134,26 @@ class UserController{ } } + public function validateEditKursForm(){ + 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("showKursEditedConfirmation"); + $this->showConfirmation(); + } + } + } + public function showConfirmation(){} public function showUserLoginConfirmation(){ diff --git a/Model/UserModel.php b/Model/UserModel.php index 4dd93a7..98c01c9 100644 --- a/Model/UserModel.php +++ b/Model/UserModel.php @@ -157,7 +157,7 @@ class UserModel extends Database } 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 + $sql = "SELECT k.id, k.name, k.preis, k.dauer, k.rabatt, k.kategorie, k.beschreibung, k.ort_id, 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 @@ -174,4 +174,74 @@ class UserModel extends Database die; } } + + public function updateCourse($course) { + $pdo = $this->linkDB(); + + try { + if (isset($course['ort_id'])) { + $this->updateAddress($course); + $addressId = $course['ort_id']; + } else { + $addressId = $this->writeNewAddress($course); + } + + $sql = "UPDATE kurs SET + `name` = :name, + `preis` = :preis, + `dauer` = :dauer, + `rabatt` = :rabatt, + `kategorie` = :kategorie, + `beschreibung` = :beschreibung, + `ort_id` = :ort_id + WHERE `id` = :id"; + + $sth = $pdo->prepare($sql); + $sth->execute([ + ':id' => $course['id'], + ':name' => $course['name'], + ':preis' => $course['preis'], + ':dauer' => $course['dauer'], + ':rabatt' => $course['rabatt'], + ':kategorie' => $course['kategorie'], + ':beschreibung' => $course['beschreibung'], + ':ort_id' => $addressId + ]); + + } catch (PDOException $e) { + new \Blog\Library\ErrorMsg("Fehler beim Aktualisieren des Kurses.", $e); + die; + } + + return true; + } + + public function updateAddress($data) { + $pdo = $this->linkDB(); + + if (!isset($data['ort_id'])) { + throw new \Exception("Keine Adress-ID vorhanden zum Aktualisieren."); + } + + $sql = "UPDATE ort SET + `strasse` = :strasse, + `stadt` = :stadt, + `plz` = :plz + WHERE `id` = :id"; + + try { + $sth = $pdo->prepare($sql); + $sth->execute([ + ':id' => $data['ort_id'], + ':strasse' => $data['strasse'], + ':stadt' => $data['stadt'], + ':plz' => $data['plz'] + ]); + } catch (PDOException $e) { + new \Blog\Library\ErrorMsg("Fehler beim Aktualisieren der Adresse.", $e); + die; + } + + return true; + } } \ No newline at end of file diff --git a/Views/User/showAdminForm.phtml b/Views/User/showAdminForm.phtml index 4929a03..0e5cff4 100644 --- a/Views/User/showAdminForm.phtml +++ b/Views/User/showAdminForm.phtml @@ -1,6 +1,21 @@

Neuer Kurs

getMyCourses(); +$id = $_GET["id"] ?? null; + +$selectedCourse = null; +foreach ($courses as $course) { + if ($course['id'] === $id) { + $selectedCourse = $course; + break; + } +} +$validData = $selectedCourse ?? null; +$isEditing = $validData != null; +$saveLabel = $isEditing ? "Speichern" : "Erstellen"; + function createInputField($label, $name, $errors, $validData, $type = 'input') { $errorClass = isset($errors[$name]) ? 'error' : ''; $value = htmlspecialchars($validData[$name] ?? ''); @@ -36,15 +51,18 @@ function createInputField($label, $name, $errors, $validData, $type = 'input') { echo ''; ?> - -
- -
+ +
+ +
+ HTML; + ?>
getMyCourses(); $doc = new DOMDocument('1.0', 'UTF-8'); if (!empty($courses)) { @@ -52,7 +70,7 @@ if (!empty($courses)) { foreach ($courses as $kurs) { $courseCard = $doc->createElement('div'); $courseCard->setAttribute('class', 'course-card'); - + $courseImage = $doc->createElement('div'); $courseImage->setAttribute('class', 'course-image'); $courseCard->appendChild($courseImage); @@ -84,6 +102,11 @@ if (!empty($courses)) { $category = $doc->createElement('div', htmlspecialchars($kurs['kategorie'] ?? 'Keine Kategorie')); $courseRight->appendChild($category); + $editLink = $doc->createElement('a', "Bearbeiten"); + $editLink->setAttribute('href', '?controller=User&do=showUserAccountPage&id=' . $kurs['id']); + $editLink->setAttribute('class', 'course-card-link'); + $courseRight->appendChild($editLink); + echo $doc->saveHTML($courseCard); } echo ''; diff --git a/Views/User/showKursEditedConfirmation.phtml b/Views/User/showKursEditedConfirmation.phtml new file mode 100644 index 0000000..effcbd2 --- /dev/null +++ b/Views/User/showKursEditedConfirmation.phtml @@ -0,0 +1,12 @@ + + +
+

Kurs erfolgreich bearbeitet.

+Weiter +
+ + + + \ No newline at end of file diff --git a/Views/User/showNewKursConfirmation.phtml b/Views/User/showNewKursConfirmation.phtml index 35de568..7ee139a 100644 --- a/Views/User/showNewKursConfirmation.phtml +++ b/Views/User/showNewKursConfirmation.phtml @@ -4,7 +4,7 @@ include dirname(__DIR__).'/header.phtml';

Kurs erfolgreich erstellt.

-Weiter +Weiter