Skip to content
Permalink
Browse files

Add config "mail_template_system" see BT#14894

Requires DB change, allow admins to setup custom email templates
  • Loading branch information...
jmontoyaa committed Oct 22, 2018
1 parent 54a8d0d commit 0c1446077b37c4fe9e8984cad0eec9a690454e8d
@@ -277,6 +277,13 @@
$items[] = ['url' => 'legal_add.php', 'label' => get_lang('TermsAndConditions')];
}
if (api_get_configuration_value('mail_template_system')) {
$items[] = [
'url' => api_get_path(WEB_CODE_PATH).'mail_template/list.php',
'label' => get_lang('MailTemplate'),
];
}
$blocks['platform']['items'] = $items;
$blocks['platform']['extra'] = null;
}
@@ -305,6 +305,19 @@ function display_drh_list(){
[],
true
);
$allowEmailTemplate = api_get_configuration_value('mail_template_system');
if ($allowEmailTemplate) {
$form->addEmailTemplate(
[
'subject_registration_platform.tpl',
'content_registration_platform.tpl',
'new_user_first_email_confirmation.tpl',
'new_user_second_email_confirmation.tpl',
]
);
}
$jquery_ready_content = $returnParams['jquery_ready_content'];
// the $jquery_ready_content variable collects all functions that will be load in the $(document).ready javascript function
@@ -381,6 +394,8 @@ function display_drh_list(){
}
}
$template = isset($user['email_template_option']) ? $user['email_template_option'] : [];
$user_id = UserManager::create_user(
$firstname,
$lastname,
@@ -399,7 +414,12 @@ function display_drh_list(){
$extra,
null,
$send_mail,
$platform_admin
$platform_admin,
'',
false,
null,
0,
$template
);
Security::clear_token();
@@ -329,6 +329,11 @@ function confirmation(name) {
);
$jqueryReadyContent = $returnParams['jquery_ready_content'];
$allowEmailTemplate = api_get_configuration_value('mail_template_system');
if ($allowEmailTemplate) {
$form->addEmailTemplate(['user_edit_content.tpl']);
}
// the $jqueryReadyContent variable collects all functions that will be load in the
// $(document).ready javascript function
$htmlHeadXtra[] = '<script>
@@ -431,6 +436,8 @@ function confirmation(name) {
$username = $email;
}
$template = isset($user['email_template_option']) ? $user['email_template_option'] : [];
UserManager::update_user(
$user_id,
$firstname,
@@ -452,7 +459,8 @@ function confirmation(name) {
null,
$send_mail,
$reset_password,
$address
$address,
$template
);
if (isset($user['student_boss'])) {
@@ -10,8 +10,20 @@
switch ($action) {
case 'select_option':
$id = isset($_REQUEST['id']) ? $_REQUEST['id'] : null;
$mail = new MailTemplateManager();
$item = $mail->get($id);
echo $item['template'];
if (!empty($id)) {
$mail = new MailTemplateManager();
$item = $mail->get($id);
echo $item['template'];
} else {
$templateName = isset($_REQUEST['template_name']) ? $_REQUEST['template_name'] : null;
if (!empty($templateName)) {
$templatePath = api_get_path(SYS_CODE_PATH).'template/default/mail/';
if (Security::check_abs_path($templatePath.$templateName, $templatePath)) {
if (file_exists($templatePath.$templateName)) {
echo file_get_contents($templatePath.$templateName);
}
}
}
}
break;
}
@@ -743,6 +743,10 @@ function getWhereClause($col, $oper, $val)
$obj = new Promotion();
$count = $obj->get_count();
break;
case 'get_mail_template':
$obj = new MailTemplateManager();
$count = $obj->get_count();
break;
case 'get_grade_models':
$obj = new GradeModel();
$count = $obj->get_count();
@@ -1940,6 +1944,32 @@ function getWhereClause($col, $oper, $val)
}
$result = $new_result;
break;
case 'get_mail_template':
$columns = ['name', 'type', 'default_template', 'actions'];
if (!in_array($sidx, $columns)) {
$sidx = 'name';
}
if (!in_array($sidx, $columns)) {
$sidx = 'name';
}
$result = Database::select(
'*',
$obj->table,
[
'where' => ['url_id = ? ' => api_get_current_access_url_id()],
'order' => "$sidx $sord",
'LIMIT' => "$start , $limit",
]
);
$new_result = [];
foreach ($result as $item) {
$new_result[] = $item;
}
$result = $new_result;
break;
case 'get_grade_models':
$columns = ['name', 'description', 'actions'];
if (!in_array($sidx, $columns)) {
@@ -2189,6 +2219,7 @@ function getWhereClause($col, $oper, $val)
$allowed_actions = [
'get_careers',
'get_promotions',
'get_mail_template',
'get_usergroups',
'get_usergroups_teacher',
'get_gradebooks',
@@ -183,4 +183,26 @@ public function setDefault($id)
return true;
}
/**
* @param int $templateId
* @param array $userInfo
*
* @return string
*/
public function parseTemplate($templateId, $userInfo)
{
$templateInfo = $this->get($templateId);
$emailTemplate = $templateInfo['template'];
$keys = array_keys($userInfo);
foreach ($keys as $key) {
$emailTemplate = str_replace("{{user.$key}}", $userInfo[$key], $emailTemplate);
}
$template = new Template();
$template->twig->setLoader(new \Twig_Loader_String());
$emailBody = $template->twig->render($emailTemplate);
return $emailBody;
}
}
@@ -1749,6 +1749,85 @@ public function addUserAvatar($name, $label, $imageSize = 'small', $subtitle = '
return $this->addElement('UserAvatar', $name, $label, ['image_size' => $imageSize, 'sub_title' => $subtitle]);
}
/**
* @param array $typeList
*/
public function addEmailTemplate($typeList)
{
$mailManager = new MailTemplateManager();
foreach ($typeList as $type) {
$list = $mailManager->get_all(
['where' => ['type = ? AND url_id = ?' => [$type, api_get_current_access_url_id()]]]
);
$options = [get_lang('Select')];
$name = $type;
$defaultId = '';
foreach ($list as $item) {
$options[$item['id']] = $item['name'];
$name = $item['name'];
if (empty($defaultId)) {
$defaultId = $item['default_template'] == 1 ? $item['id'] : '';
}
}
$url = api_get_path(WEB_AJAX_PATH).'mail.ajax.php?a=select_option';
$typeNoDots = 'email_template_option_'.str_replace('.tpl', '', $type);
$this->addSelect(
'email_template_option['.$type.']',
$name,
$options,
['id' => $typeNoDots]
);
$templateNoDots = 'email_template_'.str_replace('.tpl', '', $type);
$templateNoDotsBlock = 'email_template_block_'.str_replace('.tpl', '', $type);
$this->addHtml('<div id="'.$templateNoDotsBlock.'" style="display:none">');
$this->addTextarea(
$templateNoDots,
get_lang('Preview'),
['disabled' => 'disabled ', 'id' => $templateNoDots, 'rows' => '5']
);
$this->addHtml('</div>');
$this->addHtml("<script>
$(document).on('ready', function() {
var defaultValue = '$defaultId';
$('#$typeNoDots').val(defaultValue);
$('#$typeNoDots').selectpicker('render');
if (defaultValue != '') {
var selected = $('#$typeNoDots option:selected').val();
$.ajax({
url: '$url' + '&id=' + selected+ '&template_name=$type',
success: function (data) {
$('#$templateNoDots').html(data);
$('#$templateNoDotsBlock').show();
return;
},
});
}
$('#$typeNoDots').on('change', function(){
var selected = $('#$typeNoDots option:selected').val();
$.ajax({
url: '$url' + '&id=' + selected,
success: function (data) {
$('#$templateNoDots').html(data);
$('#$templateNoDotsBlock').show();
return;
},
});
});
});
</script>");
}
}
/**
* @param string $url page that will handle the upload
* @param string $inputName
@@ -139,6 +139,7 @@ public static function fetch($id)
* @param int|null $accessUrlId Optional.
*
* @return mixed Session ID on success, error message otherwise
*
* @todo use an array to replace all this parameters or use the model.lib.php ...
*/
public static function create_session(

0 comments on commit 0c14460

Please sign in to comment.
You can’t perform that action at this time.