Webbserver - Love Blomberg

Show sourcecode

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

public_html/smartkortet/pages/

hem.php
historik.php
installningar.php
restauranger.php
statistik.php

historik.php

106 lines UTF-8 Windows (CRLF)
<?php

declare(strict_types=1);

require_once 
__DIR__ '/../includes/data.php';

$userId = (int) $user['id'];
$selectedMonth normalizeYearMonth($_POST['month'] ?? ($_GET['month'] ?? null));

if (
$_SERVER['REQUEST_METHOD'] === 'POST') {
    
verifyCsrf();
    
$action $_POST['action'] ?? '';

    if (
$action === 'update_entry') {
        
$entryId = (int) ($_POST['entry_id'] ?? 0);
        
$amount parseAmount($_POST['amount'] ?? '0');
        
$spentOn normalizeIsoDate($_POST['spent_on'] ?? null'');
        
$placeName normalizeNullableString($_POST['place_name'] ?? null);

        if (
$entryId <= || $amount || $amount 90 || $spentOn === '') {
            
setFlash('error''Ogiltiga värden. Kontrollera datum och belopp (0-90 kr).');
        } else {
            
updateSpendEntry($userId$entryId$amount$spentOn$placeName);
            
setFlash('success''Historikpost uppdaterad.');
        }

        
redirect('/index.php?tab=historik&month=' rawurlencode($selectedMonth));
    }

    if (
$action === 'delete_entry') {
        
$entryId = (int) ($_POST['entry_id'] ?? 0);

        if (
$entryId <= 0) {
            
setFlash('error''Ogiltig historikpost.');
        } else {
            
deleteSpendEntry($userId$entryId);
            
setFlash('success''Historikpost raderad.');
        }

        
redirect('/index.php?tab=historik&month=' rawurlencode($selectedMonth));
    }
}

[
$year$month] = array_map('intval'explode('-'$selectedMonth));
$rows getHistoryEntriesForMonth($userId$year$month);
$flash getFlash();
?>
<article class="card panel">
    <div class="row-between">
        <h3>Historik (redigera / radera)</h3>
        <form method="get" class="inline-form">
            <input type="hidden" name="tab" value="historik">
            <input type="month" name="month" value="<?= e($selectedMonth?>">
            <button class="btn btn-secondary" type="submit">Visa</button>
        </form>
    </div>

    <?php if ($flash): ?>
        <div class="notice <?= e($flash['type']) ?>"><?= e($flash['message']) ?></div>
    <?php endif; ?>

    <?php if (!$rows): ?>
        <p class="subtitle">Ingen historik för vald månad ännu.</p>
    <?php else: ?>
        <div class="history-list">
            <?php foreach ($rows as $row): ?>
                <article class="history-item history-entry">
                    <form method="post" class="history-edit-form">
                        <input type="hidden" name="csrf_token" value="<?= e(csrfToken()) ?>">
                        <input type="hidden" name="action" value="update_entry">
                        <input type="hidden" name="entry_id" value="<?= e((string) $row['id']) ?>">
                        <input type="hidden" name="month" value="<?= e($selectedMonth?>">

                        <label class="field">
                            <span>Datum</span>
                            <input type="date" name="spent_on" value="<?= e((string) $row['spent_on']) ?>" required>
                        </label>

                        <label class="field">
                            <span>Belopp (0-90)</span>
                            <input type="number" name="amount" min="0" max="90" step="0.01" value="<?= e((string) $row['amount']) ?>" required>
                        </label>

                        <label class="field">
                            <span>Restaurang / butik</span>
                            <input type="text" name="place_name" value="<?= e((string) $row['place_name']) ?>" placeholder="Valfritt">
                        </label>

                        <div class="history-actions">
                            <button class="btn btn-secondary" type="submit">Spara</button>
                        </div>
                    </form>

                    <form method="post" class="history-delete-form" onsubmit="return confirm('Vill du radera denna post?');">
                        <input type="hidden" name="csrf_token" value="<?= e(csrfToken()) ?>">
                        <input type="hidden" name="action" value="delete_entry">
                        <input type="hidden" name="entry_id" value="<?= e((string) $row['id']) ?>">
                        <input type="hidden" name="month" value="<?= e($selectedMonth?>">
                        <button class="btn btn-danger" type="submit">Radera</button>
                    </form>
                </article>
            <?php endforeach; ?>
        </div>
    <?php endif; ?>
</article>