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_restaurants.php

179 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/Restaurant.php';

$restaurantClass = new Restaurant($pdo);
$error '';
$success '';

// Handle Delete Restaurant
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['delete_restaurant_id'])) {
    
$delete_id = (int)$_POST['delete_restaurant_id'];
    try {
        
$restaurantClass->deleteRestaurant($delete_id);
        
$success "Restaurangen har tagits bort.";
    } catch (
Exception $e) {
        
$error "Ett fel uppstod vid borttagning av restaurangen.";
    }
}

// Handle Add Restaurant
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_restaurant'])) {
    
$name htmlspecialchars($_POST['restaurant_name']);
    
$location htmlspecialchars($_POST['location']);
    
$lat = (float)$_POST['lat'];
    
$lng = (float)$_POST['lng'];

    if (!empty(
$name) && !empty($location) && !empty($lat) && !empty($lng)) {
        try {
            
$restaurantClass->create($name$location$lat$lng);
            
$success "Restaurangen lades till!";
        } catch (
Exception $e) {
            
$error $e->getMessage();
        }
    } else {
        
$error "Fyll i alla fält med giltiga värden.";
    }
}

// Handle Update Restaurant
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['edit_restaurant'])) {
    
$id = (int)$_POST['edit_restaurant_id'];
    
$name htmlspecialchars($_POST['edit_restaurant_name']);
    
$location htmlspecialchars($_POST['edit_location']);
    
$lat = (float)$_POST['edit_lat'];
    
$lng = (float)$_POST['edit_lng'];

    if (!empty(
$id) && !empty($name) && !empty($location) && !empty($lat) && !empty($lng)) {
        try {
            
$restaurantClass->updateRestaurant($id$name$location$lat$lng);
            
$success "Restaurangen uppdaterades!";
        } catch (
Exception $e) {
            
$error $e->getMessage();
        }
    } else {
        
$error "Fyll i alla fält med giltiga värden för uppdatering.";
    }
}

$restaurants $restaurantClass->getAllWithStats();

