kurs bearbeiten
This commit is contained in:
parent
6fd7b8d918
commit
af6f6238ae
@ -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(){
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -1,6 +1,21 @@
|
||||
<h2>Neuer Kurs</h2>
|
||||
<form method="post">
|
||||
<?php
|
||||
$userModel = new \Blog\Model\UserModel();
|
||||
$courses = $userModel->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 '</div>';
|
||||
?>
|
||||
<input type="hidden" name="controller" value="user">
|
||||
<input type="hidden" name="do" value="validateKursForm">
|
||||
<div class="form-grid-3" style="margin-top: 16px;">
|
||||
<input style="grid-column: 3;" type="submit" name="submit" value="Absenden">
|
||||
</div>
|
||||
<?php
|
||||
$action = $isEditing ? 'validateEditKursForm' : 'validateKursForm';
|
||||
echo <<<HTML
|
||||
<input type="hidden" name="do" value="{$action}">
|
||||
<div class="form-grid-3" style="margin-top: 16px;">
|
||||
<input style="grid-column: 3;" type="submit" name="submit" value="{$saveLabel}">
|
||||
</div>
|
||||
HTML;
|
||||
?>
|
||||
</form>
|
||||
|
||||
<?php
|
||||
$userModel = new \Blog\Model\UserModel();
|
||||
$courses = $userModel->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 '</div>';
|
||||
|
12
Views/User/showKursEditedConfirmation.phtml
Normal file
12
Views/User/showKursEditedConfirmation.phtml
Normal file
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
include dirname(__DIR__).'/header.phtml';
|
||||
?>
|
||||
|
||||
<div class="msg">
|
||||
<p>Kurs erfolgreich bearbeitet.</p>
|
||||
<a href="?controller=User&do=showUserAccountPage">Weiter</a>
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
<?php include dirname(__DIR__).'/footer.phtml'; ?>
|
@ -4,7 +4,7 @@ include dirname(__DIR__).'/header.phtml';
|
||||
|
||||
<div class="msg">
|
||||
<p>Kurs erfolgreich erstellt.</p>
|
||||
<a href="?controller=Welcome&do=showWelcome">Weiter</a>
|
||||
<a href="?controller=User&do=showUserAccountPage">Weiter</a>
|
||||
</div>
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user