223 lines
10 KiB
PHP
223 lines
10 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'] ?? '');
|
|
$description = trim($_POST['description'] ?? '');
|
|
$status = trim($_POST['status'] ?? 'active');
|
|
$video_url = trim($_POST['video_url'] ?? '');
|
|
$file_path = null;
|
|
$editId = isset($_POST['id']) ? (int) $_POST['id'] : null;
|
|
|
|
if (isset($_FILES['tutorial_file']) && $_FILES['tutorial_file']['error'] !== UPLOAD_ERR_NO_FILE) {
|
|
$file = $_FILES['tutorial_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 = 'tutorial_' . 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 === '' || $description === '') {
|
|
$error = 'Title and description are required.';
|
|
} elseif (!$error) {
|
|
$payload = compact('title', 'description', 'status', 'file_path', 'video_url');
|
|
if ($editId) {
|
|
update_tutorial($editId, $payload);
|
|
header('Location: tutorials.php?updated=1');
|
|
} else {
|
|
add_tutorial($payload);
|
|
header('Location: tutorials.php?success=1');
|
|
}
|
|
exit;
|
|
}
|
|
}
|
|
|
|
if (isset($_GET['delete'])) {
|
|
$id = (int) $_GET['delete'];
|
|
delete_tutorial($id);
|
|
header('Location: tutorials.php?deleted=1');
|
|
exit;
|
|
}
|
|
|
|
$tutorials = get_tutorials(false);
|
|
$editing = null;
|
|
if (isset($_GET['edit'])) {
|
|
$editId = (int) $_GET['edit'];
|
|
foreach ($tutorials as $row) {
|
|
if ((int) $row['id'] === $editId) {
|
|
$editing = $row;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
$assetPathPrefix = '../';
|
|
$pageTitle = 'Manage Tutorials';
|
|
$currentPage = 'tutorials';
|
|
$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, form select { 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">Tutorials</p>
|
|
<h2>Manage tutorials</h2>
|
|
<p class="body">Add training tutorials with downloads or video links.</p>
|
|
</div>
|
|
<button class="btn primary" onclick="document.getElementById('tutorial-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">Tutorial added successfully.</div>
|
|
<?php endif; ?>
|
|
<?php if (isset($_GET['deleted'])): ?>
|
|
<div class="alert good">Tutorial deleted.</div>
|
|
<?php endif; ?>
|
|
<?php if (isset($_GET['updated'])): ?>
|
|
<div class="alert good">Tutorial updated successfully.</div>
|
|
<?php endif; ?>
|
|
<?php if ($error): ?>
|
|
<div class="alert bad"><?php echo htmlspecialchars($error); ?></div>
|
|
<?php endif; ?>
|
|
|
|
<?php if (empty($tutorials)): ?>
|
|
<p class="body muted">No tutorials yet. Add your first entry below.</p>
|
|
<?php else: ?>
|
|
<table class="table">
|
|
<thead>
|
|
<tr>
|
|
<th>Title</th>
|
|
<th>Description</th>
|
|
<th>Status</th>
|
|
<th>Asset</th>
|
|
<th></th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($tutorials as $tutorial): ?>
|
|
<?php
|
|
if (!is_array($tutorial)) {
|
|
continue;
|
|
}
|
|
$title = htmlspecialchars((string)($tutorial['title'] ?? ''), ENT_QUOTES, 'UTF-8');
|
|
$description = htmlspecialchars((string)($tutorial['description'] ?? ''), ENT_QUOTES, 'UTF-8');
|
|
$status = htmlspecialchars((string)($tutorial['status'] ?? ''), ENT_QUOTES, 'UTF-8');
|
|
$filePath = (string)($tutorial['file_path'] ?? '');
|
|
$videoUrl = (string)($tutorial['video_url'] ?? '');
|
|
?>
|
|
<tr>
|
|
<td><?php echo $title; ?></td>
|
|
<td><?php echo $description; ?></td>
|
|
<td><span class="badge"><?php echo $status; ?></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="?edit=<?php echo (int)$tutorial['id']; ?>">Edit</a> |
|
|
<a class="inline-btn" href="?delete=<?php echo (int)$tutorial['id']; ?>" onclick="return confirm('Delete this tutorial?')">Delete</a>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
<?php endif; ?>
|
|
|
|
<section id="tutorial-form" style="margin-top:32px;">
|
|
<div class="card">
|
|
<h3><?php echo $editing ? 'Edit Tutorial' : 'Add Tutorial'; ?></h3>
|
|
<form method="post" enctype="multipart/form-data">
|
|
<?php if ($editing): ?>
|
|
<input type="hidden" name="id" value="<?php echo (int)$editing['id']; ?>">
|
|
<?php endif; ?>
|
|
<label for="title">Title</label>
|
|
<input type="text" id="title" name="title" required placeholder="Tutorial title" value="<?php echo htmlspecialchars($editing['title'] ?? '', ENT_QUOTES, 'UTF-8'); ?>">
|
|
|
|
<label for="description">Description</label>
|
|
<textarea id="description" name="description" required placeholder="Brief description"><?php echo htmlspecialchars($editing['description'] ?? '', ENT_QUOTES, 'UTF-8'); ?></textarea>
|
|
|
|
<label for="status">Status</label>
|
|
<select id="status" name="status">
|
|
<option value="active" <?php echo (($editing['status'] ?? '') === 'active') ? 'selected' : ''; ?>>Active</option>
|
|
<option value="draft" <?php echo (($editing['status'] ?? '') === 'draft') ? 'selected' : ''; ?>>Draft</option>
|
|
</select>
|
|
|
|
<label for="video_url">YouTube / Video URL (optional)</label>
|
|
<input type="url" id="video_url" name="video_url" placeholder="https://youtube.com/..." value="<?php echo htmlspecialchars($editing['video_url'] ?? '', ENT_QUOTES, 'UTF-8'); ?>">
|
|
|
|
<label for="tutorial_file">Upload PDF / Word (optional)</label>
|
|
<input type="file" id="tutorial_file" name="tutorial_file" accept=".pdf,.doc,.docx">
|
|
<?php if (!empty($editing['file_path'])): ?>
|
|
<p class="body muted">Current file: <a href="/<?php echo htmlspecialchars($editing['file_path'], ENT_QUOTES, 'UTF-8'); ?>" target="_blank">Download</a></p>
|
|
<?php endif; ?>
|
|
<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"><?php echo $editing ? 'Update tutorial' : 'Save tutorial'; ?></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'; ?>
|