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 @@
Kurs erfolgreich bearbeitet.
+Weiter +