-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtemplate.php
More file actions
154 lines (133 loc) · 5.09 KB
/
template.php
File metadata and controls
154 lines (133 loc) · 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Create or edit a template.
*
* @package local_bbcomailing
* @copyright 2026 David Herney @ BambuCo
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use core\output\html_writer;
require_once('../../config.php');
require_once($CFG->libdir . '/filelib.php');
require_login();
$context = context_system::instance();
require_capability('local/bbcomailing:manage', $context);
$id = optional_param('id', 0, PARAM_INT);
$preview = optional_param('preview', 0, PARAM_BOOL);
$PAGE->set_context($context);
$PAGE->set_url(new moodle_url('/local/bbcomailing/template.php', ['id' => $id]));
$PAGE->set_title($id ? get_string('edittemplate', 'local_bbcomailing') : get_string('newtemplate', 'local_bbcomailing'));
$PAGE->set_heading($id ? get_string('edittemplate', 'local_bbcomailing') : get_string('newtemplate', 'local_bbcomailing'));
$PAGE->set_pagelayout('admin');
$indexurl = new moodle_url('/local/bbcomailing/templates.php');
// Editor options for file management.
$editoroptions = [
'maxfiles' => -1,
'maxbytes' => 0,
'context' => $context,
'noclean' => !empty(get_config('local_bbcomailing', 'allowrawhtml')),
];
$template = null;
if ($id) {
$template = \local_bbcomailing\manager::get_template($id);
if (!$template) {
throw new moodle_exception('invalidrecord', 'error', '', 'local_bbcomailing_tpl');
}
}
// Preview mode: render the template HTML and exit.
if ($preview && $template) {
$PAGE->set_title(get_string('preview', 'local_bbcomailing') . ': ' . $template->name);
$PAGE->set_heading(get_string('preview', 'local_bbcomailing') . ': ' . $template->name);
echo $OUTPUT->header();
$previewhtml = \local_bbcomailing\manager::get_preview($id, []);
echo html_writer::div($previewhtml, 'template-preview border p-3');
$backurl = new moodle_url('/local/bbcomailing/templates.php');
echo html_writer::link($backurl, get_string('back'), ['class' => 'btn btn-secondary mt-3']);
echo $OUTPUT->footer();
die();
}
// Prepare form data.
$formdata = new stdClass();
$formdata->id = $id;
$formdata->usehtmleditor = 1;
if ($template) {
$formdata->name = $template->name;
$formdata->subject = $template->subject;
$formdata->html = $template->html;
$formdata->htmlformat = FORMAT_HTML;
$formdata->html_text = $template->html;
} else {
$formdata->html = '';
$formdata->htmlformat = FORMAT_HTML;
}
// Prepare editor with file support: converts @@PLUGINFILE@@ to draft URLs.
$formdata = file_prepare_standard_editor(
$formdata,
'html',
$editoroptions,
$context,
'local_bbcomailing',
'templatehtml',
$id ?: 0
);
$form = new \local_bbcomailing\form\template_form();
$form->set_data($formdata);
if ($form->is_cancelled()) {
redirect($indexurl);
} else if ($data = $form->get_data()) {
$record = new stdClass();
$record->id = $data->id;
$record->name = $data->name;
$record->subject = $data->subject;
$record->userid = $USER->id;
if (!empty($data->usehtmleditor)) {
// Save using standard editor flow: converts draft URLs to @@PLUGINFILE@@.
$record->html = '';
$record->htmlformat = FORMAT_HTML;
$record->id = \local_bbcomailing\manager::save_template($record);
$data->id = $record->id;
$data = file_postupdate_standard_editor(
$data,
'html',
$editoroptions,
$context,
'local_bbcomailing',
'templatehtml',
$record->id
);
$record->html = empty(get_config('local_bbcomailing', 'allowrawhtml'))
? \local_bbcomailing\manager::sanitize_email_html($data->html)
: $data->html;
} else {
$record->html = empty(get_config('local_bbcomailing', 'allowrawhtml'))
? \local_bbcomailing\manager::sanitize_email_html($data->html_text)
: $data->html_text;
$record->id = \local_bbcomailing\manager::save_template($record);
}
// Update html content after file processing.
\local_bbcomailing\manager::save_template($record);
// Trigger event.
$eventclass = $id ? \local_bbcomailing\event\template_updated::class : \local_bbcomailing\event\template_created::class;
$eventclass::create([
'context' => $context,
'objectid' => $record->id,
])->trigger();
redirect($indexurl, get_string('templatesaved', 'local_bbcomailing'));
}
echo $OUTPUT->header();
$form->display();
echo $OUTPUT->footer();