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 { | final class PhabricatorMetaMTAMailBody { | ||||
| private $sections = array(); | private $sections = array(); | ||||
| private $htmlSections = array(); | |||||
| private $attachments = array(); | private $attachments = array(); | ||||
| /* -( 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)) { | ||||
| $this->sections[] = rtrim($text); | $text = rtrim($text); | ||||
| $this->sections[] = $text; | |||||
| $this->htmlSections[] = phutil_escape_html_newlines( | |||||
| phutil_tag('div', array(), $text)); | |||||
| } | } | ||||
| return $this; | return $this; | ||||
| } | } | ||||
| public function addRawPlaintextSection($text) { | |||||
| if (strlen($text)) { | |||||
| $text = rtrim($text); | |||||
| $this->sections[] = $text; | |||||
| } | |||||
| return $this; | |||||
| } | |||||
| public function addRawHTMLSection($html) { | |||||
| $this->htmlSections[] = phutil_safe_html($html); | |||||
| return $this; | |||||
| } | |||||
epriestley: strlen() on a `phutil_tag()` isn't meaningful -- just add this section unconditionally? These… | |||||
| /** | /** | ||||
| * Add a block of text with a section header. This is rendered like this: | * Add a block of text with a section header. This is rendered like this: | ||||
| * | * | ||||
| * HEADER | * HEADER | ||||
| * Text is indented. | * Text is indented. | ||||
| * | * | ||||
| * @param string Header text. | * @param string Header text. | ||||
| * @param string Section text. | * @param string Section text. | ||||
| * @return this | * @return this | ||||
| * @task compose | * @task compose | ||||
| */ | */ | ||||
| public function addTextSection($header, $text) { | public function addTextSection($header, $section) { | ||||
| if ($section instanceof PhabricatorMetaMTAMailSection) { | |||||
| $plaintext = $section->getPlaintext(); | |||||
| $html = $section->getHTML(); | |||||
| } else { | |||||
| $plaintext = $section; | |||||
| $html = phutil_escape_html_newlines(phutil_tag('div', array(), $section)); | |||||
| } | |||||
| $this->addPlaintextSection($header, $plaintext); | |||||
| $this->addHTMLSection($header, $html); | |||||
| return $this; | |||||
| } | |||||
| public function addPlaintextSection($header, $text) { | |||||
| $this->sections[] = $header."\n".$this->indent($text); | $this->sections[] = $header."\n".$this->indent($text); | ||||
| return $this; | return $this; | ||||
| } | } | ||||
| public function addHTMLSection($header, $html_fragment) { | |||||
| $this->htmlSections[] = array( | |||||
| phutil_tag('div', array('style' => 'font-weight:800;'), $header), | |||||
| $html_fragment); | |||||
| return $this; | |||||
Not Done Inline ActionsUse an array, not string concatenation: $this->htmlSections[] = array( phutil_tag(...), $html_fragment, ); epriestley: Use an array, not string concatenation:
$this->htmlSections[] = array(
phutil_tag(...)… | |||||
| } | |||||
| /** | /** | ||||
| * Add a Herald section with a rule management URI and a transcript URI. | * Add a Herald section with a rule management URI and a transcript URI. | ||||
| * | * | ||||
| * @param string URI to rule transcripts. | * @param string URI to rule transcripts. | ||||
| * @return this | * @return this | ||||
| * @task compose | * @task compose | ||||
| */ | */ | ||||
| ▲ Show 20 Lines • Show All 52 Lines • ▼ Show 20 Lines | /* -( Rendering )---------------------------------------------------------- */ | ||||
| * | * | ||||
| * @return string Rendered body. | * @return string Rendered body. | ||||
| * @task render | * @task render | ||||
| */ | */ | ||||
| public function render() { | public function render() { | ||||
| return implode("\n\n", $this->sections)."\n"; | return implode("\n\n", $this->sections)."\n"; | ||||
| } | } | ||||
| public function renderHTML() { | |||||
| $br = phutil_tag('br'); | |||||
Not Done Inline ActionsUse: $br = phutil_tag('br');
$body = phutil_implode_html(array($br, $br), $this->htmlSections);
return array($body, $br);epriestley: Use:
$br = phutil_tag('br');
$body = phutil_implode_html(array($br, $br), $this… | |||||
Not Done Inline Actionsclients expect renderHTML() to a return a string, no an array talshiri: clients expect renderHTML() to a return a string, no an array | |||||
Not Done Inline ActionsOh, sorry -- do this at the end: hsprintf('%s', array($body, $br))epriestley: Oh, sorry -- do this at the end:
hsprintf('%s', array($body, $br)) | |||||
| $body = phutil_implode_html(array($br, $br), $this->htmlSections); | |||||
| return (string)hsprintf('%s', array($body, $br)); | |||||
Not Done Inline ActionsThis is a weird hack to force it to emit a string. a PhutilSafeHTML doesn't seem to get serialized? talshiri: This is a weird hack to force it to emit a string. a `PhutilSafeHTML` doesn't seem to get… | |||||
Not Done Inline ActionsThis will probably work and is a little more conventional: return (string)hsprintf('%s', array($body, $br));epriestley: This will probably work and is a little more conventional:
return (string)hsprintf('%s'… | |||||
Not Done Inline ActionsOooh you can cast things! talshiri: Oooh you can cast things! | |||||
| } | |||||
| /** | /** | ||||
| * Retrieve attachments. | * Retrieve attachments. | ||||
| * | * | ||||
| * @return list<PhabricatorMetaMTAAttachment> Attachments. | * @return list<PhabricatorMetaMTAAttachment> Attachments. | ||||
| * @task render | * @task render | ||||
| */ | */ | ||||
| public function getAttachments() { | public function getAttachments() { | ||||
| Show All 16 Lines | |||||
strlen() on a phutil_tag() isn't meaningful -- just add this section unconditionally? These strlen() checks probably aren't really necessary anyway.