94 lines
3.7 KiB
PHP
94 lines
3.7 KiB
PHP
<?php
|
|
session_start();
|
|
require_once 'db/config.php';
|
|
|
|
// Ensure user is admin
|
|
if (!isset($_SESSION['user_id']) || !isset($_SESSION['user_role']) || $_SESSION['user_role'] !== 'admin') {
|
|
header("Location: index.php");
|
|
exit;
|
|
}
|
|
|
|
$pdo = db();
|
|
$message = '';
|
|
|
|
// Handle approve/reject actions
|
|
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['application_id'])) {
|
|
$application_id = $_POST['application_id'];
|
|
$user_id = $_POST['user_id'];
|
|
|
|
if (isset($_POST['approve'])) {
|
|
// Update application status
|
|
$stmt = $pdo->prepare("UPDATE writer_applications SET status = 'approved' WHERE id = ?");
|
|
$stmt->execute([$application_id]);
|
|
|
|
// Update user role
|
|
$stmt = $pdo->prepare("UPDATE users SET role = 'writer' WHERE id = ?");
|
|
$stmt->execute([$user_id]);
|
|
|
|
$message = '<div class="alert alert-success">Application approved. User is now a writer.</div>';
|
|
} elseif (isset($_POST['reject'])) {
|
|
// Update application status
|
|
$stmt = $pdo->prepare("UPDATE writer_applications SET status = 'rejected' WHERE id = ?");
|
|
$stmt->execute([$application_id]);
|
|
|
|
$message = '<div class="alert alert-info">Application rejected.</div>';
|
|
}
|
|
}
|
|
|
|
// Fetch pending applications
|
|
$stmt = $pdo->prepare("SELECT wa.*, u.username FROM writer_applications wa JOIN users u ON wa.user_id = u.id WHERE wa.status = 'pending'");
|
|
$stmt->execute();
|
|
$applications = $stmt->fetchAll();
|
|
|
|
?>
|
|
<?php require_once 'header.php'; ?>
|
|
|
|
<h2>Admin Panel - Writer Applications</h2>
|
|
|
|
<?php echo $message; ?>
|
|
|
|
<p>To test this page, you need to manually set a user's role to 'admin' in your database. For example: <br><code>UPDATE users SET role = 'admin' WHERE id = 1;</code></p>
|
|
|
|
<div class="card">
|
|
<div class="card-header">Pending Applications</div>
|
|
<div class="card-body">
|
|
<table class="table">
|
|
<thead>
|
|
<tr>
|
|
<th>Username</th>
|
|
<th>Bio</th>
|
|
<th>Action</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php if (empty($applications)): ?>
|
|
<tr>
|
|
<td colspan="3">No pending applications.</td>
|
|
</tr>
|
|
<?php else: ?>
|
|
<?php foreach ($applications as $app): ?>
|
|
<tr>
|
|
<td><?php echo htmlspecialchars($app['username']); ?></td>
|
|
<td><?php echo htmlspecialchars($app['bio']); ?></td>
|
|
<td>
|
|
<form action="admin.php" method="post" style="display: inline-block;">
|
|
<input type="hidden" name="application_id" value="<?php echo $app['id']; ?>">
|
|
<input type="hidden" name="user_id" value="<?php echo $app['user_id']; ?>">
|
|
<button type="submit" name="approve" class="btn btn-success btn-sm">Approve</button>
|
|
</form>
|
|
<form action="admin.php" method="post" style="display: inline-block;">
|
|
<input type="hidden" name="application_id" value="<?php echo $app['id']; ?>">
|
|
<input type="hidden" name="user_id" value="<?php echo $app['user_id']; ?>">
|
|
<button type="submit" name="reject" class="btn btn-danger btn-sm">Reject</button>
|
|
</form>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
<?php endif; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
|
|
<?php require_once 'footer.php'; ?>
|