From 0528de1de162a2b4a2ed563a40dec3581dab3ba9 Mon Sep 17 00:00:00 2001 From: Timm Ortloff Date: Tue, 8 Nov 2022 08:34:54 +0100 Subject: [PATCH 01/10] Add EmailProviderHook --- library/Reporting/Hook/EmailProviderHook.php | 23 ++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 library/Reporting/Hook/EmailProviderHook.php diff --git a/library/Reporting/Hook/EmailProviderHook.php b/library/Reporting/Hook/EmailProviderHook.php new file mode 100644 index 00000000..9f17f2e5 --- /dev/null +++ b/library/Reporting/Hook/EmailProviderHook.php @@ -0,0 +1,23 @@ + Date: Tue, 8 Nov 2022 08:36:23 +0100 Subject: [PATCH 02/10] Add Select list and manual Email input to SendForm --- library/Reporting/Web/Forms/SendForm.php | 29 ++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/library/Reporting/Web/Forms/SendForm.php b/library/Reporting/Web/Forms/SendForm.php index 7e206029..9dd9a2aa 100644 --- a/library/Reporting/Web/Forms/SendForm.php +++ b/library/Reporting/Web/Forms/SendForm.php @@ -6,6 +6,7 @@ use Icinga\Module\Reporting\Actions\SendMail; use Icinga\Module\Reporting\Database; +use Icinga\Module\Reporting\Hook\EmailProviderHook; use Icinga\Module\Reporting\ProvidedReports; use Icinga\Module\Reporting\Report; use Icinga\Module\Reporting\Web\Forms\Decorator\CompatDecorator; @@ -32,6 +33,34 @@ protected function assemble() (new SendMail())->initConfigForm($this, $this->report); + $radio = $this->addElement('radio', 'source_radio', [ + 'label' => 'E-Mail Source', + 'options' => [ + 'manual' => 'Manual', + 'contacts' => 'Contacts', + ], + 'value' => 'contacts', + 'class' => 'autosubmit' + ]); + + if ($radio->getValue('source_radio') === 'contacts') { + $emails = [null => 'Select Contacts']; + foreach (EmailProviderHook::getProvider() as $provider) { + var_dump($provider); + $emails = array_merge($emails, $provider->getContactEmails()); + } + + $this->addElement('select', 'emails_list', [ + 'multiple' => true, + 'label' => 'Contacts', + 'options' => $emails + ]); + } else { + $this->addElement('textarea', 'emails_manual', [ + 'label' => 'Contact E-Mails' + ]); + } + $this->addElement('submit', 'submit', [ 'label' => $this->translate('Send Report') ]); From e8e38dca3b4dd6139e4bc177ff8de5b38acfc4e8 Mon Sep 17 00:00:00 2001 From: Timm Ortloff Date: Tue, 31 Jan 2023 10:28:43 +0100 Subject: [PATCH 03/10] Rename `getProvider` to `getProviders` om EmailProviderHook and all usages of it --- library/Reporting/Hook/EmailProviderHook.php | 2 +- library/Reporting/Web/Forms/SendForm.php | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/library/Reporting/Hook/EmailProviderHook.php b/library/Reporting/Hook/EmailProviderHook.php index 9f17f2e5..4f76c9de 100644 --- a/library/Reporting/Hook/EmailProviderHook.php +++ b/library/Reporting/Hook/EmailProviderHook.php @@ -16,7 +16,7 @@ abstract public function getContactEmails(); /** * @return array */ - final public static function getProvider(): array + final public static function getProviders(): array { return Hook::all('Reporting/EmailProvider'); } diff --git a/library/Reporting/Web/Forms/SendForm.php b/library/Reporting/Web/Forms/SendForm.php index 9dd9a2aa..fcaec455 100644 --- a/library/Reporting/Web/Forms/SendForm.php +++ b/library/Reporting/Web/Forms/SendForm.php @@ -45,8 +45,7 @@ protected function assemble() if ($radio->getValue('source_radio') === 'contacts') { $emails = [null => 'Select Contacts']; - foreach (EmailProviderHook::getProvider() as $provider) { - var_dump($provider); + foreach (EmailProviderHook::getProviders() as $provider) { $emails = array_merge($emails, $provider->getContactEmails()); } From 859526c4e76553dbba65a960528ca8d2a2222d0d Mon Sep 17 00:00:00 2001 From: Timm Ortloff Date: Tue, 31 Jan 2023 10:35:19 +0100 Subject: [PATCH 04/10] SendForm: translate texts --- library/Reporting/Web/Forms/SendForm.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/library/Reporting/Web/Forms/SendForm.php b/library/Reporting/Web/Forms/SendForm.php index fcaec455..25af7298 100644 --- a/library/Reporting/Web/Forms/SendForm.php +++ b/library/Reporting/Web/Forms/SendForm.php @@ -10,12 +10,14 @@ use Icinga\Module\Reporting\ProvidedReports; use Icinga\Module\Reporting\Report; use Icinga\Module\Reporting\Web\Forms\Decorator\CompatDecorator; +use ipl\I18n\Translation; use ipl\Web\Compat\CompatForm; class SendForm extends CompatForm { use Database; use ProvidedReports; + use Translation; /** @var Report */ protected $report; @@ -34,29 +36,29 @@ protected function assemble() (new SendMail())->initConfigForm($this, $this->report); $radio = $this->addElement('radio', 'source_radio', [ - 'label' => 'E-Mail Source', + 'label' => $this->translate('E-Mail Source'), 'options' => [ - 'manual' => 'Manual', - 'contacts' => 'Contacts', + 'manual' => $this->translate('Manual input'), + 'contacts' => $this->translate('Contacts'), ], 'value' => 'contacts', 'class' => 'autosubmit' ]); if ($radio->getValue('source_radio') === 'contacts') { - $emails = [null => 'Select Contacts']; + $emails = [null => $this->translate('Select Contacts')]; foreach (EmailProviderHook::getProviders() as $provider) { $emails = array_merge($emails, $provider->getContactEmails()); } $this->addElement('select', 'emails_list', [ 'multiple' => true, - 'label' => 'Contacts', + 'label' => $this->translate('Contacts'), 'options' => $emails ]); } else { $this->addElement('textarea', 'emails_manual', [ - 'label' => 'Contact E-Mails' + 'label' => $this->translate('Contact E-Mails') ]); } From 3628a734e4ca56a130269ba6871bde7e9bbe5c81 Mon Sep 17 00:00:00 2001 From: Timm Ortloff Date: Tue, 31 Jan 2023 10:36:04 +0100 Subject: [PATCH 05/10] EmailProviderHook: Set return type of `getContactEmails` to array --- library/Reporting/Hook/EmailProviderHook.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/Reporting/Hook/EmailProviderHook.php b/library/Reporting/Hook/EmailProviderHook.php index 4f76c9de..2a0dddc2 100644 --- a/library/Reporting/Hook/EmailProviderHook.php +++ b/library/Reporting/Hook/EmailProviderHook.php @@ -9,9 +9,9 @@ abstract class EmailProviderHook /** * Get all Contact eMails * - * @return mixed + * @return array */ - abstract public function getContactEmails(); + abstract public function getContactEmails(): array; /** * @return array From e022416e0cbe3216b535349f9e68807b18b5fcee Mon Sep 17 00:00:00 2001 From: Timm Ortloff Date: Thu, 2 Feb 2023 10:49:56 +0100 Subject: [PATCH 06/10] SendForm: Fix getting value from radio element --- library/Reporting/Web/Forms/SendForm.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/Reporting/Web/Forms/SendForm.php b/library/Reporting/Web/Forms/SendForm.php index 25af7298..7ef263b9 100644 --- a/library/Reporting/Web/Forms/SendForm.php +++ b/library/Reporting/Web/Forms/SendForm.php @@ -35,7 +35,7 @@ protected function assemble() (new SendMail())->initConfigForm($this, $this->report); - $radio = $this->addElement('radio', 'source_radio', [ + $this->addElement('radio', 'source_radio', [ 'label' => $this->translate('E-Mail Source'), 'options' => [ 'manual' => $this->translate('Manual input'), @@ -45,7 +45,7 @@ protected function assemble() 'class' => 'autosubmit' ]); - if ($radio->getValue('source_radio') === 'contacts') { + if ($this->getPopulatedValue('source_radio', 'contacts') === 'contacts') { $emails = [null => $this->translate('Select Contacts')]; foreach (EmailProviderHook::getProviders() as $provider) { $emails = array_merge($emails, $provider->getContactEmails()); From 867a83686306d7e6eaf99b235f002eba4e7ef1e5 Mon Sep 17 00:00:00 2001 From: Timm Ortloff Date: Thu, 2 Feb 2023 13:41:18 +0100 Subject: [PATCH 07/10] Rename `EmailProviderHook` to `EmailAddressBookHook` --- .../Reporting/Hook/EmailAddressBookHook.php | 23 +++++++++++++++++++ library/Reporting/Hook/EmailProviderHook.php | 23 ------------------- 2 files changed, 23 insertions(+), 23 deletions(-) create mode 100644 library/Reporting/Hook/EmailAddressBookHook.php delete mode 100644 library/Reporting/Hook/EmailProviderHook.php diff --git a/library/Reporting/Hook/EmailAddressBookHook.php b/library/Reporting/Hook/EmailAddressBookHook.php new file mode 100644 index 00000000..96469502 --- /dev/null +++ b/library/Reporting/Hook/EmailAddressBookHook.php @@ -0,0 +1,23 @@ + Date: Thu, 2 Feb 2023 13:40:47 +0100 Subject: [PATCH 08/10] SendForm: Check if more than one contact is available --- library/Reporting/Web/Forms/SendForm.php | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/library/Reporting/Web/Forms/SendForm.php b/library/Reporting/Web/Forms/SendForm.php index 7ef263b9..dea1b15e 100644 --- a/library/Reporting/Web/Forms/SendForm.php +++ b/library/Reporting/Web/Forms/SendForm.php @@ -6,7 +6,7 @@ use Icinga\Module\Reporting\Actions\SendMail; use Icinga\Module\Reporting\Database; -use Icinga\Module\Reporting\Hook\EmailProviderHook; +use Icinga\Module\Reporting\Hook\EmailAddressBookHook; use Icinga\Module\Reporting\ProvidedReports; use Icinga\Module\Reporting\Report; use Icinga\Module\Reporting\Web\Forms\Decorator\CompatDecorator; @@ -35,20 +35,24 @@ protected function assemble() (new SendMail())->initConfigForm($this, $this->report); + $emailAddressBooks = EmailAddressBookHook::getEmailAddressBooks(); + $this->addElement('radio', 'source_radio', [ - 'label' => $this->translate('E-Mail Source'), - 'options' => [ + 'label' => $this->translate('E-Mail Source'), + 'options' => [ 'manual' => $this->translate('Manual input'), 'contacts' => $this->translate('Contacts'), ], - 'value' => 'contacts', - 'class' => 'autosubmit' + 'disabled' => count($emailAddressBooks) === 0, + 'value' => 'contacts', + 'class' => 'autosubmit', + 'description' => count($emailAddressBooks) === 0 ? $this->translate("No contacts available") : null ]); - if ($this->getPopulatedValue('source_radio', 'contacts') === 'contacts') { + if ($this->getPopulatedValue('source_radio', 'manual') === 'contacts') { $emails = [null => $this->translate('Select Contacts')]; - foreach (EmailProviderHook::getProviders() as $provider) { - $emails = array_merge($emails, $provider->getContactEmails()); + foreach ($emailAddressBooks as $addressBook) { + $emails = array_merge($emails, $addressBook->getContactEmails()); } $this->addElement('select', 'emails_list', [ From 2bb46d3afd57d1d52b6c8dbcc184355e3cec93b5 Mon Sep 17 00:00:00 2001 From: Timm Ortloff Date: Thu, 2 Feb 2023 13:42:55 +0100 Subject: [PATCH 09/10] EmailAddressBookHook: Fix PHPDocs --- library/Reporting/Hook/EmailAddressBookHook.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/Reporting/Hook/EmailAddressBookHook.php b/library/Reporting/Hook/EmailAddressBookHook.php index 96469502..f6ca2b6d 100644 --- a/library/Reporting/Hook/EmailAddressBookHook.php +++ b/library/Reporting/Hook/EmailAddressBookHook.php @@ -7,7 +7,7 @@ abstract class EmailAddressBookHook { /** - * Get all Contact eMails + * Get a list of email addresses as email-label pairs * * @return array */ From 5b2161890db227b25e5fbdd2b751717794f951e1 Mon Sep 17 00:00:00 2001 From: Timm Ortloff Date: Thu, 2 Feb 2023 14:21:07 +0100 Subject: [PATCH 10/10] EmailAddressBookHook: Add `getName()` and use Translation trait --- library/Reporting/Hook/EmailAddressBookHook.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/library/Reporting/Hook/EmailAddressBookHook.php b/library/Reporting/Hook/EmailAddressBookHook.php index f6ca2b6d..cdc0bd51 100644 --- a/library/Reporting/Hook/EmailAddressBookHook.php +++ b/library/Reporting/Hook/EmailAddressBookHook.php @@ -3,9 +3,12 @@ namespace Icinga\Module\Reporting\Hook; use Icinga\Application\Hook; +use ipl\I18n\Translation; abstract class EmailAddressBookHook { + use Translation; + /** * Get a list of email addresses as email-label pairs * @@ -20,4 +23,12 @@ final public static function getEmailAddressBooks(): array { return Hook::all('Reporting/EmailAddressBook'); } + + /** + * @return string + */ + public function getName(): string + { + return $this->translate('E-Mail Address Books'); + } }