📄 downloads.php
📁 Path: //home/skhata/pimsnepal.com.np/admin/downloads.php
📊 Size: 5.16 KB
🔒 Perm: 0644
📝 MIME: text/x-php
<?php
require_once __DIR__ . '/../includes/functions.php';
requireAdmin();
require __DIR__ . '/includes/ui.php';
$pageTitle = 'Downloads';
if ($_SERVER['REQUEST_METHOD'] === 'POST' && csrfVerify($_POST['csrf_token'] ?? null)) {
$action = $_POST['action'] ?? '';
if ($action === 'save') {
$id = (int) ($_POST['id'] ?? 0);
$filePath = handleUpload('file', 'downloads');
$fields = [
trim($_POST['category'] ?? ''),
trim($_POST['title'] ?? ''),
trim($_POST['version'] ?? ''),
$_POST['release_date'] ?: null,
trim($_POST['file_size'] ?? ''),
isset($_POST['is_active']) ? 1 : 0,
];
if ($id > 0) {
if ($filePath) {
db()->prepare('UPDATE downloads SET category=?,title=?,version=?,release_date=?,file_size=?,is_active=?,file_path=? WHERE id=?')->execute([...$fields, $filePath, $id]);
} else {
db()->prepare('UPDATE downloads SET category=?,title=?,version=?,release_date=?,file_size=?,is_active=? WHERE id=?')->execute([...$fields, $id]);
}
} else {
db()->prepare('INSERT INTO downloads (category,title,version,release_date,file_size,is_active,file_path) VALUES (?,?,?,?,?,?,?)')->execute([...$fields, $filePath]);
}
flash('success', 'Download saved.');
} elseif ($action === 'delete') {
db()->prepare('DELETE FROM downloads WHERE id = ?')->execute([(int) ($_POST['id'] ?? 0)]);
flash('success', 'Download deleted.');
}
redirect('downloads.php');
}
require __DIR__ . '/includes/admin-header.php';
adminFlashBox();
$downloads = db()->query('SELECT * FROM downloads ORDER BY id DESC')->fetchAll();
$editId = (int) ($_GET['edit'] ?? 0);
$editing = null;
foreach ($downloads as $d) { if ((int) $d['id'] === $editId) $editing = $d; }
?>
<div class="grid lg:grid-cols-3 gap-6">
<div class="lg:col-span-2 bg-white rounded-2xl border border-gray-100 overflow-hidden">
<table class="w-full text-sm text-left">
<thead class="bg-gray-50 text-xs uppercase text-gray-500"><tr><th class="px-4 py-3">Title</th><th class="px-4 py-3">Category</th><th class="px-4 py-3">Version</th><th class="px-4 py-3">Downloads</th><th class="px-4 py-3"></th></tr></thead>
<tbody class="divide-y divide-gray-100">
<?php foreach ($downloads as $d): ?>
<tr>
<td class="px-4 py-3 font-medium"><?= e($d['title']) ?><?= $d['file_path'] ? '' : ' <span class="text-amber-600 text-xs">(no file uploaded)</span>' ?></td>
<td class="px-4 py-3 text-gray-500"><?= e($d['category']) ?></td>
<td class="px-4 py-3 text-gray-500"><?= e($d['version']) ?></td>
<td class="px-4 py-3 text-gray-500"><?= (int) $d['download_count'] ?></td>
<td class="px-4 py-3 text-right space-x-3">
<a href="?edit=<?= (int) $d['id'] ?>" class="text-primary font-semibold">Edit</a>
<form method="post" class="inline" onsubmit="return confirm('Delete this download?');">
<input type="hidden" name="csrf_token" value="<?= e(csrfToken()) ?>" />
<input type="hidden" name="action" value="delete" />
<input type="hidden" name="id" value="<?= (int) $d['id'] ?>" />
<button class="text-red-600 font-semibold">Delete</button>
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<div class="bg-white rounded-2xl border border-gray-100 p-6 h-fit">
<h2 class="font-heading font-semibold mb-4"><?= $editing ? 'Edit Download' : 'Add Download' ?></h2>
<form method="post" enctype="multipart/form-data">
<input type="hidden" name="csrf_token" value="<?= e(csrfToken()) ?>" />
<input type="hidden" name="action" value="save" />
<input type="hidden" name="id" value="<?= (int) ($editing['id'] ?? 0) ?>" />
<?php inputField('Title', 'title', $editing['title'] ?? '', 'text', true); ?>
<?php inputField('Category (TallyPrime, Educational Version, Updates, Brochures, PDF Manuals)', 'category', $editing['category'] ?? '', 'text', true); ?>
<?php inputField('Version', 'version', $editing['version'] ?? ''); ?>
<?php inputField('Release Date', 'release_date', $editing['release_date'] ?? '', 'date'); ?>
<?php inputField('File Size (e.g. 190 MB)', 'file_size', $editing['file_size'] ?? ''); ?>
<label class="block mb-4">
<span class="block text-sm font-medium text-gray-700 mb-1.5">File</span>
<?php if (!empty($editing['file_path'])): ?><p class="text-xs text-gray-500 mb-2">Current: <?= e($editing['file_path']) ?></p><?php endif; ?>
<input type="file" name="file" class="text-sm" />
</label>
<?php checkboxField('Active', 'is_active', $editing ? !empty($editing['is_active']) : true); ?>
<button class="btn-primary-brand bg-primary text-white px-5 py-2.5 rounded-lg font-semibold w-full"><?= $editing ? 'Update' : 'Add Download' ?></button>
<?php if ($editing): ?><a href="downloads.php" class="block text-center text-sm text-gray-500 mt-3">Cancel edit</a><?php endif; ?>
</form>
</div>
</div>
<?php require __DIR__ . '/includes/admin-footer.php'; ?>