registerFunction($moduleName, 'runtime_settings', static function (): array { $moduleBasePath = __DIR__; $config = ModuleConfig::load($moduleBasePath); $projectKey = $config->defaultProjectKey(); $user = auth_user() ?? []; $ownerSub = trim((string) ($user['sub'] ?? '')) !== '' ? trim((string) ($user['sub'] ?? '')) : 'local'; $pdo = ConnectionFactory::make($config); $repository = new MiningRepository($pdo, $config->tablePrefix(), null, $ownerSub); $settings = $repository->getSettings($projectKey) ?? []; $displayTimezone = trim((string) ($settings['display_timezone'] ?? 'Europe/Berlin')); if ($displayTimezone === '') { $displayTimezone = 'Europe/Berlin'; } $baselineMeasuredAt = trim((string) ($settings['baseline_measured_at'] ?? '')); if ($baselineMeasuredAt !== '') { try { $baselineMeasuredAt = (new DateTimeImmutable($baselineMeasuredAt, new DateTimeZone('UTC'))) ->setTimezone(new DateTimeZone($displayTimezone)) ->format('Y-m-d\TH:i'); } catch (\Throwable) { $baselineMeasuredAt = ''; } } $preferredCurrencies = $settings['preferred_currencies'] ?? ['DOGE', 'USD', 'EUR']; if (is_string($preferredCurrencies)) { $preferredCurrencies = preg_split('/[\s,;]+/', $preferredCurrencies) ?: []; } $preferredCurrencies = array_values(array_filter(array_map( static fn (mixed $value): string => strtoupper(trim((string) $value)), is_array($preferredCurrencies) ? $preferredCurrencies : [] ), static fn (string $value): bool => $value !== '')); return [ 'baseline_measured_at' => $baselineMeasuredAt, 'baseline_coins_total' => isset($settings['baseline_coins_total']) ? (string) $settings['baseline_coins_total'] : '', 'daily_cost_amount' => isset($settings['daily_cost_amount']) ? (string) $settings['daily_cost_amount'] : '', 'daily_cost_currency' => strtoupper(trim((string) ($settings['daily_cost_currency'] ?? 'EUR'))) ?: 'EUR', 'report_currency' => strtoupper(trim((string) ($settings['report_currency'] ?? 'EUR'))) ?: 'EUR', 'crypto_currency' => strtoupper(trim((string) ($settings['crypto_currency'] ?? 'DOGE'))) ?: 'DOGE', 'display_timezone' => $displayTimezone, 'preferred_currencies' => $preferredCurrencies, ]; }); $mm->registerFunction($moduleName, 'save_runtime_settings', static function (array $payload): array { $moduleBasePath = __DIR__; $config = ModuleConfig::load($moduleBasePath); $projectKey = $config->defaultProjectKey(); $user = auth_user() ?? []; $ownerSub = trim((string) ($user['sub'] ?? '')) !== '' ? trim((string) ($user['sub'] ?? '')) : 'local'; $pdo = ConnectionFactory::make($config); $schema = new SchemaManager($pdo, $config->tablePrefix(), $moduleBasePath); $schema->ensureSchema(); $repository = new MiningRepository($pdo, $config->tablePrefix(), null, $ownerSub); $repository->ensureProject($projectKey, strtoupper(str_replace('-', ' ', $projectKey))); $existing = $repository->getSettings($projectKey) ?? []; $displayTimezone = trim((string) ($payload['display_timezone'] ?? ($existing['display_timezone'] ?? 'Europe/Berlin'))); if ($displayTimezone === '') { $displayTimezone = 'Europe/Berlin'; } try { $displayTz = new DateTimeZone($displayTimezone); } catch (\Throwable) { throw new RuntimeException('Ungueltige Anzeige-Zeitzone.'); } $baselineInput = trim((string) ($payload['baseline_measured_at'] ?? ($existing['baseline_measured_at'] ?? ''))); if ($baselineInput === '') { throw new RuntimeException('Baseline Zeitpunkt fehlt.'); } try { $baselineUtc = (new DateTimeImmutable($baselineInput, $displayTz)) ->setTimezone(new DateTimeZone('UTC')) ->format('Y-m-d H:i:s'); } catch (\Throwable) { throw new RuntimeException('Baseline Zeitpunkt ist ungueltig.'); } $baselineCoins = trim((string) ($payload['baseline_coins_total'] ?? ($existing['baseline_coins_total'] ?? ''))); $dailyCostAmount = trim((string) ($payload['daily_cost_amount'] ?? ($existing['daily_cost_amount'] ?? ''))); if ($baselineCoins === '' || !is_numeric($baselineCoins)) { throw new RuntimeException('Baseline Coins muessen numerisch sein.'); } if ($dailyCostAmount === '' || !is_numeric($dailyCostAmount)) { throw new RuntimeException('Taegliche Kosten muessen numerisch sein.'); } $preferredCurrencies = $payload['preferred_currencies'] ?? ($existing['preferred_currencies'] ?? ['DOGE', 'USD', 'EUR']); if (is_string($preferredCurrencies)) { $preferredCurrencies = preg_split('/[\s,;]+/', $preferredCurrencies) ?: []; } $preferredCurrencies = array_values(array_unique(array_filter(array_map( static fn (mixed $value): string => strtoupper(trim((string) $value)), is_array($preferredCurrencies) ? $preferredCurrencies : [] ), static fn (string $value): bool => $value !== ''))); $settings = [ 'baseline_measured_at' => $baselineUtc, 'baseline_coins_total' => (float) $baselineCoins, 'daily_cost_amount' => (float) $dailyCostAmount, 'daily_cost_currency' => strtoupper(trim((string) ($payload['daily_cost_currency'] ?? ($existing['daily_cost_currency'] ?? 'EUR')))) ?: 'EUR', 'report_currency' => strtoupper(trim((string) ($payload['report_currency'] ?? ($existing['report_currency'] ?? 'EUR')))) ?: 'EUR', 'crypto_currency' => strtoupper(trim((string) ($payload['crypto_currency'] ?? ($existing['crypto_currency'] ?? 'DOGE')))) ?: 'DOGE', 'display_timezone' => $displayTimezone, 'fx_max_age_hours' => isset($existing['fx_max_age_hours']) && is_numeric((string) $existing['fx_max_age_hours']) ? (int) $existing['fx_max_age_hours'] : 3, 'module_theme_mode' => in_array((string) ($existing['module_theme_mode'] ?? 'inherit'), ['inherit', 'custom'], true) ? (string) $existing['module_theme_mode'] : 'inherit', 'module_theme_accent' => in_array((string) ($existing['module_theme_accent'] ?? 'teal'), ['teal', 'logo', 'pink', 'cyan', 'orange', 'green'], true) ? (string) $existing['module_theme_accent'] : 'teal', 'preferred_currencies' => $preferredCurrencies, ]; $repository->saveSettings($projectKey, $settings); if (modules()->isEnabled('fx-rates') && modules()->hasFunction('fx-rates', 'save_runtime_settings')) { module_fn('fx-rates', 'save_runtime_settings', [ 'preferred_currencies' => $preferredCurrencies, ]); } return module_fn($moduleName, 'runtime_settings'); }); $mm->registerFunction('mining-checker', 'sql_import_target', static function (): array { $moduleBasePath = __DIR__; $config = ModuleConfig::load($moduleBasePath); return [ 'pdo' => ConnectionFactory::make($config), 'label' => 'Mining-Checker Projekt-Datenbank', ]; });