Skip to content
Permalink
Browse files

WIP - Add Mail template manager see BT#14894

  • Loading branch information...
jmontoyaa committed Oct 22, 2018
1 parent 53f18dc commit 59d8aec747510cd3d3a583571d02421739b73dc6
Showing with 347 additions and 0 deletions.
  1. +188 −0 main/inc/lib/MailTemplateManager.php
  2. +159 −0 main/mail_template/list.php
@@ -0,0 +1,188 @@
<?php
/* For licensing terms, see /license.txt */
use Symfony\Component\Finder\Finder;
/**
* Class MailTemplateManager.
*/
class MailTemplateManager extends Model
{
public $columns = [
'id',
'name',
'template',
'type',
'system',
'url_id',
'default_template',
'created_at',
'updated_at',
'author_id'
];
/**
*/
public function __construct()
{
parent::__construct();
$this->table = 'mail_template';
}
/**
* @return int
*/
public function get_count()
{
$row = Database::select(
'count(*) as count',
$this->table,
['where' => ['url_id = ? ' => api_get_current_access_url_id()]],
'first'
);
return $row['count'];
}
/**
* Displays the title + grid.
*
* @return string html code
*/
public function display()
{
// Action links
$html = '<div class="actions" style="margin-bottom:20px">';
$html .= '<a href="'.api_get_path(WEB_CODE_PATH).'admin">'.
Display::return_icon(
'back.png',
get_lang('Back'),
'',
'32'
)
.'</a>';
$html .= '<a href="'.api_get_self().'?action=add">'.
Display::return_icon(
'add.png',
get_lang('Add'),
'',
'32'
).'</a>';
$html .='</div>';
$html .= Display::grid_html('mail_template');
return $html;
}
/**
* Returns a Form validator Obj.
*
* @param string $url
* @param string $action
*
* @return FormValidator
*/
public function returnForm($url, $action = 'add')
{
$form = new FormValidator('template', 'post', $url);
// Setting the form elements
$header = get_lang('Add');
if ($action === 'edit') {
$header = get_lang('Modify');
}
$id = isset($_GET['id']) ? (int) $_GET['id'] : '';
$form->addElement('header', '', $header);
$form->addElement('hidden', 'id', $id);
$form->addElement(
'text',
'name',
get_lang('Name'),
['size' => '70', 'id' => 'name']
);
/*$form->addHtmlEditor(
'email_template',
get_lang('Template'),
false,
false,
[
'ToolbarSet' => 'Careers',
'Width' => '100%',
'Height' => '250',
]
);*/
$form->addTextarea(
'email_template',
get_lang('Template')
);
$finder = new Finder();
$files = $finder
->files()
->in(api_get_path(SYS_CODE_PATH).'template/default/mail')
->sort(
function ($a, $b) {
return strcmp($a->getRealpath(), $b->getRealpath());
}
);
$options = [];
/** @var SplFileInfo $file */
foreach ($files as $file) {
$options[$file->getFilename()] = $file->getFilename();
}
$form->addSelect(
'type',
get_lang('Type'),
$options
);
$defaults = $this->get($id);
if ($action === 'edit') {
$form->addLabel(get_lang('CreatedAt'), Display::dateToStringAgoAndLongDate($defaults['created_at']));
$form->addLabel(get_lang('UpdatedAt'), Display::dateToStringAgoAndLongDate($defaults['updated_at']));
$form->addButtonSave(get_lang('Modify'), 'submit');
} else {
$form->addButtonCreate(get_lang('Add'), 'submit');
}
// Setting the defaults
if (!empty($defaults)) {
$defaults['email_template'] = $defaults['template'];
}
$form->setDefaults($defaults);
// Setting the rules
$form->addRule('name', get_lang('ThisFieldIsRequired'), 'required');
return $form;
}
/**
* @param int $id
*
* @return bool
*/
public function setDefault($id)
{
$template = $this->get($id);
if (empty($template)) {
return false;
}
$type = $template['type'];
$urlId = api_get_current_access_url_id();
$sql = "UPDATE {$this->table} SET default_template = 0
WHERE type = '$type' AND url_id = $urlId";
Database::query($sql);
$sql = "UPDATE {$this->table} SET default_template = 1
WHERE id = $id";
Database::query($sql);
return true;
}
}
@@ -0,0 +1,159 @@
<?php
/* For licensing terms, see /license.txt */
require_once __DIR__.'/../inc/global.inc.php';
api_protect_admin_script();
//Adds the JS needed to use the jqgrid
$htmlHeadXtra[] = api_get_jqgrid_js();
$action = !empty($_REQUEST['action']) ? $_REQUEST['action'] : 'list';
$check = Security::check_token('request');
$token = Security::get_token();
$id = !empty($_REQUEST['id']) ? $_REQUEST['id'] : 0;
$mailTemplate = new MailTemplateManager();
$content = '';
switch ($action) {
case 'add':
$url = api_get_self().'?action='.Security::remove_XSS($_GET['action']);
$form = $mailTemplate->returnForm($url, 'add');
// The validation or display
if ($form->validate()) {
if ($check) {
$values = $form->exportValues();
$values['template'] = $values['email_template'];
$values['author_id'] = api_get_user_id();
$values['url_id'] = api_get_current_access_url_id();
$res = $mailTemplate->save($values);
if ($res) {
Display::addFlash(Display::return_message(get_lang('ItemAdded'), 'confirm'));
}
}
header('Location: '.api_get_self());
exit;
} else {
$content .= '<div class="actions">';
$content .= Display::url(
Display::return_icon('back.png', get_lang('Back'), '', ICON_SIZE_MEDIUM),
api_get_self()
);
$content .= '</div>';
$form->addElement('hidden', 'sec_token');
$form->setConstants(['sec_token' => $token]);
$content .= $form->returnForm();
}
break;
case 'edit':
$url = api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&id='.$id;
$form = $mailTemplate->returnForm($url, 'edit');
$content .= '<div class="actions">';
$content .= Display::url(
Display::return_icon('back.png', get_lang('Back'), '', ICON_SIZE_MEDIUM),
api_get_self()
);
$content .= '</div>';
$content .= $form->returnForm();
// The validation or display
if ($form->validate()) {
//if ($check) {
$values = $form->exportValues();
$values['template'] = $values['email_template'];
$res = $mailTemplate->update($values);
if ($res) {
Display::addFlash(
Display::return_message(get_lang('ItemUpdated').': '.$values['name'], 'confirm')
);
}
//}
header('Location: '.api_get_self());
exit;
}
break;
case 'delete':
$mailTemplate->delete($id);
Display::addFlash(
Display::return_message(get_lang('Deleted'),'confirm')
);
header('Location: '.api_get_self());
exit;
break;
case 'set_default':
$mailTemplate->setDefault($id);
Display::addFlash(
Display::return_message(get_lang('Updated'),'confirm')
);
header('Location: '.api_get_self());
break;
case 'list':
$url = api_get_path(WEB_AJAX_PATH).'model.ajax.php?a=get_mail_template';
$columns = [
get_lang('Name'),
get_lang('Type'),
get_lang('Default'),
get_lang('Actions'),
];
$column_model = [
[
'name' => 'name',
'index' => 'name',
'width' => '180',
'align' => 'left',
],
[
'name' => 'type',
'index' => 'type',
'width' => '100',
'align' => 'left',
],
[
'name' => 'default_template',
'index' => 'default_template',
'width' => '100',
'align' => 'left',
'hidden' => 'true'
],
[
'name' => 'actions',
'index' => 'actions',
'width' => '100',
'align' => 'left',
'formatter' => 'action_formatter',
'sortable' => 'false',
],
];
$extra_params['autowidth'] = 'true'; //use the width of the parent
//$extra_params['editurl'] = $url; //use the width of the parent
$extra_params['height'] = 'auto'; //use the width of the parent
//With this function we can add actions to the jgrid
$action_links = 'function action_formatter (cellvalue, options, rowObject) {
var defaultIcon = "<i class=\"fa fa-circle fa-2x\"></i>";
if (rowObject[2] == 1) {
defaultIcon = "<i class=\"fa fa-check-circle fa-2x\"></i>";
}
return \'&nbsp;<a href="?action=edit&id=\'+options.rowId+\'"><i class=\"fa fa-pencil fa-2x\"></i></a>'.
'&nbsp;<a href="?action=set_default&id=\'+options.rowId+\'" title=\"'.get_lang('Default').'\">\'+ defaultIcon + \'</a>'.
'&nbsp;<a onclick="javascript:if(!confirm('."\'".addslashes(api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES))."\'".')) return false;" href="?sec_token='.$token.'&action=delete&id=\'+options.rowId+\'"><i class=\"fa fa-trash fa-2x\"></i></a> \';
}';
$content = $mailTemplate->display();
$content .= '
<script>
$(function() {
'.Display::grid_js('mail_template', $url, $columns, $column_model, $extra_params, [], $action_links, true).'
});
</script>';
break;
}
$template = new Template();
$template->assign('content', $content);
$template->display_one_col_template();

0 comments on commit 59d8aec

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