Skip to content
This repository has been archived by the owner on Sep 10, 2021. It is now read-only.

Commit

Permalink
Revise mail module configuration form and add install script
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamie Snape committed Dec 8, 2014
1 parent 6ccd95a commit 72cca12
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 40 deletions.
24 changes: 12 additions & 12 deletions modules/mail/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,22 @@ public function init()
*/
protected function handleSendMailMessage($params)
{
$provider = $this->Setting->getValueByName('provider', $this->moduleName);
$provider = $this->Setting->getValueByName(MAIL_PROVIDER_KEY, $this->moduleName);

if ($provider === 'app_engine') {
if ($provider === MAIL_PROVIDER_APP_ENGINE) {
$service = new Midas_Service_AppEngine_Mail();
$transport = new Midas_Mail_Transport_Service($service);
} elseif ($provider === 'send_grid') {
$username = $this->Setting->getValueByName('send_grid_username', $this->moduleName);
$password = $this->Setting->getValueByName('send_grid_password', $this->moduleName);
} elseif ($provider === MAIL_PROVIDER_SEND_GRID) {
$username = $this->Setting->getValueByName(MAIL_SEND_GRID_USERNAME_KEY, $this->moduleName);
$password = $this->Setting->getValueByName(MAIL_SEND_GRID_PASSWORD_KEY, $this->moduleName);
$service = new Midas_Service_SendGrid_Mail($username, $password);
$transport = new Midas_Mail_Transport_Service($service);
} elseif ($provider = 'smtp') {
$host = $this->Setting->getValueByName('smtp_host', $this->moduleName);
$port = $this->Setting->getValueByName('smtp_port', $this->moduleName);
$ssl = $this->Setting->getValueByName('smtp_use_ssl', $this->moduleName);
$username = $this->Setting->getValueByName('smtp_username', $this->moduleName);
$password = $this->Setting->getValueByName('smtp_password', $this->moduleName);
} elseif ($provider === MAIL_PROVIDER_SMTP) {
$host = $this->Setting->getValueByName(MAIL_SMTP_HOST_KEY, $this->moduleName);
$port = $this->Setting->getValueByName(MAIL_SMTP_PORT_KEY, $this->moduleName);
$ssl = $this->Setting->getValueByName(MAIL_SMTP_USE_SSL_KEY, $this->moduleName);
$username = $this->Setting->getValueByName(MAIL_SMTP_USERNAME_KEY, $this->moduleName);
$password = $this->Setting->getValueByName(MAIL_SMTP_PASSWORD_KEY, $this->moduleName);
$config = array();

if (!empty($port)) {
Expand All @@ -80,7 +80,7 @@ protected function handleSendMailMessage($params)
}

$mail = new Midas_Mail();
$mail->setFrom($this->Setting->getValueByName('from_address', $this->moduleName));
$mail->setFrom($this->Setting->getValueByName(MAIL_FROM_ADDRESS_KEY, $this->moduleName));

if (isset($params['bcc'])) {
$mail->addBcc($params['bcc']);
Expand Down
2 changes: 0 additions & 2 deletions modules/mail/configs/module.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,5 @@
fullname = "Mail"
description = "Send informational messages using email"
category = "Messaging"
dependencies =
conflicts =
uuid = b902fded-6820-4dad-8094-e9ef13143e32
version = "1.0.0"
44 changes: 44 additions & 0 deletions modules/mail/constant/module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/*=========================================================================
MIDAS Server
Copyright (c) Kitware SAS. 26 rue Louis Guérin. 69100 Villeurbanne, FRANCE
All rights reserved.
More information http://www.kitware.com
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0.txt
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=========================================================================*/

define('MAIL_PROVIDER_KEY', 'provider');
define('MAIL_PROVIDER_DEFAULT_VALUE', 'smtp');
define('MAIL_PROVIDER_APP_ENGINE', 'app_engine');
define('MAIL_PROVIDER_MAIL', 'mail');
define('MAIL_PROVIDER_SEND_GRID', 'send_grid');
define('MAIL_PROVIDER_SMTP', 'smtp');
define('MAIL_FROM_ADDRESS_KEY', 'from_address');
define('MAIL_FROM_ADDRESS_DEFAULT_VALUE', '');
define('MAIL_ADDRESS_VERIFICATION_KEY', 'address_verification');
define('MAIL_ADDRESS_VERIFICATION_DEFAULT_VALUE', 0);
define('MAIL_SEND_GRID_USERNAME_KEY', 'send_grid_username');
define('MAIL_SEND_GRID_USERNAME_DEFAULT_VALUE', '');
define('MAIL_SEND_GRID_PASSWORD_KEY', 'send_grid_password');
define('MAIL_SEND_GRID_PASSWORD_DEFAULT_VALUE', '');
define('MAIL_SMTP_HOST_KEY', 'smtp_host');
define('MAIL_SMTP_HOST_DEFAULT_VALUE', '');
define('MAIL_SMTP_PORT_KEY', 'smtp_port');
define('MAIL_SMTP_PORT_DEFAULT_VALUE', 587);
define('MAIL_SMTP_USE_SSL_KEY', 'smtp_use_ssl');
define('MAIL_SMTP_USE_SSL_DEFAULT_VALUE', 1);
define('MAIL_SMTP_USERNAME_KEY', 'smtp_username');
define('MAIL_SMTP_USERNAME_DEFAULT_VALUE', '');
define('MAIL_SMTP_PASSWORD_KEY', 'smtp_password');
define('MAIL_SMTP_PASSWORD_DEFAULT_VALUE', '');
7 changes: 5 additions & 2 deletions modules/mail/controllers/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ public function indexAction()
$values = $form->getValues();

foreach ($values as $key => $value) {
$this->Setting->setConfig($key, $value, $this->moduleName);
if ($value !== null) {
$this->Setting->setConfig($key, $value, $this->moduleName);
}
}
}

Expand All @@ -50,7 +52,7 @@ public function indexAction()
foreach ($elements as $element) {
$name = $element->getName();

if ($name !== 'submit') {
if ($name !== 'csrf' && $name !== 'submit') {
$value = $this->Setting->getValueByName($name, $this->moduleName);

if (!is_null($value)) {
Expand All @@ -61,5 +63,6 @@ public function indexAction()
}

$this->view->form = $form;
session_start();
}
}
45 changes: 45 additions & 0 deletions modules/mail/database/InstallScript.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/*=========================================================================
MIDAS Server
Copyright (c) Kitware SAS. 26 rue Louis Guérin. 69100 Villeurbanne, FRANCE
All rights reserved.
More information http://www.kitware.com
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0.txt
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=========================================================================*/

require_once BASE_PATH.'/modules/mail/constant/module.php';

/** Install the mail module. */
class Mail_InstallScript extends MIDASModuleInstallScript
{
/** @var string */
public $moduleName = 'mail';

/** Post database install. */
public function postInstall()
{
/** @var SettingModel $settingModel */
$settingModel = MidasLoader::loadModel('Setting');
$settingModel->setConfig(MAIL_PROVIDER_KEY, MAIL_PROVIDER_DEFAULT_VALUE, $this->moduleName);
$settingModel->setConfig(MAIL_FROM_ADDRESS_KEY, MAIL_FROM_ADDRESS_DEFAULT_VALUE, $this->moduleName);
$settingModel->setConfig(MAIL_ADDRESS_VERIFICATION_KEY, MAIL_ADDRESS_VERIFICATION_DEFAULT_VALUE, $this->moduleName);
$settingModel->setConfig(MAIL_SEND_GRID_USERNAME_KEY, MAIL_SEND_GRID_USERNAME_DEFAULT_VALUE, $this->moduleName);
$settingModel->setConfig(MAIL_SEND_GRID_PASSWORD_KEY, MAIL_SEND_GRID_PASSWORD_DEFAULT_VALUE, $this->moduleName);
$settingModel->setConfig(MAIL_SMTP_HOST_KEY, MAIL_SMTP_HOST_DEFAULT_VALUE, $this->moduleName);
$settingModel->setConfig(MAIL_SMTP_PORT_KEY, MAIL_SMTP_PORT_DEFAULT_VALUE, $this->moduleName);
$settingModel->setConfig(MAIL_SMTP_USE_SSL_KEY, MAIL_SMTP_USE_SSL_DEFAULT_VALUE, $this->moduleName);
$settingModel->setConfig(MAIL_SMTP_USERNAME_KEY, MAIL_SMTP_USERNAME_DEFAULT_VALUE, $this->moduleName);
$settingModel->setConfig(MAIL_SMTP_PASSWORD_KEY, MAIL_SMTP_PASSWORD_DEFAULT_VALUE, $this->moduleName);
}
}
43 changes: 22 additions & 21 deletions modules/mail/forms/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,64 +24,65 @@ class Mail_Form_Admin extends Zend_Form
/** Initialize this form. */
public function init()
{
$this->setName('mail_config');
$this->setName('mail_admin');
$this->setMethod('POST');

$provider = new Zend_Form_Element_Select('provider');
$csrf = new Midas_Form_Element_Hash('csrf');
$csrf->setSalt('5qzSHzCdNuPfYaT99Jq5WcKe');
$csrf->setDecorators(array('ViewHelper'));

$provider = new Zend_Form_Element_Select(MAIL_PROVIDER_KEY);
$provider->setLabel('Provider');
$provider->setRequired(true);
$provider->addValidator('NotEmpty', true);

if (class_exists('\google\appengine\api\mail\Message', false)) {
$provider->addMultiOption('app_engine', 'Google App Engine');
}

$provider->addMultiOptions(array(
'mail' => 'PHP Mail Function',
'send_grid' => 'SendGrid Service',
'smtp' => 'External SMTP Server',
MAIL_PROVIDER_APP_ENGINE => 'Google App Engine',
MAIL_PROVIDER_MAIL => 'PHP Mail Function',
MAIL_PROVIDER_SEND_GRID => 'SendGrid Service',
MAIL_PROVIDER_SMTP => 'External SMTP Server',
));

$fromAddress = new Zend_Form_Element_Text('from_address');
$fromAddress = new Zend_Form_Element_Text(MAIL_FROM_ADDRESS_KEY);
$fromAddress->setLabel('From email address');
$fromAddress->setRequired(true);
$fromAddress->addValidator('NotEmpty', true);
$fromAddress->addValidator('EmailAddress', true);

$addressVerification = new Zend_Form_Element_Checkbox('address_verification');
$addressVerification = new Zend_Form_Element_Checkbox(MAIL_ADDRESS_VERIFICATION_KEY);
$addressVerification->setLabel('Require email address verification');

$this->addDisplayGroup(array($provider, $fromAddress, $addressVerification), 'global');

$sendGridUsername = new Zend_Form_Element_Text('send_grid_username');
$sendGridUsername = new Zend_Form_Element_Text(MAIL_SEND_GRID_USERNAME_KEY);
$sendGridUsername->setLabel('SendGrid User Name');
$sendGridUsername->addValidator('NotEmpty', true);

$sendGridPassword = new Zend_Form_Element_Text('send_grid_password');
$sendGridPassword = new Zend_Form_Element_Text(MAIL_SEND_GRID_PASSWORD_KEY);
$sendGridPassword->setLabel('SendGrid Password');
$sendGridPassword->addValidator('NotEmpty', true);

$this->addDisplayGroup(array($sendGridUsername, $sendGridPassword), 'send_grid');

$smtpHost = new Zend_Form_Element_Text('smtp_host');
$smtpHost = new Zend_Form_Element_Text(MAIL_SMTP_HOST_KEY);
$smtpHost->setLabel('Server name');
$smtpHost->addValidator('NotEmpty', true);
$smtpHost->addValidator('Hostname', true);

$smtpPort = new Zend_Form_Element_Text('smtp_port');
$smtpPort = new Zend_Form_Element_Text(MAIL_SMTP_PORT_KEY);
$smtpPort->setLabel('Port');
$smtpPort->addValidator('NotEmpty', true);
$smtpPort->addValidator('Digits', true);
$smtpPort->addValidator('Between', array('min' => 1, 'max' => 65535));
$smtpPort->addValidator('Between', true, array('min' => 1, 'max' => 65535));
$smtpPort->setAttrib('maxlength', 5);

$smtpUseSsl = new Zend_Form_Element_Checkbox('smtp_use_ssl');
$smtpUseSsl = new Zend_Form_Element_Checkbox(MAIL_SMTP_USE_SSL_KEY);
$smtpUseSsl->setLabel('Use SSL');

$smtpUsername = new Zend_Form_Element_Text('smtp_username');
$smtpUsername = new Zend_Form_Element_Text(MAIL_SMTP_USERNAME_KEY);
$smtpUsername->setLabel('User name');
$smtpUsername->addValidator('NotEmpty', true);

$smtpPassword = new Zend_Form_Element_Text('smtp_password');
$smtpPassword = new Zend_Form_Element_Password(MAIL_SMTP_PASSWORD_KEY);
$smtpPassword->setLabel('Password');
$smtpPassword->addValidator('NotEmpty', true);

Expand All @@ -90,6 +91,6 @@ public function init()
$submit = new Zend_Form_Element_Submit('submit');
$submit->setLabel('Save');

$this->addElements(array($provider, $fromAddress, $addressVerification, $smtpHost, $smtpPort, $smtpUseSsl, $smtpUsername, $smtpPassword, $submit));
$this->addElements(array($csrf, $provider, $fromAddress, $addressVerification, $smtpHost, $smtpPort, $smtpUseSsl, $smtpUsername, $smtpPassword, $submit));
}
}
6 changes: 3 additions & 3 deletions modules/mail/views/admin/index.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
=========================================================================*/

$this->declareVars('form', 'pageTitle');
$this->headTitle(htmlspecialchars($this->pageTitle, ENT_QUOTES, 'UTF-8'));
$this->headTitle($this->escape($this->pageTitle));
?>

<div class="viewMain">
<h1><?php echo htmlspecialchars($this->pageTitle, ENT_QUOTES, 'UTF-8'); ?></h1>
<h1><?php echo $this->escape($this->pageTitle); ?></h1>
<?php echo $this->form; ?>
<p><a href="<?php $this->url(array('controller' => 'admin', 'action' => 'index')); ?>#tabs-modules">&laquo; Back to Modules Administration</a></p>
<p><a href="<?php echo $this->url(array('controller' => 'admin', 'action' => 'index'), 'default'); ?>#tabs-modules">&laquo; Back to Modules Administration</a></p>
</div>

0 comments on commit 72cca12

Please sign in to comment.