X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=admin_email_templates.php;fp=admin_email_templates.php;h=10ed922acc2f43627bf380523af0d929eba70722;hb=6dcd0cadcca91c18d12d9674e6ec3ce60cb44d31;hp=0000000000000000000000000000000000000000;hpb=2078c2762990009d890fb6c36f0b0d947f559687;p=wfpl-cms.git diff --git a/admin_email_templates.php b/admin_email_templates.php new file mode 100644 index 0000000..10ed922 --- /dev/null +++ b/admin_email_templates.php @@ -0,0 +1,186 @@ + [ +# 'title' => "Title shown in admin only", +# 'description' => "explain (for admins) what this template is for", +# 'variables' => [ +# ['name', "explan (for admins) what this variable is for"], +# ['verbing', "admins can put these variables into the template"] +# ], +# 'subject' => "email subject", +# 'content' => "Hi, ~name~ this is the email body, thanks for ~verbing~!", +# 'from_addr' => 'noreply@airservices.info', +# 'to_addr' => 'optional@to.address' # optional +# ] +# # , 'slug2' => ... +# ]; + +# To save results to a database, you'll need to create the email_templates table. +# The file admin_email_templates.sql should help with this +# +# if you rename any of the database fields, you'll need to update this: +define('ADMIN_EMAIL_TEMPLATES_DB_FIELDS', 'slug,notes,from_addr,to_addr,cc_addr,bcc_addr,subject,content'); + + +$GLOBALS['admin_email_templates_field_to_caption'] = array( + 'slug' => 'Slug', + 'notes' => 'Notes', + 'from_addr' => 'From Address', + 'to_addr' => 'To Address', + 'cc_addr' => 'Cc Address', + 'bcc_addr' => 'Bcc Address', + 'subject' => 'Subject', + 'content' => 'Content' +); + +function admin_email_templates_get_fields() { + $data = array(); + + # slug is cut in *_main() + $data['notes'] = format_unix(_REQUEST_cut('notes')); + $data['to_addr'] = format_email(trim(_REQUEST_cut('to_addr'))); + $data['from_addr'] = format_email(trim(_REQUEST_cut('from_addr'))); + $data['cc_addr'] = format_email(trim(_REQUEST_cut('cc_addr'))); + $data['bcc_addr'] = format_email(trim(_REQUEST_cut('bcc_addr'))); + $data['subject'] = format_oneline(trim(_REQUEST_cut('subject'))); + $data['content'] = format_unix(_REQUEST_cut('content')); + + return $data; +} + + +function admin_email_templates_main() { + session_auth_must('admin_email_templates'); + + $slug = _REQUEST_cut('slug'); + if ($slug && isset($GLOBALS['email_templates'][$slug])) { + return admin_email_templates_main_form($slug); + } + + # default action: + return admin_email_templates_main_listing(); +} + +function admin_email_templates_main_sort_title($a, $b) { + return strcasecmp($a['title'], $b['title']); +} +function admin_email_templates_main_sort_title_reverse($a, $b) { + return strcasecmp($b['title'], $a['title']); +} +function admin_email_templates_main_sort_subject($a, $b) { + return strcasecmp($a['subject'], $b['subject']); +} +function admin_email_templates_main_sort_subject_reverse($a, $b) { + return strcasecmp($b['subject'], $a['subject']); +} + +function admin_email_templates_main_listing() { + $data = array(); + $reverse = ''; + $sort = _REQUEST_cut('sort'); + if ($sort && substr($sort, 0, 1) === '-') { + $sort = substr($sort, 1); + $reverse = "_reverse"; + } else { + $data["sorting-by-$sort"] = '-'; + } + $legal_sorts = array('title', 'subject'); + if (!$sort || !in_array($sort, $legal_sorts)) { + $sort = 'title'; + } + + $data['rows'] = array(); + + $rows = db_get_assocs('email_templates', 'slug,from_addr,cc_addr,bcc_addr,subject'); + $by_slug = array(); + foreach ($rows as $row) { + $by_slug[$row['slug']] = $row; + } + foreach ($GLOBALS['email_templates'] as $slug => $row) { + $out = array('slug' => $slug); + # defaults from config + foreach($row as $k => $v) { + $out[$k] = $v; + } + # overwrite with db (if it's in the db) + if ($by_slug[$slug]) { + foreach($by_slug[$slug] as $k => $v) { + $out[$k] = $v; + } + } + $data['rows'][] = $out; + } + + usort($data['rows'], "admin_email_templates_main_sort_$sort$reverse"); + + tem_set('listings', $data); +} + +function admin_email_templates_main_form($slug) { + if (isset($_POST['subject'])) { + $data = admin_email_templates_get_fields(); + $data['slug'] = $slug; + + $all_good = true; + $email_fields = ['from', 'to', 'cc', 'bcc']; + foreach ($email_fields as &$field) { + $value = $data[$field . '_addr']; + if (strlen($value)) { + if (!email_header($value)) { + $pretty = ucfirst($field) . ':'; + message("ERROR: invalid value in \"$pretty\" field. Be very careful with formatting, and only put one address in this field."); + $all_good = false; + } + } + } unset($field); + + if (strlen($data['from_addr']) == 0) { + message("ERROR: the \"From:\" field is required."); + $all_good = false; + } + + if (strlen($data['to_addr']) == 0 && isset($GLOBALS['email_templates'][$slug]['to_addr'])) { + message("ERROR: the \"To:\" field is required for this template."); + $all_good = false; + } + if ($all_good) { + if (0 < db_count('email_templates', 'where slug=%"', $slug)) { + db_update_assoc('email_templates', $data, 'where slug=%"', $slug); + } else { + db_insert_assoc('email_templates', $data); + } + message('Email template updated.'); + if ($error !== true) { + return './admin_email_templates'; + } + } else { + $custom = $data; + } + } else { + $custom = db_get_assoc('email_templates', ADMIN_EMAIL_TEMPLATES_DB_FIELDS, 'where slug=%"', $slug); + } + + $out = array('slug' => $slug); + # defaults from globals + foreach($GLOBALS['email_templates'][$slug] as $k => $v) { + $out[$k] = $v; + } + # show 'to_addr' field if it's relevant + if (isset($out['to_addr'])) { + $out['want_to_addr'] = true; + } + # override with db values + if ($custom) { + foreach($custom as $k => $v) { + $out[$k] = $v; + } + } + tem_set('form', $out); +}