diff --git a/server/controllers/system.php b/server/controllers/system.php
new file mode 100644
index 00000000..cea7f0c1
--- /dev/null
+++ b/server/controllers/system.php
@@ -0,0 +1,9 @@
+setGroupPath('/system');
+
+$systemControllerGroup->addController(new InitSettingsController);
+
+$systemControllerGroup->finalize();
\ No newline at end of file
diff --git a/server/controllers/system/init-settings.php b/server/controllers/system/init-settings.php
new file mode 100644
index 00000000..578791b5
--- /dev/null
+++ b/server/controllers/system/init-settings.php
@@ -0,0 +1,66 @@
+ '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();
+ }
+ }
+}
\ No newline at end of file
diff --git a/server/models/ERRORS.php b/server/data/ERRORS.php
similarity index 87%
rename from server/models/ERRORS.php
rename to server/data/ERRORS.php
index 4c3017ce..52e5ad95 100644
--- a/server/models/ERRORS.php
+++ b/server/data/ERRORS.php
@@ -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';
}
diff --git a/server/models/EXCEPTIONS.php b/server/data/EXCEPTIONS.php
similarity index 100%
rename from server/models/EXCEPTIONS.php
rename to server/data/EXCEPTIONS.php
diff --git a/server/data/InitialMails.php b/server/data/InitialMails.php
new file mode 100644
index 00000000..cadfa401
--- /dev/null
+++ b/server/data/InitialMails.php
@@ -0,0 +1,18 @@
+ [
+ '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')
+ ]
+ ]
+ ];
+ }
+}
\ No newline at end of file
diff --git a/server/data/mail-templates/user-signup-en.html b/server/data/mail-templates/user-signup-en.html
new file mode 100644
index 00000000..371a2441
--- /dev/null
+++ b/server/data/mail-templates/user-signup-en.html
@@ -0,0 +1,4 @@
+
+ Welcome, {{name}} to our support center,
+ your email is {{to}}
+
diff --git a/server/data/mail-templates/user-signup-es.html b/server/data/mail-templates/user-signup-es.html
new file mode 100644
index 00000000..0f633383
--- /dev/null
+++ b/server/data/mail-templates/user-signup-es.html
@@ -0,0 +1,4 @@
+
+ Bienvenido, {{name}} a nuestro centro de soporte,
+ su email es {{to}}
+
\ No newline at end of file
diff --git a/server/index.php b/server/index.php
index 04edd2e7..7a72813e 100644
--- a/server/index.php
+++ b/server/index.php
@@ -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";
diff --git a/server/libs/MailSender.php b/server/libs/MailSender.php
index e419f07f..869b97c2 100644
--- a/server/libs/MailSender.php
+++ b/server/libs/MailSender.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()) {
diff --git a/server/models/Comment.php b/server/models/Comment.php
index da011200..c5dfb3c5 100644
--- a/server/models/Comment.php
+++ b/server/models/Comment.php
@@ -12,8 +12,4 @@ class Comment extends DataStore {
'date'
);
}
-
- protected function getDefaultProps() {
- return array();
- }
}
\ No newline at end of file
diff --git a/server/models/DataStore.php b/server/models/DataStore.php
index 7a782707..b2ce2d64 100644
--- a/server/models/DataStore.php
+++ b/server/models/DataStore.php
@@ -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;
}
}
diff --git a/server/models/MailTemplate.php b/server/models/MailTemplate.php
index 27742437..14bf7fa0 100644
--- a/server/models/MailTemplate.php
+++ b/server/models/MailTemplate.php
@@ -1,4 +1,6 @@
$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'
];
}
diff --git a/server/models/SessionCookie.php b/server/models/SessionCookie.php
index 5a9e7f06..94460333 100644
--- a/server/models/SessionCookie.php
+++ b/server/models/SessionCookie.php
@@ -12,8 +12,4 @@ class SessionCookie extends DataStore {
'expirationDate'
);
}
-
- public function getDefaultProps() {
- return array();
- }
}
diff --git a/server/models/Setting.php b/server/models/Setting.php
new file mode 100644
index 00000000..c6015d8d
--- /dev/null
+++ b/server/models/Setting.php
@@ -0,0 +1,19 @@
+value : null;
+ }
+
+ public static function getProps() {
+ return array(
+ 'name',
+ 'value',
+ 'permission'
+ );
+ }
+}
\ No newline at end of file
diff --git a/server/tests/controllers/user/loginTest.php b/server/tests/controllers/user/loginTest.php
index 7b80f38b..13d29800 100644
--- a/server/tests/controllers/user/loginTest.php
+++ b/server/tests/controllers/user/loginTest.php
@@ -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';
diff --git a/tests/init.rb b/tests/init.rb
index 9a3a25e6..5c5f792b 100644
--- a/tests/init.rb
+++ b/tests/init.rb
@@ -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'
diff --git a/tests/libs.rb b/tests/libs.rb
index 7472135a..1da036ee 100644
--- a/tests/libs.rb
+++ b/tests/libs.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
diff --git a/tests/system/init-settings.rb b/tests/system/init-settings.rb
new file mode 100644
index 00000000..e12fd28d
--- /dev/null
+++ b/tests/system/init-settings.rb
@@ -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
\ No newline at end of file