$page_title 'Administrera Restauranger';
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 Restauranger</h1>
    
    <?php if ($error): ?>
        <div class="error-message"><?php echo htmlspecialchars($error); ?></div>
    <?php endif; ?>
    <?php if ($success): ?>
        <div class="success-message"><?php echo htmlspecialchars($success); ?></div>
    <?php endif; ?>

    <div style="background: #fff; padding: 20px; border-radius: 8px; box-shadow: 0 1px 3px rgba(0,0,0,0.1); margin-bottom: 30px;">
        <h2>Lägg till ny restaurang</h2>
        <form method="POST" style="display: flex; gap: 15px; align-items: flex-end; flex-wrap: wrap;">
            <input type="hidden" name="add_restaurant" value="1">
            <div style="flex: 2;">
                <label>Namn:</label>
                <input type="text" name="restaurant_name" required style="width: 100%; padding: 8px; border: 1px solid #ccc; border-radius: 4px;">
            </div>
            <div style="flex: 2;">
                <label>Adress:</label>
                <input type="text" name="location" required style="width: 100%; padding: 8px; border: 1px solid #ccc; border-radius: 4px;">
            </div>
            <div style="flex: 1;">
                <label>Latitud:</label>
                <input type="number" step="any" name="lat" required style="width: 100%; padding: 8px; border: 1px solid #ccc; border-radius: 4px;">
            </div>
            <div style="flex: 1;">
                <label>Longitud:</label>
                <input type="number" step="any" name="lng" required style="width: 100%; padding: 8px; border: 1px solid #ccc; border-radius: 4px;">
            </div>
            <div>
                <button type="submit" class="btn btn-primary" style="padding: 9px 20px;">Spara</button>
            </div>
        </form>
    </div>

    <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;">Adress</th>
                <th style="padding: 15px; text-align: center; border-bottom: 2px solid #dee2e6;">Antal besök (loggar)</th>
                <th style="padding: 15px; text-align: center; border-bottom: 2px solid #dee2e6;">Nyttighet (Snitt)</th>
                <th style="padding: 15px; text-align: center; border-bottom: 2px solid #dee2e6;">Glädje (Snitt)</th>
                <th style="padding: 15px; text-align: right; border-bottom: 2px solid #dee2e6;">Åtgärder</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($restaurants as $rest): ?>
            <tr>
                <td style="padding: 15px; border-bottom: 1px solid #e9ecef;"><b><?php echo htmlspecialchars($rest['restaurant_name']); ?></b></td>
                <td style="padding: 15px; border-bottom: 1px solid #e9ecef;"><?php echo htmlspecialchars($rest['location']); ?></td>
                <td style="padding: 15px; border-bottom: 1px solid #e9ecef; text-align: center;"><?php echo $rest['total_logs']; ?></td>
                <td style="padding: 15px; border-bottom: 1px solid #e9ecef; text-align: center;"><?php echo $rest['avg_health'] ? round($rest['avg_health'], 1) . ' / 5' '-'?></td>
                <td style="padding: 15px; border-bottom: 1px solid #e9ecef; text-align: center;"><?php echo $rest['avg_happy'] ? round($rest['avg_happy'], 1) . ' / 3' '-'?></td>
                <td style="padding: 15px; border-bottom: 1px solid #e9ecef; text-align: right;">
                    <button type="button" class="btn btn-secondary" onclick="document.getElementById('edit-form-<?php echo $rest['id']; ?>').style.display = 'table-row';" style="padding: 5px 10px; font-size: 0.9rem; margin-right: 5px;">Ändra</button>
                    <form method="POST" onsubmit="return confirm('Är du säker? Detta raderar även alla matloggar för denna restaurang om det är sammankopplat.');" style="display:inline;">
                        <input type="hidden" name="delete_restaurant_id" value="<?php echo $rest['id']; ?>">
                        <button type="submit" class="btn btn-danger" style="padding: 5px 10px; font-size: 0.9rem;">Ta bort</button>
                    </form>
                </td>
            </tr>
            <tr id="edit-form-<?php echo $rest['id']; ?>" style="display: none; background: #f8f9fa;">
                <td colspan="6" style="padding: 15px; border-bottom: 2px solid #ccc;">
                    <form method="POST" style="display: flex; gap: 10px; align-items: flex-end; flex-wrap: wrap;">
                        <input type="hidden" name="edit_restaurant" value="1">
                        <input type="hidden" name="edit_restaurant_id" value="<?php echo $rest['id']; ?>">
                        <div style="flex: 2;">
                            <label style="font-size: 0.85rem;">Namn:</label>
                            <input type="text" name="edit_restaurant_name" value="<?php echo htmlspecialchars($rest['restaurant_name']); ?>" required style="width: 100%; padding: 6px; border: 1px solid #ccc; border-radius: 4px;">
                        </div>
                        <div style="flex: 2;">
                            <label style="font-size: 0.85rem;">Adress:</label>
                            <input type="text" name="edit_location" value="<?php echo htmlspecialchars($rest['location']); ?>" required style="width: 100%; padding: 6px; border: 1px solid #ccc; border-radius: 4px;">
                        </div>
                        <div style="flex: 1;">
                            <label style="font-size: 0.85rem;">Lat:</label>
                            <input type="number" step="any" name="edit_lat" value="<?php echo htmlspecialchars($rest['lat'] ?? ''); ?>" required style="width: 100%; padding: 6px; border: 1px solid #ccc; border-radius: 4px;">
                        </div>
                        <div style="flex: 1;">
                            <label style="font-size: 0.85rem;">Lng:</label>
                            <input type="number" step="any" name="edit_lng" value="<?php echo htmlspecialchars($rest['lng'] ?? ''); ?>" required style="width: 100%; padding: 6px; border: 1px solid #ccc; border-radius: 4px;">
                        </div>
                        <div style="display: flex; gap: 10px; width: 100%;">
                            <button type="submit" class="btn btn-primary" style="flex: 1; padding: 7px 15px;">Spara</button>
                            <button type="button" class="btn btn-secondary" onclick="document.getElementById('edit-form-<?php echo $rest['id']; ?>').style.display = 'none';" style="flex: 1; padding: 7px 15px;">Avbryt</button>
                        </div>
                    </form>
                </td>
            </tr>
            <?php endforeach; ?>
            <?php if(empty($restaurants)): ?>
            <tr>
                <td colspan="6" style="padding: 15px; text-align: center;">Inga restauranger inlagda ännu.</td>
            </tr>
            <?php endif; ?>
        </tbody>
    </table>
</div>

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