3 # This form requires wfpl. See: http://sametwice.com/wfpl
7 # in config.php you'll need something like this:
9 # $GLOBALS['email_templates'] = [
11 # 'title' => "Title shown in admin only",
12 # 'description' => "explain (for admins) what this template is for",
14 # ['name', "explan (for admins) what this variable is for"],
15 # ['verbing', "admins can put these variables into the template"]
17 # 'subject' => "email subject",
18 # 'content' => "Hi, ~name~ this is the email body, thanks for ~verbing~!",
19 # 'from_addr' => 'noreply@airservices.info',
20 # 'to_addr' => 'optional@to.address' # optional
25 # To save results to a database, you'll need to create the email_templates table.
26 # The file admin_email_templates.sql should help with this
28 # if you rename any of the database fields, you'll need to update this:
29 define('ADMIN_EMAIL_TEMPLATES_DB_FIELDS', 'slug,notes,from_addr,to_addr,cc_addr,bcc_addr,subject,content');
32 $GLOBALS['admin_email_templates_field_to_caption'] = array(
35 'from_addr' => 'From Address',
36 'to_addr' => 'To Address',
37 'cc_addr' => 'Cc Address',
38 'bcc_addr' => 'Bcc Address',
39 'subject' => 'Subject',
40 'content' => 'Content'
43 function admin_email_templates_get_fields() {
46 # slug is cut in *_main()
47 $data['notes'] = format_unix(_REQUEST_cut('notes'));
48 $data['to_addr'] = format_email(trim(_REQUEST_cut('to_addr')));
49 $data['from_addr'] = format_email(trim(_REQUEST_cut('from_addr')));
50 $data['cc_addr'] = format_email(trim(_REQUEST_cut('cc_addr')));
51 $data['bcc_addr'] = format_email(trim(_REQUEST_cut('bcc_addr')));
52 $data['subject'] = format_oneline(trim(_REQUEST_cut('subject')));
53 $data['content'] = format_unix(_REQUEST_cut('content'));
59 function admin_email_templates_main() {
60 session_auth_must('admin_email_templates');
62 $slug = _REQUEST_cut('slug');
63 if ($slug && isset($GLOBALS['email_templates'][$slug])) {
64 return admin_email_templates_main_form($slug);
68 return admin_email_templates_main_listing();
71 function admin_email_templates_main_sort_title($a, $b) {
72 return strcasecmp($a['title'], $b['title']);
74 function admin_email_templates_main_sort_title_reverse($a, $b) {
75 return strcasecmp($b['title'], $a['title']);
77 function admin_email_templates_main_sort_subject($a, $b) {
78 return strcasecmp($a['subject'], $b['subject']);
80 function admin_email_templates_main_sort_subject_reverse($a, $b) {
81 return strcasecmp($b['subject'], $a['subject']);
84 function admin_email_templates_main_listing() {
87 $sort = _REQUEST_cut('sort');
88 if ($sort && substr($sort, 0, 1) === '-') {
89 $sort = substr($sort, 1);
90 $reverse = "_reverse";
92 $data["sorting-by-$sort"] = '-';
94 $legal_sorts = array('title', 'subject');
95 if (!$sort || !in_array($sort, $legal_sorts)) {
99 $data['rows'] = array();
101 $rows = db_get_assocs('email_templates', 'slug,from_addr,cc_addr,bcc_addr,subject');
103 foreach ($rows as $row) {
104 $by_slug[$row['slug']] = $row;
106 foreach ($GLOBALS['email_templates'] as $slug => $row) {
107 $out = array('slug' => $slug);
108 # defaults from config
109 foreach($row as $k => $v) {
112 # overwrite with db (if it's in the db)
113 if ($by_slug[$slug]) {
114 foreach($by_slug[$slug] as $k => $v) {
118 $data['rows'][] = $out;
121 usort($data['rows'], "admin_email_templates_main_sort_$sort$reverse");
123 tem_set('listings', $data);
126 function admin_email_templates_main_form($slug) {
127 if (isset($_POST['subject'])) {
128 $data = admin_email_templates_get_fields();
129 $data['slug'] = $slug;
132 $email_fields = ['from', 'to', 'cc', 'bcc'];
133 foreach ($email_fields as &$field) {
134 $value = $data[$field . '_addr'];
135 if (strlen($value)) {
136 if (!email_header($value)) {
137 $pretty = ucfirst($field) . ':';
138 message("ERROR: invalid value in \"$pretty\" field. Be very careful with formatting, and only put one address in this field.");
144 if (strlen($data['from_addr']) == 0) {
145 message("ERROR: the \"From:\" field is required.");
149 if (strlen($data['to_addr']) == 0 && isset($GLOBALS['email_templates'][$slug]['to_addr'])) {
150 message("ERROR: the \"To:\" field is required for this template.");
154 if (0 < db_count('email_templates', 'where slug=%"', $slug)) {
155 db_update_assoc('email_templates', $data, 'where slug=%"', $slug);
157 db_insert_assoc('email_templates', $data);
159 message('Email template updated.');
160 if ($error !== true) {
161 return './admin_email_templates';
167 $custom = db_get_assoc('email_templates', ADMIN_EMAIL_TEMPLATES_DB_FIELDS, 'where slug=%"', $slug);
170 $out = array('slug' => $slug);
171 # defaults from globals
172 foreach($GLOBALS['email_templates'][$slug] as $k => $v) {
175 # show 'to_addr' field if it's relevant
176 if (isset($out['to_addr'])) {
177 $out['want_to_addr'] = true;
179 # override with db values
181 foreach($custom as $k => $v) {
185 tem_set('form', $out);