mirror of
https://github.com/opensupports/opensupports.git
synced 2025-04-08 18:35:06 +02:00
Merged in email-sender-architecture (pull request #27)
Email sender architecture
This commit is contained in:
commit
511098e608
9
server/controllers/system.php
Normal file
9
server/controllers/system.php
Normal file
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
require_once 'system/init-settings.php';
|
||||
|
||||
$systemControllerGroup = new ControllerGroup();
|
||||
$systemControllerGroup->setGroupPath('/system');
|
||||
|
||||
$systemControllerGroup->addController(new InitSettingsController);
|
||||
|
||||
$systemControllerGroup->finalize();
|
66
server/controllers/system/init-settings.php
Normal file
66
server/controllers/system/init-settings.php
Normal file
@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
class InitSettingsController extends Controller {
|
||||
const PATH = '/init-settings';
|
||||
|
||||
public function validations() {
|
||||
return [
|
||||
'permission' => 'any',
|
||||
'requestData' => []
|
||||
];
|
||||
}
|
||||
|
||||
public function handler() {
|
||||
if (Setting::isTableEmpty()) {
|
||||
$this->storeGlobalSettings();
|
||||
$this->storeMailTemplates();
|
||||
|
||||
Response::respondSuccess();
|
||||
} else {
|
||||
Response::respondError(ERRORS::INIT_SETTINGS_DONE);
|
||||
}
|
||||
}
|
||||
|
||||
private function storeGlobalSettings() {
|
||||
$this->storeSettings([
|
||||
'language' => 'en',
|
||||
'no-reply-email' => 'noreply@opensupports.com',
|
||||
'smtp-host' => 'localhost',
|
||||
'smtp-port' => 7070,
|
||||
'smtp-user' => '',
|
||||
'smtp-pass' => '',
|
||||
]);
|
||||
}
|
||||
|
||||
private function storeMailTemplates() {
|
||||
$mails = InitialMails::retrieve();
|
||||
|
||||
foreach ($mails as $mailType => $mailLanguages) {
|
||||
foreach ($mailLanguages as $mailLanguage => $mailContent) {
|
||||
$mailTemplate = new MailTemplate();
|
||||
|
||||
$mailTemplate->setProperties([
|
||||
'type' => $mailType,
|
||||
'language' => $mailLanguage,
|
||||
'subject' => $mailContent['subject'],
|
||||
'body' => $mailContent['body']
|
||||
]);
|
||||
|
||||
$mailTemplate->store();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function storeSettings($settings) {
|
||||
foreach ($settings as $settingName => $settingValue) {
|
||||
$setting = new Setting();
|
||||
|
||||
$setting->setProperties([
|
||||
'name' => $settingName,
|
||||
'value' => $settingValue
|
||||
]);
|
||||
|
||||
$setting->store();
|
||||
}
|
||||
}
|
||||
}
|
@ -8,4 +8,5 @@ class ERRORS {
|
||||
const INVALID_EMAIL = 'Invalid email';
|
||||
const INVALID_PASSWORD = 'Invalid password';
|
||||
const INVALID_NAME = 'Invalid name';
|
||||
const INIT_SETTINGS_DONE = 'Settings already initialized';
|
||||
}
|
18
server/data/InitialMails.php
Normal file
18
server/data/InitialMails.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
class InitialMails {
|
||||
public static function retrieve() {
|
||||
return [
|
||||
'USER_SIGNUP' => [
|
||||
'en' => [
|
||||
'subject' => 'Signup {{to}} - OpenSupports',
|
||||
'body' => file_get_contents('data/mail-templates/user-signup-en.html')
|
||||
],
|
||||
'es' => [
|
||||
'subject' => 'Registrado {{to}} - OpenSupports',
|
||||
'body' => file_get_contents('data/mail-templates/user-signup-es.html')
|
||||
]
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
4
server/data/mail-templates/user-signup-en.html
Normal file
4
server/data/mail-templates/user-signup-en.html
Normal file
@ -0,0 +1,4 @@
|
||||
<div>
|
||||
Welcome, {{name}} to our support center,
|
||||
your email is {{to}}
|
||||
</div>
|
4
server/data/mail-templates/user-signup-es.html
Normal file
4
server/data/mail-templates/user-signup-es.html
Normal file
@ -0,0 +1,4 @@
|
||||
<div>
|
||||
Bienvenido, {{name}} a nuestro centro de soporte,
|
||||
su email es {{to}}
|
||||
</div>
|
@ -16,6 +16,15 @@ include_once 'libs/ControllerGroup.php';
|
||||
include_once 'libs/Hashing.php';
|
||||
include_once 'libs/MailSender.php';
|
||||
|
||||
// LOAD DATA
|
||||
spl_autoload_register(function ($class) {
|
||||
$classPath = "data/{$class}.php";
|
||||
|
||||
if(file_exists($classPath)) {
|
||||
include_once $classPath;
|
||||
}
|
||||
});
|
||||
|
||||
// LOAD MODELS
|
||||
spl_autoload_register(function ($class) {
|
||||
$classPath = "models/{$class}.php";
|
||||
|
@ -3,15 +3,13 @@ class MailSender {
|
||||
|
||||
private $mailOptions = [];
|
||||
|
||||
//TODO: Add real initial options when Settings class is available
|
||||
public function __construct() {
|
||||
$this->mailOptions['from'] = 'noreply@opensupports.com';
|
||||
$this->mailOptions['from'] = Setting::getSetting('no-reply-email');
|
||||
|
||||
//SMTP Options
|
||||
$this->mailOptions['smtp_host'] = 'localhost';
|
||||
$this->mailOptions['smtp_port'] = 7070;
|
||||
$this->mailOptions['smtp_user'] = '';
|
||||
$this->mailOptions['smtp_pass'] = '';
|
||||
$this->mailOptions['smtp-host'] = Setting::getSetting('smtp-host');
|
||||
$this->mailOptions['smtp-port'] = Setting::getSetting('smtp-host');
|
||||
$this->mailOptions['smtp-user'] = Setting::getSetting('smtp-host');
|
||||
$this->mailOptions['smtp-pass'] = Setting::getSetting('smtp-host');
|
||||
}
|
||||
|
||||
public function setTemplate($type, $config) {
|
||||
@ -29,14 +27,12 @@ class MailSender {
|
||||
$mailer->Subject = $this->mailOptions['subject'];
|
||||
$mailer->Body = $this->mailOptions['body'];
|
||||
|
||||
//$mailer->SMTPDebug = 3;
|
||||
$mailer->isSMTP();
|
||||
$mailer->SMTPAuth = true;
|
||||
$mailer->Host = $this->mailOptions['smtp_host'];
|
||||
$mailer->Port = $this->mailOptions['smtp_port'];
|
||||
$mailer->Username = $this->mailOptions['smtp_user'];
|
||||
$mailer->Password = $this->mailOptions['smtp_pass'];
|
||||
//$mailer->SMTPSecure = "tls";
|
||||
$mailer->Host = $this->mailOptions['smtp-host'];
|
||||
$mailer->Port = $this->mailOptions['smtp-port'];
|
||||
$mailer->Username = $this->mailOptions['smtp-user'];
|
||||
$mailer->Password = $this->mailOptions['smtp-pass'];
|
||||
$mailer->Timeout = 1000;
|
||||
|
||||
if ($mailer->smtpConnect()) {
|
||||
|
@ -12,8 +12,4 @@ class Comment extends DataStore {
|
||||
'date'
|
||||
);
|
||||
}
|
||||
|
||||
protected function getDefaultProps() {
|
||||
return array();
|
||||
}
|
||||
}
|
@ -4,8 +4,8 @@ use RedBeanPHP\Facade as RedBean;
|
||||
abstract class DataStore {
|
||||
protected $_bean;
|
||||
|
||||
public function getDefaultProps() {
|
||||
return [];
|
||||
public static function isTableEmpty() {
|
||||
return (RedBean::count(static::TABLE) === 0);
|
||||
}
|
||||
|
||||
public static function getDataStore($value, $property = 'id') {
|
||||
@ -31,6 +31,10 @@ abstract class DataStore {
|
||||
}
|
||||
}
|
||||
|
||||
public function getDefaultProps() {
|
||||
return [];
|
||||
}
|
||||
|
||||
public function delete() {
|
||||
RedBean::trash($this->getBeanInstance());
|
||||
unset($this);
|
||||
@ -65,7 +69,7 @@ abstract class DataStore {
|
||||
$validProp = false;
|
||||
|
||||
foreach (static::getProps() as $prop) {
|
||||
if($propToValidate === $prop) {
|
||||
if ($propToValidate === $prop) {
|
||||
$validProp = true;
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,6 @@
|
||||
<?php
|
||||
use RedBeanPHP\Facade as RedBean;
|
||||
|
||||
class MailTemplate extends DataStore {
|
||||
const TABLE = 'mailtemplate';
|
||||
|
||||
@ -6,22 +8,21 @@ class MailTemplate extends DataStore {
|
||||
const USER_PASSWORD = 'USER_PASSWORD';
|
||||
|
||||
public static function getTemplate($type) {
|
||||
//TODO: Add initial mails templates to the database
|
||||
//return MailTemplate::getDataStore($type, 'type');
|
||||
$globalLanguage = Setting::getSetting('language');
|
||||
|
||||
$bean = RedBean::findOne(MailTemplate::TABLE, 'type = :type AND language = :language', array(
|
||||
':type' => $type,
|
||||
':language' => $globalLanguage
|
||||
));
|
||||
|
||||
$template = new MailTemplate();
|
||||
$template->setProperties([
|
||||
'type' => $type,
|
||||
'subject' => 'Test Subject for {{to}}',
|
||||
'body' => 'Test body for {{to}}'
|
||||
]);
|
||||
return $template;
|
||||
return ($bean) ? new MailTemplate($bean) : null;
|
||||
}
|
||||
|
||||
public static function getProps() {
|
||||
return [
|
||||
'type',
|
||||
'subject',
|
||||
'language',
|
||||
'body'
|
||||
];
|
||||
}
|
||||
|
@ -12,8 +12,4 @@ class SessionCookie extends DataStore {
|
||||
'expirationDate'
|
||||
);
|
||||
}
|
||||
|
||||
public function getDefaultProps() {
|
||||
return array();
|
||||
}
|
||||
}
|
||||
|
19
server/models/Setting.php
Normal file
19
server/models/Setting.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
class Setting extends DataStore {
|
||||
const TABLE = 'setting';
|
||||
|
||||
public static function getSetting($name) {
|
||||
$dataStore = parent::getDataStore($name, 'name');
|
||||
|
||||
return ($dataStore !== null) ? $dataStore->value : null;
|
||||
}
|
||||
|
||||
public static function getProps() {
|
||||
return array(
|
||||
'name',
|
||||
'value',
|
||||
'permission'
|
||||
);
|
||||
}
|
||||
}
|
@ -5,7 +5,7 @@ include_once 'tests/__mocks__/ResponseMock.php';
|
||||
include_once 'tests/__mocks__/ControllerMock.php';
|
||||
include_once 'tests/__mocks__/SessionMock.php';
|
||||
include_once 'tests/__mocks__/UserMock.php';
|
||||
include_once 'models/ERRORS.php';
|
||||
include_once 'data/ERRORS.php';
|
||||
|
||||
include_once 'controllers/user/login.php';
|
||||
|
||||
|
@ -8,6 +8,7 @@ require './libs.rb'
|
||||
require './scripts.rb'
|
||||
|
||||
# TESTS
|
||||
require './system/init-settings.rb'
|
||||
require './user/signup.rb'
|
||||
require './user/login.rb'
|
||||
require './user/send-recover-password.rb'
|
||||
|
@ -1,5 +1,5 @@
|
||||
|
||||
def request(path, data)
|
||||
def request(path, data = {})
|
||||
uri = URI('http://localhost:8080' + path)
|
||||
response = Net::HTTP.post_form(uri, data)
|
||||
|
||||
@ -8,8 +8,8 @@ end
|
||||
|
||||
class Database
|
||||
def initialize()
|
||||
mysqlUser = ENV['MYSQL_USER'] || 'root';
|
||||
mysqlPass = ENV['MYSQL_PASSWORD'] || '';
|
||||
mysqlUser = ENV['MYSQL_USER'] || 'root'
|
||||
mysqlPass = ENV['MYSQL_PASSWORD'] || ''
|
||||
@connection = Mysql.new('localhost', mysqlUser , mysqlPass, 'development')
|
||||
end
|
||||
|
||||
|
10
tests/system/init-settings.rb
Normal file
10
tests/system/init-settings.rb
Normal file
@ -0,0 +1,10 @@
|
||||
describe '/system/init-settings' do
|
||||
it 'should initialize correctly' do
|
||||
result = request('/system/init-settings')
|
||||
|
||||
lang = $database.getRow('setting', 'language', 'name')
|
||||
|
||||
(result['status']).should.equal('success')
|
||||
(lang['value']).should.equal('en')
|
||||
end
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user