-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsend.php
More file actions
166 lines (145 loc) · 7.4 KB
/
send.php
File metadata and controls
166 lines (145 loc) · 7.4 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
155
156
157
158
159
160
161
162
163
164
165
166
<?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/>.
/**
* Send emails page with multi-step flow: form, preview, confirm.
*
* @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;
use core\url as moodle_url;
use core\context\system as context_system;
require_once('../../config.php');
require_login();
$context = context_system::instance();
require_capability('local/bbcomailing:manage', $context);
$PAGE->set_context($context);
$PAGE->set_url(new moodle_url('/local/bbcomailing/send.php'));
$PAGE->set_title(get_string('sendmails', 'local_bbcomailing'));
$PAGE->set_heading(get_string('sendmails', 'local_bbcomailing'));
$PAGE->set_pagelayout('admin');
$indexurl = new moodle_url('/local/bbcomailing/index.php');
$step = optional_param('step', 'form', PARAM_ALPHA);
$templates = \local_bbcomailing\manager::get_templates();
$templateoptions = [];
foreach ($templates as $tpl) {
$templateoptions[$tpl->id] = s($tpl->name);
}
$form = null;
// Handle form cancellation before any output.
if ($step === 'form') {
$form = new \local_bbcomailing\form\send_form(null, ['templates' => $templateoptions]);
if ($form->is_cancelled()) {
redirect($indexurl);
}
}
echo $OUTPUT->header();
// Navigation tabs.
echo \local_bbcomailing\manager::render_tabs('send');
if ($step === 'confirm' && confirm_sesskey()) {
// Step 3: Actually queue the sends.
$tplid = required_param('tplid', PARAM_INT);
$separator = optional_param('separator', '', PARAM_RAW);
$recipients = required_param('recipients', PARAM_RAW);
$result = \local_bbcomailing\manager::parse_recipients($recipients, $separator);
if (!empty($result['errors'])) {
// Should not happen as we already validated, but just in case.
$errormsg = html_writer::start_tag('ul');
foreach ($result['errors'] as $error) {
$errormsg .= html_writer::tag('li', s($error));
}
$errormsg .= html_writer::end_tag('ul');
echo $OUTPUT->notification($errormsg, 'error');
} else {
$queueresult = \local_bbcomailing\manager::queue_sends($tplid, $result['records']);
\local_bbcomailing\event\emails_sent::create([
'context' => $context,
'objectid' => $tplid,
'other' => ['count' => $queueresult['queued']],
])->trigger();
echo $OUTPUT->notification(get_string('sendqueued', 'local_bbcomailing', $queueresult['queued']), 'success');
if ($queueresult['skipped'] > 0) {
echo $OUTPUT->notification(get_string('unsubscribedskipped', 'local_bbcomailing', $queueresult['skipped']), 'warning');
}
echo $OUTPUT->single_button($indexurl, get_string('manage', 'local_bbcomailing'));
}
} else if ($step === 'preview' && data_submitted() && confirm_sesskey()) {
// Step 2: Show preview.
$tplid = required_param('tplid', PARAM_INT);
$separator = optional_param('separator', '', PARAM_RAW);
$recipients = required_param('recipients', PARAM_RAW);
$result = \local_bbcomailing\manager::parse_recipients($recipients, $separator);
if (!empty($result['errors'])) {
$errormsg = html_writer::start_tag('ul');
foreach ($result['errors'] as $error) {
$errormsg .= html_writer::tag('li', s($error));
}
$errormsg .= html_writer::end_tag('ul');
echo $OUTPUT->notification($errormsg, 'error');
// Show form again.
$form = new \local_bbcomailing\form\send_form(null, ['templates' => $templateoptions]);
$form->set_data(['tplid' => $tplid, 'separator' => $separator, 'recipients' => $recipients]);
$form->display();
} else {
$template = \local_bbcomailing\manager::get_template($tplid);
$recordcount = count($result['records']);
// Show preview of first record.
$firstrecord = $result['records'][0];
$previewhtml = \local_bbcomailing\manager::get_preview($tplid, $firstrecord['fields']);
$previewsubject = $template->subject;
foreach ($firstrecord['fields'] as $key => $value) {
$previewsubject = str_replace('{' . $key . '}', $value, $previewsubject);
}
echo $OUTPUT->heading(get_string('preview', 'local_bbcomailing'), 3);
echo $OUTPUT->heading(get_string('previewfor', 'local_bbcomailing', $firstrecord['email']), 5);
echo html_writer::tag('p', '<strong>' . get_string('subject', 'local_bbcomailing') .
':</strong> ' . s($previewsubject));
echo html_writer::div($previewhtml, 'border p-3 mb-3');
echo $OUTPUT->heading(get_string('confirmsend', 'local_bbcomailing'), 3);
echo html_writer::tag('p', get_string('confirmsend_message', 'local_bbcomailing', $recordcount));
// Confirmation form.
echo html_writer::start_tag('form', ['method' => 'post', 'action' => 'send.php']);
echo html_writer::input_hidden_params(new moodle_url('', ['step' => 'confirm', 'sesskey' => sesskey()]));
echo html_writer::empty_tag('input', ['type' => 'hidden', 'name' => 'tplid', 'value' => $tplid]);
echo html_writer::empty_tag('input', ['type' => 'hidden', 'name' => 'separator', 'value' => s($separator)]);
echo html_writer::empty_tag('input', ['type' => 'hidden', 'name' => 'recipients', 'value' => s($recipients)]);
echo html_writer::empty_tag('input', [
'type' => 'submit',
'value' => get_string('confirmsend', 'local_bbcomailing'),
'class' => 'btn btn-primary mr-2',
]);
echo html_writer::link($PAGE->url, get_string('back', 'local_bbcomailing'), ['class' => 'btn btn-secondary']);
echo html_writer::end_tag('form');
}
} else if ($form) {
// Step 1: Show the send form.
// $form was already instantiated before output for cancellation check.
if ($data = $form->get_data()) {
// Form validated, auto-submit to preview step.
echo html_writer::start_tag('form', ['method' => 'post', 'action' => 'send.php', 'id' => 'previewform']);
echo html_writer::empty_tag('input', ['type' => 'hidden', 'name' => 'step', 'value' => 'preview']);
echo html_writer::empty_tag('input', ['type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()]);
echo html_writer::empty_tag('input', ['type' => 'hidden', 'name' => 'tplid', 'value' => $data->tplid]);
echo html_writer::empty_tag('input', ['type' => 'hidden', 'name' => 'separator', 'value' => $data->separator]);
echo html_writer::empty_tag('input', ['type' => 'hidden', 'name' => 'recipients', 'value' => $data->recipients]);
echo html_writer::end_tag('form');
echo html_writer::script('document.getElementById("previewform").submit();');
} else {
$form->display();
}
}
echo $OUTPUT->footer();