155 lines
5.6 KiB
PHP
155 lines
5.6 KiB
PHP
<?php
|
||
$moduleName = (string)($_GET['module'] ?? '');
|
||
$module = modules()->get($moduleName);
|
||
$error = null;
|
||
$notice = null;
|
||
|
||
require_admin();
|
||
|
||
if (!$module) {
|
||
http_response_code(404);
|
||
echo '<div class="card">Modul nicht gefunden.</div>';
|
||
return;
|
||
}
|
||
|
||
$fields = (array)($module['setup']['fields'] ?? []);
|
||
$fieldTypes = [];
|
||
$fieldMeta = [];
|
||
foreach ($fields as $field) {
|
||
$fname = (string)($field['name'] ?? '');
|
||
if ($fname === '') {
|
||
continue;
|
||
}
|
||
$fieldTypes[$fname] = (string)($field['type'] ?? 'text');
|
||
$fieldMeta[$fname] = $field;
|
||
}
|
||
$current = modules()->settings($moduleName);
|
||
$defaults = $module['db_defaults'] ?? [];
|
||
if (empty($current['db']) && is_array($defaults)) {
|
||
$current['db'] = $defaults;
|
||
}
|
||
|
||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||
$payload = [];
|
||
$db = $current['db'] ?? [];
|
||
|
||
foreach ($fields as $field) {
|
||
$name = (string)($field['name'] ?? '');
|
||
if ($name === '') {
|
||
continue;
|
||
}
|
||
$type = (string)($field['type'] ?? 'text');
|
||
$postKey = str_replace('.', '_', $name);
|
||
$value = $_POST[$postKey] ?? null;
|
||
|
||
if ($type === 'checkbox') {
|
||
$value = isset($_POST[$postKey]) ? '1' : '0';
|
||
}
|
||
if (is_array($value)) {
|
||
continue;
|
||
}
|
||
$value = is_string($value) ? trim($value) : $value;
|
||
|
||
if ($name === 'kea_auto_init') {
|
||
$payload[$name] = $value === '1';
|
||
continue;
|
||
}
|
||
|
||
if (str_starts_with($name, 'db.')) {
|
||
$key = substr($name, 3);
|
||
$db[$key] = $value;
|
||
continue;
|
||
}
|
||
|
||
$payload[$name] = $value;
|
||
}
|
||
|
||
if (!empty($db)) {
|
||
$payload['db'] = $db;
|
||
}
|
||
|
||
modules()->saveSettings($moduleName, $payload);
|
||
modules()->saveAuth($moduleName, [
|
||
'required' => isset($_POST['auth_required']),
|
||
'users' => (string)($_POST['auth_users'] ?? ''),
|
||
'groups' => (string)($_POST['auth_groups'] ?? ''),
|
||
]);
|
||
$notice = 'Setup gespeichert.';
|
||
$current = array_replace_recursive($current, $payload);
|
||
$module = modules()->get($moduleName) ?: $module;
|
||
}
|
||
$authConfig = is_array($module['auth'] ?? null) ? $module['auth'] : ['required' => false, 'users' => [], 'groups' => []];
|
||
?>
|
||
<div class="card">
|
||
<div class="pill">Setup</div>
|
||
<h1 style="margin-top:.75rem;"><?= e($module['title']) ?> – Einrichtung</h1>
|
||
<p class="muted">Trage die benötigten Informationen für das Modul ein.</p>
|
||
|
||
<?php if ($error): ?>
|
||
<div class="bg-red-900 border-l-4 border-red-500 text-red-100 p-4 mb-6" role="alert">
|
||
<?= e($error) ?>
|
||
</div>
|
||
<?php elseif ($notice): ?>
|
||
<div class="card" style="margin-top:1rem; border-color:var(--accent-2);">
|
||
<?= e($notice) ?>
|
||
</div>
|
||
<?php endif; ?>
|
||
|
||
<form method="post" style="margin-top:1rem; display:grid; gap:14px; max-width:520px;">
|
||
<?php foreach ($fields as $field): ?>
|
||
<?php
|
||
$name = (string)($field['name'] ?? '');
|
||
$label = (string)($field['label'] ?? $name);
|
||
$type = (string)($field['type'] ?? 'text');
|
||
$required = !empty($field['required']);
|
||
$help = (string)($field['help'] ?? $field['description'] ?? '');
|
||
|
||
$value = '';
|
||
if ($name === 'kea_auto_init') {
|
||
$value = !empty($current[$name]) ? '1' : '0';
|
||
} elseif (str_starts_with($name, 'db.')) {
|
||
$key = substr($name, 3);
|
||
$value = (string)($current['db'][$key] ?? '');
|
||
} else {
|
||
$value = (string)($current[$name] ?? '');
|
||
}
|
||
?>
|
||
<label class="muted" style="display:grid; gap:6px;">
|
||
<span><?= e($label) ?></span>
|
||
<?php if ($type === 'textarea'): ?>
|
||
<textarea name="<?= e($name) ?>" rows="3" <?= $required ? 'required' : '' ?>><?= e($value) ?></textarea>
|
||
<?php elseif ($type === 'checkbox'): ?>
|
||
<input type="checkbox" name="<?= e($name) ?>" value="1" <?= $value === '1' ? 'checked' : '' ?>>
|
||
<?php else: ?>
|
||
<input type="<?= e($type) ?>" name="<?= e($name) ?>" value="<?= e($value) ?>" <?= $required ? 'required' : '' ?>>
|
||
<?php endif; ?>
|
||
<?php if ($help !== ''): ?>
|
||
<small class="muted"><?= e($help) ?></small>
|
||
<?php endif; ?>
|
||
</label>
|
||
<?php endforeach; ?>
|
||
|
||
<div class="card" style="padding:14px; background:var(--panel-2); display:grid; gap:12px;">
|
||
<strong>Modulzugriff</strong>
|
||
<label class="muted" style="display:flex; align-items:center; gap:10px;">
|
||
<input type="checkbox" name="auth_required" value="1" <?= !empty($authConfig['required']) ? 'checked' : '' ?>>
|
||
<span>Login fuer dieses Modul erforderlich</span>
|
||
</label>
|
||
<label class="muted" style="display:grid; gap:6px;">
|
||
<span>Erlaubte Benutzer</span>
|
||
<textarea name="auth_users" rows="3" placeholder="Keycloak-Sub, Benutzername oder E-Mail, je Zeile oder Komma"><?= e(implode("\n", is_array($authConfig['users'] ?? null) ? $authConfig['users'] : [])) ?></textarea>
|
||
</label>
|
||
<label class="muted" style="display:grid; gap:6px;">
|
||
<span>Erlaubte Gruppen</span>
|
||
<textarea name="auth_groups" rows="3" placeholder="/admin oder mining-users, je Zeile oder Komma"><?= e(implode("\n", is_array($authConfig['groups'] ?? null) ? $authConfig['groups'] : [])) ?></textarea>
|
||
</label>
|
||
<small class="muted">Wenn Login aktiv ist und Benutzer/Gruppen leer bleiben, darf jeder eingeloggte Benutzer das Modul oeffnen.</small>
|
||
</div>
|
||
|
||
<div style="display:flex; gap:10px;">
|
||
<button class="cta-button" type="submit">Speichern</button>
|
||
<a class="nav-link" href="/modules">Zurück</a>
|
||
</div>
|
||
</form>
|
||
</div>
|