Webbserverprogrammering 1

Show sourcecode

The following files exists in this folder. Click to view.

webbserverprogrammering/submissions/projekt-matkort-handler/admin/

admin_dashboard.php
admin_restaurants.php
admin_users.php

admin_users.php

81 lines UTF-8 Windows (CRLF)
<?php
session_start
();
$base_path '../';

if (!isset(
$_SESSION['is_admin']) || $_SESSION['is_admin'] !== true) {
    
header("Location: ../index.php");
    exit();
}

include_once 
'../config/database.php';
include_once 
'../classes/User.php';

// Handle Delete User
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['delete_user_id'])) {
    
$delete_id = (int)$_POST['delete_user_id'];
    try {
        
User::deleteUser($pdo$delete_id);
    } catch (
Exception $e) {
        
$error "Ett fel uppstod vid borttagning av användaren.";
    }
}

$users User::getAdminUserStats($pdo);

$page_title 'Administrera Användare';
require_once 
'../includes/header.php';
?>

<div class="container">
    <div style="margin-bottom: 20px;">
        <a href="admin_dashboard.php" class="btn" style="background-color: transparent; border: 1px solid #fff; color: #fff;">Tillbaka till Dashboard</a>
    </div>

    <h1>Administrera Användare</h1>
    
    <?php if (isset($error)): ?>
        <div class="error-message"><?php echo htmlspecialchars($error); ?></div>
    <?php endif; ?>

    <table style="width: 100%; border-collapse: collapse; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,0.1); border-radius: 8px; overflow: hidden;">
        <thead style="background: #f8f9fa;">
            <tr>
                <th style="padding: 15px; text-align: left; border-bottom: 2px solid #dee2e6;">Namn</th>
                <th style="padding: 15px; text-align: left; border-bottom: 2px solid #dee2e6;">E-post</th>
                <th style="padding: 15px; text-align: center; border-bottom: 2px solid #dee2e6;">Saldo Kvar</th>
                <th style="padding: 15px; text-align: center; border-bottom: 2px solid #dee2e6;">Matloggar</th>
                <th style="padding: 15px; text-align: left; border-bottom: 2px solid #dee2e6;">Mest Besökt</th>
                <th style="padding: 15px; text-align: center; border-bottom: 2px solid #dee2e6;">Snitt Nyttig</th>
                <th style="padding: 15px; text-align: center; border-bottom: 2px solid #dee2e6;">Snitt Nöjd</th>
                <th style="padding: 15px; text-align: right; border-bottom: 2px solid #dee2e6;">Åtgärder</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($users as $user): ?>
            <tr>
                <td style="padding: 15px; border-bottom: 1px solid #e9ecef;"><?php echo htmlspecialchars($user['name']); ?></td>
                <td style="padding: 15px; border-bottom: 1px solid #e9ecef;"><?php echo htmlspecialchars($user['email']); ?></td>
                <td style="padding: 15px; border-bottom: 1px solid #e9ecef; text-align: center; font-weight: bold; color: #28a745;"><?php echo number_format($user['current_balance'], 0','' '); ?> kr</td>
                <td style="padding: 15px; border-bottom: 1px solid #e9ecef; text-align: center;"><?php echo $user['total_logs']; ?></td>
                <td style="padding: 15px; border-bottom: 1px solid #e9ecef; text-align: left;"><?php echo $user['most_visited_restaurant'] ? htmlspecialchars($user['most_visited_restaurant']) : '-'?></td>
                <td style="padding: 15px; border-bottom: 1px solid #e9ecef; text-align: center;"><?php echo $user['avg_healthy'] ? round($user['avg_healthy'], 1) . ' / 5' '-'?></td>
                <td style="padding: 15px; border-bottom: 1px solid #e9ecef; text-align: center;"><?php echo $user['avg_happy'] ? round($user['avg_happy'], 1) . ' / 3' '-'?></td>
                <td style="padding: 15px; border-bottom: 1px solid #e9ecef; text-align: right;">
                    <form method="POST" onsubmit="return confirm('Är du säker på att du vill ta bort <?php echo htmlspecialchars($user['name']); ?>? Detta kan inte ångras.');" style="display:inline;">
                        <input type="hidden" name="delete_user_id" value="<?php echo $user['id']; ?>">
                        <button type="submit" class="btn btn-danger" style="padding: 5px 10px; font-size: 0.9rem;">Ta bort</button>
                    </form>
                </td>
            </tr>
            <?php endforeach; ?>
            <?php if(empty($users)): ?>
            <tr>
                <td colspan="8" style="padding: 15px; text-align: center;">Inga elever registrerade ännu.</td>
            </tr>
            <?php endif; ?>
        </tbody>
    </table>
</div>

<?php require_once '../includes/footer.php'?>