Path: blob/master/src/applications/metamta/view/PhabricatorMetaMTAMailBody.php
12256 views
<?php12/**3* Render the body of an application email by building it up section-by-section.4*5* @task compose Composition6* @task render Rendering7*/8final class PhabricatorMetaMTAMailBody extends Phobject {910private $sections = array();11private $htmlSections = array();12private $attachments = array();1314private $viewer;15private $contextObject;1617public function getViewer() {18return $this->viewer;19}2021public function setViewer($viewer) {22$this->viewer = $viewer;23return $this;24}2526public function setContextObject($context_object) {27$this->contextObject = $context_object;28return $this;29}3031public function getContextObject() {32return $this->contextObject;33}343536/* -( Composition )-------------------------------------------------------- */373839/**40* Add a raw block of text to the email. This will be rendered as-is.41*42* @param string Block of text.43* @return this44* @task compose45*/46public function addRawSection($text) {47if (strlen($text)) {48$text = rtrim($text);49$this->sections[] = $text;50$this->htmlSections[] = phutil_escape_html_newlines(51phutil_tag('div', array(), $text));52}53return $this;54}5556public function addRemarkupSection($header, $text) {57try {58$engine = $this->newMarkupEngine()59->setMode(PhutilRemarkupEngine::MODE_TEXT);6061$styled_text = $engine->markupText($text);62$this->addPlaintextSection($header, $styled_text);63} catch (Exception $ex) {64phlog($ex);65$this->addTextSection($header, $text);66}6768try {69$mail_engine = $this->newMarkupEngine()70->setMode(PhutilRemarkupEngine::MODE_HTML_MAIL);7172$html = $mail_engine->markupText($text);73$this->addHTMLSection($header, $html);74} catch (Exception $ex) {75phlog($ex);76$this->addHTMLSection($header, $text);77}7879return $this;80}8182public function addRawPlaintextSection($text) {83if (strlen($text)) {84$text = rtrim($text);85$this->sections[] = $text;86}87return $this;88}8990public function addRawHTMLSection($html) {91$this->htmlSections[] = phutil_safe_html($html);92return $this;93}949596/**97* Add a block of text with a section header. This is rendered like this:98*99* HEADER100* Text is indented.101*102* @param string Header text.103* @param string Section text.104* @return this105* @task compose106*/107public function addTextSection($header, $section) {108if ($section instanceof PhabricatorMetaMTAMailSection) {109$plaintext = $section->getPlaintext();110$html = $section->getHTML();111} else {112$plaintext = $section;113$html = phutil_escape_html_newlines(phutil_tag('div', array(), $section));114}115116$this->addPlaintextSection($header, $plaintext);117$this->addHTMLSection($header, $html);118return $this;119}120121public function addPlaintextSection($header, $text, $indent = true) {122if ($indent) {123$text = $this->indent($text);124}125$this->sections[] = $header."\n".$text;126return $this;127}128129public function addHTMLSection($header, $html_fragment) {130if ($header !== null) {131$header = phutil_tag('strong', array(), $header);132}133134$this->htmlSections[] = array(135phutil_tag(136'div',137array(),138array(139$header,140phutil_tag('div', array(), $html_fragment),141)),142);143return $this;144}145146public function addLinkSection($header, $link) {147$html = phutil_tag('a', array('href' => $link), $link);148$this->addPlaintextSection($header, $link);149$this->addHTMLSection($header, $html);150return $this;151}152153154/**155* Add an attachment.156*157* @param PhabricatorMailAttachment Attachment.158* @return this159* @task compose160*/161public function addAttachment(PhabricatorMailAttachment $attachment) {162$this->attachments[] = $attachment;163return $this;164}165166167/* -( Rendering )---------------------------------------------------------- */168169170/**171* Render the email body.172*173* @return string Rendered body.174* @task render175*/176public function render() {177return implode("\n\n", $this->sections)."\n";178}179180public function renderHTML() {181$br = phutil_tag('br');182$body = phutil_implode_html($br, $this->htmlSections);183return (string)hsprintf('%s', array($body, $br));184}185186/**187* Retrieve attachments.188*189* @return list<PhabricatorMailAttachment> Attachments.190* @task render191*/192public function getAttachments() {193return $this->attachments;194}195196197/**198* Indent a block of text for rendering under a section heading.199*200* @param string Text to indent.201* @return string Indented text.202* @task render203*/204private function indent($text) {205return rtrim(" ".str_replace("\n", "\n ", $text));206}207208209private function newMarkupEngine() {210$engine = PhabricatorMarkupEngine::newMarkupEngine(array())211->setConfig('viewer', $this->getViewer())212->setConfig('uri.base', PhabricatorEnv::getProductionURI('/'));213214$context = $this->getContextObject();215if ($context) {216$engine->setConfig('contextObject', $context);217}218219return $engine;220}221222}223224225