318 lines
15 KiB
PHP
318 lines
15 KiB
PHP
<?php
|
|
$current = auth_check() ? user() : null;
|
|
$flashError = flash_pull('error');
|
|
$pushPublicKey = $pushPublicKey ?? null;
|
|
$pushEnabled = $pushEnabled ?? false;
|
|
$isOwnProfile = $current !== null && (int) $current->id === (int) $user->id;
|
|
?>
|
|
|
|
<div class="container text-center text-light mt-5">
|
|
<?php if ($flashError !== null): ?>
|
|
<div class="alert alert-danger" role="alert"><?= e($flashError) ?></div>
|
|
<?php endif; ?>
|
|
|
|
<h1 class="text-center"><?= $isOwnProfile ? 'Einstellungen' : e($user->name) ?></h1>
|
|
<?php if ($isOwnProfile): ?>
|
|
<p class="text-center text-muted mb-4"><?= e($user->name) ?></p>
|
|
<?php endif; ?>
|
|
|
|
<form style="background-color: #fff;" class="text-dark" method="post" action="/member/<?= e($user->id) ?>"
|
|
enctype="multipart/form-data">
|
|
<?= csrf_field() ?>
|
|
<input type="hidden" name="_method" value="PUT">
|
|
<input type="hidden" name="id" value="<?= e($user->id) ?>">
|
|
|
|
<div class="form-group" style="display:flex;">
|
|
<label class="ml-2 mt-4" style="width:150px; text-align:left;" for="name"><h4>Name:</h4></label>
|
|
<input class="mt-3 mr-2 form-control" type="text" id="name" style="font-size:20px;" name="name"
|
|
value="<?= e(old('name') ?? $user->name) ?>" required>
|
|
</div>
|
|
<div class="form-group" style="display:flex;">
|
|
<label class="ml-2 mt-4" style="width:150px; text-align:left;" for="mobile"><h4>Telefonnummer:</h4></label>
|
|
<input type="text" class="mt-3 mr-2 form-control" id="mobile" style="font-size:20px;" name="mobile"
|
|
value="<?= e(old('mobile') ?? $user->mobile) ?>" required>
|
|
</div>
|
|
<div class="form-group" style="display:flex;">
|
|
<label class="ml-2 mt-4" style="width:150px; text-align:left;" for="email"><h4>EMail:</h4></label>
|
|
<input type="text" class="mt-3 mr-2 form-control" id="email" style="font-size:20px;" name="email"
|
|
value="<?= e(old('email') ?? $user->email) ?>" required>
|
|
</div>
|
|
|
|
<div class="form-group" style="display:flex;">
|
|
<label class="ml-2 mt-4" style="width:150px; text-align:left;" for="bild"><h4>Bild:</h4></label>
|
|
|
|
<?php if (file_exists(storage_path('img_storage/user/' . $user->id . '.jpg'))): ?>
|
|
<a href="/img_storage/user/<?= e($user->id) ?>_gross.jpg" data-lightbox="<?= e($user->id) ?>_gross.jpg"
|
|
data-title="<?= e($user->name) ?>">
|
|
<img src="/img_storage/user/<?= e($user->id) ?>.jpg" id="bild" class="mt-3 ml-3"
|
|
style="width:256px;height:256px">
|
|
</a>
|
|
<?php else: ?>
|
|
<img src="/img/placeholder/user64x96.jpg" id="bild" class="mt-3 ml-3">
|
|
<?php endif; ?>
|
|
|
|
<input type="file" class="mt-3 mr-2 form-control" style="font-size:20px;" name="bild" value=""
|
|
accept="image/*" onchange="preview_image(event)">
|
|
</div>
|
|
|
|
<?php if ($current && $current->isAdmin()): ?>
|
|
<div class="form-group" style="display:flex;">
|
|
<label class="ml-2 mt-4" style="width:130px; text-align:left;" for="role"><h4>Rolle:</h4></label>
|
|
|
|
<select name="role" class="dropdown-item ml-3 border form-control mt-3 mr-2" style="font-size:18px;">
|
|
<option value="GUEST" style="font-size:18px;" class="dropdown-item bg-light form-control" <?= $user->role === 'GUEST' ? 'selected' : '' ?>>GUEST</option>
|
|
<option value="MEMBER" style="font-size:18px;" class="dropdown-item bg-light" <?= $user->role === 'MEMBER' ? 'selected' : '' ?>>MEMBER</option>
|
|
<option value="TOUR GUIDE" style="font-size:18px;" class="dropdown-item bg-light" <?= $user->role === 'TOUR GUIDE' ? 'selected' : '' ?>>TOUR GUIDE</option>
|
|
<option value="ADMIN" style="font-size:18px;" class="dropdown-item bg-light" <?= $user->role === 'ADMIN' ? 'selected' : '' ?>>ADMIN</option>
|
|
</select>
|
|
</div>
|
|
<?php else: ?>
|
|
<div class="form-group" style="display:flex;">
|
|
<label class="ml-2 mt-4" style="width:130px; text-align:left;" for="role"><h4>Rolle:</h4></label>
|
|
<h4 class="mt-4"><?= e($user->role) ?></h4>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<a href="/member" class="btn btn-primary col-2 mb-2 float-left" style="margin-right:20px;">Zurück</a>
|
|
|
|
<?php if ($current && ($current->isAdmin() || (int) $current->id === (int) $user->id)): ?>
|
|
<button type="submit" class="btn btn-danger col-2 mb-2 float-right mr-2">Speichern</button>
|
|
<button type="submit" form="destroy-user-form" class="btn btn-primary col-2 mb-2 float-left"
|
|
style="margin-right:20px;">Löschen</button>
|
|
<?php endif; ?>
|
|
</form>
|
|
|
|
<form id="destroy-user-form" action="/admin/destroy_user/<?= e($user->id) ?>" method="POST" style="display:none;">
|
|
<?= csrf_field() ?>
|
|
</form>
|
|
|
|
<?php if ($isOwnProfile && $pushPublicKey !== null && !is_desktop()): ?>
|
|
<div class="card text-dark mt-4 text-left" id="push-settings" style="clear: both;"
|
|
data-vapid-key="<?= e($pushPublicKey) ?>"
|
|
data-push-enabled="<?= $pushEnabled ? '1' : '0' ?>">
|
|
<div class="card-body">
|
|
<h2 class="h5 mb-2">Benachrichtigungen (App)</h2>
|
|
<p class="text-muted mb-3">
|
|
Diese Einstellung ist nur auf dem Smartphone sichtbar. Du erhältst eine
|
|
Nachricht, wenn eine neue Tour veröffentlicht oder eine Tour abgesagt wird.
|
|
</p>
|
|
|
|
<div class="form-check mb-2">
|
|
<input class="form-check-input" type="checkbox" id="push-events" <?= $pushEnabled ? 'checked' : '' ?>>
|
|
<label class="form-check-label" for="push-events">
|
|
Bei neuen Touren und Absagen benachrichtigen
|
|
</label>
|
|
</div>
|
|
|
|
<p class="small text-muted mb-2" id="push-hint"></p>
|
|
|
|
<button type="button" class="btn btn-sm btn-outline-primary" id="push-test" <?= $pushEnabled ? '' : 'hidden' ?>>
|
|
Test-Nachricht senden
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
(function () {
|
|
var group = document.getElementById('push-settings');
|
|
var box = document.getElementById('push-events');
|
|
var hint = document.getElementById('push-hint');
|
|
var testButton = document.getElementById('push-test');
|
|
|
|
if (!group || !box || !hint || !testButton) {
|
|
return;
|
|
}
|
|
|
|
var vapidKey = group.getAttribute('data-vapid-key') || '';
|
|
var supported = 'serviceWorker' in navigator && 'PushManager' in window && 'Notification' in window;
|
|
var isStandalone = window.matchMedia('(display-mode: standalone)').matches
|
|
|| window.navigator.standalone === true;
|
|
|
|
function setHint(text, isError) {
|
|
hint.textContent = text || '';
|
|
hint.className = 'small mb-2 ' + (isError ? 'text-danger' : 'text-muted');
|
|
}
|
|
|
|
function syncTestButton() {
|
|
testButton.hidden = !box.checked;
|
|
}
|
|
|
|
if (!vapidKey) {
|
|
box.disabled = true;
|
|
testButton.hidden = true;
|
|
setHint('Benachrichtigungen sind auf diesem Server noch nicht eingerichtet.', true);
|
|
return;
|
|
}
|
|
|
|
if (!supported) {
|
|
box.disabled = true;
|
|
testButton.hidden = true;
|
|
setHint(
|
|
isStandalone
|
|
? 'Dieser Browser unterstützt keine Push-Benachrichtigungen.'
|
|
: 'Push-Benachrichtigungen brauchen einen unterstützten Browser. Auf dem iPhone: Seite in Safari öffnen, „Teilen“ → „Zum Home-Bildschirm“ wählen und die App vom Startbildschirm aus öffnen.',
|
|
true
|
|
);
|
|
return;
|
|
}
|
|
|
|
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(path, payload) {
|
|
var token = document.querySelector('meta[name="csrf-token"]');
|
|
var headers = {'Content-Type': 'application/json'};
|
|
|
|
if (token) {
|
|
headers['X-CSRF-Token'] = token.getAttribute('content');
|
|
}
|
|
|
|
return fetch(path, {
|
|
method: 'POST',
|
|
headers: headers,
|
|
credentials: 'same-origin',
|
|
body: JSON.stringify(payload || {})
|
|
}).then(function (response) {
|
|
return response.json().catch(function () {
|
|
return {};
|
|
}).then(function (data) {
|
|
if (!response.ok) {
|
|
throw new Error(data && data.error ? data.error : 'Server meldet HTTP ' + response.status + '.');
|
|
}
|
|
|
|
return data;
|
|
});
|
|
});
|
|
}
|
|
|
|
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('Benachrichtigungen sind jetzt auf diesem Gerät aktiv.');
|
|
});
|
|
} 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('Benachrichtigungen 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;
|
|
syncTestButton();
|
|
});
|
|
});
|
|
|
|
testButton.addEventListener('click', function () {
|
|
if (testButton.disabled) {
|
|
return;
|
|
}
|
|
|
|
testButton.disabled = true;
|
|
setHint('Test-Nachricht wird gesendet …');
|
|
|
|
navigator.serviceWorker.ready.then(function (registration) {
|
|
return registration.pushManager.getSubscription();
|
|
}).then(function (subscription) {
|
|
if (!subscription) {
|
|
throw new Error('Dieses Gerät hat noch kein Abo. Bitte den Haken aus- und wieder einschalten.');
|
|
}
|
|
|
|
return post('/push/test', {endpoint: subscription.endpoint});
|
|
}).then(function () {
|
|
setHint('Test-Nachricht gesendet. Sie sollte jetzt auf diesem Gerät erscheinen.');
|
|
}).catch(function (error) {
|
|
setHint(error && error.message ? error.message : 'Test fehlgeschlagen.', true);
|
|
}).then(function () {
|
|
testButton.disabled = false;
|
|
});
|
|
});
|
|
|
|
syncTestButton();
|
|
})();
|
|
</script>
|
|
<?php endif; ?>
|
|
</div>
|
|
|
|
<script>
|
|
function preview_image(event) {
|
|
var reader = new FileReader();
|
|
reader.onload = function () {
|
|
var output = document.getElementById('bild');
|
|
output.src = reader.result;
|
|
}
|
|
reader.readAsDataURL(event.target.files[0]);
|
|
}
|
|
</script>
|