LuminaWebsite/shop.php
Maaxxs 17dea6d41f Lumishop partial approval, Lumipunkte on dashboard and in sidebar box
Members may now request more than is in stock (or items that are sold
out); only their points have to cover the full request. When approving,
the manager can approve fewer than requested. The quantity field is
prefilled with what is in stock, the cost updates as you type, and only
the approved part is charged and taken from stock. The order keeps the
requested quantity, so the member sees e.g. "3 von 10 bewilligt". The
order list also shows the current stock and flags when it isn't enough.

The column upgrade check now covers every table that has upgrades,
instead of only users and roles. Tables that are about to be created
fresh are skipped.

The dashboard gets a card with your Lumipunkte and your rank. In the
sidebar, the points move from under your name into a gold box above the
profile.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-21 10:56:59 +02:00

167 lines
8.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
require_once __DIR__ . '/includes/bootstrap.php';
require_once __DIR__ . '/includes/shop.php';
require_permission('shop.view');
$canManage = user_has_permission('shop.manage');
$userId = (int) current_user()['id'];
$balance = contribution_user_balance($userId);
$reservedPoints = shop_reserved_points($userId);
$spendable = max(0, $balance - $reservedPoints);
$items = shop_items(true);
$categories = [];
foreach ($items as $item) {
if ($item['category_id'] !== null) {
$categories[(int) $item['category_id']] = (string) $item['category_name'];
}
}
$hasUncategorized = (bool) array_filter($items, static fn (array $i): bool => $i['category_id'] === null);
// Eigene Antraege
$perPage = 10;
$totalOrders = shop_order_count([], $userId);
$pageCount = max(1, (int) ceil($totalOrders / $perPage));
$page = min($pageCount, max(1, (int) ($_GET['page'] ?? 1)));
$myOrders = shop_orders([], $userId, $perPage, ($page - 1) * $perPage);
$pendingCount = $canManage ? shop_pending_count() : 0;
// Eingaben nach einem Fehler (siehe shop_action.php)
$old = $_SESSION['shop_form'] ?? [];
unset($_SESSION['shop_form']);
$pageTitle = 'Lumishop';
$containerWide = true;
include __DIR__ . '/includes/header.php';
?>
<div class="panel contribution-hero shop-hero">
<img class="contribution-hero-coin" src="/assets/images/lumipunkt.svg" alt="" width="72" height="72">
<div class="contribution-hero-text">
<h1>Lumishop</h1>
<p class="text-muted">Tausche deine Lumipunkte gegen Gegenstände aus dem Legionslager. Nach der Bewilligung bekommst du den Gegenstand im Spiel überreicht.</p>
</div>
<div class="contribution-hero-balance" title="Punktestand abzüglich offener Anträge">
<span class="text-muted">Verfügbar</span>
<strong><?= contribution_coin(26) ?> <?= contribution_format($spendable) ?></strong>
<?php if ($reservedPoints > 0): ?>
<span class="text-muted shop-reserved"><?= contribution_format($reservedPoints) ?> in offenen Anträgen vorgemerkt</span>
<?php endif; ?>
</div>
<?php if ($canManage): ?>
<div class="shop-hero-actions">
<a class="button" href="/shop_orders.php">Anträge<?php if ($pendingCount > 0): ?> <span class="shop-count"><?= $pendingCount ?></span><?php endif; ?></a>
<a class="button button-secondary" href="/shop_manage.php">Shop verwalten</a>
</div>
<?php endif; ?>
</div>
<div class="panel">
<div class="warehouse-filters">
<input type="search" id="shop-search" placeholder="Gegenstand suchen …" autocomplete="off">
<?php if (count($categories) + ($hasUncategorized ? 1 : 0) > 1): ?>
<select id="shop-category">
<option value="">Alle Kategorien</option>
<?php foreach ($categories as $id => $name): ?>
<option value="<?= (int) $id ?>"><?= e($name) ?></option>
<?php endforeach; ?>
<?php if ($hasUncategorized): ?>
<option value="0">Ohne Kategorie</option>
<?php endif; ?>
</select>
<?php endif; ?>
</div>
<?php if (empty($items)): ?>
<p class="text-muted">Im Lumishop gibt es gerade nichts.<?= $canManage ? ' Unter „Shop verwalten“ legst du Gegenstände an oder übernimmst sie aus dem Lagerhaus.' : '' ?></p>
<?php else: ?>
<div class="shop-grid" id="shop-items">
<?php foreach ($items as $item): ?>
<?php
$available = $item['available'];
$price = (int) $item['price'];
$soldOut = $available !== null && $available < 1;
$affordable = $price === 0 ? SHOP_MAX_ORDER_QUANTITY : intdiv($spendable, $price);
// Mehr als verfuegbar darf beantragt werden (die Bewilligung kann kuerzen) - nur die
// Punkte muessen reichen.
$max = min(SHOP_MAX_ORDER_QUANTITY, $affordable);
$reason = $max < 1 ? 'Nicht genug Lumipunkte' : '';
?>
<div class="shop-card" data-name="<?= e(mb_strtolower((string) $item['name'])) ?>" data-category="<?= (int) ($item['category_id'] ?? 0) ?>">
<span class="shop-card-image"><?= shop_icon_html($item, 72) ?></span>
<span class="shop-card-name"><?= e((string) $item['name']) ?></span>
<?php if ($item['category_name'] !== null): ?>
<span class="warehouse-card-category"><?= e((string) $item['category_name']) ?></span>
<?php endif; ?>
<?php if (!empty($item['description'])): ?>
<span class="shop-card-description"><?= nl2br(e((string) $item['description'])) ?></span>
<?php endif; ?>
<span class="shop-card-price"><?= contribution_coin(18) ?> <?= contribution_format($price) ?></span>
<span class="shop-card-stock<?= $soldOut ? ' is-empty' : '' ?>"><?= e(shop_format_availability($available)) ?></span>
<button type="button" class="button-small shop-buy" <?= $reason !== '' ? 'disabled title="' . e($reason) . '"' : '' ?>
data-item-id="<?= (int) $item['id'] ?>" data-item-name="<?= e((string) $item['name']) ?>"
data-price="<?= $price ?>" data-max="<?= max(0, $max) ?>">
<?= $reason !== '' ? e($reason) : 'Beantragen' ?>
</button>
</div>
<?php endforeach; ?>
</div>
<p class="text-muted warehouse-no-match" id="shop-no-match" hidden>Kein Gegenstand passt zur Suche.</p>
<?php endif; ?>
</div>
<div class="panel" id="antraege">
<h2>Meine Anträge</h2>
<?php if (empty($myOrders)): ?>
<p class="text-muted">Du hast noch nichts beantragt.</p>
<?php else: ?>
<div class="shop-orders">
<?php foreach ($myOrders as $order): ?>
<?php include __DIR__ . '/includes/shop_order_row.php'; ?>
<?php endforeach; ?>
</div>
<div class="warehouse-pages">
<span class="text-muted">
Anträge <?= ($page - 1) * $perPage + 1 ?>–<?= min($page * $perPage, $totalOrders) ?>
von <?= number_format($totalOrders, 0, ',', '.') ?>
</span>
<?php render_pagination('/shop.php?', $page, $pageCount, '#antraege'); ?>
</div>
<?php endif; ?>
</div>
<!-- Beantragen -->
<div class="modal-overlay" id="shop-buy-modal" <?= !empty($old) ? '' : 'hidden' ?>>
<div class="modal">
<button type="button" class="modal-close icon-button" title="Schließen"><?= icon('x') ?></button>
<h2>Beantragen</h2>
<form method="post" action="/shop_action.php">
<?= csrf_field() ?>
<input type="hidden" name="action" value="order">
<input type="hidden" name="item_id" id="shop-buy-item" value="<?= e((string) ($old['item_id'] ?? '')) ?>">
<p class="shop-buy-title"><strong id="shop-buy-name"><?= e((string) ($old['item_name'] ?? '')) ?></strong></p>
<div class="warehouse-row">
<div>
<label for="shop-buy-quantity">Anzahl</label>
<input type="number" id="shop-buy-quantity" name="quantity" min="1" required value="<?= e((string) ($old['quantity'] ?? '1')) ?>">
</div>
<div>
<label>Kosten</label>
<div class="shop-buy-total"><?= contribution_coin(20) ?> <span id="shop-buy-total">0</span></div>
</div>
</div>
<div>
<label for="shop-buy-note">Bemerkung <span class="text-muted">(optional, z.B. Name deines Charakters)</span></label>
<input type="text" id="shop-buy-note" name="note" maxlength="255" value="<?= e((string) ($old['note'] ?? '')) ?>">
</div>
<p class="text-muted shop-buy-hint">Du kannst auch mehr beantragen, als gerade verfügbar ist – dann wird eventuell nur ein Teil bewilligt. Die Lumipunkte werden erst bei der Bewilligung abgezogen, und nur für die bewilligte Anzahl. Bis dahin kannst du den Antrag zurückziehen.</p>
<div class="actions">
<button type="submit" class="button-success">Antrag stellen</button>
</div>
</form>
</div>
</div>
<script src="<?= e(asset_url('/assets/js/shop.js')) ?>"></script>
<?php include __DIR__ . '/includes/footer.php'; ?>