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_EMAIL = 'Invalid email';
|
||||||
const INVALID_PASSWORD = 'Invalid password';
|
const INVALID_PASSWORD = 'Invalid password';
|
||||||
const INVALID_NAME = 'Invalid name';
|
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/Hashing.php';
|
||||||
include_once 'libs/MailSender.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
|
// LOAD MODELS
|
||||||
spl_autoload_register(function ($class) {
|
spl_autoload_register(function ($class) {
|
||||||
$classPath = "models/{$class}.php";
|
$classPath = "models/{$class}.php";
|
||||||
|
@ -3,15 +3,13 @@ class MailSender {
|
|||||||
|
|
||||||
private $mailOptions = [];
|
private $mailOptions = [];
|
||||||
|
|
||||||
//TODO: Add real initial options when Settings class is available
|
|
||||||
public function __construct() {
|
public function __construct() {
|
||||||
$this->mailOptions['from'] = 'noreply@opensupports.com';
|
$this->mailOptions['from'] = Setting::getSetting('no-reply-email');
|
||||||
|
|
||||||
//SMTP Options
|
$this->mailOptions['smtp-host'] = Setting::getSetting('smtp-host');
|
||||||
$this->mailOptions['smtp_host'] = 'localhost';
|
$this->mailOptions['smtp-port'] = Setting::getSetting('smtp-host');
|
||||||
$this->mailOptions['smtp_port'] = 7070;
|
$this->mailOptions['smtp-user'] = Setting::getSetting('smtp-host');
|
||||||
$this->mailOptions['smtp_user'] = '';
|
$this->mailOptions['smtp-pass'] = Setting::getSetting('smtp-host');
|
||||||
$this->mailOptions['smtp_pass'] = '';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setTemplate($type, $config) {
|
public function setTemplate($type, $config) {
|
||||||
@ -29,14 +27,12 @@ class MailSender {
|
|||||||
$mailer->Subject = $this->mailOptions['subject'];
|
$mailer->Subject = $this->mailOptions['subject'];
|
||||||
$mailer->Body = $this->mailOptions['body'];
|
$mailer->Body = $this->mailOptions['body'];
|
||||||
|
|
||||||
//$mailer->SMTPDebug = 3;
|
|
||||||
$mailer->isSMTP();
|
$mailer->isSMTP();
|
||||||
$mailer->SMTPAuth = true;
|
$mailer->SMTPAuth = true;
|
||||||
$mailer->Host = $this->mailOptions['smtp_host'];
|
$mailer->Host = $this->mailOptions['smtp-host'];
|
||||||
$mailer->Port = $this->mailOptions['smtp_port'];
|
$mailer->Port = $this->mailOptions['smtp-port'];
|
||||||
$mailer->Username = $this->mailOptions['smtp_user'];
|
$mailer->Username = $this->mailOptions['smtp-user'];
|
||||||
$mailer->Password = $this->mailOptions['smtp_pass'];
|
$mailer->Password = $this->mailOptions['smtp-pass'];
|
||||||
//$mailer->SMTPSecure = "tls";
|
|
||||||
$mailer->Timeout = 1000;
|
$mailer->Timeout = 1000;
|
||||||
|
|
||||||
if ($mailer->smtpConnect()) {
|
if ($mailer->smtpConnect()) {
|
||||||
|
@ -12,8 +12,4 @@ class Comment extends DataStore {
|
|||||||
'date'
|
'date'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getDefaultProps() {
|
|
||||||
return array();
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -4,8 +4,8 @@ use RedBeanPHP\Facade as RedBean;
|
|||||||
abstract class DataStore {
|
abstract class DataStore {
|
||||||
protected $_bean;
|
protected $_bean;
|
||||||
|
|
||||||
public function getDefaultProps() {
|
public static function isTableEmpty() {
|
||||||
return [];
|
return (RedBean::count(static::TABLE) === 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getDataStore($value, $property = 'id') {
|
public static function getDataStore($value, $property = 'id') {
|
||||||
@ -31,6 +31,10 @@ abstract class DataStore {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getDefaultProps() {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
public function delete() {
|
public function delete() {
|
||||||
RedBean::trash($this->getBeanInstance());
|
RedBean::trash($this->getBeanInstance());
|
||||||
unset($this);
|
unset($this);
|
||||||
@ -65,7 +69,7 @@ abstract class DataStore {
|
|||||||
$validProp = false;
|
$validProp = false;
|
||||||
|
|
||||||
foreach (static::getProps() as $prop) {
|
foreach (static::getProps() as $prop) {
|
||||||
if($propToValidate === $prop) {
|
if ($propToValidate === $prop) {
|
||||||
$validProp = true;
|
$validProp = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
use RedBeanPHP\Facade as RedBean;
|
||||||
|
|
||||||
class MailTemplate extends DataStore {
|
class MailTemplate extends DataStore {
|
||||||
const TABLE = 'mailtemplate';
|
const TABLE = 'mailtemplate';
|
||||||
|
|
||||||
@ -6,22 +8,21 @@ class MailTemplate extends DataStore {
|
|||||||
const USER_PASSWORD = 'USER_PASSWORD';
|
const USER_PASSWORD = 'USER_PASSWORD';
|
||||||
|
|
||||||
public static function getTemplate($type) {
|
public static function getTemplate($type) {
|
||||||
//TODO: Add initial mails templates to the database
|
$globalLanguage = Setting::getSetting('language');
|
||||||
//return MailTemplate::getDataStore($type, 'type');
|
|
||||||
|
$bean = RedBean::findOne(MailTemplate::TABLE, 'type = :type AND language = :language', array(
|
||||||
|
':type' => $type,
|
||||||
|
':language' => $globalLanguage
|
||||||
|
));
|
||||||
|
|
||||||
$template = new MailTemplate();
|
return ($bean) ? new MailTemplate($bean) : null;
|
||||||
$template->setProperties([
|
|
||||||
'type' => $type,
|
|
||||||
'subject' => 'Test Subject for {{to}}',
|
|
||||||
'body' => 'Test body for {{to}}'
|
|
||||||
]);
|
|
||||||
return $template;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getProps() {
|
public static function getProps() {
|
||||||
return [
|
return [
|
||||||
'type',
|
'type',
|
||||||
'subject',
|
'subject',
|
||||||
|
'language',
|
||||||
'body'
|
'body'
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
@ -12,8 +12,4 @@ class SessionCookie extends DataStore {
|
|||||||
'expirationDate'
|
'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__/ControllerMock.php';
|
||||||
include_once 'tests/__mocks__/SessionMock.php';
|
include_once 'tests/__mocks__/SessionMock.php';
|
||||||
include_once 'tests/__mocks__/UserMock.php';
|
include_once 'tests/__mocks__/UserMock.php';
|
||||||
include_once 'models/ERRORS.php';
|
include_once 'data/ERRORS.php';
|
||||||
|
|
||||||
include_once 'controllers/user/login.php';
|
include_once 'controllers/user/login.php';
|
||||||
|
|
||||||
|
@ -8,6 +8,7 @@ require './libs.rb'
|
|||||||
require './scripts.rb'
|
require './scripts.rb'
|
||||||
|
|
||||||
# TESTS
|
# TESTS
|
||||||
|
require './system/init-settings.rb'
|
||||||
require './user/signup.rb'
|
require './user/signup.rb'
|
||||||
require './user/login.rb'
|
require './user/login.rb'
|
||||||
require './user/send-recover-password.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)
|
uri = URI('http://localhost:8080' + path)
|
||||||
response = Net::HTTP.post_form(uri, data)
|
response = Net::HTTP.post_form(uri, data)
|
||||||
|
|
||||||
@ -8,8 +8,8 @@ end
|
|||||||
|
|
||||||
class Database
|
class Database
|
||||||
def initialize()
|
def initialize()
|
||||||
mysqlUser = ENV['MYSQL_USER'] || 'root';
|
mysqlUser = ENV['MYSQL_USER'] || 'root'
|
||||||
mysqlPass = ENV['MYSQL_PASSWORD'] || '';
|
mysqlPass = ENV['MYSQL_PASSWORD'] || ''
|
||||||
@connection = Mysql.new('localhost', mysqlUser , mysqlPass, 'development')
|
@connection = Mysql.new('localhost', mysqlUser , mysqlPass, 'development')
|
||||||
end
|
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