From 649fd74c1ab40d4353dc8e52f4f6449c8dd40906 Mon Sep 17 00:00:00 2001 From: SteveD Date: Wed, 20 Feb 2013 21:15:36 +0000 Subject: [PATCH] Issue #70 - missed new file from previous commit --- e107_handlers/mail_template_class.php | 276 ++++++++++++++++++++++++++ 1 file changed, 276 insertions(+) create mode 100644 e107_handlers/mail_template_class.php diff --git a/e107_handlers/mail_template_class.php b/e107_handlers/mail_template_class.php new file mode 100644 index 000000000..bec3f678e --- /dev/null +++ b/e107_handlers/mail_template_class.php @@ -0,0 +1,276 @@ +lastTemplateData = FALSE; + } + + + /** + * Set a template to be used. + * Clears any cached data + * + * @param array|string $newTemplate - if a string, the name of a template. (The internal name of the variable, not the associated name) + * If an array, an existing template - must be in the correct format - no checking done + * + * @return boolean TRUE if accepted, FALSE if rejected + */ + public function setNewTemplate($newTemplate) + { + $this->mainBodyText = FALSE; + $this->altBodyText = FALSE; + + if (is_array($newTemplate)) + { + $this->lastTemplateData = $newTemplate; + return TRUE; + } + return $this->loadTemplateInfo($newTemplate); + } + + + + /** + * Given a template name, assembles the array of data required by sendTemplated() and saves in our cache + * + * Template file name is 'email_template.php' + * The template is first sought in the template file of the current theme directory, and data read as available. + * If $extraFile is specified, that is searched next + * Gaps are then filled in from the core template file. + * + * @param string $templateName - name of required template + * @param string $extraFile - optional path to additional template file (intended for plugins) + * (This is read between the theme-specific file and the defaults) + * + * @return boolean TRUE - template found and loaded. FALSE if not found. + * If successful, we store an array in $lastTemplateData, with exactly six elements: + * 'template_name' + * 'email_overrides' - any override information (often an empty array) + * 'email_header' - any header information (usually loaded from the default) + * 'email_body' + * 'email_footer' + * 'email_plainText' - optional template for plain text part of email + */ + public function loadTemplateInfo($templateName, $extraFile = FALSE) + { + static $requiredFields = array ('email_overrides', 'email_header', 'email_body', 'email_footer', 'email_plainText'); + + if (is_array($this->lastTemplateData)) + { + if ($this->lastTemplateData['template_name'] == $templateName) + { + return $this->lastTemplateData; + } + $this->lastTemplateData = FALSE; // Obviously a new template + } + + $ret = array('email_overrides' => '', 'email_header' => '', 'email_body' => '', 'email_footer' => '', 'email_plainText' => ''); + if (!in_array($templateName, array('textonly', 'texthtml', 'texttheme'))) + { + $found = 0; // Count number of field definitions we've found + + $fileList = array(THEME.'templates/email_template.php'); + if ($extraFile) + { + $fileList[] = $extraFile; + } + $fileList[] = e_THEME.'templates/email_template.php'; + foreach ($fileList as $templateFileName ) // Override file, optional plugin file then defaults + { + + if (($found < count($requiredFields)) && is_readable($templateFileName)) + { + require_once($templateFileName); + + //$tVars = get_defined_vars(); + //if (isset($tVars['GLOBALS'])) unset($tVars['GLOBALS']); + //print_a($tVars); + + if (isset($$templateName)) + { + if (is_array($$templateName)) + { + foreach ($requiredFields as $k) + { + if (!$ret[$k] && isset(${$templateName}[$k])) + { + $ret[$k] = ${$templateName}[$k]; + $found++; + } + } + } + else + { + $ret['email_body'] = $$templateName; // Non-array just defines body of email + $found++; + } + } + } + } + + // Now fill in the gaps from the defaults + if ($found < count($requiredFields)) + { + foreach ($requiredFields as $k) + { + $override = strtoupper($k); + if (!$ret[$k] && isset($$override)) + { + $ret[$k] = $$override; + $found++; + } + } + } + if (($found == 0) || !$ret['email_body']) // Pointless if we haven't defined a body + { + return FALSE; + } + } + + $this->lastTemplateData = $ret; + $this->lastTemplateData['template_name'] = $templateName; // Cache template + return $this->lastTemplateData; // Return this rather than $ret, so return is consistent with cached data + } + + + + /** + * Creates email body text according to options, using the cached template information. + * Caches body, and potentially alternate body + * + * @param $text string - text to process + * @param boolean $incImages - valid only with HTML and templated output: + * if true any 'absolute' format images are embedded in the source of the email. + * if FALSE, absolute links are converted to URLs on the local server + * + * @return boolean TRUE for success, FALSE on error (no template defined) + */ + public function makeEmailBody($text, $incImages = TRUE) + { + if (!is_array( $this->lastTemplateData)) return FALSE; + if (!isset($this->lastTemplateData['template_name'])) return FALSE; + + $tp = e107::getParser(); + + // textonly - generate plain text email + // texthtml - HTML format email, no theme info + // texttheme - HTML format email, including current theme stylesheet etc + $format = $this->lastTemplateData['template_name']; + if (!$format) + { + echo 'No format specified!'; + return FALSE; + } + + if ($format == 'textonly') + { // Plain text email - strip bbcodes etc + $temp = $tp->toHTML($text, TRUE, 'E_BODY_PLAIN'); // Decode bbcodes into HTML, plain text as far as possible etc + $temp = stripslashes(strip_tags($temp)); // Have to do strip_tags() again in case bbcode added some + $this->mainBodyText = $temp; + $this->altBodyText = ''; + return TRUE; + } + + $consts = $incImages ? ',consts_abs' : 'consts_full'; // If inline images, absolute constants so we can change them + + if (($format != 'texthtml') && ($format != 'texttheme')) + { // Specific theme - loaded already + $mailHeader = $tp->parseTemplate($this->lastTemplateData['email_header'], TRUE); + $mailBody = $tp->parseTemplate(str_replace('{BODY}', $text, $this->lastTemplateData['email_body']), TRUE); + $mailFooter = $tp->parseTemplate($this->lastTemplateData['email_footer'], TRUE); + + $mailBody = $mailHeader.$mailBody.$mailFooter; + } + + + if (($format == 'texthtml') || ($format == 'texttheme')) + { + // HTML format email here, using hard-coded defaults + $mailHeader = "\n"; + $mailHeader .= "\n"; + $mailHeader .= "\n"; + if ($format == 'texttheme') + { + $styleFile = THEME.'emailstyle.css'; + if (!is_readable($styleFile)) { $styleFile = THEME."/style.css"; } + $style_css = file_get_contents($styleFile); + $mailHeader .= ""; + } + $mailHeader .= "\n"; + + + $mailBody = $mailHeader."\n"; + if ($format == 'texttheme') + { + $mailBody .= "
\n"; + $mailBody .= $tp->toHTML($text, TRUE, 'E_BODY'.$consts)."
"; + } + else + { + $mailBody .= $tp->toHTML($text, TRUE, 'E_BODY'.$consts).""; + $mailBody = str_replace(""", '"', $mailBody); + } + + $mailBody = stripslashes($mailBody); + } + + + if (!$incImages) + { + // Handle internally generated 'absolute' links - they need the full URL + $mailBody = str_replace("src='".e_HTTP, "src='".SITEURL, $mailBody); + $mailBody = str_replace('src="'.e_HTTP, 'src="'.SITEURL, $mailBody); + $mailBody = str_replace("href='".e_HTTP, "src='".SITEURL, $mailBody); + $mailBody = str_replace('href="'.e_HTTP, 'src="'.SITEURL, $mailBody); + } + +// print_a($mailBody); + $this->mainBodyText = $mailBody; + $this->altBodyText = ''; + if ($this->lastTemplateData['email_plainText']) + { + $this->altBodyText = $tp->parseTemplate(str_replace('{BODY}', $text, $this->lastTemplateData['email_plainText']), TRUE); + } + return TRUE; + } +} + +?>