Ivan - Add initial settings and initial mail templates [skip ci]

This commit is contained in:
ivan 2016-07-26 18:30:51 -03:00
parent 8fa096f662
commit 4ccbd75759
13 changed files with 139 additions and 20 deletions

View File

@ -0,0 +1,9 @@
<?php
require_once 'system/init-settings.php';
$systemControllerGroup = new ControllerGroup();
$systemControllerGroup->setGroupPath('/system');
$systemControllerGroup->addController(new InitSettingsController);
$systemControllerGroup->finalize();

View File

@ -0,0 +1,61 @@
<?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'
]);
}
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();
}
}
}

View File

@ -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';
}

View 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')
]
]
];
}
}

View File

@ -0,0 +1,4 @@
<div>
Welcome, {{name}} to our support center,
your email is {{to}}
</div>

View File

@ -0,0 +1,4 @@
<div>
Bienvenido, {{name}} a nuestro centro de soporte,
su email es {{to}}
</div>

View File

@ -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";

View File

@ -12,8 +12,4 @@ class Comment extends DataStore {
'date'
);
}
protected function getDefaultProps() {
return array();
}
}

View File

@ -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;
}
}

View File

@ -1,4 +1,6 @@
<?php
use RedBeanPHP\Facade as RedBean;
class MailTemplate extends DataStore {
const TABLE = 'mailtemplate';
@ -6,16 +8,14 @@ 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() {

View File

@ -12,8 +12,4 @@ class SessionCookie extends DataStore {
'expirationDate'
);
}
public function getDefaultProps() {
return array();
}
}

17
server/models/Setting.php Normal file
View File

@ -0,0 +1,17 @@
<?php
class Setting extends DataStore {
const TABLE = 'setting';
public static function getSetting($name) {
return parent::getDataStore('name', $name);
}
public static function getProps() {
return array(
'name',
'value',
'permission'
);
}
}