summaryrefslogtreecommitdiff
blob: fe59aaa3ade71ef1ae37797f105ab579ae541324 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?php

class EchoHtmlDigestEmailFormatter extends EchoEventDigestFormatter {

	/**
	 * @var string 'daily' or 'weekly'
	 */
	protected $digestMode;

	public function __construct( User $user, Language $language, $digestMode ) {
		parent::__construct( $user, $language );
		$this->digestMode = $digestMode;
	}

	/**
	 * @param EchoEventPresentationModel[] $models
	 * @return string[] Array of the following format:
	 *               [ 'body'    => formatted email body,
	 *                 'subject' => formatted email subject ]
	 */
	protected function formatModels( array $models ) {
		// echo-email-batch-body-intro-daily
		// echo-email-batch-body-intro-weekly
		$intro = $this->msg( 'echo-email-batch-body-intro-' . $this->digestMode )
			->params( $this->user->getName() )
			->parse();
		$intro = nl2br( $intro );

		$eventsByCategory = $this->groupByCategory( $models );
		ksort( $eventsByCategory );
		$digestList = $this->renderDigestList( $eventsByCategory );

		$htmlFormatter = new EchoHtmlEmailFormatter( $this->user, $this->language );

		$body = $this->renderBody(
			$this->language,
			$intro,
			$digestList,
			$this->renderAction(),
			$htmlFormatter->getFooter()
		);

		// echo-email-batch-subject-daily
		// echo-email-batch-subject-weekly
		$subject = $this->msg( 'echo-email-batch-subject-' . $this->digestMode )
			->numParams( count( $models ), count( $models ) )
			->text();

		return [
			'subject' => $subject,
			'body' => $body,
		];
	}

	private function renderBody( Language $language, $intro, $digestList, $action, $footer ) {
		$alignStart = $language->alignStart();
		$langCode = $language->getHtmlCode();
		$langDir = $language->getDir();

		// phpcs:disable Generic.Files.LineLength
		return <<< EOF
<html><head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
	<style>
		@media only screen and (max-width: 480px){
			table[id="email-container"]{max-width:600px !important; width:100% !important;}
		}
	</style>
</head><body>
<table cellspacing="0" cellpadding="0" border="0" width="100%" align="center" lang="$langCode" dir="$langDir">
<tr>
	<td bgcolor="#EAECF0"><center>
		<br /><br />
		<table cellspacing="0" cellpadding="0" border="0" width="600" id="email-container">
			<tr>
				<td bgcolor="#FFFFFF" width="5%">&nbsp;</td>
				<td bgcolor="#FFFFFF" width="6%">&nbsp;</td>
				<td bgcolor="#FFFFFF" width="79%" style="line-height:40px;">&nbsp;</td>
				<td bgcolor="#FFFFFF" width="10%">&nbsp;</td>
			</tr>
			<tr>
				<td bgcolor="#FFFFFF" rowspan="2">&nbsp;</td>
				<td bgcolor="#FFFFFF" rowspan="2">&nbsp;</td>
				<td bgcolor="#FFFFFF" align="center" style="font-family: Arial, Helvetica, sans-serif; font-size:13px; line-height:20px; color:#72777D; text-align: center;">$intro</td>
				<td bgcolor="#FFFFFF" rowspan="2">&nbsp;</td>
			</tr>
			<tr>
				<td bgcolor="#FFFFFF" align="$alignStart" style="font-family: Arial, Helvetica, sans-serif; line-height: 20px; font-weight: 600;">
					<table cellspacing="0" cellpadding="0" border="0" width="100%">
						<tr>
							<td bgcolor="#FFFFFF" align="$alignStart" style="font-family: Arial, Helvetica, sans-serif; font-size:13px; color: #54595D; padding-top: 25px;">
								$digestList
							</td>
						</tr>
					</table>
					<br /><br />
				</td>
			</tr>
			<tr>
				<td bgcolor="#FFFFFF">&nbsp;</td>
				<td bgcolor="#FFFFFF">&nbsp;</td>
				<td bgcolor="#FFFFFF" style="line-height:60px;" align="center">$action</td>
				<td bgcolor="#FFFFFF">&nbsp;</td>
			</tr>
			<tr>
				<td bgcolor="#FFFFFF">&nbsp;</td>
				<td bgcolor="#FFFFFF">&nbsp;</td>
				<td bgcolor="#FFFFFF" style="line-height:40px;">&nbsp;</td>
				<td bgcolor="#FFFFFF">&nbsp;</td>
			</tr>
			<tr>
				<td>&nbsp;</td>
				<td>&nbsp;</td>
				<td align="$alignStart" style="font-family: Arial, Helvetica, sans-serif; font-size:10px; line-height:13px; color:#72777D; padding: 10px 20px;"><br />
					$footer
					<br /><br />
				</td>
				<td>&nbsp;</td>
			</tr>
			<tr>
				<td colspan="4">&nbsp;</td>
			</tr>
		</table>
		<br><br></center>
	</td>
</tr>
</table>
</body></html>
EOF;
		// phpcs:enable Generic.Files.LineLength
	}

