Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LOOP-1158: Fixed handling of revision messages in mail content #255

Merged
merged 1 commit into from
Nov 18, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ public function mail($key, &$message, $params) {
* True if mail is sent.
*/
public function sendNotification(User $user, array $groupedMessages) {
$lang_code = $user->getPreferredLangcode();
$langcode = $user->getPreferredLangcode();

$sections = [];
foreach ($groupedMessages as $type => $messages) {
$section = array_map(static function (Message $message) use ($lang_code) {
return $message->getText($lang_code)[0] ?? NULL;
$section = array_map(function (Message $message) use ($langcode) {
return $this->getMessageContent($message, $langcode);
}, $messages);
$section = implode(PHP_EOL, $section);
$sections[$type] = $section;
Expand All @@ -96,22 +96,22 @@ public function sendNotification(User $user, array $groupedMessages) {
//
// A message on the form
//
// Something new: <a href="…">New stuff</a>.
// Something new: <a href="…">New stuff</a> (Revision message: Updated
// stuff.)
//
// will be put under the heading "Something new" (colon and space are
// removed) as <a href="…">New stuff</a> (only a element is used).
// will be put under the heading 'Something new' (colon and space are
// removed) with content
//
// <a href="…">New stuff</a> (Revision message: Updated
// stuff.)
$messageSections = [];
foreach ($groupedMessages as $messages) {
foreach ($messages as $message) {
$text = $message->getText($lang_code)[0] ?? NULL;
// Use text before a element as heading and keep only the a element as
// text.
if (NULL !== $text
&& preg_match('@^(?P<heading>[^<]+?)(:\s*)?(?P<content><a.+</a>)@', $text, $matches)) {
[$heading, $content] = [$matches['heading'], $matches['content']];
if ($message->hasField('os2loop_revision_message') && !empty($message->get('os2loop_revision_message')->getValue())) {
$content .= ' (' . $this->t('Revision message: @revision_message', ['@revision_message' => $message->get('os2loop_revision_message')->getString()]) . ')';
}
$content = $this->getMessageContent($message, $langcode);
// Use text before colon as heading.
$parts = preg_split('/:\s*/', $content, 2);
if (2 === count($parts)) {
[$heading, $content] = array_map('trim', $parts);
$messageSections[$heading][] = $content;
}
}
Expand All @@ -127,7 +127,7 @@ public function sendNotification(User $user, array $groupedMessages) {
$params['messages'] = implode(PHP_EOL . PHP_EOL, $sections);
$params['user'] = $user;

$result = $this->mailer->mail(Helper::MODULE, self::NOTIFICATION_MAIL, $user->getEmail(), $lang_code, $params, NULL, TRUE);
$result = $this->mailer->mail(Helper::MODULE, self::NOTIFICATION_MAIL, $user->getEmail(), $langcode, $params, NULL, TRUE);

return TRUE === $result['result'];
}
Expand Down Expand Up @@ -186,4 +186,18 @@ public function tokenInfo() {
];
}

/**
* Get content of a message including any revision message.
*/
private function getMessageContent(Message $message, string $langCode): string {
$content = $message->getText($langCode)[0] ?? '';

// Append any revision message.
if ($message->hasField('os2loop_revision_message') && !empty($message->get('os2loop_revision_message')->getValue())) {
$content .= ' (' . $this->t('Revision message: @revision_message', ['@revision_message' => $message->get('os2loop_revision_message')->getString()]) . ')';
}

return $content;
}

}