|
@@ -0,0 +1,563 @@
|
|
|
|
|
+<?php
|
|
|
|
|
+
|
|
|
|
|
+declare(strict_types=1);
|
|
|
|
|
+
|
|
|
|
|
+require_once dirname(__DIR__, 2) . '/src/bootstrap.php';
|
|
|
|
|
+
|
|
|
|
|
+$auth = app_admin_auth();
|
|
|
|
|
+$auth->start();
|
|
|
|
|
+$configRepository = app_config_repository();
|
|
|
|
|
+$message = null;
|
|
|
|
|
+$messageType = 'success';
|
|
|
|
|
+
|
|
|
|
|
+if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
|
|
|
+ $action = $_POST['action'] ?? '';
|
|
|
|
|
+
|
|
|
|
|
+ if ($action === 'login') {
|
|
|
|
|
+ $success = $auth->login(
|
|
|
|
|
+ trim((string) ($_POST['username'] ?? '')),
|
|
|
|
|
+ (string) ($_POST['password'] ?? '')
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ if ($success) {
|
|
|
|
|
+ app_redirect('/admin/');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $message = 'Login fehlgeschlagen. Bitte Zugangsdaten pruefen.';
|
|
|
|
|
+ $messageType = 'error';
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if ($action === 'save_config' && $auth->isAuthenticated()) {
|
|
|
|
|
+ $config = $configRepository->getConfig();
|
|
|
|
|
+ $newPassword = trim((string) ($_POST['admin_password'] ?? ''));
|
|
|
|
|
+ $config['app'] = [
|
|
|
|
|
+ 'name' => trim((string) ($_POST['app_name'] ?? 'Getraenkeautomat Monitor')),
|
|
|
|
|
+ 'timezone' => trim((string) ($_POST['timezone'] ?? 'Europe/Berlin')),
|
|
|
|
|
+ 'dashboard_refresh_seconds' => max(5, (int) ($_POST['dashboard_refresh_seconds'] ?? 15)),
|
|
|
|
|
+ 'default_from_email' => trim((string) ($_POST['default_from_email'] ?? 'monitor@example.local')),
|
|
|
|
|
+ ];
|
|
|
|
|
+ $config['api'] = [
|
|
|
|
|
+ 'bearer_token' => trim((string) ($_POST['bearer_token'] ?? 'change-me-token')),
|
|
|
|
|
+ ];
|
|
|
|
|
+ $config['admin'] = [
|
|
|
|
|
+ 'username' => trim((string) ($_POST['admin_username'] ?? 'admin')),
|
|
|
|
|
+ 'password_hash' => $newPassword !== ''
|
|
|
|
|
+ ? password_hash($newPassword, PASSWORD_BCRYPT)
|
|
|
|
|
+ : (string) ($config['admin']['password_hash'] ?? ''),
|
|
|
|
|
+ ];
|
|
|
|
|
+ $config['alerts'] = [
|
|
|
|
|
+ 'webhooks' => normalizeWebhooks($_POST['webhooks'] ?? []),
|
|
|
|
|
+ 'emails' => normalizeEmails($_POST['emails'] ?? []),
|
|
|
|
|
+ ];
|
|
|
|
|
+ $config['machines'] = normalizeMachines($_POST['machines'] ?? []);
|
|
|
|
|
+
|
|
|
|
|
+ $configRepository->saveConfig($config);
|
|
|
|
|
+ $message = 'Konfiguration gespeichert.';
|
|
|
|
|
+ $messageType = 'success';
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+$config = $configRepository->getConfig();
|
|
|
|
|
+
|
|
|
|
|
+if (!$auth->isAuthenticated()) {
|
|
|
|
|
+ renderLogin($message, $messageType);
|
|
|
|
|
+ exit;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+renderAdmin($config, $message, $messageType);
|
|
|
|
|
+
|
|
|
|
|
+function renderLogin(?string $message, string $messageType): void
|
|
|
|
|
+{
|
|
|
|
|
+ ?>
|
|
|
|
|
+ <!DOCTYPE html>
|
|
|
|
|
+ <html lang="de">
|
|
|
|
|
+ <head>
|
|
|
|
|
+ <meta charset="UTF-8">
|
|
|
|
|
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
|
|
+ <title>Adminpanel Login</title>
|
|
|
|
|
+ <link rel="stylesheet" href="/styles.css">
|
|
|
|
|
+ </head>
|
|
|
|
|
+ <body>
|
|
|
|
|
+ <main class="auth-page">
|
|
|
|
|
+ <section class="auth-card">
|
|
|
|
|
+ <p class="eyebrow">Adminbereich</p>
|
|
|
|
|
+ <h1>Konfiguration sichern</h1>
|
|
|
|
|
+ <p>Logge dich mit den statischen Zugangsdaten aus der JSON-Config ein.</p>
|
|
|
|
|
+ <?php if ($message !== null): ?>
|
|
|
|
|
+ <div class="message message--<?= htmlspecialchars($messageType, ENT_QUOTES) ?>"><?= htmlspecialchars($message, ENT_QUOTES) ?></div>
|
|
|
|
|
+ <?php endif; ?>
|
|
|
|
|
+ <form method="post">
|
|
|
|
|
+ <input type="hidden" name="action" value="login">
|
|
|
|
|
+ <label>
|
|
|
|
|
+ Benutzername
|
|
|
|
|
+ <input type="text" name="username" required>
|
|
|
|
|
+ </label>
|
|
|
|
|
+ <label>
|
|
|
|
|
+ Passwort
|
|
|
|
|
+ <input type="password" name="password" required>
|
|
|
|
|
+ </label>
|
|
|
|
|
+ <button class="button button--primary" type="submit">Einloggen</button>
|
|
|
|
|
+ <a class="button button--ghost" href="/">Zurueck zum Dashboard</a>
|
|
|
|
|
+ </form>
|
|
|
|
|
+ </section>
|
|
|
|
|
+ </main>
|
|
|
|
|
+ </body>
|
|
|
|
|
+ </html>
|
|
|
|
|
+ <?php
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function renderAdmin(array $config, ?string $message, string $messageType): void
|
|
|
|
|
+{
|
|
|
|
|
+ ?>
|
|
|
|
|
+ <!DOCTYPE html>
|
|
|
|
|
+ <html lang="de">
|
|
|
|
|
+ <head>
|
|
|
|
|
+ <meta charset="UTF-8">
|
|
|
|
|
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
|
|
+ <title>Adminpanel</title>
|
|
|
|
|
+ <link rel="stylesheet" href="/styles.css">
|
|
|
|
|
+ </head>
|
|
|
|
|
+ <body>
|
|
|
|
|
+ <main class="admin-page">
|
|
|
|
|
+ <section class="admin-shell">
|
|
|
|
|
+ <div class="admin-header">
|
|
|
|
|
+ <div>
|
|
|
|
|
+ <p class="eyebrow">Adminpanel</p>
|
|
|
|
|
+ <h1>Konfiguration der Automaten</h1>
|
|
|
|
|
+ <p>Hier werden API-Token, Zugangsdaten, Faecher und Alarmwege direkt in der JSON-Config gepflegt.</p>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ <div class="inline-actions">
|
|
|
|
|
+ <a class="button button--ghost" href="/">Dashboard</a>
|
|
|
|
|
+ <a class="button button--secondary" href="/admin/logout.php">Logout</a>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ </div>
|
|
|
|
|
+
|
|
|
|
|
+ <?php if ($message !== null): ?>
|
|
|
|
|
+ <div class="message message--<?= htmlspecialchars($messageType, ENT_QUOTES) ?>"><?= htmlspecialchars($message, ENT_QUOTES) ?></div>
|
|
|
|
|
+ <?php endif; ?>
|
|
|
|
|
+
|
|
|
|
|
+ <form method="post" class="admin-form" id="admin-form">
|
|
|
|
|
+ <input type="hidden" name="action" value="save_config">
|
|
|
|
|
+
|
|
|
|
|
+ <section class="form-section">
|
|
|
|
|
+ <h2>Allgemein</h2>
|
|
|
|
|
+ <div class="field-grid">
|
|
|
|
|
+ <label>
|
|
|
|
|
+ App-Name
|
|
|
|
|
+ <input type="text" name="app_name" value="<?= htmlspecialchars((string) ($config['app']['name'] ?? ''), ENT_QUOTES) ?>" required>
|
|
|
|
|
+ </label>
|
|
|
|
|
+ <label>
|
|
|
|
|
+ Zeitzone
|
|
|
|
|
+ <input type="text" name="timezone" value="<?= htmlspecialchars((string) ($config['app']['timezone'] ?? 'Europe/Berlin'), ENT_QUOTES) ?>" required>
|
|
|
|
|
+ </label>
|
|
|
|
|
+ <label>
|
|
|
|
|
+ Auto-Refresh Sekunden
|
|
|
|
|
+ <input type="number" min="5" name="dashboard_refresh_seconds" value="<?= (int) ($config['app']['dashboard_refresh_seconds'] ?? 15) ?>" required>
|
|
|
|
|
+ </label>
|
|
|
|
|
+ <label>
|
|
|
|
|
+ Absender fuer Email
|
|
|
|
|
+ <input type="email" name="default_from_email" value="<?= htmlspecialchars((string) ($config['app']['default_from_email'] ?? ''), ENT_QUOTES) ?>">
|
|
|
|
|
+ </label>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ </section>
|
|
|
|
|
+
|
|
|
|
|
+ <section class="admin-grid">
|
|
|
|
|
+ <section class="form-section">
|
|
|
|
|
+ <h2>API</h2>
|
|
|
|
|
+ <label>
|
|
|
|
|
+ Bearer-Token
|
|
|
|
|
+ <input type="text" name="bearer_token" value="<?= htmlspecialchars((string) ($config['api']['bearer_token'] ?? ''), ENT_QUOTES) ?>" required>
|
|
|
|
|
+ </label>
|
|
|
|
|
+ <p class="field-help">ESP32-Clients senden dieses Token im Header <code>Authorization: Bearer ...</code>.</p>
|
|
|
|
|
+ </section>
|
|
|
|
|
+
|
|
|
|
|
+ <section class="form-section">
|
|
|
|
|
+ <h2>Adminzugang</h2>
|
|
|
|
|
+ <div class="field-grid">
|
|
|
|
|
+ <label>
|
|
|
|
|
+ Benutzername
|
|
|
|
|
+ <input type="text" name="admin_username" value="<?= htmlspecialchars((string) ($config['admin']['username'] ?? ''), ENT_QUOTES) ?>" required>
|
|
|
|
|
+ </label>
|
|
|
|
|
+ <label>
|
|
|
|
|
+ Neues Passwort
|
|
|
|
|
+ <input type="password" name="admin_password" placeholder="Leer lassen, um es nicht zu aendern">
|
|
|
|
|
+ </label>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ </section>
|
|
|
|
|
+ </section>
|
|
|
|
|
+
|
|
|
|
|
+ <section class="form-section">
|
|
|
|
|
+ <div class="panel__header">
|
|
|
|
|
+ <div>
|
|
|
|
|
+ <h2>Webhooks</h2>
|
|
|
|
|
+ <p>Mehrere Ziele sind moeglich. Header koennen als JSON hinterlegt werden.</p>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ <button class="button button--secondary" type="button" data-add="webhook">Webhook hinzufuegen</button>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ <div class="repeat-stack" id="webhook-stack">
|
|
|
|
|
+ <?php foreach (($config['alerts']['webhooks'] ?? []) as $index => $webhook): ?>
|
|
|
|
|
+ <?= renderWebhookRow((int) $index, $webhook) ?>
|
|
|
|
|
+ <?php endforeach; ?>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ </section>
|
|
|
|
|
+
|
|
|
|
|
+ <section class="form-section">
|
|
|
|
|
+ <div class="panel__header">
|
|
|
|
|
+ <div>
|
|
|
|
|
+ <h2>Email-Empfaenger</h2>
|
|
|
|
|
+ <p>Emails werden ueber die Serverkonfiguration von <code>mail()</code> versendet.</p>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ <button class="button button--secondary" type="button" data-add="email">Email-Empfaenger hinzufuegen</button>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ <div class="repeat-stack" id="email-stack">
|
|
|
|
|
+ <?php foreach (($config['alerts']['emails'] ?? []) as $index => $email): ?>
|
|
|
|
|
+ <?= renderEmailRow((int) $index, $email) ?>
|
|
|
|
|
+ <?php endforeach; ?>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ </section>
|
|
|
|
|
+
|
|
|
|
|
+ <section class="form-section">
|
|
|
|
|
+ <div class="panel__header">
|
|
|
|
|
+ <div>
|
|
|
|
|
+ <h2>Automaten und Faecher</h2>
|
|
|
|
|
+ <p>Jeder Automat enthaelt beliebig viele Faecher, die jeweils genau einem Sensor zugeordnet sind.</p>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ <button class="button button--secondary" type="button" data-add="machine">Automat hinzufuegen</button>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ <div class="repeat-stack" id="machine-stack">
|
|
|
|
|
+ <?php foreach (($config['machines'] ?? []) as $machineIndex => $machine): ?>
|
|
|
|
|
+ <?= renderMachineRow((int) $machineIndex, $machine) ?>
|
|
|
|
|
+ <?php endforeach; ?>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ </section>
|
|
|
|
|
+
|
|
|
|
|
+ <div class="section-actions">
|
|
|
|
|
+ <button class="button button--primary" type="submit">Konfiguration speichern</button>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ </form>
|
|
|
|
|
+ </section>
|
|
|
|
|
+ </main>
|
|
|
|
|
+
|
|
|
|
|
+ <template id="tpl-webhook"><?= renderWebhookRow('__INDEX__', []) ?></template>
|
|
|
|
|
+ <template id="tpl-email"><?= renderEmailRow('__INDEX__', []) ?></template>
|
|
|
|
|
+ <template id="tpl-machine"><?= renderMachineRow('__MACHINE__', []) ?></template>
|
|
|
|
|
+ <template id="tpl-slot"><?= renderSlotRow('__MACHINE__', '__SLOT__', []) ?></template>
|
|
|
|
|
+
|
|
|
|
|
+ <script>
|
|
|
|
|
+ const webhookStack = document.getElementById('webhook-stack');
|
|
|
|
|
+ const emailStack = document.getElementById('email-stack');
|
|
|
|
|
+ const machineStack = document.getElementById('machine-stack');
|
|
|
|
|
+
|
|
|
|
|
+ const renderTemplate = (id, replacements) => {
|
|
|
|
|
+ let html = document.getElementById(id).innerHTML;
|
|
|
|
|
+ Object.entries(replacements).forEach(([needle, value]) => {
|
|
|
|
|
+ html = html.replaceAll(needle, value);
|
|
|
|
|
+ });
|
|
|
|
|
+ return html;
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ const countChildren = (node, selector) => node.querySelectorAll(selector).length;
|
|
|
|
|
+
|
|
|
|
|
+ document.addEventListener('click', (event) => {
|
|
|
|
|
+ const addType = event.target.getAttribute('data-add');
|
|
|
|
|
+ const removeType = event.target.getAttribute('data-remove');
|
|
|
|
|
+
|
|
|
|
|
+ if (addType === 'webhook') {
|
|
|
|
|
+ const index = countChildren(webhookStack, '[data-webhook-row]');
|
|
|
|
|
+ webhookStack.insertAdjacentHTML('beforeend', renderTemplate('tpl-webhook', { '__INDEX__': index }));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (addType === 'email') {
|
|
|
|
|
+ const index = countChildren(emailStack, '[data-email-row]');
|
|
|
|
|
+ emailStack.insertAdjacentHTML('beforeend', renderTemplate('tpl-email', { '__INDEX__': index }));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (addType === 'machine') {
|
|
|
|
|
+ const index = countChildren(machineStack, '[data-machine-row]');
|
|
|
|
|
+ machineStack.insertAdjacentHTML('beforeend', renderTemplate('tpl-machine', { '__MACHINE__': index, '__SLOT__': 0 }));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (addType === 'slot') {
|
|
|
|
|
+ const machineIndex = event.target.getAttribute('data-machine-index');
|
|
|
|
|
+ const container = document.querySelector(`[data-slot-stack="${machineIndex}"]`);
|
|
|
|
|
+ const slotIndex = countChildren(container, '[data-slot-row]');
|
|
|
|
|
+ container.insertAdjacentHTML('beforeend', renderTemplate('tpl-slot', {
|
|
|
|
|
+ '__MACHINE__': machineIndex,
|
|
|
|
|
+ '__SLOT__': slotIndex
|
|
|
|
|
+ }));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (removeType) {
|
|
|
|
|
+ const row = event.target.closest(`[data-${removeType}-row]`);
|
|
|
|
|
+ if (row) {
|
|
|
|
|
+ row.remove();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ </script>
|
|
|
|
|
+ </body>
|
|
|
|
|
+ </html>
|
|
|
|
|
+ <?php
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function renderWebhookRow(int|string $index, array $webhook): string
|
|
|
|
|
+{
|
|
|
|
|
+ $headers = $webhook['headers'] ?? [];
|
|
|
|
|
+ $headerJson = json_encode($headers, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
|
|
|
|
|
+
|
|
|
|
|
+ ob_start();
|
|
|
|
|
+ ?>
|
|
|
|
|
+ <div class="repeat-card" data-webhook-row>
|
|
|
|
|
+ <div class="panel__header">
|
|
|
|
|
+ <h4>Webhook</h4>
|
|
|
|
|
+ <button class="button button--danger" type="button" data-remove="webhook">Entfernen</button>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ <div class="field-grid">
|
|
|
|
|
+ <label>
|
|
|
|
|
+ ID
|
|
|
|
|
+ <input type="text" name="webhooks[<?= $index ?>][id]" value="<?= htmlspecialchars((string) ($webhook['id'] ?? ''), ENT_QUOTES) ?>">
|
|
|
|
|
+ </label>
|
|
|
|
|
+ <label>
|
|
|
|
|
+ Name
|
|
|
|
|
+ <input type="text" name="webhooks[<?= $index ?>][name]" value="<?= htmlspecialchars((string) ($webhook['name'] ?? ''), ENT_QUOTES) ?>">
|
|
|
|
|
+ </label>
|
|
|
|
|
+ <label>
|
|
|
|
|
+ URL
|
|
|
|
|
+ <input type="url" name="webhooks[<?= $index ?>][url]" value="<?= htmlspecialchars((string) ($webhook['url'] ?? ''), ENT_QUOTES) ?>">
|
|
|
|
|
+ </label>
|
|
|
|
|
+ <label>
|
|
|
|
|
+ Aktiv
|
|
|
|
|
+ <select name="webhooks[<?= $index ?>][enabled]">
|
|
|
|
|
+ <option value="1" <?= !empty($webhook['enabled']) ? 'selected' : '' ?>>Ja</option>
|
|
|
|
|
+ <option value="0" <?= empty($webhook['enabled']) ? 'selected' : '' ?>>Nein</option>
|
|
|
|
|
+ </select>
|
|
|
|
|
+ </label>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ <label>
|
|
|
|
|
+ Header als JSON-Objekt
|
|
|
|
|
+ <textarea name="webhooks[<?= $index ?>][headers_json]"><?= htmlspecialchars((string) $headerJson, ENT_QUOTES) ?></textarea>
|
|
|
|
|
+ </label>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ <?php
|
|
|
|
|
+ return (string) ob_get_clean();
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function renderEmailRow(int|string $index, array $email): string
|
|
|
|
|
+{
|
|
|
|
|
+ ob_start();
|
|
|
|
|
+ ?>
|
|
|
|
|
+ <div class="repeat-card" data-email-row>
|
|
|
|
|
+ <div class="panel__header">
|
|
|
|
|
+ <h4>Email-Empfaenger</h4>
|
|
|
|
|
+ <button class="button button--danger" type="button" data-remove="email">Entfernen</button>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ <div class="field-grid">
|
|
|
|
|
+ <label>
|
|
|
|
|
+ ID
|
|
|
|
|
+ <input type="text" name="emails[<?= $index ?>][id]" value="<?= htmlspecialchars((string) ($email['id'] ?? ''), ENT_QUOTES) ?>">
|
|
|
|
|
+ </label>
|
|
|
|
|
+ <label>
|
|
|
|
|
+ Name
|
|
|
|
|
+ <input type="text" name="emails[<?= $index ?>][name]" value="<?= htmlspecialchars((string) ($email['name'] ?? ''), ENT_QUOTES) ?>">
|
|
|
|
|
+ </label>
|
|
|
|
|
+ <label>
|
|
|
|
|
+ Adresse
|
|
|
|
|
+ <input type="email" name="emails[<?= $index ?>][address]" value="<?= htmlspecialchars((string) ($email['address'] ?? ''), ENT_QUOTES) ?>">
|
|
|
|
|
+ </label>
|
|
|
|
|
+ <label>
|
|
|
|
|
+ Aktiv
|
|
|
|
|
+ <select name="emails[<?= $index ?>][enabled]">
|
|
|
|
|
+ <option value="1" <?= !empty($email['enabled']) ? 'selected' : '' ?>>Ja</option>
|
|
|
|
|
+ <option value="0" <?= empty($email['enabled']) ? 'selected' : '' ?>>Nein</option>
|
|
|
|
|
+ </select>
|
|
|
|
|
+ </label>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ <?php
|
|
|
|
|
+ return (string) ob_get_clean();
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function renderMachineRow(int|string $machineIndex, array $machine): string
|
|
|
|
|
+{
|
|
|
|
|
+ ob_start();
|
|
|
|
|
+ ?>
|
|
|
|
|
+ <div class="repeat-card" data-machine-row>
|
|
|
|
|
+ <div class="panel__header">
|
|
|
|
|
+ <div>
|
|
|
|
|
+ <h4>Automat</h4>
|
|
|
|
|
+ <p>Faecher koennen direkt darunter verwaltet werden.</p>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ <div class="inline-actions">
|
|
|
|
|
+ <button class="button button--secondary" type="button" data-add="slot" data-machine-index="<?= htmlspecialchars((string) $machineIndex, ENT_QUOTES) ?>">Fach hinzufuegen</button>
|
|
|
|
|
+ <button class="button button--danger" type="button" data-remove="machine">Automat entfernen</button>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ <div class="field-grid">
|
|
|
|
|
+ <label>
|
|
|
|
|
+ ID
|
|
|
|
|
+ <input type="text" name="machines[<?= $machineIndex ?>][id]" value="<?= htmlspecialchars((string) ($machine['id'] ?? ''), ENT_QUOTES) ?>">
|
|
|
|
|
+ </label>
|
|
|
|
|
+ <label>
|
|
|
|
|
+ Name
|
|
|
|
|
+ <input type="text" name="machines[<?= $machineIndex ?>][name]" value="<?= htmlspecialchars((string) ($machine['name'] ?? ''), ENT_QUOTES) ?>">
|
|
|
|
|
+ </label>
|
|
|
|
|
+ <label>
|
|
|
|
|
+ Standort
|
|
|
|
|
+ <input type="text" name="machines[<?= $machineIndex ?>][location]" value="<?= htmlspecialchars((string) ($machine['location'] ?? ''), ENT_QUOTES) ?>">
|
|
|
|
|
+ </label>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ <div class="repeat-stack" data-slot-stack="<?= htmlspecialchars((string) $machineIndex, ENT_QUOTES) ?>">
|
|
|
|
|
+ <?php foreach (($machine['slots'] ?? []) as $slotIndex => $slot): ?>
|
|
|
|
|
+ <?= renderSlotRow($machineIndex, (int) $slotIndex, $slot) ?>
|
|
|
|
|
+ <?php endforeach; ?>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ <?php
|
|
|
|
|
+ return (string) ob_get_clean();
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function renderSlotRow(int|string $machineIndex, int|string $slotIndex, array $slot): string
|
|
|
|
|
+{
|
|
|
|
|
+ $webhookIds = implode(',', $slot['webhook_ids'] ?? []);
|
|
|
|
|
+ $emailIds = implode(',', $slot['email_ids'] ?? []);
|
|
|
|
|
+
|
|
|
|
|
+ ob_start();
|
|
|
|
|
+ ?>
|
|
|
|
|
+ <div class="slot-editor" data-slot-row>
|
|
|
|
|
+ <div class="panel__header">
|
|
|
|
|
+ <h5>Fach</h5>
|
|
|
|
|
+ <button class="button button--danger" type="button" data-remove="slot">Fach entfernen</button>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ <div class="field-grid">
|
|
|
|
|
+ <label>
|
|
|
|
|
+ Sensor-ID
|
|
|
|
|
+ <input type="text" name="machines[<?= $machineIndex ?>][slots][<?= $slotIndex ?>][sensor_id]" value="<?= htmlspecialchars((string) ($slot['sensor_id'] ?? ''), ENT_QUOTES) ?>">
|
|
|
|
|
+ </label>
|
|
|
|
|
+ <label>
|
|
|
|
|
+ Label
|
|
|
|
|
+ <input type="text" name="machines[<?= $machineIndex ?>][slots][<?= $slotIndex ?>][label]" value="<?= htmlspecialchars((string) ($slot['label'] ?? ''), ENT_QUOTES) ?>">
|
|
|
|
|
+ </label>
|
|
|
|
|
+ <label>
|
|
|
|
|
+ Produktname
|
|
|
|
|
+ <input type="text" name="machines[<?= $machineIndex ?>][slots][<?= $slotIndex ?>][product_name]" value="<?= htmlspecialchars((string) ($slot['product_name'] ?? ''), ENT_QUOTES) ?>">
|
|
|
|
|
+ </label>
|
|
|
|
|
+ <label>
|
|
|
|
|
+ Voll-Distanz in mm
|
|
|
|
|
+ <input type="number" step="0.01" name="machines[<?= $machineIndex ?>][slots][<?= $slotIndex ?>][full_distance_mm]" value="<?= htmlspecialchars((string) ($slot['full_distance_mm'] ?? ''), ENT_QUOTES) ?>">
|
|
|
|
|
+ </label>
|
|
|
|
|
+ <label>
|
|
|
|
|
+ Leer-Distanz in mm
|
|
|
|
|
+ <input type="number" step="0.01" name="machines[<?= $machineIndex ?>][slots][<?= $slotIndex ?>][empty_distance_mm]" value="<?= htmlspecialchars((string) ($slot['empty_distance_mm'] ?? ''), ENT_QUOTES) ?>">
|
|
|
|
|
+ </label>
|
|
|
|
|
+ <label>
|
|
|
|
|
+ Distanz pro Flasche
|
|
|
|
|
+ <input type="number" step="0.01" name="machines[<?= $machineIndex ?>][slots][<?= $slotIndex ?>][distance_per_unit]" value="<?= htmlspecialchars((string) ($slot['distance_per_unit'] ?? ''), ENT_QUOTES) ?>">
|
|
|
|
|
+ </label>
|
|
|
|
|
+ <label>
|
|
|
|
|
+ Alarm unter Bestand
|
|
|
|
|
+ <input type="number" step="1" min="0" name="machines[<?= $machineIndex ?>][slots][<?= $slotIndex ?>][alert_below_units]" value="<?= htmlspecialchars((string) ($slot['alert_below_units'] ?? ''), ENT_QUOTES) ?>">
|
|
|
|
|
+ </label>
|
|
|
|
|
+ <label>
|
|
|
|
|
+ Webhook-IDs
|
|
|
|
|
+ <input type="text" name="machines[<?= $machineIndex ?>][slots][<?= $slotIndex ?>][webhook_ids]" value="<?= htmlspecialchars((string) $webhookIds, ENT_QUOTES) ?>" placeholder="ops,lager">
|
|
|
|
|
+ </label>
|
|
|
|
|
+ <label>
|
|
|
|
|
+ Email-IDs
|
|
|
|
|
+ <input type="text" name="machines[<?= $machineIndex ?>][slots][<?= $slotIndex ?>][email_ids]" value="<?= htmlspecialchars((string) $emailIds, ENT_QUOTES) ?>" placeholder="lager,service">
|
|
|
|
|
+ </label>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ <?php
|
|
|
|
|
+ return (string) ob_get_clean();
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function normalizeWebhooks(array $rows): array
|
|
|
|
|
+{
|
|
|
|
|
+ $items = [];
|
|
|
|
|
+ foreach ($rows as $row) {
|
|
|
|
|
+ $id = trim((string) ($row['id'] ?? ''));
|
|
|
|
|
+ $url = trim((string) ($row['url'] ?? ''));
|
|
|
|
|
+ if ($id === '' && $url === '') {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $headers = json_decode((string) ($row['headers_json'] ?? '{}'), true);
|
|
|
|
|
+ $items[] = [
|
|
|
|
|
+ 'id' => $id,
|
|
|
|
|
+ 'name' => trim((string) ($row['name'] ?? '')),
|
|
|
|
|
+ 'url' => $url,
|
|
|
|
|
+ 'enabled' => (string) ($row['enabled'] ?? '1') === '1',
|
|
|
|
|
+ 'headers' => is_array($headers) ? $headers : [],
|
|
|
|
|
+ ];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return array_values($items);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function normalizeEmails(array $rows): array
|
|
|
|
|
+{
|
|
|
|
|
+ $items = [];
|
|
|
|
|
+ foreach ($rows as $row) {
|
|
|
|
|
+ $id = trim((string) ($row['id'] ?? ''));
|
|
|
|
|
+ $address = trim((string) ($row['address'] ?? ''));
|
|
|
|
|
+ if ($id === '' && $address === '') {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $items[] = [
|
|
|
|
|
+ 'id' => $id,
|
|
|
|
|
+ 'name' => trim((string) ($row['name'] ?? '')),
|
|
|
|
|
+ 'address' => $address,
|
|
|
|
|
+ 'enabled' => (string) ($row['enabled'] ?? '1') === '1',
|
|
|
|
|
+ ];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return array_values($items);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function normalizeMachines(array $rows): array
|
|
|
|
|
+{
|
|
|
|
|
+ $items = [];
|
|
|
|
|
+ foreach ($rows as $row) {
|
|
|
|
|
+ $id = trim((string) ($row['id'] ?? ''));
|
|
|
|
|
+ $name = trim((string) ($row['name'] ?? ''));
|
|
|
|
|
+ if ($id === '' && $name === '') {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $slots = [];
|
|
|
|
|
+ foreach (($row['slots'] ?? []) as $slot) {
|
|
|
|
|
+ $sensorId = trim((string) ($slot['sensor_id'] ?? ''));
|
|
|
|
|
+ if ($sensorId === '') {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $slots[] = [
|
|
|
|
|
+ 'sensor_id' => $sensorId,
|
|
|
|
|
+ 'label' => trim((string) ($slot['label'] ?? $sensorId)),
|
|
|
|
|
+ 'product_name' => trim((string) ($slot['product_name'] ?? '')),
|
|
|
|
|
+ 'full_distance_mm' => (float) ($slot['full_distance_mm'] ?? 0),
|
|
|
|
|
+ 'empty_distance_mm' => (float) ($slot['empty_distance_mm'] ?? 0),
|
|
|
|
|
+ 'distance_per_unit' => max(0.01, (float) ($slot['distance_per_unit'] ?? 1)),
|
|
|
|
|
+ 'alert_below_units' => max(0, (int) ($slot['alert_below_units'] ?? 0)),
|
|
|
|
|
+ 'webhook_ids' => parseIdList((string) ($slot['webhook_ids'] ?? '')),
|
|
|
|
|
+ 'email_ids' => parseIdList((string) ($slot['email_ids'] ?? '')),
|
|
|
|
|
+ ];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $items[] = [
|
|
|
|
|
+ 'id' => $id,
|
|
|
|
|
+ 'name' => $name,
|
|
|
|
|
+ 'location' => trim((string) ($row['location'] ?? '')),
|
|
|
|
|
+ 'slots' => $slots,
|
|
|
|
|
+ ];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return array_values($items);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function parseIdList(string $value): array
|
|
|
|
|
+{
|
|
|
|
|
+ $parts = array_map('trim', explode(',', $value));
|
|
|
|
|
+ return array_values(array_filter($parts, static fn (string $item): bool => $item !== ''));
|
|
|
|
|
+}
|