Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/metamta/view/PhabricatorMetaMTAMailBody.php
12256 views
1
<?php
2
3
/**
4
* Render the body of an application email by building it up section-by-section.
5
*
6
* @task compose Composition
7
* @task render Rendering
8
*/
9
final class PhabricatorMetaMTAMailBody extends Phobject {
10
11
private $sections = array();
12
private $htmlSections = array();
13
private $attachments = array();
14
15
private $viewer;
16
private $contextObject;
17
18
public function getViewer() {
19
return $this->viewer;
20
}
21
22
public function setViewer($viewer) {
23
$this->viewer = $viewer;
24
return $this;
25
}
26
27
public function setContextObject($context_object) {
28
$this->contextObject = $context_object;
29
return $this;
30
}
31
32
public function getContextObject() {
33
return $this->contextObject;
34
}
35
36
37
/* -( Composition )-------------------------------------------------------- */
38
39
40
/**
41
* Add a raw block of text to the email. This will be rendered as-is.
42
*
43
* @param string Block of text.
44
* @return this
45
* @task compose
46
*/
47
public function addRawSection($text) {
48
if (strlen($text)) {
49
$text = rtrim($text);
50
$this->sections[] = $text;
51
$this->htmlSections[] = phutil_escape_html_newlines(
52
phutil_tag('div', array(), $text));
53
}
54
return $this;
55
}
56
57
public function addRemarkupSection($header, $text) {
58
try {
59
$engine = $this->newMarkupEngine()
60
->setMode(PhutilRemarkupEngine::MODE_TEXT);
61
62
$styled_text = $engine->markupText($text);
63
$this->addPlaintextSection($header, $styled_text);
64
} catch (Exception $ex) {
65
phlog($ex);
66
$this->addTextSection($header, $text);
67
}
68
69
try {
70
$mail_engine = $this->newMarkupEngine()
71
->setMode(PhutilRemarkupEngine::MODE_HTML_MAIL);
72
73
$html = $mail_engine->markupText($text);
74
$this->addHTMLSection($header, $html);
75
} catch (Exception $ex) {
76
phlog($ex);
77
$this->addHTMLSection($header, $text);
78
}
79
80
return $this;
81
}
82
83
public function addRawPlaintextSection($text) {
84
if (strlen($text)) {
85
$text = rtrim($text);
86
$this->sections[] = $text;
87
}
88
return $this;
89
}
90
91
public function addRawHTMLSection($html) {
92
$this->htmlSections[] = phutil_safe_html($html);
93
return $this;
94
}
95
96
97
/**
98
* Add a block of text with a section header. This is rendered like this:
99
*
100
* HEADER
101
* Text is indented.
102
*
103
* @param string Header text.
104
* @param string Section text.
105
* @return this
106
* @task compose
107
*/
108
public function addTextSection($header, $section) {
109
if ($section instanceof PhabricatorMetaMTAMailSection) {
110
$plaintext = $section->getPlaintext();
111
$html = $section->getHTML();
112
} else {
113
$plaintext = $section;
114
$html = phutil_escape_html_newlines(phutil_tag('div', array(), $section));
115
}
116
117
$this->addPlaintextSection($header, $plaintext);
118
$this->addHTMLSection($header, $html);
119
return $this;
120
}
121
122
public function addPlaintextSection($header, $text, $indent = true) {
123
if ($indent) {
124
$text = $this->indent($text);
125
}
126
$this->sections[] = $header."\n".$text;
127
return $this;
128
}
129
130
public function addHTMLSection($header, $html_fragment) {
131
if ($header !== null) {
132
$header = phutil_tag('strong', array(), $header);
133
}
134
135
$this->htmlSections[] = array(
136
phutil_tag(
137
'div',
138
array(),
139
array(
140
$header,
141
phutil_tag('div', array(), $html_fragment),
142
)),
143
);
144
return $this;
145
}
146
147
public function addLinkSection($header, $link) {
148
$html = phutil_tag('a', array('href' => $link), $link);
149
$this->addPlaintextSection($header, $link);
150
$this->addHTMLSection($header, $html);
151
return $this;
152
}
153
154
155
/**
156
* Add an attachment.
157
*
158
* @param PhabricatorMailAttachment Attachment.
159
* @return this
160
* @task compose
161
*/
162
public function addAttachment(PhabricatorMailAttachment $attachment) {
163
$this->attachments[] = $attachment;
164
return $this;
165
}
166
167
168
/* -( Rendering )---------------------------------------------------------- */
169
170
171
/**
172
* Render the email body.
173
*
174
* @return string Rendered body.
175
* @task render
176
*/
177
public function render() {
178
return implode("\n\n", $this->sections)."\n";
179
}
180
181
public function renderHTML() {
182
$br = phutil_tag('br');
183
$body = phutil_implode_html($br, $this->htmlSections);
184
return (string)hsprintf('%s', array($body, $br));
185
}
186
187
/**
188
* Retrieve attachments.
189
*
190
* @return list<PhabricatorMailAttachment> Attachments.
191
* @task render
192
*/
193
public function getAttachments() {
194
return $this->attachments;
195
}
196
197
198
/**
199
* Indent a block of text for rendering under a section heading.
200
*
201
* @param string Text to indent.
202
* @return string Indented text.
203
* @task render
204
*/
205
private function indent($text) {
206
return rtrim(" ".str_replace("\n", "\n ", $text));
207
}
208
209
210
private function newMarkupEngine() {
211
$engine = PhabricatorMarkupEngine::newMarkupEngine(array())
212
->setConfig('viewer', $this->getViewer())
213
->setConfig('uri.base', PhabricatorEnv::getProductionURI('/'));
214
215
$context = $this->getContextObject();
216
if ($context) {
217
$engine->setConfig('contextObject', $context);
218
}
219
220
return $engine;
221
}
222
223
}
224
225