* @license PHP License * @package WB * @subpackage MailMime */ WBClass::load('WBDatasource_Newsletter' , 'WBString' , 'patI18n'); /** * Wombat Mail Newsletter * * @version 0.1.0 * @package WB * @subpackage MailMime */ class WBMail_Newsletter extends WBStdClass { /** * Mime Mailer * @var WBMail_Mime */ private $mailMime; /** * Template Engine * @var patTemplate */ private $tmpl; /** * Newsletter Issue * @var WBDatasource_Newsletter_Issue */ private $issue; /** * Subscriber Data * @var array */ private $sub; /** * Current Locale Settings * @var string */ private $langCurrent; /** * Standard Constructor * * @param array */ public function __construct($parameter = array()) { $this->langCurrent = patI18n::getLocale(patI18n::LOCALE_TYPE_COMPLETE); $this->tmpl = WBClass::create('patTemplate'); $this->mailMime = WBClass::create('WBMail_Mime'); } /** * Tell Which Newsletter Issue 2 Mail * * Set issue object * @param WBDatasource_Newsletter_Issue */ public function setIssue(WBDatasource_Newsletter_Issue $issue) { $this->issue = $issue; } /** * Set Subscriber Data As Recipient * * Data must contain: * - email * - forename (may be empty) * - surname (may be empty) * @param array */ public function setSubscriberData($data) { $this->sub = $data; } /** * Prepare Content and Header * * Setup e-mail recipient and from address. Also load e-mail templates and * fill placeholders with content from issue and articles */ public function prepare() { $this->setLocale(); $issue = $this->issue->get(); // $this->mailMime->setDate(strtotime($issue['created'])); $from = $this->issue->getConfig('from'); /** @var WBMail_Mime_Part_Address */ $addrFrom = WBClass::create('WBMail_Mime_Part_Address'); $addrFrom->setForename($from['forename']); $addrFrom->setSurname($from['surname']); $addrFrom->setEmailAddress($from['email']); $this->mailMime->setFrom($addrFrom); /** @var WBMail_Mime_Part_Address */ $addrRcpt = WBClass::create('WBMail_Mime_Part_Address'); $addrRcpt->setForename($this->sub['forename']); $addrRcpt->setSurname($this->sub['surname']); $addrRcpt->setEmailAddress($this->sub['email']); $this->mailMime->setRcpt($addrRcpt); $this->tmpl->clearAllTemplates(); // add locale settings $lang = array( 'short' => patI18n::getLocale(patI18n::LOCALE_TYPE_SHORT), 'lang' => patI18n::getLocale(patI18n::LOCALE_TYPE_LANG), 'complete' => patI18n::getLocale(patI18n::LOCALE_TYPE_COMPLETE) ); $this->tmpl->addGlobalVars($lang, 'LOCALE_LANG_'); $this->tmpl->readTemplatesFromInput('Newsletter/Issue/' . $issue['template'] . '.tmpl'); // add CSS-Template $css = array(); $list = $this->tmpl->getList(); foreach ($list as $name) { if (0 != strncmp('css_', $name, 4)) { continue; } $name = substr($name, 4); $css[$name] = trim($this->tmpl->getParsedTemplate('css_' . $name)); } $this->tmpl->addGlobalVars($css, 'css_'); $this->tmpl->addGlobalVars($issue, 'issue_'); $this->tmpl->addGlobalVars($this->sub, 'sub_'); $art = $this->issue->getArticle(); $this->tmpl->addGlobalVar('article_count', count($art)); if ($this->tmpl->exists('article_list_entry_html')) { $this->tmpl->addRows('article_list_entry_html', $art); } if ($this->tmpl->exists('article_list_entry_plain')) { $this->tmpl->addRows('article_list_entry_plain', $art); } $this->resetLocale(); } /** * Get E-Mail Subject * * @return string */ public function getSubject() { $this->setLocale(); $subject = trim($this->tmpl->getParsedTemplate('email_subject')); $subject = $this->replaceSubscriberInfo($subject); $subject = str_replace("\n", ' ', $subject); $subject = WBString::replaceSuperPlaceholders($subject); $this->resetLocale(); return $subject; } /** * Get E-Mail Plain Body * * @return string */ public function getPlainBody() { $this->setLocale(); $txt = $this->tmpl->getParsedTemplate('email_plain'); $txt = $this->replaceSubscriberInfo($txt); $txt = WBString::replaceSuperPlaceholders($txt); $this->resetLocale(); return $txt; } /** * Get E-Mail HTML Body * * @return string */ public function getHtmlBody($doc = false) { $this->setLocale(); if ($doc) { $html = $this->tmpl->getParsedTemplate('email_html_doc'); } else { $html = $this->tmpl->getParsedTemplate('email_html'); } $html = $this->replaceSubscriberInfo($html); $html = WBString::replaceSuperPlaceholders($html);; $this->resetLocale(); return $html; } /** * Replace String with Subscriber's Data * * @todo consider to replace more data * @param string * @return string */ private function replaceSubscriberInfo($txt) { $search = array(); $search[] = 'OBSCUREID'; $replace = array(); $replace[] = $this->sub['obscureid']; return str_replace($search, $replace, $txt); } /** * Get Complete E-Mail body * * Return Mime-Mail string ready to be send * Also pass e-mail headers using reference header parameter * * @param array $headers * @return string */ public function getMailBody(&$header = array()) { $mm = $this->getMimeMail(); /** @var WBMail_Mime_Storage_String */ $ms = WBClass::create('WBMail_Mime_Storage_String'); $ms->setMimeMail($mm); $header = array(); return $ms->saveToString($header); } /** * Get Prepared Mime Mail * * @return WBMail_Mime */ public function getMimeMail() { // subject must be one line! $this->mailMime->setSubject($this->getSubject()); // body $this->mailMime->setPlainBody($this->getPlainBody()); $this->mailMime->setHtmlBody($this->getHtmlBody(true)); return $this->mailMime; } /** * Set Locale * * Set current locale config to newsletter issue's language setting */ private function setLocale() { // switch to recipient's language first $this->langCurrent = patI18n::getLocale(patI18n::LOCALE_TYPE_COMPLETE); $lang = $this->issue->getLang(); if (!empty($lang)) { patI18n::setLocale($lang); } } /** * Restore Locale Settings * * @see setLocale() */ private function resetLocale() { // undo changes in locale settings patI18n::setLocale($this->langCurrent); } }