diff --git a/application/forms/ChannelForm.php b/application/forms/ChannelForm.php index 200936b30..eb7e02bf1 100644 --- a/application/forms/ChannelForm.php +++ b/application/forms/ChannelForm.php @@ -6,14 +6,16 @@ use DateTime; use Icinga\Exception\Http\HttpNotFoundException; -use Icinga\Module\Notifications\Model\Channel; use Icinga\Module\Notifications\Model\AvailableChannelType; +use Icinga\Module\Notifications\Model\Channel; use Icinga\Module\Notifications\Model\Contact; use Icinga\Module\Notifications\Model\RuleEscalationRecipient; use Icinga\Web\Session; +use ipl\Html\Attributes; use ipl\Html\Contract\FormSubmitElement; use ipl\Html\FormElement\BaseFormElement; use ipl\Html\FormElement\FieldsetElement; +use ipl\Html\HtmlElement; use ipl\I18n\GettextTranslator; use ipl\I18n\StaticTranslator; use ipl\Sql\Connection; @@ -267,6 +269,24 @@ protected function createConfigElements(string $type, string $config): void $elementsConfig = json_decode($config, true); if (empty($elementsConfig)) { + $this->prependHtml( + HtmlElement::create( + 'ul', + Attributes::create(['class' => 'errors']), + HtmlElement::create( + 'li', + null, + sprintf( + $this->translate( + 'Could not decode options for type \'%s\'.' + . ' Check if your database\'s character set is correctly configured.' + ), + $type + ) + ) + ) + ); + return; } @@ -449,4 +469,20 @@ private function fetchDbValues(): array 'config' => json_decode($channel->config, true) ?? [] ]; } + + /** + * Validate all elements + * + * @return $this + */ + public function validate(): self + { + parent::validate(); + + if (! $this->hasElement('config')) { + $this->isValid = false; + } + + return $this; + } } diff --git a/library/Notifications/Common/Database.php b/library/Notifications/Common/Database.php index f046a1089..c5fc689e2 100644 --- a/library/Notifications/Common/Database.php +++ b/library/Notifications/Common/Database.php @@ -82,6 +82,13 @@ private static function getConnection(): Connection . ",NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'"; } + if (empty($config->charset)) { + $config->charset = match ($config->db) { + 'mysql' => 'utf8mb4', + default => 'utf8', + }; + } + $db = new Connection($config); $adapter = $db->getAdapter();