index.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. <?php
  2. declare(strict_types=1);
  3. require_once dirname(__DIR__, 2) . '/src/bootstrap.php';
  4. $auth = app_admin_auth();
  5. $auth->start();
  6. $configRepository = app_config_repository();
  7. $message = null;
  8. $messageType = 'success';
  9. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  10. $action = $_POST['action'] ?? '';
  11. if ($action === 'login') {
  12. $success = $auth->login(
  13. trim((string) ($_POST['username'] ?? '')),
  14. (string) ($_POST['password'] ?? '')
  15. );
  16. if ($success) {
  17. app_redirect('/admin/');
  18. }
  19. $message = 'Login fehlgeschlagen. Bitte Zugangsdaten pruefen.';
  20. $messageType = 'error';
  21. }
  22. if ($action === 'save_config' && $auth->isAuthenticated()) {
  23. $config = $configRepository->getConfig();
  24. $newPassword = trim((string) ($_POST['admin_password'] ?? ''));
  25. $config['app'] = [
  26. 'name' => trim((string) ($_POST['app_name'] ?? 'Getraenkeautomat Monitor')),
  27. 'timezone' => trim((string) ($_POST['timezone'] ?? 'Europe/Berlin')),
  28. 'dashboard_refresh_seconds' => max(5, (int) ($_POST['dashboard_refresh_seconds'] ?? 15)),
  29. 'default_from_email' => trim((string) ($_POST['default_from_email'] ?? 'monitor@example.local')),
  30. ];
  31. $config['api'] = [
  32. 'bearer_token' => trim((string) ($_POST['bearer_token'] ?? 'change-me-token')),
  33. ];
  34. $config['admin'] = [
  35. 'username' => trim((string) ($_POST['admin_username'] ?? 'admin')),
  36. 'password_hash' => $newPassword !== ''
  37. ? password_hash($newPassword, PASSWORD_BCRYPT)
  38. : (string) ($config['admin']['password_hash'] ?? ''),
  39. ];
  40. $config['alerts'] = [
  41. 'webhooks' => normalizeWebhooks($_POST['webhooks'] ?? []),
  42. 'emails' => normalizeEmails($_POST['emails'] ?? []),
  43. ];
  44. $config['machines'] = normalizeMachines($_POST['machines'] ?? []);
  45. $configRepository->saveConfig($config);
  46. $message = 'Konfiguration gespeichert.';
  47. $messageType = 'success';
  48. }
  49. }
  50. $config = $configRepository->getConfig();
  51. if (!$auth->isAuthenticated()) {
  52. renderLogin($message, $messageType);
  53. exit;
  54. }
  55. renderAdmin($config, $message, $messageType);
  56. function renderLogin(?string $message, string $messageType): void
  57. {
  58. ?>
  59. <!DOCTYPE html>
  60. <html lang="de">
  61. <head>
  62. <meta charset="UTF-8">
  63. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  64. <title>Adminpanel Login</title>
  65. <link rel="stylesheet" href="/styles.css">
  66. </head>
  67. <body>
  68. <main class="auth-page">
  69. <section class="auth-card">
  70. <p class="eyebrow">Adminbereich</p>
  71. <h1>Konfiguration sichern</h1>
  72. <p>Logge dich mit den statischen Zugangsdaten aus der JSON-Config ein.</p>
  73. <?php if ($message !== null): ?>
  74. <div class="message message--<?= htmlspecialchars($messageType, ENT_QUOTES) ?>"><?= htmlspecialchars($message, ENT_QUOTES) ?></div>
  75. <?php endif; ?>
  76. <form method="post">
  77. <input type="hidden" name="action" value="login">
  78. <label>
  79. Benutzername
  80. <input type="text" name="username" required>
  81. </label>
  82. <label>
  83. Passwort
  84. <input type="password" name="password" required>
  85. </label>
  86. <button class="button button--primary" type="submit">Einloggen</button>
  87. <a class="button button--ghost" href="/">Zurueck zum Dashboard</a>
  88. </form>
  89. </section>
  90. </main>
  91. </body>
  92. </html>
  93. <?php
  94. }
  95. function renderAdmin(array $config, ?string $message, string $messageType): void
  96. {
  97. ?>
  98. <!DOCTYPE html>
  99. <html lang="de">
  100. <head>
  101. <meta charset="UTF-8">
  102. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  103. <title>Adminpanel</title>
  104. <link rel="stylesheet" href="/styles.css">
  105. </head>
  106. <body>
  107. <main class="admin-page">
  108. <section class="admin-shell">
  109. <div class="admin-header">
  110. <div>
  111. <p class="eyebrow">Adminpanel</p>
  112. <h1>Konfiguration der Automaten</h1>
  113. <p>Hier werden API-Token, Zugangsdaten, Faecher und Alarmwege direkt in der JSON-Config gepflegt.</p>
  114. </div>
  115. <div class="inline-actions">
  116. <a class="button button--ghost" href="/">Dashboard</a>
  117. <a class="button button--secondary" href="/admin/logout.php">Logout</a>
  118. </div>
  119. </div>
  120. <?php if ($message !== null): ?>
  121. <div class="message message--<?= htmlspecialchars($messageType, ENT_QUOTES) ?>"><?= htmlspecialchars($message, ENT_QUOTES) ?></div>
  122. <?php endif; ?>
  123. <form method="post" class="admin-form" id="admin-form">
  124. <input type="hidden" name="action" value="save_config">
  125. <section class="form-section">
  126. <h2>Allgemein</h2>
  127. <div class="field-grid">
  128. <label>
  129. App-Name
  130. <input type="text" name="app_name" value="<?= htmlspecialchars((string) ($config['app']['name'] ?? ''), ENT_QUOTES) ?>" required>
  131. </label>
  132. <label>
  133. Zeitzone
  134. <input type="text" name="timezone" value="<?= htmlspecialchars((string) ($config['app']['timezone'] ?? 'Europe/Berlin'), ENT_QUOTES) ?>" required>
  135. </label>
  136. <label>
  137. Auto-Refresh Sekunden
  138. <input type="number" min="5" name="dashboard_refresh_seconds" value="<?= (int) ($config['app']['dashboard_refresh_seconds'] ?? 15) ?>" required>
  139. </label>
  140. <label>
  141. Absender fuer Email
  142. <input type="email" name="default_from_email" value="<?= htmlspecialchars((string) ($config['app']['default_from_email'] ?? ''), ENT_QUOTES) ?>">
  143. </label>
  144. </div>
  145. </section>
  146. <section class="admin-grid">
  147. <section class="form-section">
  148. <h2>API</h2>
  149. <label>
  150. Bearer-Token
  151. <input type="text" name="bearer_token" value="<?= htmlspecialchars((string) ($config['api']['bearer_token'] ?? ''), ENT_QUOTES) ?>" required>
  152. </label>
  153. <p class="field-help">ESP32-Clients senden dieses Token im Header <code>Authorization: Bearer ...</code>.</p>
  154. </section>
  155. <section class="form-section">
  156. <h2>Adminzugang</h2>
  157. <div class="field-grid">
  158. <label>
  159. Benutzername
  160. <input type="text" name="admin_username" value="<?= htmlspecialchars((string) ($config['admin']['username'] ?? ''), ENT_QUOTES) ?>" required>
  161. </label>
  162. <label>
  163. Neues Passwort
  164. <input type="password" name="admin_password" placeholder="Leer lassen, um es nicht zu aendern">
  165. </label>
  166. </div>
  167. </section>
  168. </section>
  169. <section class="form-section">
  170. <div class="panel__header">
  171. <div>
  172. <h2>Webhooks</h2>
  173. <p>Mehrere Ziele sind moeglich. Header koennen als JSON hinterlegt werden.</p>
  174. </div>
  175. <button class="button button--secondary" type="button" data-add="webhook">Webhook hinzufuegen</button>
  176. </div>
  177. <div class="repeat-stack" id="webhook-stack">
  178. <?php foreach (($config['alerts']['webhooks'] ?? []) as $index => $webhook): ?>
  179. <?= renderWebhookRow((int) $index, $webhook) ?>
  180. <?php endforeach; ?>
  181. </div>
  182. </section>
  183. <section class="form-section">
  184. <div class="panel__header">
  185. <div>
  186. <h2>Email-Empfaenger</h2>
  187. <p>Emails werden ueber die Serverkonfiguration von <code>mail()</code> versendet.</p>
  188. </div>
  189. <button class="button button--secondary" type="button" data-add="email">Email-Empfaenger hinzufuegen</button>
  190. </div>
  191. <div class="repeat-stack" id="email-stack">
  192. <?php foreach (($config['alerts']['emails'] ?? []) as $index => $email): ?>
  193. <?= renderEmailRow((int) $index, $email) ?>
  194. <?php endforeach; ?>
  195. </div>
  196. </section>
  197. <section class="form-section">
  198. <div class="panel__header">
  199. <div>
  200. <h2>Automaten und Faecher</h2>
  201. <p>Jeder Automat enthaelt beliebig viele Faecher, die jeweils genau einem Sensor zugeordnet sind.</p>
  202. </div>
  203. <button class="button button--secondary" type="button" data-add="machine">Automat hinzufuegen</button>
  204. </div>
  205. <div class="repeat-stack" id="machine-stack">
  206. <?php foreach (($config['machines'] ?? []) as $machineIndex => $machine): ?>
  207. <?= renderMachineRow((int) $machineIndex, $machine) ?>
  208. <?php endforeach; ?>
  209. </div>
  210. </section>
  211. <div class="section-actions">
  212. <button class="button button--primary" type="submit">Konfiguration speichern</button>
  213. </div>
  214. </form>
  215. </section>
  216. </main>
  217. <template id="tpl-webhook"><?= renderWebhookRow('__INDEX__', []) ?></template>
  218. <template id="tpl-email"><?= renderEmailRow('__INDEX__', []) ?></template>
  219. <template id="tpl-machine"><?= renderMachineRow('__MACHINE__', []) ?></template>
  220. <template id="tpl-slot"><?= renderSlotRow('__MACHINE__', '__SLOT__', []) ?></template>
  221. <script>
  222. const webhookStack = document.getElementById('webhook-stack');
  223. const emailStack = document.getElementById('email-stack');
  224. const machineStack = document.getElementById('machine-stack');
  225. const renderTemplate = (id, replacements) => {
  226. let html = document.getElementById(id).innerHTML;
  227. Object.entries(replacements).forEach(([needle, value]) => {
  228. html = html.replaceAll(needle, value);
  229. });
  230. return html;
  231. };
  232. const countChildren = (node, selector) => node.querySelectorAll(selector).length;
  233. document.addEventListener('click', (event) => {
  234. const addType = event.target.getAttribute('data-add');
  235. const removeType = event.target.getAttribute('data-remove');
  236. if (addType === 'webhook') {
  237. const index = countChildren(webhookStack, '[data-webhook-row]');
  238. webhookStack.insertAdjacentHTML('beforeend', renderTemplate('tpl-webhook', { '__INDEX__': index }));
  239. }
  240. if (addType === 'email') {
  241. const index = countChildren(emailStack, '[data-email-row]');
  242. emailStack.insertAdjacentHTML('beforeend', renderTemplate('tpl-email', { '__INDEX__': index }));
  243. }
  244. if (addType === 'machine') {
  245. const index = countChildren(machineStack, '[data-machine-row]');
  246. machineStack.insertAdjacentHTML('beforeend', renderTemplate('tpl-machine', { '__MACHINE__': index, '__SLOT__': 0 }));
  247. }
  248. if (addType === 'slot') {
  249. const machineIndex = event.target.getAttribute('data-machine-index');
  250. const container = document.querySelector(`[data-slot-stack="${machineIndex}"]`);
  251. const slotIndex = countChildren(container, '[data-slot-row]');
  252. container.insertAdjacentHTML('beforeend', renderTemplate('tpl-slot', {
  253. '__MACHINE__': machineIndex,
  254. '__SLOT__': slotIndex
  255. }));
  256. }
  257. if (removeType) {
  258. const row = event.target.closest(`[data-${removeType}-row]`);
  259. if (row) {
  260. row.remove();
  261. }
  262. }
  263. });
  264. </script>
  265. </body>
  266. </html>
  267. <?php
  268. }
  269. function renderWebhookRow(int|string $index, array $webhook): string
  270. {
  271. $headers = $webhook['headers'] ?? [];
  272. $headerJson = json_encode($headers, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
  273. ob_start();
  274. ?>
  275. <div class="repeat-card" data-webhook-row>
  276. <div class="panel__header">
  277. <h4>Webhook</h4>
  278. <button class="button button--danger" type="button" data-remove="webhook">Entfernen</button>
  279. </div>
  280. <div class="field-grid">
  281. <label>
  282. ID
  283. <input type="text" name="webhooks[<?= $index ?>][id]" value="<?= htmlspecialchars((string) ($webhook['id'] ?? ''), ENT_QUOTES) ?>">
  284. </label>
  285. <label>
  286. Name
  287. <input type="text" name="webhooks[<?= $index ?>][name]" value="<?= htmlspecialchars((string) ($webhook['name'] ?? ''), ENT_QUOTES) ?>">
  288. </label>
  289. <label>
  290. URL
  291. <input type="url" name="webhooks[<?= $index ?>][url]" value="<?= htmlspecialchars((string) ($webhook['url'] ?? ''), ENT_QUOTES) ?>">
  292. </label>
  293. <label>
  294. Aktiv
  295. <select name="webhooks[<?= $index ?>][enabled]">
  296. <option value="1" <?= !empty($webhook['enabled']) ? 'selected' : '' ?>>Ja</option>
  297. <option value="0" <?= empty($webhook['enabled']) ? 'selected' : '' ?>>Nein</option>
  298. </select>
  299. </label>
  300. </div>
  301. <label>
  302. Header als JSON-Objekt
  303. <textarea name="webhooks[<?= $index ?>][headers_json]"><?= htmlspecialchars((string) $headerJson, ENT_QUOTES) ?></textarea>
  304. </label>
  305. </div>
  306. <?php
  307. return (string) ob_get_clean();
  308. }
  309. function renderEmailRow(int|string $index, array $email): string
  310. {
  311. ob_start();
  312. ?>
  313. <div class="repeat-card" data-email-row>
  314. <div class="panel__header">
  315. <h4>Email-Empfaenger</h4>
  316. <button class="button button--danger" type="button" data-remove="email">Entfernen</button>
  317. </div>
  318. <div class="field-grid">
  319. <label>
  320. ID
  321. <input type="text" name="emails[<?= $index ?>][id]" value="<?= htmlspecialchars((string) ($email['id'] ?? ''), ENT_QUOTES) ?>">
  322. </label>
  323. <label>
  324. Name
  325. <input type="text" name="emails[<?= $index ?>][name]" value="<?= htmlspecialchars((string) ($email['name'] ?? ''), ENT_QUOTES) ?>">
  326. </label>
  327. <label>
  328. Adresse
  329. <input type="email" name="emails[<?= $index ?>][address]" value="<?= htmlspecialchars((string) ($email['address'] ?? ''), ENT_QUOTES) ?>">
  330. </label>
  331. <label>
  332. Aktiv
  333. <select name="emails[<?= $index ?>][enabled]">
  334. <option value="1" <?= !empty($email['enabled']) ? 'selected' : '' ?>>Ja</option>
  335. <option value="0" <?= empty($email['enabled']) ? 'selected' : '' ?>>Nein</option>
  336. </select>
  337. </label>
  338. </div>
  339. </div>
  340. <?php
  341. return (string) ob_get_clean();
  342. }
  343. function renderMachineRow(int|string $machineIndex, array $machine): string
  344. {
  345. ob_start();
  346. ?>
  347. <div class="repeat-card" data-machine-row>
  348. <div class="panel__header">
  349. <div>
  350. <h4>Automat</h4>
  351. <p>Faecher koennen direkt darunter verwaltet werden.</p>
  352. </div>
  353. <div class="inline-actions">
  354. <button class="button button--secondary" type="button" data-add="slot" data-machine-index="<?= htmlspecialchars((string) $machineIndex, ENT_QUOTES) ?>">Fach hinzufuegen</button>
  355. <button class="button button--danger" type="button" data-remove="machine">Automat entfernen</button>
  356. </div>
  357. </div>
  358. <div class="field-grid">
  359. <label>
  360. ID
  361. <input type="text" name="machines[<?= $machineIndex ?>][id]" value="<?= htmlspecialchars((string) ($machine['id'] ?? ''), ENT_QUOTES) ?>">
  362. </label>
  363. <label>
  364. Name
  365. <input type="text" name="machines[<?= $machineIndex ?>][name]" value="<?= htmlspecialchars((string) ($machine['name'] ?? ''), ENT_QUOTES) ?>">
  366. </label>
  367. <label>
  368. Standort
  369. <input type="text" name="machines[<?= $machineIndex ?>][location]" value="<?= htmlspecialchars((string) ($machine['location'] ?? ''), ENT_QUOTES) ?>">
  370. </label>
  371. </div>
  372. <div class="repeat-stack" data-slot-stack="<?= htmlspecialchars((string) $machineIndex, ENT_QUOTES) ?>">
  373. <?php foreach (($machine['slots'] ?? []) as $slotIndex => $slot): ?>
  374. <?= renderSlotRow($machineIndex, (int) $slotIndex, $slot) ?>
  375. <?php endforeach; ?>
  376. </div>
  377. </div>
  378. <?php
  379. return (string) ob_get_clean();
  380. }
  381. function renderSlotRow(int|string $machineIndex, int|string $slotIndex, array $slot): string
  382. {
  383. $webhookIds = implode(',', $slot['webhook_ids'] ?? []);
  384. $emailIds = implode(',', $slot['email_ids'] ?? []);
  385. ob_start();
  386. ?>
  387. <div class="slot-editor" data-slot-row>
  388. <div class="panel__header">
  389. <h5>Fach</h5>
  390. <button class="button button--danger" type="button" data-remove="slot">Fach entfernen</button>
  391. </div>
  392. <div class="field-grid">
  393. <label>
  394. Sensor-ID
  395. <input type="text" name="machines[<?= $machineIndex ?>][slots][<?= $slotIndex ?>][sensor_id]" value="<?= htmlspecialchars((string) ($slot['sensor_id'] ?? ''), ENT_QUOTES) ?>">
  396. </label>
  397. <label>
  398. Label
  399. <input type="text" name="machines[<?= $machineIndex ?>][slots][<?= $slotIndex ?>][label]" value="<?= htmlspecialchars((string) ($slot['label'] ?? ''), ENT_QUOTES) ?>">
  400. </label>
  401. <label>
  402. Produktname
  403. <input type="text" name="machines[<?= $machineIndex ?>][slots][<?= $slotIndex ?>][product_name]" value="<?= htmlspecialchars((string) ($slot['product_name'] ?? ''), ENT_QUOTES) ?>">
  404. </label>
  405. <label>
  406. Voll-Distanz in mm
  407. <input type="number" step="0.01" name="machines[<?= $machineIndex ?>][slots][<?= $slotIndex ?>][full_distance_mm]" value="<?= htmlspecialchars((string) ($slot['full_distance_mm'] ?? ''), ENT_QUOTES) ?>">
  408. </label>
  409. <label>
  410. Leer-Distanz in mm
  411. <input type="number" step="0.01" name="machines[<?= $machineIndex ?>][slots][<?= $slotIndex ?>][empty_distance_mm]" value="<?= htmlspecialchars((string) ($slot['empty_distance_mm'] ?? ''), ENT_QUOTES) ?>">
  412. </label>
  413. <label>
  414. Distanz pro Flasche
  415. <input type="number" step="0.01" name="machines[<?= $machineIndex ?>][slots][<?= $slotIndex ?>][distance_per_unit]" value="<?= htmlspecialchars((string) ($slot['distance_per_unit'] ?? ''), ENT_QUOTES) ?>">
  416. </label>
  417. <label>
  418. Alarm unter Bestand
  419. <input type="number" step="1" min="0" name="machines[<?= $machineIndex ?>][slots][<?= $slotIndex ?>][alert_below_units]" value="<?= htmlspecialchars((string) ($slot['alert_below_units'] ?? ''), ENT_QUOTES) ?>">
  420. </label>
  421. <label>
  422. Webhook-IDs
  423. <input type="text" name="machines[<?= $machineIndex ?>][slots][<?= $slotIndex ?>][webhook_ids]" value="<?= htmlspecialchars((string) $webhookIds, ENT_QUOTES) ?>" placeholder="ops,lager">
  424. </label>
  425. <label>
  426. Email-IDs
  427. <input type="text" name="machines[<?= $machineIndex ?>][slots][<?= $slotIndex ?>][email_ids]" value="<?= htmlspecialchars((string) $emailIds, ENT_QUOTES) ?>" placeholder="lager,service">
  428. </label>
  429. </div>
  430. </div>
  431. <?php
  432. return (string) ob_get_clean();
  433. }
  434. function normalizeWebhooks(array $rows): array
  435. {
  436. $items = [];
  437. foreach ($rows as $row) {
  438. $id = trim((string) ($row['id'] ?? ''));
  439. $url = trim((string) ($row['url'] ?? ''));
  440. if ($id === '' && $url === '') {
  441. continue;
  442. }
  443. $headers = json_decode((string) ($row['headers_json'] ?? '{}'), true);
  444. $items[] = [
  445. 'id' => $id,
  446. 'name' => trim((string) ($row['name'] ?? '')),
  447. 'url' => $url,
  448. 'enabled' => (string) ($row['enabled'] ?? '1') === '1',
  449. 'headers' => is_array($headers) ? $headers : [],
  450. ];
  451. }
  452. return array_values($items);
  453. }
  454. function normalizeEmails(array $rows): array
  455. {
  456. $items = [];
  457. foreach ($rows as $row) {
  458. $id = trim((string) ($row['id'] ?? ''));
  459. $address = trim((string) ($row['address'] ?? ''));
  460. if ($id === '' && $address === '') {
  461. continue;
  462. }
  463. $items[] = [
  464. 'id' => $id,
  465. 'name' => trim((string) ($row['name'] ?? '')),
  466. 'address' => $address,
  467. 'enabled' => (string) ($row['enabled'] ?? '1') === '1',
  468. ];
  469. }
  470. return array_values($items);
  471. }
  472. function normalizeMachines(array $rows): array
  473. {
  474. $items = [];
  475. foreach ($rows as $row) {
  476. $id = trim((string) ($row['id'] ?? ''));
  477. $name = trim((string) ($row['name'] ?? ''));
  478. if ($id === '' && $name === '') {
  479. continue;
  480. }
  481. $slots = [];
  482. foreach (($row['slots'] ?? []) as $slot) {
  483. $sensorId = trim((string) ($slot['sensor_id'] ?? ''));
  484. if ($sensorId === '') {
  485. continue;
  486. }
  487. $slots[] = [
  488. 'sensor_id' => $sensorId,
  489. 'label' => trim((string) ($slot['label'] ?? $sensorId)),
  490. 'product_name' => trim((string) ($slot['product_name'] ?? '')),
  491. 'full_distance_mm' => (float) ($slot['full_distance_mm'] ?? 0),
  492. 'empty_distance_mm' => (float) ($slot['empty_distance_mm'] ?? 0),
  493. 'distance_per_unit' => max(0.01, (float) ($slot['distance_per_unit'] ?? 1)),
  494. 'alert_below_units' => max(0, (int) ($slot['alert_below_units'] ?? 0)),
  495. 'webhook_ids' => parseIdList((string) ($slot['webhook_ids'] ?? '')),
  496. 'email_ids' => parseIdList((string) ($slot['email_ids'] ?? '')),
  497. ];
  498. }
  499. $items[] = [
  500. 'id' => $id,
  501. 'name' => $name,
  502. 'location' => trim((string) ($row['location'] ?? '')),
  503. 'slots' => $slots,
  504. ];
  505. }
  506. return array_values($items);
  507. }
  508. function parseIdList(string $value): array
  509. {
  510. $parts = array_map('trim', explode(',', $value));
  511. return array_values(array_filter($parts, static fn (string $item): bool => $item !== ''));
  512. }