CrestRoad/website/admin/resources.php

192 lines
8.9 KiB
PHP

<?php
require_once __DIR__ . '/../includes/auth.php';
require_login();
$error = '';
$uploadDir = __DIR__ . '/../uploads/';
if (!is_dir($uploadDir)) {
mkdir($uploadDir, 0775, true);
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$title = trim($_POST['title'] ?? '');
$summary = trim($_POST['summary'] ?? '');
$label = trim($_POST['label'] ?? '');
$video_url = trim($_POST['video_url'] ?? '');
$file_path = null;
if (isset($_FILES['resource_file']) && $_FILES['resource_file']['error'] !== UPLOAD_ERR_NO_FILE) {
$file = $_FILES['resource_file'];
if ($file['error'] === UPLOAD_ERR_OK) {
$allowed = [
'application/pdf',
'application/msword',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
];
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $file['tmp_name']);
finfo_close($finfo);
if (!in_array($mime, $allowed, true)) {
$error = 'Only PDF or Word documents are allowed.';
} else {
$ext = pathinfo($file['name'], PATHINFO_EXTENSION);
$newName = 'resource_' . time() . '_' . mt_rand(1000, 9999) . '.' . $ext;
$dest = $uploadDir . $newName;
if (move_uploaded_file($file['tmp_name'], $dest)) {
$file_path = 'uploads/' . $newName;
} else {
$error = 'File upload failed.';
}
}
} else {
$error = 'File upload failed.';
}
}
if ($title === '' || $summary === '' || $label === '') {
$error = 'All fields are required.';
} elseif (!$error) {
add_resource(compact('title', 'summary', 'label', 'file_path', 'video_url'));
header('Location: resources.php?success=1');
exit;
}
}
if (isset($_GET['delete'])) {
$id = (int) $_GET['delete'];
delete_resource($id);
header('Location: resources.php?deleted=1');
exit;
}
$resources = get_resources();
echo '<pre>'; print_r($resources); exit;
$assetPathPrefix = '../';
$pageTitle = 'Manage Resources';
$currentPage = 'resources';
$extraStyles = <<<CSS
<style>
body { background: #f6faf7; }
.admin-shell { padding: 40px 0; }
.table { width: 100%; border-collapse: collapse; margin-top: 20px; }
.table th, .table td { text-align: left; padding: 12px 10px; border-bottom: 1px solid #e1ede7; }
.table th { color: #0f4333; }
.badge { display: inline-block; padding: 6px 10px; border-radius: 10px; background: #e8f5ef; color: #0f4333; font-weight: 700; font-size: 13px; }
.inline-btn { color: #b9342f; font-weight: 700; }
form textarea { min-height: 120px; resize: vertical; }
form input, form textarea { width: 100%; padding: 12px 14px; border-radius: 10px; border: 1px solid #d8e7e0; font-size: 15px; margin-top: 8px; }
form input[type=file] { padding: 8px; }
form label { font-weight: 700; color: #0f4333; display: block; margin-top: 12px; }
.topbar { display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px; }
</style>
CSS;
require __DIR__ . '/../includes/admin-header.php';
?>
<main class="admin-shell">
<div class="container">
<div class="topbar">
<div>
<p class="eyebrow">Resources</p>
<h2>Resources for Learning and Research</h2>
<p class="body">Add short descriptions and labels; they will appear instantly on the public site.</p>
</div>
<button class="btn primary" onclick="document.getElementById('resource-form').scrollIntoView({behavior: 'smooth'})">Add new</button>
</div>
<div class="admin-nav">
<a href="/admin/tutorials.php">Tutorials</a>
<a href="/admin/sessions.php">Upcoming Sessions</a>
<a href="/admin/contacts.php">Contact Entries</a>
<a href="/admin/newsletter.php">Newsletter</a>
<a href="/admin/admins.php">Admins</a>
<a href="/admin/logout.php">Logout (<?php echo htmlspecialchars(current_admin_email() ?? ''); ?>)</a>
</div>
<?php if (isset($_GET['success'])): ?>
<div class="alert good">Resource added successfully.</div>
<?php endif; ?>
<?php if (isset($_GET['deleted'])): ?>
<div class="alert good">Resource deleted.</div>
<?php endif; ?>
<?php if ($error): ?>
<div class="alert bad"><?php echo htmlspecialchars($error); ?></div>
<?php endif; ?>
<?php if (empty($resources)): ?>
<p class="body muted">No resources yet. Add your first entry below.</p>
<?php else: ?>
<table class="table">
<thead>
<tr>
<th>Title</th>
<th>Summary</th>
<th>Label</th>
<th>Asset</th>
<th></th>
</tr>
</thead>
<tbody>
<?php foreach ($resources as $index => $resource): ?>
<?php
if (!is_array($resource)) {
continue;
}
$title = htmlspecialchars((string)($resource['title'] ?? ''), ENT_QUOTES, 'UTF-8');
$summary = htmlspecialchars((string)($resource['summary'] ?? ''), ENT_QUOTES, 'UTF-8');
$label = htmlspecialchars((string)($resource['label'] ?? ''), ENT_QUOTES, 'UTF-8');
$filePath = (string)($resource['file_path'] ?? '');
$videoUrl = (string)($resource['video_url'] ?? '');
?>
<tr>
<td><?php echo $title; ?></td>
<td><?php echo $summary; ?></td>
<td><span class="badge"><?php echo $label; ?></span></td>
<td>
<?php if ($filePath): ?>
<a class="text-link" href="/<?php echo htmlspecialchars(ltrim($filePath, '/'), ENT_QUOTES, 'UTF-8'); ?>" target="_blank">Download</a>
<?php elseif ($videoUrl): ?>
<a class="text-link" href="<?php echo htmlspecialchars($videoUrl, ENT_QUOTES, 'UTF-8'); ?>" target="_blank">Watch</a>
<?php else: ?>
<span class="body muted">None</span>
<?php endif; ?>
</td>
<td><a class="inline-btn" href="?delete=<?php echo (int)$resource['id']; ?>" onclick="return confirm('Delete this resource?')">Delete</a></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
<section id="resource-form" style="margin-top:32px;">
<div class="card">
<h3>Add Resource</h3>
<form method="post" enctype="multipart/form-data">
<label for="title">Title</label>
<input type="text" id="title" name="title" required placeholder="Resource title">
<label for="summary">Summary</label>
<textarea id="summary" name="summary" required placeholder="Brief description"></textarea>
<label for="label">Label / Type</label>
<input type="text" id="label" name="label" required placeholder="e.g., Publication, Toolkit">
<label for="video_url">YouTube / Video URL (optional)</label>
<input type="url" id="video_url" name="video_url" placeholder="https://youtube.com/...">
<label for="resource_file">Upload PDF / Word (optional)</label>
<input type="file" id="resource_file" name="resource_file" accept=".pdf,.doc,.docx">
<p class="body muted" style="margin:6px 0 0;">Upload a file or provide a video URL (or both).</p>
<div style="margin-top:16px;">
<button type="submit" class="btn primary">Save resource</button>
<a class="text-link" href="../index.php" style="margin-left:10px;">View live site</a>
</div>
</form>
</div>
</section>
</div>
</main>
<?php require __DIR__ . '/../includes/admin-footer.php'; ?>