	/**
	 * @param string $type Notification type
	 * @param int $count Number of notifications in this type's section
	 * @return string Formatted category section title
	 */
	private function getCategoryTitle( $type, $count ) {
		return $this->msg( "echo-category-title-$type" )
			->numParams( $count )
			->parse();
	}

	/**
	 * @param EchoEventPresentationModel[] $models
	 * @return array [ 'category name' => EchoEventPresentationModel[] ]
	 */
	private function groupByCategory( array $models ) {
		$eventsByCategory = [];
		foreach ( $models as $model ) {
			$eventsByCategory[$model->getCategory()][] = $model;
		}
		return $eventsByCategory;
	}

	/**
	 * Apply style to notification category header
	 * @param string $category Can contain HTML. Is included as-is in HTML template, is not escaped.
	 * @return string
	 */
	protected function applyStyleToCategory( $category ) {
		return <<< EOF
<tr>
	<td colspan="2" style="color: #72777D; font-weight: normal; font-size: 13px; padding-top: 15px;">
		$category <br />
		<hr style="background-color:#FFFFFF; color:#FFFFFF; border: 1px solid #EAECF0;" />
	</td>
</tr>
EOF;
	}

	/**
	 * Apply style to individual notification event
	 * @param EchoEventPresentationModel $model
	 * @return string
	 */
	protected function applyStyleToEvent( EchoEventPresentationModel $model ) {
		$iconUrl = wfExpandUrl(
			EchoIcon::getRasterizedUrl( $model->getIconType(), $this->language->getCode() ),
			PROTO_CANONICAL
		);

		$imgSrc = Sanitizer::encodeAttribute( $iconUrl );

		// notification text
		$text = $model->getHeaderMessage()->parse();

		return <<< EOF
<tr>
	<td width="30">
		<img src="$imgSrc" width="30" height="30" style="vertical-align:middle;">
	</td>
	<td style="font-family: Arial, Helvetica, sans-serif; font-size:13px; color: #54595D;">
		$text
	</td>
</tr>
EOF;
	}

	private function renderDigestList( $eventsByCategory ) {
		$result = [];
		// build the html section for each category
		foreach ( $eventsByCategory as $category => $models ) {
			$output = $this->applyStyleToCategory(
				$this->getCategoryTitle( $category, count( $models ) )
			);
			foreach ( $models as $model ) {
				$output .= "\n" . $this->applyStyleToEvent( $model );
			}
			$result[] = '<table border="0" width="100%">' . $output . '</table>';
		}

		return trim( implode( "\n", $result ) );
	}

	private function renderAction() {
		return Html::element(
			'a',
			[
				'href' => SpecialPage::getTitleFor( 'Notifications' )
					->getFullURL( '', false, PROTO_CANONICAL ),
				'style' => EchoHtmlEmailFormatter::PRIMARY_LINK_STYLE,
			],
			$this->msg( 'echo-email-batch-link-text-view-all-notifications' )->text()
		);
	}

}