254 lines
8.5 KiB
PHP
254 lines
8.5 KiB
PHP
<?php
|
|
require_once "../src/adverts.php";
|
|
require_once "../src/categories.php";
|
|
|
|
// Variablen Initialisieren
|
|
$userName = '';
|
|
$email = '';
|
|
$telephone = '';
|
|
$advertName = '';
|
|
$description = '';
|
|
$selectedCategories = [];
|
|
$errors = [];
|
|
$success = false;
|
|
|
|
// Alle Kategorien für das Formular abrufen
|
|
$categories = Categories::getAllCategories();
|
|
|
|
// Process form submission
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
// Validate and sanitize input
|
|
$userName = trim($_POST['userName'] ?? '');
|
|
$email = trim($_POST['email'] ?? '');
|
|
$telephone = trim($_POST['telephone'] ?? '');
|
|
$advertName = trim($_POST['advertName'] ?? '');
|
|
$description = trim($_POST['description'] ?? '');
|
|
$selectedCategories = $_POST['categories'] ?? [];
|
|
|
|
// Formularübermittlung verarbeiten
|
|
if (empty($userName)) {
|
|
$errors['userName'] = 'Inserentname ist erforderlich';
|
|
}
|
|
|
|
// E-Mail validieren
|
|
if (empty($email)) {
|
|
$errors['email'] = 'E-Mail ist erforderlich';
|
|
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
$errors['email'] = 'Gültige E-Mail ist erforderlich';
|
|
}
|
|
|
|
// Telefonnummer validieren
|
|
if (empty($telephone)) {
|
|
$errors['telephone'] = 'Telefon ist erforderlich';
|
|
}
|
|
|
|
// Insertatsnamen validieren
|
|
if (empty($advertName)) {
|
|
$errors['advertName'] = 'Inserat-Name ist erforderlich';
|
|
}
|
|
|
|
// Beschreibung validieren
|
|
if (empty($description)) {
|
|
$errors['description'] = 'Beschreibung ist erforderlich';
|
|
} elseif (strlen($description) > 255) {
|
|
$errors['description'] = 'Beschreibung darf maximal 255 Zeichen lang sein';
|
|
}
|
|
|
|
// Kategorien validieren
|
|
if (empty($selectedCategories)) {
|
|
$errors['categories'] = 'Mindestens eine Kategorie ist erforderlich';
|
|
} elseif (count($selectedCategories) > 3) {
|
|
$errors['categories'] = 'Maximal 3 Kategorien erlaubt';
|
|
}
|
|
|
|
// Wenn keine Fehler auftreten, Anzeige erstellen
|
|
if (empty($errors)) {
|
|
$result = Adverts::createAdvert(
|
|
$advertName,
|
|
$description,
|
|
$selectedCategories,
|
|
$userName,
|
|
$email,
|
|
$telephone
|
|
);
|
|
|
|
if ($result) {
|
|
$success = true;
|
|
// Formularfelder nach erfolgreicher Übermittlung zurücksetzen
|
|
$userName = '';
|
|
$email = '';
|
|
$telephone = '';
|
|
$advertName = '';
|
|
$description = '';
|
|
$selectedCategories = [];
|
|
} else {
|
|
$errors['general'] = 'Fehler beim Erstellen des Inserats. Bitte versuchen Sie es erneut.';
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
|
|
<!DOCTYPE html>
|
|
<html lang="de">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Inserat erstellen - Blackboard</title>
|
|
<style>
|
|
body {
|
|
font-family: Arial, sans-serif;
|
|
max-width: 800px;
|
|
margin: 0 auto;
|
|
padding: 20px;
|
|
}
|
|
h1 {
|
|
color: #333;
|
|
}
|
|
h2 {
|
|
color: #4CAF50;
|
|
margin-top: 20px;
|
|
margin-bottom: 10px;
|
|
}
|
|
.form-group {
|
|
margin-bottom: 15px;
|
|
}
|
|
label {
|
|
display: block;
|
|
margin-bottom: 5px;
|
|
font-weight: bold;
|
|
}
|
|
input[type="text"],
|
|
input[type="email"],
|
|
input[type="tel"],
|
|
textarea,
|
|
select {
|
|
width: 100%;
|
|
padding: 8px;
|
|
border: 1px solid #ddd;
|
|
border-radius: 4px;
|
|
box-sizing: border-box;
|
|
}
|
|
textarea {
|
|
height: 100px;
|
|
resize: vertical;
|
|
}
|
|
select[multiple] {
|
|
height: 120px;
|
|
}
|
|
.error {
|
|
color: red;
|
|
font-size: 0.9em;
|
|
margin-top: 5px;
|
|
}
|
|
.success {
|
|
background-color: #dff0d8;
|
|
color: #3c763d;
|
|
padding: 10px;
|
|
border-radius: 4px;
|
|
margin-bottom: 20px;
|
|
}
|
|
.char-count {
|
|
font-size: 0.8em;
|
|
color: #666;
|
|
margin-top: 5px;
|
|
}
|
|
.button {
|
|
background-color: #4CAF50;
|
|
color: white;
|
|
padding: 10px 15px;
|
|
border: none;
|
|
border-radius: 4px;
|
|
cursor: pointer;
|
|
}
|
|
.button:hover {
|
|
background-color: #45a049;
|
|
}
|
|
.back-link {
|
|
margin-top: 20px;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<h1>Neues Inserat erstellen</h1>
|
|
|
|
<?php if ($success): ?>
|
|
<div class="success">
|
|
Inserat erfolgreich erstellt!
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<?php if (isset($errors['general'])): ?>
|
|
<div class="error"><?php echo htmlspecialchars($errors['general']); ?></div>
|
|
<?php endif; ?>
|
|
|
|
<form method="post" action="">
|
|
<h2>Inserenten-Informationen</h2>
|
|
<div style="border: 1px solid #ddd; padding: 15px; margin-bottom: 20px; border-radius: 5px;">
|
|
<div class="form-group">
|
|
<label for="userName">Name:</label>
|
|
<input type="text" id="userName" name="userName" value="<?php echo htmlspecialchars($userName); ?>">
|
|
<?php if (isset($errors['userName'])): ?>
|
|
<div class="error"><?php echo htmlspecialchars($errors['userName']); ?></div>
|
|
<?php endif; ?>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label for="email">E-Mail:</label>
|
|
<input type="email" id="email" name="email" value="<?php echo htmlspecialchars($email); ?>">
|
|
<?php if (isset($errors['email'])): ?>
|
|
<div class="error"><?php echo htmlspecialchars($errors['email']); ?></div>
|
|
<?php endif; ?>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label for="telephone">Telefon:</label>
|
|
<input type="tel" id="telephone" name="telephone" value="<?php echo htmlspecialchars($telephone); ?>">
|
|
<?php if (isset($errors['telephone'])): ?>
|
|
<div class="error"><?php echo htmlspecialchars($errors['telephone']); ?></div>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
|
|
<h2>Inserat-Informationen</h2>
|
|
<div style="border: 1px solid #ddd; padding: 15px; margin-bottom: 20px; border-radius: 5px;">
|
|
<div class="form-group">
|
|
<label for="advertName">Name:</label>
|
|
<input type="text" id="advertName" name="advertName" value="<?php echo htmlspecialchars($advertName); ?>">
|
|
<?php if (isset($errors['advertName'])): ?>
|
|
<div class="error"><?php echo htmlspecialchars($errors['advertName']); ?></div>
|
|
<?php endif; ?>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label for="description">Beschreibung (maximal 255 Zeichen):</label>
|
|
<textarea id="description" name="description" maxlength="255"><?php echo htmlspecialchars($description); ?></textarea>
|
|
<?php if (isset($errors['description'])): ?>
|
|
<div class="error"><?php echo htmlspecialchars($errors['description']); ?></div>
|
|
<?php endif; ?>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label for="categories">Kategorien (wählen Sie bis zu 3):</label>
|
|
<select multiple id="categories" name="categories[]">
|
|
<?php foreach ($categories as $category): ?>
|
|
<option value="<?php echo $category['id']; ?>"
|
|
<?php echo in_array($category['id'], $selectedCategories) ? 'selected' : ''; ?>>
|
|
<?php echo htmlspecialchars($category['name']); ?>
|
|
</option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
<?php if (isset($errors['categories'])): ?>
|
|
<div class="error"><?php echo htmlspecialchars($errors['categories']); ?></div>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
|
|
<button type="submit" class="button">Inserat erstellen</button>
|
|
</form>
|
|
|
|
<div class="back-link">
|
|
<a href="index.php">Zurück zu den Kategorien</a>
|
|
</div>
|
|
</body>
|
|
</html>
|