first commit
This commit is contained in:
		
						commit
						8804d13f00
					
				|  | @ -0,0 +1,6 @@ | |||
| <?php | ||||
| 
 | ||||
| define('DB_SERVER', 'localhost'); | ||||
| define('DB_USERNAME', 'root'); | ||||
| define('DB_PASSWORD', ''); | ||||
| define('DB_NAME', 'blackboard'); | ||||
|  | @ -0,0 +1,108 @@ | |||
| <?php | ||||
| require_once "../src/adverts.php"; | ||||
| require_once "../src/categories.php"; | ||||
| 
 | ||||
| // Überprüft ob Kategorie ID vorhanden ist
 | ||||
| if (!isset($_GET['id']) || !is_numeric($_GET['id'])) { | ||||
|     echo "Fehler: Kategorie-ID ist erforderlich"; | ||||
|     exit; | ||||
| } | ||||
| 
 | ||||
| $categoryId = (int)$_GET['id']; | ||||
| 
 | ||||
| // Get category details
 | ||||
| $allCategories = Categories::getAllCategories(); | ||||
| $categoryName = ""; | ||||
| 
 | ||||
| foreach ($allCategories as $category) { | ||||
|     if ($category['id'] == $categoryId) { | ||||
|         $categoryName = $category['name']; | ||||
|         break; | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| if (empty($categoryName)) { | ||||
|     echo "Fehler: Kategorie nicht gefunden"; | ||||
|     exit; | ||||
| } | ||||
| 
 | ||||
| // Anzeigen für diese Kategorie erhalten
 | ||||
| $adverts = Adverts::getAdvertsByCategoryId($categoryId); | ||||
| ?>
 | ||||
| 
 | ||||
| <!DOCTYPE html> | ||||
| <html lang="de"> | ||||
| <head> | ||||
|     <meta charset="UTF-8"> | ||||
|     <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||||
|     <title>Inserate in <?php echo htmlspecialchars($categoryName); ?></title>
 | ||||
|     <style> | ||||
|         body { | ||||
|             font-family: Arial, sans-serif; | ||||
|             max-width: 800px; | ||||
|             margin: 0 auto; | ||||
|             padding: 20px; | ||||
|         } | ||||
|         h1 { | ||||
|             color: #333;
 | ||||
|         } | ||||
|         .inserat { | ||||
|             border: 1px solid #ddd;
 | ||||
|             padding: 15px; | ||||
|             margin-bottom: 15px; | ||||
|             border-radius: 5px; | ||||
|         } | ||||
|         .inserat h2 { | ||||
|             margin-top: 0; | ||||
|         } | ||||
|         .inserat-date { | ||||
|             color: #666;
 | ||||
|             font-size: 0.8em; | ||||
|         } | ||||
|         .no-inserate { | ||||
|             color: #666;
 | ||||
|             font-style: italic; | ||||
|         } | ||||
|         .back-link { | ||||
|             margin-top: 20px; | ||||
|         } | ||||
|     </style> | ||||
| </head> | ||||
| <body> | ||||
|     <h1>Inserate in Kategorie: <?php echo htmlspecialchars($categoryName); ?></h1>
 | ||||
| 
 | ||||
|     <div style="margin-bottom: 20px;"> | ||||
|         <a href="create_advert.php" style="display: inline-block; padding: 10px 15px; background-color: #4CAF50; color: white; text-decoration: none; border-radius: 5px;">Neues Inserat erstellen</a> | ||||
|     </div> | ||||
| 
 | ||||
|     <?php if (empty($adverts)): ?>
 | ||||
|         <p class="no-inserate">Keine Inserate in dieser Kategorie gefunden.</p> | ||||
|     <?php else: ?>
 | ||||
|         <?php foreach ($adverts as $advert): ?>
 | ||||
|             <div class="inserat"> | ||||
|                 <h2><?php echo htmlspecialchars($advert['name']); ?></h2>
 | ||||
|                 <p><?php echo htmlspecialchars($advert['description']); ?></p>
 | ||||
|                 <p class="inserat-date">Erstellt am: <?php echo htmlspecialchars($advert['date']); ?></p>
 | ||||
|                 <?php if (isset($advert['user_name']) || isset($advert['user_email']) || isset($advert['user_telephone'])): ?>
 | ||||
|                     <div class="user-contact"> | ||||
|                         <h3>Kontaktinformationen:</h3> | ||||
|                         <?php if (isset($advert['user_name'])): ?>
 | ||||
|                             <p><strong>Name:</strong> <?php echo htmlspecialchars($advert['user_name']); ?></p>
 | ||||
|                         <?php endif; ?>
 | ||||
|                         <?php if (isset($advert['user_email'])): ?>
 | ||||
|                             <p><strong>E-Mail:</strong> <?php echo htmlspecialchars($advert['user_email']); ?></p>
 | ||||
|                         <?php endif; ?>
 | ||||
|                         <?php if (isset($advert['user_telephone'])): ?>
 | ||||
|                             <p><strong>Telefon:</strong> <?php echo htmlspecialchars($advert['user_telephone']); ?></p>
 | ||||
|                         <?php endif; ?>
 | ||||
|                     </div> | ||||
|                 <?php endif; ?>
 | ||||
|             </div> | ||||
|         <?php endforeach; ?>
 | ||||
|     <?php endif; ?>
 | ||||
| 
 | ||||
|     <div class="back-link"> | ||||
|         <a href="index.php">Zurück zu den Kategorien</a> | ||||
|     </div> | ||||
| </body> | ||||
| </html> | ||||
|  | @ -0,0 +1,253 @@ | |||
| <?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> | ||||
|  | @ -0,0 +1,136 @@ | |||
| <?php | ||||
| require_once "../src/categories.php"; | ||||
| require_once "../src/adverts.php"; | ||||
| 
 | ||||
| // Hole alle Kategorien und Inserate
 | ||||
| $categories = Categories::getAllCategories(); | ||||
| $adverts = Adverts::getAllAdverts(); | ||||
| ?>
 | ||||
| 
 | ||||
| <!DOCTYPE html> | ||||
| <html lang="de"> | ||||
| <head> | ||||
|     <meta charset="UTF-8"> | ||||
|     <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||||
|     <title>Schwarzes Brett</title> | ||||
|     <style> | ||||
|         body { | ||||
|             font-family: Arial, sans-serif; | ||||
|             max-width: 800px; | ||||
|             margin: 0 auto; | ||||
|             padding: 20px; | ||||
|         } | ||||
|         h1, h2 { | ||||
|             color: #333;
 | ||||
|         } | ||||
|         ul { | ||||
|             list-style-type: none; | ||||
|             padding: 0; | ||||
|         } | ||||
|         li { | ||||
|             margin-bottom: 10px; | ||||
|         } | ||||
|         .category-link { | ||||
|             display: block; | ||||
|             padding: 10px 15px; | ||||
|             background-color: #f5f5f5;
 | ||||
|             border-radius: 5px; | ||||
|             text-decoration: none; | ||||
|             color: #333;
 | ||||
|             transition: background-color 0.2s; | ||||
|         } | ||||
|         .category-link:hover { | ||||
|             background-color: #e0e0e0;
 | ||||
|         } | ||||
|         .no-categories, .no-inserate { | ||||
|             color: #666;
 | ||||
|             font-style: italic; | ||||
|         } | ||||
|         .inserat { | ||||
|             border: 1px solid #ddd;
 | ||||
|             padding: 15px; | ||||
|             margin-bottom: 15px; | ||||
|             border-radius: 5px; | ||||
|         } | ||||
|         .inserat h2 { | ||||
|             margin-top: 0; | ||||
|         } | ||||
|         .inserat-date { | ||||
|             color: #666;
 | ||||
|             font-size: 0.8em; | ||||
|         } | ||||
|         .filter-form { | ||||
|             margin: 20px 0; | ||||
|             padding: 15px; | ||||
|             background-color: #f5f5f5;
 | ||||
|             border-radius: 5px; | ||||
|         } | ||||
|         .filter-form select { | ||||
|             padding: 8px; | ||||
|             border-radius: 4px; | ||||
|             border: 1px solid #ddd;
 | ||||
|             margin-right: 10px; | ||||
|         } | ||||
|         .filter-form button { | ||||
|             padding: 8px 15px; | ||||
|             background-color: #4CAF50;
 | ||||
|             color: white; | ||||
|             border: none; | ||||
|             border-radius: 4px; | ||||
|             cursor: pointer; | ||||
|         } | ||||
|         .filter-form button:hover { | ||||
|             background-color: #45a049;
 | ||||
|         } | ||||
|     </style> | ||||
| </head> | ||||
| <body> | ||||
|     <h1>Schwarzes Brett</h1> | ||||
| 
 | ||||
|     <div style="margin-bottom: 20px;"> | ||||
|         <a href="create_advert.php" style="display: inline-block; padding: 10px 15px; background-color: #4CAF50; color: white; text-decoration: none; border-radius: 5px;">Neues Inserat erstellen</a> | ||||
|     </div> | ||||
| 
 | ||||
|     <h2>Kategorien</h2> | ||||
|     <?php if (empty($categories)): ?>
 | ||||
|         <p class="no-categories">Keine Kategorien gefunden.</p> | ||||
|     <?php else: ?>
 | ||||
|         <ul> | ||||
|             <?php foreach ($categories as $category): ?>
 | ||||
|                 <li> | ||||
|                     <a class="category-link" href="category.php?id=<?php echo $category['id']; ?>"> | ||||
|                         <?php echo htmlspecialchars($category['name']); ?>
 | ||||
|                     </a> | ||||
|                 </li> | ||||
|             <?php endforeach; ?>
 | ||||
|         </ul> | ||||
|     <?php endif; ?>
 | ||||
| 
 | ||||
|     <h2>Aktuelle Inserate</h2> | ||||
|     <?php if (empty($adverts)): ?>
 | ||||
|         <p class="no-inserate">Keine Inserate gefunden.</p> | ||||
|     <?php else: ?>
 | ||||
|         <?php foreach ($adverts as $advert): ?>
 | ||||
|             <div class="inserat"> | ||||
|                 <h2><?php echo htmlspecialchars($advert['name']); ?></h2>
 | ||||
|                 <p><?php echo htmlspecialchars($advert['description']); ?></p>
 | ||||
|                 <p class="inserat-date">Erstellt am: <?php echo htmlspecialchars($advert['date']); ?></p>
 | ||||
|                 <?php if (isset($advert['user_name']) || isset($advert['user_email']) || isset($advert['user_telephone'])): ?>
 | ||||
|                     <div class="user-contact"> | ||||
|                         <h3>Kontaktinformationen:</h3> | ||||
|                         <?php if (isset($advert['user_name'])): ?>
 | ||||
|                             <p><strong>Name:</strong> <?php echo htmlspecialchars($advert['user_name']); ?></p>
 | ||||
|                         <?php endif; ?>
 | ||||
|                         <?php if (isset($advert['user_email'])): ?>
 | ||||
|                             <p><strong>E-Mail:</strong> <?php echo htmlspecialchars($advert['user_email']); ?></p>
 | ||||
|                         <?php endif; ?>
 | ||||
|                         <?php if (isset($advert['user_telephone'])): ?>
 | ||||
|                             <p><strong>Telefon:</strong> <?php echo htmlspecialchars($advert['user_telephone']); ?></p>
 | ||||
|                         <?php endif; ?>
 | ||||
|                     </div> | ||||
|                 <?php endif; ?>
 | ||||
|             </div> | ||||
|         <?php endforeach; ?>
 | ||||
|     <?php endif; ?>
 | ||||
| </body> | ||||
| </html> | ||||
|  | @ -0,0 +1,116 @@ | |||
| <?php | ||||
| 
 | ||||
| require_once "database.php"; | ||||
| require_once "users.php"; | ||||
| 
 | ||||
| // Eine einfache Klasse zur Verwaltung von Anzeigen
 | ||||
| // Diese Klasse bietet statische Methoden zum Abrufen und Erstellen von Anzeigen in der Datenbank.
 | ||||
| class Adverts { | ||||
|     // Holt alle Anzeigen für eine bestimmte Kategorie
 | ||||
|     // $categoryId: Die ID der Kategorie
 | ||||
|     // Gibt ein Array von Anzeigen zurück, die zur angegebenen Kategorie gehören
 | ||||
|     public static function getAdvertsByCategoryId(int $categoryId): array { | ||||
|         $db = new Database(); | ||||
|         $sql = "SELECT a.id, a.name, a.description, a.date, a.user_id 
 | ||||
|                 FROM advert a | ||||
|                 JOIN advert_category ac ON a.id = ac.advert_id | ||||
|                 WHERE ac.category_id = ? | ||||
|                 ORDER BY a.date DESC";
 | ||||
| 
 | ||||
|         $result = $db->query($sql, [$categoryId]); | ||||
|         $adverts = $db->fetchAll($result); | ||||
| 
 | ||||
|         // Füge Benutzerinformationen zu jeder Anzeige hinzu
 | ||||
|         foreach ($adverts as &$advert) { | ||||
|             if (isset($advert['user_id'])) { | ||||
|                 $user = Users::getUserById($advert['user_id']); | ||||
|                 if ($user) { | ||||
|                     $advert['user_name'] = $user['name']; | ||||
|                     $advert['user_email'] = $user['email']; | ||||
|                     $advert['user_telephone'] = $user['telephone']; | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         return $adverts; | ||||
|     } | ||||
| 
 | ||||
|     // Holt alle Anzeigen sortiert nach Erstellungsdatum
 | ||||
|     // Gibt ein Array aller Anzeigen zurück
 | ||||
|     public static function getAllAdverts(): array { | ||||
|         $db = new Database(); | ||||
|         $sql = "SELECT id, name, description, date, user_id 
 | ||||
|                 FROM advert | ||||
|                 ORDER BY date DESC";
 | ||||
| 
 | ||||
|         $result = $db->query($sql); | ||||
|         $adverts = $db->fetchAll($result); | ||||
| 
 | ||||
|         // Füge Benutzerinformationen zu jeder Anzeige hinzu
 | ||||
|         foreach ($adverts as &$advert) { | ||||
|             if (isset($advert['user_id'])) { | ||||
|                 $user = Users::getUserById($advert['user_id']); | ||||
|                 if ($user) { | ||||
|                     $advert['user_name'] = $user['name']; | ||||
|                     $advert['user_email'] = $user['email']; | ||||
|                     $advert['user_telephone'] = $user['telephone']; | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         return $adverts; | ||||
|     } | ||||
| 
 | ||||
|     // Erstellt eine neue Anzeige in der Datenbank
 | ||||
|     // Diese Methode erstellt eine neue Anzeige und verknüpft sie mit einem Benutzer und Kategorien.
 | ||||
|     // Die Benutzerverwaltung wird von der Users-Klasse übernommen.
 | ||||
|     // 
 | ||||
|     // $name: Der Name der Anzeige
 | ||||
|     // $description: Die Beschreibung der Anzeige
 | ||||
|     // $categoryIds: Array von Kategorie-IDs, die mit der Anzeige verknüpft werden sollen
 | ||||
|     // $userName: Der Name des Benutzers
 | ||||
|     // $userEmail: Die E-Mail des Benutzers
 | ||||
|     // $userTelephone: Die Telefonnummer des Benutzers
 | ||||
|     // Gibt die ID der erstellten Anzeige zurück oder false bei einem Fehler
 | ||||
|     public static function createAdvert( | ||||
|         string $name,  | ||||
|         string $description,  | ||||
|         array $categoryIds,  | ||||
|         string $userName,  | ||||
|         string $userEmail,  | ||||
|         string $userTelephone | ||||
|     ): int|false { | ||||
|         $db = new Database(); | ||||
| 
 | ||||
|         try { | ||||
|             // Prüfe, ob Benutzer mit der angegebenen E-Mail existiert
 | ||||
|             $userId = Users::getUserIdByEmail($userEmail); | ||||
| 
 | ||||
|             if ($userId === null) { | ||||
|                 // Benutzer existiert nicht, erstelle einen neuen Benutzer
 | ||||
|                 $userId = Users::createUser($userName, $userEmail, $userTelephone); | ||||
|             } | ||||
| 
 | ||||
|             // Erstelle die Anzeige
 | ||||
|             $sql = "INSERT INTO advert (name, description, date, user_id) VALUES (?, ?, NOW(), ?)"; | ||||
|             $db->query($sql, [$name, $description, $userId]); | ||||
| 
 | ||||
|             // Hole die ID der neu erstellten Anzeige
 | ||||
|             $sql = "SELECT MAX(id) as id FROM advert WHERE name = ? AND user_id = ?"; | ||||
|             $result = $db->query($sql, [$name, $userId]); | ||||
|             $advert = $db->fetchOne($result); | ||||
|             $advertId = $advert['id']; | ||||
| 
 | ||||
|             // Verknüpfe die Anzeige mit Kategorien
 | ||||
|             foreach ($categoryIds as $categoryId) { | ||||
|                 $sql = "INSERT INTO advert_category (advert_id, category_id) VALUES (?, ?)"; | ||||
|                 $db->query($sql, [$advertId, $categoryId]); | ||||
|             } | ||||
| 
 | ||||
|             return $advertId; | ||||
|         } catch (Exception $e) { | ||||
|             // Fehler protokollieren oder nach Bedarf behandeln
 | ||||
|             return false; | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | @ -0,0 +1,18 @@ | |||
| <?php | ||||
| 
 | ||||
| require_once "database.php"; | ||||
| 
 | ||||
| // Eine einfache Klasse zur Verwaltung von Kategorien
 | ||||
| // Diese Klasse bietet statische Methoden zum Abrufen von Kategorien
 | ||||
| // aus der Datenbank.
 | ||||
| class Categories { | ||||
|     // Holt alle Kategorien aus der Datenbank
 | ||||
|     // Gibt ein Array von Kategorien mit ID und Namen zurück
 | ||||
|     public static function getAllCategories(): array { | ||||
|         $db = new Database(); | ||||
|         $sql = "SELECT id, name FROM category ORDER BY id"; | ||||
|         $result = $db->query($sql); | ||||
| 
 | ||||
|         return $db->fetchAll($result); | ||||
|     } | ||||
| } | ||||
|  | @ -0,0 +1,142 @@ | |||
| <?php | ||||
| 
 | ||||
| require "../config/config.php"; | ||||
| 
 | ||||
| // Eine minimale Wrapper-Klasse für mysqli-Funktionen
 | ||||
| // Diese Klasse bietet eine einfache Schnittstelle für Datenbankoperationen
 | ||||
| // mit mysqli und verwendet nur Prepared Statements. Sie enthält auch
 | ||||
| // Methoden zum Abrufen von Abfrageergebnissen.
 | ||||
| class Database { | ||||
|     private mysqli $connection; | ||||
| 
 | ||||
|     // Konstruktor - stellt eine Datenbankverbindung her
 | ||||
|     // Wirft eine Exception, wenn die Verbindung fehlschlägt
 | ||||
|     public function __construct() { | ||||
|         // Verbindung mit Konstanten aus der Konfiguration erstellen
 | ||||
|         $this->connection = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME); | ||||
| 
 | ||||
|         // Verbindung überprüfen
 | ||||
|         if ($this->connection->connect_error) { | ||||
|             throw new Exception("Connection failed: " . $this->connection->connect_error); | ||||
|         } | ||||
| 
 | ||||
|         $this->initialize(); | ||||
|     } | ||||
| 
 | ||||
|     // Führt eine Abfrage mit Prepared Statements aus
 | ||||
|     // $sql: Die auszuführende SQL-Abfrage
 | ||||
|     // $params: Parameter für das Prepared Statement
 | ||||
|     // Gibt ein mysqli_result-Objekt oder einen booleschen Wert zurück
 | ||||
|     // Wirft eine Exception, wenn die Abfrage fehlschlägt
 | ||||
|     public function query(string $sql, array $params = []): mysqli_result|bool { | ||||
|         // Prepared Statement mit Parametern
 | ||||
|         $stmt = $this->connection->prepare($sql); | ||||
| 
 | ||||
|         if ($stmt === false) { | ||||
|             throw new Exception("Prepare failed: " . $this->connection->error); | ||||
|         } | ||||
| 
 | ||||
|         // Bestimme den Typen-String für bind_param
 | ||||
|         $types = ''; | ||||
|         foreach ($params as $param) { | ||||
|             if (is_int($param)) { | ||||
|                 $types .= 'i'; | ||||
|             } elseif (is_float($param)) { | ||||
|                 $types .= 'd'; | ||||
|             } elseif (is_string($param)) { | ||||
|                 $types .= 's'; | ||||
|             } else { | ||||
|                 $types .= 'b'; | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         // Parameter binden
 | ||||
|         if (!empty($params)) { | ||||
|             $stmt->bind_param($types, ...$params); | ||||
|         } | ||||
| 
 | ||||
|         // Statement ausführen
 | ||||
|         if (!$stmt->execute()) { | ||||
|             throw new Exception("Execute failed: " . $stmt->error); | ||||
|         } | ||||
| 
 | ||||
|         $result = $stmt->get_result(); | ||||
|         $stmt->close(); | ||||
| 
 | ||||
|         return $result ?: true; | ||||
|     } | ||||
| 
 | ||||
|     // Holt alle Zeilen aus einem Ergebnissatz als assoziatives Array
 | ||||
|     // $result: Der Ergebnissatz
 | ||||
|     // Gibt ein Array von Zeilen zurück
 | ||||
|     public function fetchAll(mysqli_result $result): array { | ||||
|         return $result->fetch_all(MYSQLI_ASSOC); | ||||
|     } | ||||
| 
 | ||||
|     // Holt eine einzelne Zeile aus einem Ergebnissatz als assoziatives Array
 | ||||
|     // $result: Der Ergebnissatz
 | ||||
|     // Gibt die Zeile als assoziatives Array zurück oder null, wenn keine Zeilen vorhanden sind
 | ||||
|     public function fetchOne(mysqli_result $result): ?array { | ||||
|         $row = $result->fetch_assoc(); | ||||
|         return $row ?: null; | ||||
|     } | ||||
| 
 | ||||
|     // Initialisiert das Datenbankschema
 | ||||
|     // 
 | ||||
|     // Erstellt die notwendigen Tabellen, falls sie nicht existieren:
 | ||||
|     // - user: id, name, email, telephone
 | ||||
|     // - category: id, name
 | ||||
|     // - advert: id, name, description, date, user_id
 | ||||
|     // - advert_category: advert_id, category_id (Viele-zu-viele-Beziehung)
 | ||||
|     // 
 | ||||
|     // Gibt true zurück, wenn die Initialisierung erfolgreich war
 | ||||
|     // Wirft eine Exception, wenn die Initialisierung fehlschlägt
 | ||||
|     public function initialize(): bool { | ||||
|         try { | ||||
|             // Erstelle Benutzer-Tabelle, falls sie nicht existiert
 | ||||
|             $this->query(" | ||||
|                 CREATE TABLE IF NOT EXISTS user ( | ||||
|                     id INT AUTO_INCREMENT PRIMARY KEY, | ||||
|                     name VARCHAR(255) NOT NULL, | ||||
|                     email VARCHAR(255) NOT NULL, | ||||
|                     telephone VARCHAR(50) NOT NULL | ||||
|                 ) | ||||
|             ");
 | ||||
| 
 | ||||
|             // Erstelle Kategorie-Tabelle, falls sie nicht existiert
 | ||||
|             $this->query(" | ||||
|                 CREATE TABLE IF NOT EXISTS category ( | ||||
|                     id INT AUTO_INCREMENT PRIMARY KEY, | ||||
|                     name VARCHAR(255) NOT NULL | ||||
|                 ) | ||||
|             ");
 | ||||
| 
 | ||||
|             // Erstelle Anzeigen-Tabelle, falls sie nicht existiert
 | ||||
|             $this->query(" | ||||
|                 CREATE TABLE IF NOT EXISTS advert ( | ||||
|                     id INT AUTO_INCREMENT PRIMARY KEY, | ||||
|                     name VARCHAR(255) NOT NULL, | ||||
|                     description VARCHAR(255) NOT NULL, | ||||
|                     date DATETIME NOT NULL, | ||||
|                     user_id INT NOT NULL, | ||||
|                     FOREIGN KEY (user_id) REFERENCES user(id) | ||||
|                 ) | ||||
|             ");
 | ||||
| 
 | ||||
|             // Erstelle Anzeigen-Kategorie-Tabelle, falls sie nicht existiert (Viele-zu-viele-Beziehung)
 | ||||
|             $this->query(" | ||||
|                 CREATE TABLE IF NOT EXISTS advert_category ( | ||||
|                     advert_id INT NOT NULL, | ||||
|                     category_id INT NOT NULL, | ||||
|                     PRIMARY KEY (advert_id, category_id), | ||||
|                     FOREIGN KEY (advert_id) REFERENCES advert(id), | ||||
|                     FOREIGN KEY (category_id) REFERENCES category(id) | ||||
|                 ) | ||||
|             ");
 | ||||
| 
 | ||||
|             return true; | ||||
|         } catch (Exception $e) { | ||||
|             throw new Exception("Schema initialization failed: " . $e->getMessage()); | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | @ -0,0 +1,45 @@ | |||
| <?php | ||||
| 
 | ||||
| require_once "database.php"; | ||||
| 
 | ||||
| // Eine einfache Klasse zur Verwaltung von Benutzern
 | ||||
| // Diese Klasse bietet statische Methoden zur Überprüfung vorhandener Benutzer
 | ||||
| // und zum Erstellen neuer Benutzer in der Datenbank.
 | ||||
| class Users { | ||||
|     // Überprüft, ob ein Benutzer mit der angegebenen E-Mail existiert und gibt dessen ID zurück
 | ||||
|     // $email: Die E-Mail des zu überprüfenden Benutzers
 | ||||
|     // Gibt die Benutzer-ID zurück, wenn gefunden, sonst null
 | ||||
|     public static function getUserIdByEmail(string $email): ?int { | ||||
|         $db = new Database(); | ||||
|         $sql = "SELECT id FROM user WHERE email = ?"; | ||||
|         $result = $db->query($sql, [$email]); | ||||
|         $user = $db->fetchOne($result); | ||||
| 
 | ||||
|         return $user ? (int)$user['id'] : null; | ||||
|     } | ||||
| 
 | ||||
|     // Erstellt einen neuen Benutzer in der Datenbank
 | ||||
|     // $name: Der Name des Benutzers
 | ||||
|     // $email: Die E-Mail des Benutzers
 | ||||
|     // $telephone: Die Telefonnummer des Benutzers
 | ||||
|     // Gibt die ID des neu erstellten Benutzers zurück
 | ||||
|     public static function createUser(string $name, string $email, string $telephone): int { | ||||
|         $db = new Database(); | ||||
|         $sql = "INSERT INTO user (name, email, telephone) VALUES (?, ?, ?)"; | ||||
|         $db->query($sql, [$name, $email, $telephone]); | ||||
| 
 | ||||
|         // Hole die ID des neu erstellten Benutzers
 | ||||
|         return self::getUserIdByEmail($email); | ||||
|     } | ||||
| 
 | ||||
|     // Holt Benutzerinformationen anhand der Benutzer-ID
 | ||||
|     // $userId: Die ID des Benutzers
 | ||||
|     // Gibt ein Array mit Benutzerinformationen zurück oder null, wenn nicht gefunden
 | ||||
|     public static function getUserById(int $userId): ?array { | ||||
|         $db = new Database(); | ||||
|         $sql = "SELECT id, name, email, telephone FROM user WHERE id = ?"; | ||||
|         $result = $db->query($sql, [$userId]); | ||||
| 
 | ||||
|         return $db->fetchOne($result); | ||||
|     } | ||||
| } | ||||
		Loading…
	
		Reference in New Issue