JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
add admin_email_templates
[wfpl-cms.git] / inc / misc.php
1 <?php
2
3 require_once(DOCROOT . 'inc/wfpl/email.php');
4
5 # call this when you have class="unix_time" or class="unix_date"
6 function render_timestamps() {
7         $GLOBALS['wfpl_main_template']->set('$render_timestamps');
8 }
9
10 # helper for email_with_template() below
11 function get_email_template($slug, $template_variables, $to_addr) {
12         # defaults
13         $out = array(
14                 'subject' => $GLOBALS['email_templates'][$slug]['subject'],
15                 'content' => $GLOBALS['email_templates'][$slug]['content'],
16                 'from_addr' => $GLOBALS['email_templates'][$slug]['from_addr'],
17                 'to_addr' => '',
18                 'cc_addr' => '',
19                 'bcc_addr' => ''
20         );
21         if (isset($GLOBALS['email_templates'][$slug]['to_addr'])) {
22                 $out['to_addr'] = $GLOBALS['email_templates'][$slug]['to_addr'];
23         } else {
24                 if ($to_addr == null) {
25                         die("ERROR: email_with_template(\"$slug\") needs a to_addr (put in \$GLOBALS['email_templates']['$slug'] or pass as argument)");
26                 }
27         }
28         # override with DB (if it exists)
29         $row = db_get_assoc('email_templates', 'from_addr,to_addr,cc_addr,bcc_addr,subject,content', 'where slug=%"', $slug);
30         if ($row) {
31                 foreach($row as $key => $value) {
32                         $out[$key] = $value;
33                 }
34         }
35         # argument wins no matter what
36         if ($to_addr !== null) {
37                 $out['to_addr'] = $to_addr;
38         }
39         if (strpos($out['content'], '~') !== false) {
40                 $tem = new tem();
41                 $tem->load_str($out['content']);
42                 $tem->sets($template_variables);
43                 $out['content'] = $tem->run();
44         }
45         if (strpos($out['subject'], '~') !== false) {
46                 $tem = new tem();
47                 $tem->load_str($out['subject']);
48                 $tem->sets($template_variables);
49                 $out['subject'] = $tem->run();
50         }
51         return $out;
52 }
53
54 # pass null as first arg if "to_addr" should come from the DB
55 function email_with_template($to_addr, $template_slug, $template_vars, $reply_to = '') {
56         $t = get_email_template($template_slug, $template_vars, $to_addr);
57         return email($t['from_addr'], $t['to_addr'], $t['subject'], $t['content'], $reply_to, $t['cc_addr'], $t['bcc_addr']);
58 }