Skip to content
34 changes: 34 additions & 0 deletions library/Reporting/Hook/EmailAddressBookHook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

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
*
* @return array
*/
abstract public function listEmailAddresses(): array;

/**
* @return array
*/
final public static function getEmailAddressBooks(): array
{
return Hook::all('Reporting/EmailAddressBook');
}

/**
* @return string
*/
public function getName(): string
{
return $this->translate('E-Mail Address Books');
}
}
34 changes: 34 additions & 0 deletions library/Reporting/Web/Forms/SendForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@

use Icinga\Module\Reporting\Actions\SendMail;
use Icinga\Module\Reporting\Database;
use Icinga\Module\Reporting\Hook\EmailAddressBookHook;
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;
Expand All @@ -32,6 +35,37 @@ protected function assemble()

(new SendMail())->initConfigForm($this, $this->report);

$emailAddressBooks = EmailAddressBookHook::getEmailAddressBooks();

$this->addElement('radio', 'source_radio', [
'label' => $this->translate('E-Mail Source'),
'options' => [
'manual' => $this->translate('Manual input'),
'contacts' => $this->translate('Contacts'),
],
'disabled' => count($emailAddressBooks) === 0,
'value' => 'contacts',
'class' => 'autosubmit',
'description' => count($emailAddressBooks) === 0 ? $this->translate("No contacts available") : null
]);

if ($this->getPopulatedValue('source_radio', 'manual') === 'contacts') {
$emails = [null => $this->translate('Select Contacts')];
foreach ($emailAddressBooks as $addressBook) {
$emails = array_merge($emails, $addressBook->getContactEmails());
}

$this->addElement('select', 'emails_list', [
'multiple' => true,
'label' => $this->translate('Contacts'),
'options' => $emails
]);
} else {
$this->addElement('textarea', 'emails_manual', [
'label' => $this->translate('Contact E-Mails')
]);
}

$this->addElement('submit', 'submit', [
'label' => $this->translate('Send Report')
]);
Expand Down