Changeset View
Changeset View
Standalone View
Standalone View
src/applications/metamta/view/PhabricatorMetaMTAMailBody.php
<?php | <?php | ||||
/** | /** | ||||
* Render the body of an application email by building it up section-by-section. | * Render the body of an application email by building it up section-by-section. | ||||
* | * | ||||
* @task compose Composition | * @task compose Composition | ||||
* @task render Rendering | * @task render Rendering | ||||
*/ | */ | ||||
final class PhabricatorMetaMTAMailBody extends Phobject { | final class PhabricatorMetaMTAMailBody extends Phobject { | ||||
private $sections = array(); | private $sections = array(); | ||||
private $htmlSections = array(); | private $htmlSections = array(); | ||||
private $attachments = array(); | private $attachments = array(); | ||||
private $viewer; | private $viewer; | ||||
private $contextObject; | |||||
public function getViewer() { | public function getViewer() { | ||||
return $this->viewer; | return $this->viewer; | ||||
} | } | ||||
public function setViewer($viewer) { | public function setViewer($viewer) { | ||||
$this->viewer = $viewer; | $this->viewer = $viewer; | ||||
return $this; | return $this; | ||||
} | } | ||||
public function setContextObject($context_object) { | |||||
$this->contextObject = $context_object; | |||||
return $this; | |||||
} | |||||
public function getContextObject() { | |||||
return $this->contextObject; | |||||
} | |||||
/* -( Composition )-------------------------------------------------------- */ | /* -( Composition )-------------------------------------------------------- */ | ||||
/** | /** | ||||
* Add a raw block of text to the email. This will be rendered as-is. | * Add a raw block of text to the email. This will be rendered as-is. | ||||
* | * | ||||
* @param string Block of text. | * @param string Block of text. | ||||
* @return this | * @return this | ||||
* @task compose | * @task compose | ||||
*/ | */ | ||||
public function addRawSection($text) { | public function addRawSection($text) { | ||||
if (strlen($text)) { | if (strlen($text)) { | ||||
$text = rtrim($text); | $text = rtrim($text); | ||||
$this->sections[] = $text; | $this->sections[] = $text; | ||||
$this->htmlSections[] = phutil_escape_html_newlines( | $this->htmlSections[] = phutil_escape_html_newlines( | ||||
phutil_tag('div', array(), $text)); | phutil_tag('div', array(), $text)); | ||||
} | } | ||||
return $this; | return $this; | ||||
} | } | ||||
public function addRemarkupSection($header, $text) { | public function addRemarkupSection($header, $text) { | ||||
try { | try { | ||||
$engine = PhabricatorMarkupEngine::newMarkupEngine(array()); | $engine = $this->newMarkupEngine() | ||||
$engine->setConfig('viewer', $this->getViewer()); | ->setMode(PhutilRemarkupEngine::MODE_TEXT); | ||||
$engine->setMode(PhutilRemarkupEngine::MODE_TEXT); | |||||
$styled_text = $engine->markupText($text); | $styled_text = $engine->markupText($text); | ||||
$this->addPlaintextSection($header, $styled_text); | $this->addPlaintextSection($header, $styled_text); | ||||
} catch (Exception $ex) { | } catch (Exception $ex) { | ||||
phlog($ex); | phlog($ex); | ||||
$this->addTextSection($header, $text); | $this->addTextSection($header, $text); | ||||
} | } | ||||
try { | try { | ||||
$mail_engine = PhabricatorMarkupEngine::newMarkupEngine(array()); | $mail_engine = $this->newMarkupEngine() | ||||
$mail_engine->setConfig('viewer', $this->getViewer()); | ->setMode(PhutilRemarkupEngine::MODE_HTML_MAIL); | ||||
$mail_engine->setMode(PhutilRemarkupEngine::MODE_HTML_MAIL); | |||||
$mail_engine->setConfig( | |||||
'uri.base', | |||||
PhabricatorEnv::getProductionURI('/')); | |||||
$html = $mail_engine->markupText($text); | $html = $mail_engine->markupText($text); | ||||
$this->addHTMLSection($header, $html); | $this->addHTMLSection($header, $html); | ||||
} catch (Exception $ex) { | } catch (Exception $ex) { | ||||
phlog($ex); | phlog($ex); | ||||
$this->addHTMLSection($header, $text); | $this->addHTMLSection($header, $text); | ||||
} | } | ||||
return $this; | return $this; | ||||
▲ Show 20 Lines • Show All 137 Lines • ▼ Show 20 Lines | /* -( Rendering )---------------------------------------------------------- */ | ||||
* | * | ||||
* @param string Text to indent. | * @param string Text to indent. | ||||
* @return string Indented text. | * @return string Indented text. | ||||
* @task render | * @task render | ||||
*/ | */ | ||||
private function indent($text) { | private function indent($text) { | ||||
return rtrim(" ".str_replace("\n", "\n ", $text)); | return rtrim(" ".str_replace("\n", "\n ", $text)); | ||||
} | } | ||||
private function newMarkupEngine() { | |||||
$engine = PhabricatorMarkupEngine::newMarkupEngine(array()) | |||||
->setConfig('viewer', $this->getViewer()) | |||||
->setConfig('uri.base', PhabricatorEnv::getProductionURI('/')); | |||||
$context = $this->getContextObject(); | |||||
if ($context) { | |||||
$engine->setConfig('contextObject', $context); | |||||
} | |||||
return $engine; | |||||
} | |||||
} | } |