admin controller

This commit is contained in:
Karol Bielski 2025-06-16 12:31:49 +02:00
parent 9e5b874fe0
commit ec08e7a74a
3 changed files with 98 additions and 0 deletions

View File

@ -0,0 +1,56 @@
<?php
namespace Blog\Controller;
use Blog\Model\AdminModel;
class AdminController
{
protected $view;
private $db;
private $validData = array();
private $errors = array();
private $labels = array("name" => "Name*", "preis" => "Preis*", "dauer" => "Dauer*", "rabatt" => "Rabatt", "kategorie" => "Kategorie", "beschreibung" => "Beschreibung");
public function __construct($view)
{
$this->db = new AdminModel();
$this->view = $view;
}
public function showForm()
{
$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 (!isset($_POST[$index]) || empty($_POST[$index])) {
$this->errors[$index] = "Bitte " . $value . " angeben";
} else {
$this->validData[$index] = $_POST[$index];
}
}
if (count($this->errors) > 0) {
$this->view->setDoMethodName("showContactForm");
$this->showContactForm();
} else {
if ($this->db->writeContactData($this->validData)) {
$this->view->setDoMethodName("showConfirmation");
$this->showConfirmation();
}
}
}
}
?>

12
Model/AdminModel.php Normal file
View File

@ -0,0 +1,12 @@
<?php
namespace Blog\Model;
use PDOException;
class AdminModel extends Database
{
public function writeContactData($values)
{
}
}

View File

@ -0,0 +1,30 @@
<?php
include dirname(__DIR__).'/header.phtml';
?>
<h2>Neuer Kurs</h2>
<form method="post">
<?php foreach ($labels as $index => $value) {
echo '<label for="' . $index . '">' . $value . '</label>';
if ($index == "beschreibung") {
echo "<textarea id=\"$index\" name=\"$index\" >";
if (isset($validData[$index])) { echo $validData[$index]; }
echo "</textarea><br>";
} else {
echo '<input type="text" name="' . $index . '" value="' . (isset($validData[$index]) ? $validData[$index] : '') . '"><br>';
}
if (isset($errors[$index])) {
echo '<label class="errorMsg">' . $errors[$index] . '</label><br>';
}
}
?>
<input type="hidden" name="controller" value="contact">
<input type="hidden" name="do" value="validateForm">
<input type="submit" name="submit" value="Absenden"></form>
<?php include dirname(__DIR__).'/footer.phtml'; ?>