tippspiel2027/tippspiel/src/Views/settings.php

222 lines
8.7 KiB
PHP
Raw Normal View History

2026-10-04 23:15:54 +02:00
<?php
$pageTitle = 'Einstellungen';
require __DIR__ . '/partials/header.php';
?>
<h1>Spielerdaten</h1>
<?php if ($message): ?>
<p style="color: green;"><?php echo htmlspecialchars($message); ?></p>
<?php endif; ?>
<?php if ($error): ?>
<p style="color: red;"><?php echo htmlspecialchars($error); ?></p>
<?php endif; ?>
<form method="post" action="/index.php?page=settings" class="settings-form">
<div class="form-group">
<label for="name">Login:</label>
<input type="text" id="name" value="<?php echo htmlspecialchars($user['name']); ?>" readonly>
</div>
<div class="form-group">
<label for="firstname">Vorname:</label>
<input type="text" id="firstname" value="<?php echo htmlspecialchars($user['firstname']); ?>" readonly>
</div>
<div class="form-group">
<label for="lastname">Nachname:</label>
<input type="text" id="lastname" value="<?php echo htmlspecialchars($user['lastname']); ?>" readonly>
</div>
<div class="form-group">
<label for="iban">IBAN:</label>
<input type="text" id="iban" name="iban" maxlength="34" value="<?php echo htmlspecialchars((string)$user['iban']); ?>">
</div>
<div class="form-group">
2026-10-27 12:59:45 +01:00
<label for="paypal">PayPal:</label>
<input type="text" id="paypal" name="paypal" maxlength="255" placeholder="E-Mail oder PayPal.Me-Link" value="<?php echo htmlspecialchars((string)$user['paypal']); ?>">
2026-10-04 23:15:54 +02:00
</div>
<div class="form-group">
<label for="mobile">Handynummer:</label>
<input type="text" id="mobile" name="mobile" value="<?php echo htmlspecialchars((string)$user['mobile']); ?>">
</div>
<div class="form-group">
<label for="email">Email:</label>
<input type="email" id="email" name="email" class="wide" required value="<?php echo htmlspecialchars((string)$user['email']); ?>">
</div>
<div class="form-group">
<label for="gets_reminders">Erinnerungen:</label>
<input type="checkbox" id="gets_reminders" name="gets_reminders" value="1" <?php if (!empty($user['gets_reminders'])) echo 'checked'; ?>>
<label for="gets_reminders" class="inline">Ich möchte E-Mail-Erinnerungen erhalten</label>
</div>
<?php // Nur in der installierten App sichtbar; das Skript unten blendet die Zeile ein. ?>
<div class="form-group" id="push-reminder-group" hidden
data-vapid-key="<?php echo htmlspecialchars((string)$pushPublicKey); ?>">
<label for="push_reminders">App-Erinnerung:</label>
<input type="checkbox" id="push_reminders" <?php if (!empty($pushEnabled)) echo 'checked'; ?>>
<label for="push_reminders" class="inline">Ich möchte Smartphone-Erinnerungen erhalten (App)</label>
<p class="settings-hint" id="push-reminder-hint"></p>
</div>
2026-10-04 23:15:54 +02:00
<div class="form-group">
<button type="submit">Ok</button>
</div>
</form>
<style>
.settings-form .form-group { margin-bottom: 0.8rem; }
.settings-form .form-group > label:first-child { display: inline-block; width: 160px; }
.settings-form label.inline { width: auto; }
.settings-form input[readonly] { background: #f4f4f4; color: #666; border: 1px solid #ccc; }
.settings-form input.wide { width: 25em; max-width: 100%; }
.settings-form button { margin-left: 164px; }
.settings-hint { margin: 0.3em 0 0 164px; font-size: 0.9em; color: #555; }
.settings-hint.is-error { color: #b00020; }
@media (max-width: 600px) {
.settings-form .form-group > label:first-child { width: auto; display: block; }
.settings-form button { margin-left: 0; }
.settings-hint { margin-left: 0; }
}
2026-10-04 23:15:54 +02:00
</style>
<script>
(function () {
var group = document.getElementById('push-reminder-group');
var box = document.getElementById('push_reminders');
var hint = document.getElementById('push-reminder-hint');
if (!group || !box || !hint) {
return;
}
var vapidKey = group.getAttribute('data-vapid-key') || '';
var isStandalone = window.matchMedia('(display-mode: standalone)').matches
|| window.navigator.standalone === true;
var supported = 'serviceWorker' in navigator && 'PushManager' in window && 'Notification' in window;
// Die Checkbox ist ausdrücklich nur für die installierte App gedacht.
if (!isStandalone || !supported || !vapidKey) {
return;
}
group.hidden = false;
function setHint(text, isError) {
hint.textContent = text || '';
hint.className = 'settings-hint' + (isError ? ' is-error' : '');
}
function keyToBytes(base64url) {
var padding = new Array((4 - (base64url.length % 4)) % 4 + 1).join('=');
var base64 = (base64url + padding).replace(/-/g, '+').replace(/_/g, '/');
var raw = window.atob(base64);
var bytes = new Uint8Array(raw.length);
for (var i = 0; i < raw.length; i++) {
bytes[i] = raw.charCodeAt(i);
}
return bytes;
}
function post(page, payload) {
return fetch('/index.php?page=' + page, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
credentials: 'same-origin',
body: JSON.stringify(payload || {})
}).then(function (response) {
if (!response.ok) {
throw new Error('Server meldet HTTP ' + response.status + '.');
}
return response.json();
});
}
function createSubscription(registration) {
return registration.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: keyToBytes(vapidKey)
});
}
function subscribe() {
return navigator.serviceWorker.ready.then(function (registration) {
return registration.pushManager.getSubscription().then(function (existing) {
if (existing) {
return existing;
}
return createSubscription(registration).catch(function (error) {
// Abo mit einem alten VAPID-Schlüssel: erneuern.
if (error && error.name === 'InvalidStateError') {
return registration.pushManager.getSubscription().then(function (old) {
return old ? old.unsubscribe() : null;
}).then(function () {
return createSubscription(registration);
});
}
throw error;
});
});
});
}
box.addEventListener('change', function () {
if (box.disabled) {
return;
}
box.disabled = true;
setHint('');
var action;
if (box.checked) {
action = Notification.requestPermission().then(function (permission) {
if (permission !== 'granted') {
throw new Error('Benachrichtigungen sind blockiert. Bitte in den Systemeinstellungen erlauben.');
}
return subscribe();
}).then(function (subscription) {
if (!subscription) {
throw new Error('Abo konnte nicht angelegt werden.');
}
return post('push_subscribe', subscription.toJSON());
}).then(function () {
setHint('Erinnerungen erscheinen jetzt auf diesem Gerät.');
});
} else {
action = navigator.serviceWorker.ready.then(function (registration) {
return registration.pushManager.getSubscription();
}).then(function (subscription) {
return subscription ? subscription.unsubscribe() : null;
}).then(function () {
return post('push_unsubscribe', {});
}).then(function () {
setHint('Smartphone-Erinnerungen sind ausgeschaltet.');
});
}
action.catch(function (error) {
if (box.checked) {
box.checked = false;
}
setHint(error && error.message ? error.message : 'Aktion fehlgeschlagen.', true);
}).then(function () {
box.disabled = false;
});
});
// Beim Öffnen das Gerät still nachtragen, wenn Erinnerungen gewünscht sind
// und die Erlaubnis schon erteilt ist (z. B. zweites Gerät). Bewusst kein
// automatisches Abmelden: Auf einem geteilten Gerät kann das Abo zu einem
// anderen Konto gehören.
if (box.checked && Notification.permission === 'granted') {
subscribe().then(function (subscription) {
if (subscription) {
return post('push_subscribe', subscription.toJSON());
}
}).catch(function () {
// still: die Einstellungsseite bleibt nutzbar
});
}
})();
</script>
2026-10-04 23:15:54 +02:00
<?php require __DIR__ . '/partials/footer.php'; ?>