ganz langer test
Erstellt am: 2025-05-13 21:18:41
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
session_start();
require_once 'config.php';
require_once 'classes/Content.class.php';
if (!isset($_SESSION['user']) || $_SESSION['user']['rank'] !== 'admin') {
header("Location: /index.php");
exit;
}
$content = new Content($pdo);
// Aktionen verarbeiten
if (isset($_GET['delete_snippet'])) {
$content->deleteSnippet((int)$_GET['delete_snippet']);
header("Location: admin_content.php");
exit;
}
if (isset($_GET['delete_meme'])) {
$content->deleteMeme((int)$_GET['delete_meme']);
header("Location: admin_content.php");
exit;
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['edit_snippet_id'])) {
$content->updateSnippet(
(int)$_POST['edit_snippet_id'],
$_POST['title'],
$_POST['code']
);
header("Location: admin_content.php");
exit;
}
if (isset($_POST['edit_meme_id'])) {
$content->updateMeme(
(int)$_POST['edit_meme_id'],
$_POST['title'],
null,
filter_var($_POST['external_url'], FILTER_VALIDATE_URL)
);
header("Location: admin_content.php");
exit;
}
// Neues Snippet hinzufügen
if (isset($_POST['add_snippet'])) {
try {
$content->addSnippet(
$_POST['title'],
$_POST['code'],
(int)$_SESSION['user']['id']
);
header("Location: admin_content.php");
exit;
} catch (Exception $e) {
die("Fehler beim Hinzufügen des Snippets: " . $e->getMessage());
}
}
// Neues Meme hinzufügen
if (isset($_POST['add_meme'])) {
try {
$filePath = null;
// Dateiupload verarbeiten
if (isset($_FILES['file_path']) && $_FILES['file_path']['error'] === UPLOAD_ERR_OK) {
$uploadDir = 'uploads/memes/';
if (!is_dir($uploadDir)) {
mkdir($uploadDir, 0755, true);
}
$fileName = uniqid() . '_' . basename($_FILES['file_path']['name']);
$filePath = $uploadDir . $fileName;
move_uploaded_file($_FILES['file_path']['tmp_name'], $filePath);
}
$content->addMeme(
$_POST['title'],
$filePath,
filter_var($_POST['external_url'], FILTER_VALIDATE_URL),
(int)$_SESSION['user']['id']
);
header("Location: admin_content.php");
exit;
} catch (Exception $e) {
die("Fehler beim Hinzufügen des Memes: " . $e->getMessage());
}
}
}
$snippets = $content->getSnippets();
$memes = $content->getMemes();
$editSnippetId = isset($_GET['edit_snippet']) ? (int)$_GET['edit_snippet'] : null;
$editMemeId = isset($_GET['edit_meme']) ? (int)$_GET['edit_meme'] : null;
include 'includes/header.php';
include 'includes/nav.php';
?>
<main class="container my-5">
<!-- CodeSnippets -->
<div class="d-flex justify-content-between align-items-center mb-4">
<h1 class="h3">CodeSnippets verwalten</h1>
<button type="button" class="btn btn-success" data-bs-toggle="modal" data-bs-target="#addSnippetModal">
+ Neues Snippet hinzufügen
</button>
</div>
<!-- Modal für neues Snippet -->
<div class="modal fade" id="addSnippetModal" tabindex="-1" aria-labelledby="addSnippetModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="addSnippetModalLabel">Neues CodeSnippet hinzufügen</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form method="post">
<div class="modal-body">
<div class="mb-3">
<label for="title" class="form-label">Titel</label>
<input type="text" class="form-control" name="title" required>
</div>
<div class="mb-3">
<label for="code" class="form-label">Code</label>
<textarea class="form-control" name="code" rows="5" required></textarea>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Abbrechen</button>
<button type="submit" name="add_snippet" class="btn btn-primary">Speichern</button>
</div>
</form>
</div>
</div>
</div>
<?php if (empty($snippets)): ?>
<p class="text-muted">Es wurden noch keine Snippets hinzugefügt.</p>
<?php else: ?>
<table class="table table-bordered table-hover">
<thead class="table-light">
<tr>
<th>Titel</th>
<th>Code (Vorschau)</th>
<th>Erstellt am</th>
<th>Erstellt von</th>
<th>Aktionen</th>
</tr>
</thead>
<tbody>
<?php foreach ($snippets as $snippet): ?>
<?php if ($editSnippetId === (int)$snippet['id']): ?>
<tr>
<form method="post">
<input type="hidden" name="edit_snippet_id" value="<?= $snippet['id'] ?>">
<td><input type="text" name="title" class="form-control" value="<?= htmlspecialchars($snippet['title']) ?>" required></td>
<td><textarea name="code" class="form-control" rows="3"><?= htmlspecialchars($snippet['code']) ?></textarea></td>
<td><?= htmlspecialchars($snippet['created_at']) ?></td>
<td><?= htmlspecialchars($snippet['author']) ?></td>
<td>
<button type="submit" class="btn btn-sm btn-success">Speichern</button>
<a href="admin_content.php" class="btn btn-sm btn-secondary">Abbrechen</a>
</td>
</form>
</tr>
<?php else: ?>
<tr>
<td><?= htmlspecialchars($snippet['title']) ?></td>
<td><code><?= htmlspecialchars(substr($snippet['code'], 0, 50)) ?>...</code></td>
<td><?= htmlspecialchars($snippet['created_at']) ?></td>
<td><?= htmlspecialchars($snippet['author']) ?></td>
<td>
<a href="?edit_snippet=<?= $snippet['id'] ?>" class="btn btn-sm btn-primary">Bearbeiten</a>
<a href="?delete_snippet=<?= $snippet['id'] ?>" class="btn btn-sm btn-danger" onclick="return confirm('Wirklich löschen?')">Löschen</a>
</td>
</tr>
<?php endif; ?>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
<hr class="my-5">
<!-- Memes -->
<div class="d-flex justify-content-between align-items-center mb-4">
<h2 class="h4">Memes verwalten</h2>
<button type="button" class="btn btn-success" data-bs-toggle="modal" data-bs-target="#addMemeModal">
+ Neues Meme hinzufügen
</button>
</div>
<!-- Modal für neues Meme -->
<div class="modal fade" id="addMemeModal" tabindex="-1" aria-labelledby="addMemeModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="addMemeModalLabel">Neues Meme hinzufügen</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form method="post" enctype="multipart/form-data">
<div class="modal-body">
<div class="mb-3">
<label for="title" class="form-label">Titel</label>
<input type="text" class="form-control" name="title" required>
</div>
<div class="mb-3">
<label for="external_url" class="form-label">Externe Bild-URL</label>
<input type="url" class="form-control" name="external_url">
</div>
<div class="mb-3">
<label for="file_path" class="form-label">Oder Datei hochladen</label>
<input type="file" class="form-control" name="file_path" accept="image/*">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Abbrechen</button>
<button type="submit" name="add_meme" class="btn btn-primary">Speichern</button>
</div>
</form>
</div>
</div>
</div>
<?php if (empty($memes)): ?>
<p class="text-muted">Es wurden noch keine Memes hinzugefügt.</p>
<?php else: ?>
<div class="row">
<?php foreach ($memes as $meme): ?>
<div class="col-md-4 mb-4">
<div class="card h-100">
<?php if ($editMemeId === (int)$meme['id']): ?>
<form method="post" class="card-body">
<input type="hidden" name="edit_meme_id" value="<?= $meme['id'] ?>">
<div class="mb-2">
<label class="form-label">Titel</label>
<input type="text" name="title" class="form-control" value="<?= htmlspecialchars($meme['title']) ?>" required>
</div>
<div class="mb-2">
<label class="form-label">Externe Bild-URL</label>
<input type="url" name="external_url" class="form-control" value="<?= htmlspecialchars($meme['external_url']) ?>">
</div>
<button type="submit" class="btn btn-success btn-sm">Speichern</button>
<a href="admin_content.php" class="btn btn-secondary btn-sm">Abbrechen</a>
</form>
<?php else: ?>
<?php if ($meme['file_path']): ?>
<img src="/<?= htmlspecialchars($meme['file_path']) ?>" class="card-img-top" alt="Meme">
<?php elseif ($meme['external_url']): ?>
<img src="<?= htmlspecialchars($meme['external_url']) ?>" class="card-img-top" alt="Meme">
<?php endif; ?>
<div class="card-body">
<h5 class="card-title"><?= htmlspecialchars($meme['title']) ?></h5>
<p class="card-text"><small class="text-muted">Erstellt von: <?= htmlspecialchars($meme['author']) ?></small></p>
<a href="?edit_meme=<?= $meme['id'] ?>" class="btn btn-primary btn-sm">Bearbeiten</a>
<a href="?delete_meme=<?= $meme['id'] ?>" class="btn btn-danger btn-sm" onclick="return confirm('Wirklich löschen?')">Löschen</a>
</div>
<?php endif; ?>
</div>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
</main>
<?php include 'includes/footer.php'; ?>
Zurück zur Übersicht