Skip to content

Commit

Permalink
Merge branch '365-required-mailer-configs' into 'master'
Browse files Browse the repository at this point in the history
Resolve "Mailer by mal hlasit chybajuci config pre aktualne zvoleny defaultny mailer"

See merge request remp/remp!272
  • Loading branch information
Adam Zoldak committed Mar 13, 2019
2 parents ea80c6c + 943a856 commit debcb8b
Show file tree
Hide file tree
Showing 13 changed files with 265 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Remp\MailerModule\Components;

interface IMissingConfigurationFactory
{
/** @return MissingConfiguration */
public function create();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Remp\MailerModule\Components;

use Nette\Application\UI\Control;
use Remp\MailerModule\Repository\ConfigsRepository;
use Remp\MailerModule\Sender\MailerFactory;

class MissingConfiguration extends Control
{
/** @var ConfigsRepository */
private $configsRepository;

/** @var MailerFactory */
private $mailerFactory;

public function __construct(
ConfigsRepository $configsRepository,
MailerFactory $mailerFactory
) {
parent::__construct();
$this->configsRepository = $configsRepository;
$this->mailerFactory = $mailerFactory;
}

public function render()
{
$defaultMailerSetting = $this->configsRepository->loadByName('default_mailer');
$mailerConfigured = false;

if ($defaultMailerSetting->value !== null) {
$activeMailer = $this->mailerFactory->getMailer($defaultMailerSetting->value);

if ($mailerConfigured = $activeMailer->isConfigured()) {
return;
}
}

$this->template->link = $this->getPresenter()->link('Settings:default');
$this->template->missingConfigs = !$mailerConfigured;

$this->template->setFile(__DIR__ . '/missing_configuration.latte');
$this->template->render();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<a href="{$link}" class="missing-configuration-widget">
<i class="zmdi zmdi-alert-triangle"></i>
Missing configuration keys
</a>
1 change: 1 addition & 0 deletions Mailer/app/config/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ services:
# Factories
- Remp\MailerModule\Components\IDataTableFactory
- Remp\MailerModule\Components\ISendingStatsFactory
- Remp\MailerModule\Components\IMissingConfigurationFactory
- Remp\MailerModule\Components\IGeneratorWidgetsFactory
- Remp\MailerModule\Components\IBatchExperimentEvaluationFactory

Expand Down
102 changes: 73 additions & 29 deletions Mailer/app/forms/ConfigFormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Nette\Application\UI\Form;
use Nette\SmartObject;
use Remp\MailerModule\Config\Config;
use Remp\MailerModule\Mailer\Mailer;
use Remp\MailerModule\Repository\ConfigsRepository;
use Remp\MailerModule\Sender\MailerFactory;

Expand Down Expand Up @@ -33,35 +34,76 @@ public function create()
$form = new Form;
$form->addProtection();

$configs = $this->configsRepository->all();
$container = $form->addContainer('settings');
$settings = $form->addContainer('settings');
$mailerContainer = $settings->addContainer('Mailer');

$configs = $this->configsRepository->all()->fetchAssoc('name');

$mailers = [];
$availableMailers = $this->mailerFactory->getAvailableMailers();
array_walk($availableMailers, function ($mailer, $name) use (&$mailers) {
$mailers[$name] = get_class($mailer);
});

$defaultMailer = $mailerContainer->addSelect('default_mailer', 'Default Mailer', $mailers)
->setDefaultValue($configs['default_mailer']['value']);
unset($configs['default_mailer']);

/** @var $mailer Mailer */
foreach ($this->mailerFactory->getAvailableMailers() as $mailer) {
$label = explode('\\', $mailers[$mailer->getAlias()]);
$mailerContainer = $settings->addContainer($label[count($label)-1]);

foreach ($mailer->getOptions() as $name => $option) {
$key = $mailer->getPrefix() . '_' . $name;
$config = $configs[$key];

if ($config['type'] === 'string') {
$item = $mailerContainer->addText($config['name'], $config['display_name'])
->setDefaultValue($config['value']);
}

if ($option['required']) {
$item->addConditionOn($defaultMailer, Form::EQUAL, $mailer->getAlias())
->setRequired("Field {$name} is required when mailer {$mailers[$mailer->getAlias()]} is selected");
}

unset($configs[$config['name']]);
}
}

$othersContainer = $settings->addContainer('Internal');

foreach ($configs as $config) {
$item = null;
if ($config->name == 'default_mailer') {
$mailers = [];
$availableMailers = $this->mailerFactory->getAvailableMailers();
array_walk($availableMailers, function ($mailer, $name) use (&$mailers) {
$mailers[$name] = get_class($mailer);
});

$item = $container->addSelect('default_mailer', 'Default Mailer', $mailers);
} elseif (in_array($config->type, [Config::TYPE_STRING, Config::TYPE_PASSWORD])) {
$item = $container->addText($config->name, $config->display_name ? $config->display_name : $config->name);
} elseif ($config->type == Config::TYPE_TEXT) {
$item = $container->addTextArea($config->name, $config->display_name ? $config->display_name : $config->name)
->getControlPrototype()->addAttributes(['class' => 'auto-size']);
} elseif ($config->type == Config::TYPE_HTML) {
$item = $container->addTextArea($config->name, $config->display_name ? $config->display_name : $config->name)
->setAttribute('rows', 15)
->getControlPrototype()->addAttributes(['class' => 'html-editor']);
} elseif ($config->type == Config::TYPE_BOOLEAN) {
$item = $container->addCheckbox($config->name, $config->display_name ? $config->display_name : $config->name);
} else {
throw new \Exception('unhandled config type: ' . $config->type);
}

$item->setDefaultValue($config->value);
// handle generic types
switch ($config['type']) :
case Config::TYPE_STRING:
case Config::TYPE_PASSWORD:
$othersContainer->addText($config['name'], $config['display_name'])
->setDefaultValue($config['value']);
break;
case Config::TYPE_TEXT:
$othersContainer->addTextArea($config['name'], $config['display_name'])
->setDefaultValue($config['value'])
->getControlPrototype()
->addAttributes(['class' => 'auto-size']);
break;
case Config::TYPE_HTML:
$othersContainer->addTextArea($config['name'], $config['display_name'])
->setAttribute('rows', 15)
->setDefaultValue($config['value'])
->getControlPrototype()
->addAttributes(['class' => 'html-editor']);
break;
case Config::TYPE_BOOLEAN:
$othersContainer->addCheckbox($config['name'], $config['display_name'])
->setDefaultValue($config['value']);
break;
default:
throw new \Exception('unhandled config type: ' . $config['type']);
endswitch;
}

$form->addSubmit('save', 'Save')
Expand All @@ -75,10 +117,12 @@ public function create()

public function formSucceeded($form, $values)
{
foreach ($values['settings'] as $name => $value) {
$config = $this->configsRepository->loadByName($name);
if ($config->value != $value) {
$this->configsRepository->update($config, ['value' => $value]);
foreach ($values['settings'] as $category => $configs) {
foreach ($configs as $name => $value) {
$config = $this->configsRepository->loadByName($name);
if ($config->value != $value) {
$this->configsRepository->update($config, ['value' => $value]);
}
}
}

Expand Down
46 changes: 38 additions & 8 deletions Mailer/app/models/Mailers/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public function __construct(
Config $config,
ConfigsRepository $configsRepository
) {

$this->configsRepository = $configsRepository;
$this->config = $config;

Expand All @@ -43,19 +42,50 @@ public function getConfig()

protected function buildConfig()
{
foreach ($this->options as $configName) {
foreach ($this->options as $name => $definition) {
$prefix = $this->getPrefix();

try {
$prefix = str_replace('-', '_', Strings::webalize(get_called_class()));
$this->options[$configName] = $this->config->get($prefix . '_' . $configName);
unset($this->options[array_search($configName, $this->options)]);
$this->options[$name]['value'] = $this->config->get($prefix . '_' . $name);
} catch (ConfigNotExistsException $e) {
$displayName = substr(get_called_class(), strrpos(get_called_class(), '\\') + 1) . ' ' . Strings::firstUpper($configName);
$description = 'Setting for ' . get_called_class();
$this->configsRepository->add($prefix . '_' . $configName, $displayName, null, $description, Config::TYPE_STRING);
$this->configsRepository->add($prefix . '_' . $name, $definition['label'], null, $description, Config::TYPE_STRING);

$this->options[$name] = [
'label' => $definition['label'],
'required' => $definition['required'],
'value' => null,
];
}
}
}

$this->options[$configName] = null;
public function getPrefix()
{
return str_replace('-', '_', Strings::webalize(get_called_class()));
}

public function isConfigured()
{
foreach ($this->getRequiredOptions() as $option) {
if (!isset($option['value'])) {
return false;
}
}

return true;
}

public function getOptions()
{
return $this->options;
}

public function getRequiredOptions()
{
return array_filter($this->options, function ($option) {
return $option['required'];
});
}

/**
Expand Down
13 changes: 11 additions & 2 deletions Mailer/app/models/Mailers/MailgunMailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,23 @@ class MailgunMailer extends Mailer implements IMailer

protected $alias = 'remp-mailgun';

protected $options = [ 'api_key', 'domain' ];
protected $options = [
'api_key' => [
'required' => true,
'label' => 'Mailgun API key',
],
'domain' => [
'required' => true,
'label' => 'Mailgun domain',
],
];

public function __construct(
Config $config,
ConfigsRepository $configsRepository
) {
parent::__construct($config, $configsRepository);
$this->mailer = Mailgun::create($this->options['api_key']);
$this->mailer = Mailgun::create($this->options['api_key']['value']);
}

public function send(Message $message)
Expand Down
23 changes: 22 additions & 1 deletion Mailer/app/models/Mailers/SmtpMailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,28 @@ class SmtpMailer extends Mailer implements IMailer

protected $alias = 'remp-smtp';

protected $options = [ 'host', 'port', 'username', 'password', 'secure' ];
protected $options = [
'host' => [
'required' => true,
'label' => 'SMTP host',
],
'port' => [
'required' => true,
'label' => 'SMTP Port',
],
'username' => [
'required' => false,
'label' => 'SMTP Username',
],
'password' => [
'required' => false,
'label' => 'SMTP Password',
],
'secure' => [
'required' => false,
'label' => 'SMTP Secure',
],
];

public function __construct(
Config $config,
Expand Down
7 changes: 7 additions & 0 deletions Mailer/app/presenters/BasePresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Kdyby\Autowired\AutowireComponentFactories;
use Kdyby\Autowired\AutowireProperties;
use Nette\Application\UI\Presenter;
use Remp\MailerModule\Components\IMissingConfigurationFactory;
use Remp\MailerModule\EnvironmentConfig;
use Remp\MailerModule\Forms\IFormFactory;

Expand Down Expand Up @@ -50,4 +51,10 @@ protected function redirectBasedOnButtonSubmitted(string $buttonSubmitted, int $
$this->redirect('Edit', $itemID);
}
}

public function createComponentMissingConfiguration(
IMissingConfigurationFactory $IMissingConfigurationFactory
) {
return $IMissingConfigurationFactory->create();
}
}
19 changes: 19 additions & 0 deletions Mailer/app/presenters/SettingsPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,30 @@
namespace Remp\MailerModule\Presenters;

use Remp\MailerModule\Forms\ConfigFormFactory;
use Remp\MailerModule\Mailer\Mailer;
use Remp\MailerModule\Sender\MailerFactory;

final class SettingsPresenter extends BasePresenter
{
/** @var MailerFactory */
private $mailerFactory;

public function __construct(MailerFactory $mailerFactory)
{
$this->mailerFactory = $mailerFactory;
}

public function renderDefault()
{
$availableMailers = $this->mailerFactory->getAvailableMailers();

$requiredFields = [];
array_walk($availableMailers, function ($mailer, $name) use (&$mailers, &$requiredFields) {
/** @var $mailer Mailer */
$requiredFields[$name] = $mailer->getRequiredOptions();
});

$this->template->requiredFields = $requiredFields;
}

public function createComponentConfigForm(ConfigFormFactory $configFormFactory)
Expand Down
7 changes: 7 additions & 0 deletions Mailer/app/presenters/templates/@layout.latte
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
<a n:ifset="$service[url]" href="{$service[url]}"><i class="zmdi zmdi-{$service[icon]} zmdi-hc-fw"></i> {$key}</a>
</li>
</ul>

<ul class="nav navbar-nav navbar-right">
<li class="dropdown hm-profile">
<a data-toggle="dropdown" href="">
Expand All @@ -74,6 +75,12 @@
</ul>
</li>
</ul>

<ul class="nav navbar-right">
<li>
{control missingConfiguration}
</li>
</ul>
</div>
</nav>
</div>
Expand Down
Loading

0 comments on commit debcb8b

Please sign in to comment.