From 4ccbd757593e6de41f4fa4a9741e3984726ec5ca Mon Sep 17 00:00:00 2001 From: ivan Date: Tue, 26 Jul 2016 18:30:51 -0300 Subject: [PATCH] Ivan - Add initial settings and initial mail templates [skip ci] --- server/controllers/system.php | 9 +++ server/controllers/system/init-settings.php | 61 +++++++++++++++++++ server/{models => data}/ERRORS.php | 1 + server/{models => data}/EXCEPTIONS.php | 0 server/data/InitialMails.php | 18 ++++++ .../data/mail-templates/user-signup-en.html | 4 ++ .../data/mail-templates/user-signup-es.html | 4 ++ server/index.php | 9 +++ server/models/Comment.php | 4 -- server/models/DataStore.php | 10 ++- server/models/MailTemplate.php | 18 +++--- server/models/SessionCookie.php | 4 -- server/models/Setting.php | 17 ++++++ 13 files changed, 139 insertions(+), 20 deletions(-) create mode 100644 server/controllers/system.php create mode 100644 server/controllers/system/init-settings.php rename server/{models => data}/ERRORS.php (87%) rename server/{models => data}/EXCEPTIONS.php (100%) create mode 100644 server/data/InitialMails.php create mode 100644 server/data/mail-templates/user-signup-en.html create mode 100644 server/data/mail-templates/user-signup-es.html create mode 100644 server/models/Setting.php 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..e1a3bbbe --- /dev/null +++ b/server/controllers/system/init-settings.php @@ -0,0 +1,61 @@ + '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' + ]); + } + + 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/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..2c41041a 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() { 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..5c961113 --- /dev/null +++ b/server/models/Setting.php @@ -0,0 +1,17 @@ +