JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
silence more warnings
[wfpl-cms.git] / inc / misc.php
index 429482d..5244635 100644 (file)
@@ -1,6 +1,58 @@
 <?php
 
+require_once(__DIR__.'/'.'wfpl/email.php');
+
 # call this when you have class="unix_time" or class="unix_date"
 function render_timestamps() {
-       $GLOBALS['wfpl_main_template']->set('$render_timestamps');
+    $GLOBALS['wfpl_main_template']->set('$render_timestamps');
+}
+
+# helper for email_with_template() below
+function get_email_template($slug, $template_variables, $to_addr) {
+    # defaults
+    $out = array(
+        'subject' => $GLOBALS['email_templates'][$slug]['subject'],
+        'content' => $GLOBALS['email_templates'][$slug]['content'],
+        'from_addr' => $GLOBALS['email_templates'][$slug]['from_addr'],
+        'to_addr' => '',
+        'cc_addr' => '',
+        'bcc_addr' => ''
+    );
+    if (isset($GLOBALS['email_templates'][$slug]['to_addr'])) {
+        $out['to_addr'] = $GLOBALS['email_templates'][$slug]['to_addr'];
+    } else {
+        if ($to_addr == null) {
+            die("ERROR: email_with_template(\"$slug\") needs a to_addr (put in \$GLOBALS['email_templates']['$slug'] or pass as argument)");
+        }
+    }
+    # override with DB (if it exists)
+    $row = db_get_assoc('email_templates', 'from_addr,to_addr,cc_addr,bcc_addr,subject,content', 'where slug=%"', $slug);
+    if ($row) {
+        foreach($row as $key => $value) {
+            $out[$key] = $value;
+        }
+    }
+    # argument wins no matter what
+    if ($to_addr !== null) {
+        $out['to_addr'] = $to_addr;
+    }
+    if (strpos($out['content'], '~') !== false) {
+        $tem = new tem();
+        $tem->load_str($out['content']);
+        $tem->sets($template_variables);
+        $out['content'] = $tem->run();
+    }
+    if (strpos($out['subject'], '~') !== false) {
+        $tem = new tem();
+        $tem->load_str($out['subject']);
+        $tem->sets($template_variables);
+        $out['subject'] = $tem->run();
+    }
+    return $out;
+}
+
+# pass null as first arg if "to_addr" should come from the DB
+function email_with_template($to_addr, $template_slug, $template_vars, $reply_to = '') {
+    $t = get_email_template($template_slug, $template_vars, $to_addr);
+    return email($t['from_addr'], $t['to_addr'], $t['subject'], $t['content'], $reply_to, $t['cc_addr'], $t['bcc_addr']);
 }