JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
don't overwrite .centerer vmargin
[wfpl-cms.git] / admin_email_templates.php
1 <?php
2
3 # This form requires wfpl. See: http://sametwice.com/wfpl
4
5 # SETUP
6 #
7 # in config.php you'll need something like this:
8 #
9 #       $GLOBALS['email_templates'] = [
10 #               'slug' => [
11 #                       'title' => "Title shown in admin only",
12 #                       'description' => "explain (for admins) what this template is for",
13 #                       'variables' => [
14 #                               ['name', "explan (for admins) what this variable is for"],
15 #                               ['verbing', "admins can put these variables into the template"]
16 #                       ],
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
21 #               ]
22 #               # , 'slug2' => ...
23 #       ];
24
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
27 #
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');
30
31
32 $GLOBALS['admin_email_templates_field_to_caption'] = array(
33         'slug' => 'Slug',
34         'notes' => 'Notes',
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'
41 );
42
43 function admin_email_templates_get_fields() {
44         $data = array();
45
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'));
54
55         return $data;
56 }
57
58
59 function admin_email_templates_main() {
60         session_auth_must('admin_email_templates');
61
62         $slug = _REQUEST_cut('slug');
63         if ($slug && isset($GLOBALS['email_templates'][$slug])) {
64                 return admin_email_templates_main_form($slug);
65         }
66
67         # default action:
68         return admin_email_templates_main_listing();
69 }
70
71 function admin_email_templates_main_sort_title($a, $b) {
72         return strcasecmp($a['title'], $b['title']);
73 }
74 function admin_email_templates_main_sort_title_reverse($a, $b) {
75         return strcasecmp($b['title'], $a['title']);
76 }
77 function admin_email_templates_main_sort_subject($a, $b) {
78         return strcasecmp($a['subject'], $b['subject']);
79 }
80 function admin_email_templates_main_sort_subject_reverse($a, $b) {
81         return strcasecmp($b['subject'], $a['subject']);
82 }
83
84 function admin_email_templates_main_listing() {
85         $data = array();
86         $reverse = '';
87         $sort = _REQUEST_cut('sort');
88         if ($sort && substr($sort, 0, 1) === '-') {
89                 $sort = substr($sort, 1);
90                 $reverse = "_reverse";
91         } else {
92                 $data["sorting-by-$sort"] = '-';
93         }
94         $legal_sorts = array('title', 'subject');
95         if (!$sort || !in_array($sort, $legal_sorts)) {
96                 $sort = 'title';
97         }
98
99         $data['rows'] = array();
100
101         $rows = db_get_assocs('email_templates', 'slug,from_addr,cc_addr,bcc_addr,subject');
102         $by_slug = array();
103         foreach ($rows as $row) {
104                 $by_slug[$row['slug']] = $row;
105         }
106         foreach ($GLOBALS['email_templates'] as $slug => $row) {
107                 $out = array('slug' => $slug);
108                 # defaults from config
109                 foreach($row as $k => $v) {
110                         $out[$k] = $v;
111                 }
112                 # overwrite with db (if it's in the db)
113                 if ($by_slug[$slug]) {
114                         foreach($by_slug[$slug] as $k => $v) {
115                                 $out[$k] = $v;
116                         }
117                 }
118                 $data['rows'][] = $out;
119         }
120
121         usort($data['rows'], "admin_email_templates_main_sort_$sort$reverse");
122
123         tem_set('listings', $data);
124 }
125
126 function admin_email_templates_main_form($slug) {
127         if (isset($_POST['subject'])) {
128                 $data = admin_email_templates_get_fields();
129                 $data['slug'] = $slug;
130
131                 $all_good = true;
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.");
139                                         $all_good = false;
140                                 }
141                         }
142                 } unset($field);
143
144                 if (strlen($data['from_addr']) == 0) {
145                         message("ERROR: the \"From:\" field is required.");
146                         $all_good = false;
147                 }
148
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.");
151                         $all_good = false;
152                 }
153                 if ($all_good) {
154                         if (0 < db_count('email_templates', 'where slug=%"', $slug)) {
155                                 db_update_assoc('email_templates', $data, 'where slug=%"', $slug);
156                         } else {
157                                 db_insert_assoc('email_templates', $data);
158                         }
159                         message('Email template updated.');
160                         if ($error !== true) {
161                                 return './admin_email_templates';
162                         }
163                 } else {
164                         $custom = $data;
165                 }
166         } else {
167                 $custom = db_get_assoc('email_templates', ADMIN_EMAIL_TEMPLATES_DB_FIELDS, 'where slug=%"', $slug);
168         }
169
170         $out = array('slug' => $slug);
171         # defaults from globals
172         foreach($GLOBALS['email_templates'][$slug] as $k => $v) {
173                 $out[$k] = $v;
174         }
175         # show 'to_addr' field if it's relevant
176         if (isset($out['to_addr'])) {
177                 $out['want_to_addr'] = true;
178         }
179         # override with db values
180         if ($custom) {
181                 foreach($custom as $k => $v) {
182                         $out[$k] = $v;
183                 }
184         }
185         tem_set('form', $out);
186 }