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

enable using announcements to add content to alerts #1842

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
37 changes: 34 additions & 3 deletions scripts/alertmailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,25 @@ function mlog($message) {
ini_set('memory_limit', -1);
include_once INCLUDESPATH . 'easyparliament/member.php';

$announcement_manager = new \MySociety\TheyWorkForYou\Model\AnnouncementManagement();
$banner = $announcement_manager->get_random_valid_item('alerts');

$text_banner = '';
$html_banner = '';
if ($banner) {
$text_banner = $banner->title . "\n\n" . $banner->content . "\n\n";
$html_banner = '<p><strong>' . $banner->title . "</strong></p><p>" . $banner->content . '</p>';

if (property_exists($banner, 'url') && $banner->url) {
$link_text = "Read more";
if (property_exists($banner, 'button_text')) {
$link_text = $banner->button_text;
}
$text_banner .= $banner->url . "\n\n";
$html_banner .= '<p><a href="' . $banner->url . '">' . $link_text . '</a></p>';
}
}

$global_start = getmicrotime();
$db = new ParlDB();

Expand Down Expand Up @@ -211,7 +230,12 @@ function mlog($message) {

if ($email != $current['email']) {
if ($email_text) {
write_and_send_email($current, $email_text, $html_text, $template);

if ($banner) {
$email_text = $text_banner . $email_text;
}

write_and_send_email($current, $email_text, $html_text, $template, $html_banner);
}
$current['email'] = $email;
$current['token'] = $alertitem['alert_id'] . '-' . $alertitem['registrationtoken'];
Expand Down Expand Up @@ -391,7 +415,10 @@ function mlog($message) {
}
}
if ($email_text) {
write_and_send_email($current, $email_text, $html_text, $template);
if ($banner) {
$email_text = $text_banner . $email_text;
}
write_and_send_email($current, $email_text, $html_text, $template, $html_banner);
}

mlog("\n");
Expand Down Expand Up @@ -460,7 +487,7 @@ function sort_by_stuff($a, $b) {
return ($a['hpos'] > $b['hpos']) ? 1 : -1;
}

function write_and_send_email($current, $text, $html, $template) {
function write_and_send_email($current, $text, $html, $template, $html_banner) {
global $globalsuccess, $sentemails, $nomail, $start_time, $domain;

$text .= '====================';
Expand All @@ -470,8 +497,12 @@ function write_and_send_email($current, $text, $html, $template) {
$m = [
'DATA' => $text,
'_HTML_' => $html,
'_BANNER_' => '',
'MANAGE' => "https://$domain/D/" . $current['token'],
];
if ($html_banner) {
$m['_BANNER_'] = $html_banner;
}
if (!$nomail) {
$success = send_template_email($d, $m, true, true, $current['lang']); # true = "Precedence: bulk", want bounces
mlog("sent ... ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
<a href="https://www.theyworkforyou.com">
<img src="images/theyworkforyou-logo.png" alt="" width="170">
</a>
{_BANNER_}
<h1 style="color: #333333; font-size: 21px; font-weight: 400; line-height: 21px; font-family: 'Source Sans Pro'; margin-top: 30px;">Email alerts</h1>
</header>

Expand Down
Loading