SettingsService = $SettingsService; } /** * Get all active (not-resolved) critical messages. * Info-type messages are auto-resolved after being retrieved (shown once). * * @return array */ public function getActiveMessages(): array { $messages = $this->getMessagesData(); $activeMessages = []; $needsSave = false; foreach ($messages as $key => $message) { if (!$message['resolved']) { $activeMessages[] = $message; if (($message['type'] ?? '') === 'info') { $messages[$key]['resolved'] = true; $needsSave = true; } } } if ($needsSave) { $this->SettingsService->set(Settings::SETTING_CRITICAL_MESSAGES, $messages); $this->SettingsService->commit(); } return array_filter($activeMessages, fn($m) => !$m['resolved']); } /** * Get first active message (if it needs to show just one) */ public function getFirstActiveMessage(): ?array { foreach ($this->getActiveMessages() as $message) { return $message; } return null; } /** * Add one new critical message */ public function addMessage( string $code, string $text, string $type = 'error', ?string $link = null, ?string $link_text = null ): void { $messages = $this->getMessagesData(); // Check, whether such message exists foreach ($messages as $message) { if ($message['code'] === $code && !$message['resolved']) { return; // already active } } $messages[] = [ 'code' => $code, 'text' => $text, 'type' => $type, // error | warning | success 'link' => $link, // optional URL 'link_text' => $link_text, // optional link text 'resolved' => false, ]; $this->SettingsService->set(Settings::SETTING_CRITICAL_MESSAGES, $messages); $this->SettingsService->commit(); } /** * Mark message as resolved */ public function resolveMessage(string $code): void { $messages = $this->getMessagesData(); foreach ($messages as &$message) { if ($message['code'] === $code) { $message['resolved'] = true; } } $this->SettingsService->set(Settings::SETTING_CRITICAL_MESSAGES, $messages); $this->SettingsService->commit(); } /** * Clear all critical messages */ public function clear(): void { $this->SettingsService->set(Settings::SETTING_CRITICAL_MESSAGES, []); $this->SettingsService->commit(); } /** * Get all messages from settings * * @return array */ private function getMessagesData(): array { $messages = $this->SettingsService->get(Settings::SETTING_CRITICAL_MESSAGES, []); return is_array($messages) ? $messages : []; } }