157 lines
5.9 KiB
PHP
157 lines
5.9 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
require_once dirname(__DIR__) . '/bootstrap.php';
|
|
|
|
$assets = app()->assets();
|
|
if ($assets) {
|
|
$assets->addStyle('/module/fx-rates/asset?file=fx-rates.css');
|
|
$assets->addScript('/module/fx-rates/asset?file=fx-rates.js', 'footer', true);
|
|
}
|
|
|
|
$settings = module_fn('fx-rates', 'settings');
|
|
$service = module_fn('fx-rates', 'service');
|
|
$preferredCurrencies = is_array($settings['preferred_currencies'] ?? null) ? $settings['preferred_currencies'] : [];
|
|
$apiDescribeUrl = APP_API_BASE . '/fx-rates/v1/endpoints';
|
|
$notice = trim((string) ($_GET['notice'] ?? ''));
|
|
$error = trim((string) ($_GET['error'] ?? ''));
|
|
|
|
if ((string) ($_GET['refresh'] ?? '') === '1') {
|
|
try {
|
|
$force = !empty($_GET['force']);
|
|
if ($force) {
|
|
$result = $service->refreshLatestRates(null, (string) ($settings['default_base_currency'] ?? ''), 'manual');
|
|
} else {
|
|
$result = $service->autoRefreshLatestRates(
|
|
(string) ($settings['default_base_currency'] ?? ''),
|
|
null,
|
|
(int) ($settings['refresh_max_age_minutes'] ?? 60),
|
|
'manual'
|
|
);
|
|
}
|
|
|
|
$params = !empty($result['reused'])
|
|
? [
|
|
'notice' => sprintf(
|
|
'Kein neuer API-Abruf. Der letzte gespeicherte Snapshot ist juenger als %d Minuten. Fuer einen erzwungenen Abruf bitte bestaetigen.',
|
|
(int) ($settings['refresh_max_age_minutes'] ?? 60)
|
|
),
|
|
]
|
|
: [
|
|
'notice' => sprintf(
|
|
'Aktuelle Kurse gespeichert. %d Werte aktualisiert.',
|
|
(int) ($result['updated_count'] ?? 0)
|
|
),
|
|
];
|
|
} catch (\Throwable $exception) {
|
|
$params = ['error' => $exception->getMessage() !== '' ? $exception->getMessage() : 'Kurse konnten nicht aktualisiert werden.'];
|
|
}
|
|
|
|
redirect('/module/fx-rates?' . http_build_query($params));
|
|
}
|
|
|
|
$latest = $service->latestStatus();
|
|
$recentFetches = $service->recentFetches(15);
|
|
$pageData = json_encode([
|
|
'settings' => $settings,
|
|
'latest' => $latest,
|
|
'preferred_currencies' => $preferredCurrencies,
|
|
'recent_fetches' => $recentFetches,
|
|
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
|
|
|
$tabs = [
|
|
['label' => 'Ueberblick', 'href' => '/module/fx-rates', 'active' => true],
|
|
['label' => 'Waehrungen', 'href' => '/module/fx-rates/currencies'],
|
|
];
|
|
?>
|
|
<div id="fx-rates-app" data-page='<?= e(is_string($pageData) ? $pageData : '{}') ?>'>
|
|
<div class="submenu-box">
|
|
<div class="fx-submenu-row">
|
|
<nav class="module-tabs" aria-label="Unterseiten von Waehrungskurse">
|
|
<?php foreach ($tabs as $tab): ?>
|
|
<a
|
|
class="<?= !empty($tab['active']) ? 'module-button module-button--tab-active' : 'module-button module-button--tab' ?>"
|
|
href="<?= e((string) $tab['href']) ?>"
|
|
><?= e((string) $tab['label']) ?></a>
|
|
<?php endforeach; ?>
|
|
</nav>
|
|
<div class="module-submenu-actions">
|
|
<a class="module-button module-button--secondary module-button--small" href="/modules/setup/fx-rates">Setup</a>
|
|
<a class="module-button module-button--secondary module-button--small" href="/module/fx-rates?refresh=1">Aktuelle Kurse abrufen</a>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php if ($notice !== ''): ?>
|
|
<section class="section-box">
|
|
<div class="fx-message is-success"><?= e($notice) ?></div>
|
|
</section>
|
|
<?php elseif ($error !== ''): ?>
|
|
<section class="section-box">
|
|
<div class="fx-message is-error"><?= e($error) ?></div>
|
|
</section>
|
|
<?php endif; ?>
|
|
|
|
<section class="section-box">
|
|
<div class="fx-section-head">
|
|
<div>
|
|
<h2>Umrechnung</h2>
|
|
<p>Umrechnung auf Basis des letzten verfuegbaren Kurses zwischen den bevorzugten Waehrungen.</p>
|
|
</div>
|
|
</div>
|
|
<p class="fx-api-note">
|
|
API-Self-Describe-Endpoint:
|
|
<a href="<?= e($apiDescribeUrl) ?>" target="_blank" rel="noopener noreferrer"><?= e($apiDescribeUrl) ?></a>
|
|
</p>
|
|
<div class="fx-form-grid">
|
|
<label>
|
|
<span>Quellwaehrung</span>
|
|
<select name="convert_from">
|
|
<?php foreach ($preferredCurrencies as $currency): ?>
|
|
<option value="<?= e((string) $currency) ?>"><?= e((string) $currency) ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</label>
|
|
<label>
|
|
<span>Zielwaehrung</span>
|
|
<select name="convert_to">
|
|
<?php foreach ($preferredCurrencies as $currency): ?>
|
|
<option value="<?= e((string) $currency) ?>" <?= (string) $currency === (string) ($preferredCurrencies[1] ?? $preferredCurrencies[0] ?? '') ? 'selected' : '' ?>><?= e((string) $currency) ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</label>
|
|
<label>
|
|
<span>Betrag</span>
|
|
<input type="number" name="convert_amount" min="0" step="0.00000001" value="1">
|
|
</label>
|
|
</div>
|
|
<div class="fx-convert-result" data-bind="convert-result">Noch keine Umrechnung berechnet.</div>
|
|
</section>
|
|
|
|
<section class="section-box">
|
|
<div class="fx-card-head">
|
|
<div>
|
|
<h2>Kursverlauf</h2>
|
|
<p>Neueste Abrufe zuerst. Verlauf der bevorzugten Waehrungen relativ zur Anzeige-Basiswaehrung.</p>
|
|
</div>
|
|
<div class="fx-card-meta">
|
|
<div><strong>Anzeige-Basis:</strong> <?= e((string) ($settings['display_base_currency'] ?? $settings['default_base_currency'] ?? '')) ?></div>
|
|
<div><strong>Letzter Abruf:</strong> <?= e((string) ($latest['fetched_at_display'] ?? $latest['fetched_at'] ?? 'noch keiner')) ?></div>
|
|
</div>
|
|
</div>
|
|
<div class="fx-table-wrap">
|
|
<table class="fx-table">
|
|
<thead data-bind="history-head">
|
|
<tr>
|
|
<th>Datum</th>
|
|
<th>Kurse</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody data-bind="history-body">
|
|
<tr><td colspan="2">Noch keine Verlaufsdaten geladen.</td></tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</section>
|
|
</div>
|