Skip to content
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
26 changes: 26 additions & 0 deletions modules/access_affinitygroup/access_affinitygroup.module
Original file line number Diff line number Diff line change
Expand Up @@ -1287,6 +1287,12 @@ function emailToAffinityGroups($emailText, $emailTitle, $pubDate, $agNames, $new
// For CC, must be unique name for campaign.
$campaignName = uniqid('Community Announcement: ' . $agNames[0] . '- ' . $pubDate . ' ', FALSE);

\Drupal::logger('access_affinitygroup')->notice('Email broadcast initiated: "@title" to @count affinity group(s): @groups', [
'@title' => $emailTitle,
'@count' => count($agNames),
'@groups' => implode(', ', $agNames),
]);

// Either use email template for access or community.
$wrapper = new EmailWrapper();
if ($communityTemplate) {
Expand All @@ -1302,6 +1308,11 @@ function emailToAffinityGroups($emailText, $emailTitle, $pubDate, $agNames, $new
$campaignActivityId = $ccResponse->campaign_activities[0]->campaign_activity_id;
sendEmailCampaign($campaignId, $campaignActivityId, $ccListIds);
}
else {
\Drupal::logger('access_affinitygroup')->error('Email broadcast failed: setupEmailCampaign returned empty response for "@title"', [
'@title' => $emailTitle,
]);
}
}

/**
Expand Down Expand Up @@ -1341,6 +1352,13 @@ function setupEmailCampaign($emailSubject, $emailHtml, $campaignName) {
$cca = new ConstantContactApi();
$ccResponse = $cca->apiCall('/emails', $postData, 'POST');

if (!empty($ccResponse) && isset($ccResponse->campaign_id)) {
\Drupal::logger('access_affinitygroup')->notice('Email campaign created: "@name" (Campaign ID: @id)', [
'@name' => $campaignName,
'@id' => $ccResponse->campaign_id,
]);
}

return $ccResponse;
}

Expand Down Expand Up @@ -1377,8 +1395,16 @@ function sendEmailCampaign($campaignId, $campaignActivityId, $ccListIds) {
}
}
if ($sent) {
\Drupal::logger('access_affinitygroup')->notice('Email campaign scheduled and sent: Campaign ID @campaign_id, Activity ID @activity_id', [
'@campaign_id' => $campaignId,
'@activity_id' => $campaignActivityId,
]);
showStatus("Your message was sent to this Affinity Group.");
} else {
\Drupal::logger('access_affinitygroup')->error('Failed to send email campaign: Campaign ID @campaign_id, Activity ID @activity_id', [
'@campaign_id' => $campaignId,
'@activity_id' => $campaignActivityId,
]);
showStatus("Error while trying to email announcement to affinity group.");
}
}
Expand Down
42 changes: 16 additions & 26 deletions modules/access_affinitygroup/src/Plugin/EmailWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function ccCommunityNewsHTML($newsBody, $newsTitle, $pubDate, $agNames, $
class="text text--padding-vertical">
<tbody>
<tr>
<td style="text-align:left;font-family:Arial, Verdana, Helvetica, sans-serif;color:#3E3E3E;font-size:14px;line-height:1.2;display:block;word-wrap:break-word;padding:10px 40px;"
<td style="text-align:left;font-family:Arial, Verdana, Helvetica, sans-serif;color:#3E3E3E;font-size:14px;line-height:1.2;display:block;word-wrap:break-word;padding:0px 40px;"
align="left"
valign="top"
class="text_content-cell content-padding-horizontal">
Expand Down Expand Up @@ -119,10 +119,15 @@ class="image_content">
$websiteButtonDisplay = '';
if ($newsUrl != NULL) {
$websiteButtonDisplay = <<<WEBSITEBUTTONHTML
<div style="line-height:20px;min-height:20px;" class="spacer"> </div>
<div style="text-align:left; padding-left: 40px;padding-top:10px;padding-bottom:15px;">
<a href="$newsUrl" rel="nofollow noopener noreferrer" class="view-on-website-btn">VIEW ON WEBSITE</a>
</div>
<table width="100%" border="0" cellpadding="0" cellspacing="0" style="table-layout:fixed;">
<tbody>
<tr>
<td style="text-align:left;font-family:Arial, Verdana, Helvetica, sans-serif;padding:20px 40px 15px 40px;" align="left" valign="top">
<a href="$newsUrl" rel="nofollow noopener noreferrer" class="view-on-website-btn">VIEW ON WEBSITE</a>
</td>
</tr>
</tbody>
</table>
WEBSITEBUTTONHTML;
}

Expand Down Expand Up @@ -384,11 +389,11 @@ class="column column--1 scale stack">
class="text text--padding-vertical">
<tbody>
<tr>
<td style="text-align:left;font-family:Arial, Verdana, Helvetica, sans-serif;color:#3E3E3E;font-size:14px;line-height:1.2;display:block;word-wrap:break-word;padding:10px 40px;"
<td style="text-align:left;font-family:Arial, Verdana, Helvetica, sans-serif;color:#3E3E3E;font-size:14px;line-height:1;display:block;word-wrap:break-word;padding:12px 40px;"
align="left"
valign="top"
class="text_content-cell content-padding-horizontal">
<p style="margin:0;">
<p style="margin:0 !important;">
<span style="color:rgb(255, 255, 255);">
$agText
</span>
Expand All @@ -401,20 +406,6 @@ class="text_content-cell content-padding-horizontal">
</tr>
</tbody>
</table>
<table style="table-layout:fixed;" width="100%"
border="0" cellpadding="0" cellspacing="0"
class="layout layout--1-column">
<tbody>
<tr>
<td style="width:100%;" align="center"
valign="top"
class="column column--1 scale stack">
<div style="line-height:30px;min-height:30px;"
class="spacer"> </div>
</td>
</tr>
</tbody>
</table>
<table style="table-layout:fixed;" width="100%"
border="0" cellpadding="0" cellspacing="0"
class="layout layout--1-column">
Expand All @@ -424,15 +415,15 @@ class="layout layout--1-column">
valign="top"
class="column column--1 scale stack">
$logoDisplay
<div style="line-height:10px;min-height:10px;"
<div style="line-height:20px;min-height:20px;"
class="spacer"> </div>
<table width="100%" border="0"
cellpadding="0" cellspacing="0"
style="table-layout:fixed;"
class="text text--padding-vertical">
<tbody>
<tr>
<td style="text-align:left;font-family:Arial, Verdana, Helvetica, sans-serif;color:#3E3E3E;font-size:14px;line-height:1.2;display:block;word-wrap:break-word;padding:10px 40px;"
<td style="text-align:left;font-family:Arial, Verdana, Helvetica, sans-serif;color:#3E3E3E;font-size:14px;line-height:1.2;display:block;word-wrap:break-word;padding:10px 40px 0px 40px;"
align="left"
valign="top"
class="text_content-cell content-padding-horizontal">
Expand Down Expand Up @@ -761,7 +752,7 @@ public function ccAccessNewsHTML($main, $title, $pubDate, $agNames, $newsUrl) {
$websiteButtonDisplay = '';
if ($newsUrl != NULL) {
$websiteButtonDisplay = <<<WEBSITEBUTTONHTML2
<div style="padding-top:10px;padding-bottom:15px;">
<div style="text-align:left; padding-top:10px;padding-bottom:15px;">
<a href="$newsUrl" rel="nofollow noopener noreferrer" class="view-on-website-btn">VIEW ON WEBSITE</a>
</div>
WEBSITEBUTTONHTML2;
Expand All @@ -776,7 +767,6 @@ public function ccAccessNewsHTML($main, $title, $pubDate, $agNames, $newsUrl) {
<td class="text_content-cell content-padding-horizontal" style="text-align: left; font-family:Roboto,sans-serif; color: #4d4d4d;
font-size: 14px; line-height: 1.2; display: block; word-wrap: break-word; padding: 20px 40px 10px 40px;" align="left" valign="top">
$titleDisplay
<br>
$pubDateDisplay
<span style="font-size: 14px;">$main</span>
<p style="margin: 0;">
Expand All @@ -799,7 +789,7 @@ public function ccAccessNewsHTML($main, $title, $pubDate, $agNames, $newsUrl) {
*/
private function titleHTML($titleText) {
$t = <<<TITLE
<h3 style="font-family:Roboto,sans-serif; color: #f07537; font-size: 18px; font-weight: bold; margin: 0; padding: 0px 0px 8px 0px">
<h3 style="font-family:Roboto,sans-serif; color: #f07537; font-size: 18px; font-weight: bold; margin: 0; padding: 0px 0px 4px 0px">
$titleText
</h3>
TITLE;
Expand Down
98 changes: 87 additions & 11 deletions modules/access_news/access_news.module
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,14 @@ function affinityGroupBroadcast(EntityInterface $node, $newsType) {
$ccListIdArray = [];
$agNames = [];
$logoUrl = NULL;
$currentDomainId = NULL;

// Get the current domain (where the broadcast is being initiated from)
// to use for URLs in the email.
$current_domain = \Drupal::service('domain.negotiator')->getActiveDomain();
if ($current_domain) {
$currentDomainId = $current_domain->id();
}

// Collect ag name and cc email list id.
foreach ($agNodes as $agNode) {
Expand All @@ -546,10 +554,10 @@ function affinityGroupBroadcast(EntityInterface $node, $newsType) {
$ag = Node::load($nid);

$name = $ag->get('title')->value;
array_push($agNames, $name);
$agNames[] = $name;

$ccListId = $ag->get('field_list_id')->value;
array_push($ccListIdArray, $ccListId);
$ccListIdArray[] = $ccListId;

// We decide which email template to use (community or access)
// looking at the type of the first AG only in send list.
Expand All @@ -576,7 +584,8 @@ function affinityGroupBroadcast(EntityInterface $node, $newsType) {
$title = $titleArray[0]['value'];

// Make link to item, including utm for analytics.
$newsUrl = ACCESS_SUPPPORT_URL . $node->toUrl()->toString();
// Use the current domain for the URL.
$newsUrl = _access_news_build_url_for_domain($node, $currentDomainId);
$utmCampaign = filter_var($agNames[0], FILTER_SANITIZE_URL) . '-' . date('mdy');
$newsUrl = appendUtmCC($newsUrl, $utmCampaign);

Expand All @@ -598,7 +607,17 @@ function affinityGroupBroadcast(EntityInterface $node, $newsType) {
// Newstype is eventseries.
$pubDate = NULL;

if ($node->getInstanceCount() > 1) {
// If there's only one event instance, link to that instance instead of the series.
if ($node->getInstanceCount() == 1) {
$instances = $node->get('event_instances')->referencedEntities();
if (!empty($instances)) {
$first_instance = reset($instances);
$newsUrl = _access_news_build_url_for_domain($first_instance, $currentDomainId);
$newsUrl = appendUtmCC($newsUrl, $utmCampaign);
}
}
elseif ($node->getInstanceCount() > 1) {
// Multiple instances - don't include a link button.
$newsUrl = NULL;
}

Expand All @@ -608,8 +627,40 @@ function affinityGroupBroadcast(EntityInterface $node, $newsType) {
$body = \Drupal::service('renderer')->renderPlain($renderArray);

$body = str_replace('Event Instances', 'Event Date & Time', $body);
$body = Html::transformRootRelativeUrlsToAbsolute($body, \Drupal::request()->getSchemeAndHttpHost());

// Un-obfuscate email addresses for email broadcast.
// Convert "contact [at] example.com" back to "contact@example.com".
$body = preg_replace('/\s*\[at\]\s*/', '@', $body);

// Make field labels bold in the email (case-insensitive).
$body = preg_replace(
'/<div class="field__label">([^<]+)<\/div>/i',
'<div class="field__label"><strong>$1</strong></div>',
$body
);

// Transform relative URLs to absolute using the current domain.
if (!empty($body)) {
$domain = \Drupal::entityTypeManager()->getStorage('domain')->load($currentDomainId);
if ($domain) {
// Build base URL: scheme + hostname.
// getScheme() returns 'https://' or 'http://' with the separator included.
$domain_url = $domain->getScheme() . $domain->getHostname();
$body = Html::transformRootRelativeUrlsToAbsolute($body, $domain_url);
}
}
else {
// Body is empty - something went wrong with rendering.
\Drupal::logger('access_news')->error('Event series @id rendered empty body, skipping email broadcast', ['@id' => $node->id()]);
return;
}

}

// Check if body is empty before sending.
if (empty($body)) {
\Drupal::logger('access_news')->error('News/Event @id has empty body, skipping email broadcast', ['@id' => $node->id()]);
return;
}

// Now emailing the group; set option broadcast off. If user wants to email again after
Expand Down Expand Up @@ -828,9 +879,6 @@ function newsRangeView($fromDate, $toDate, $utmCampaign) {
$newsUrl = ACCESS_SUPPPORT_URL . $node->toUrl()->toString();
$newsUrl = appendUtmCC($newsUrl, $utmCampaign);

if ($nCount > 1) {
$listDisplay = $listDisplay;
}
$itemDisplay = $wrapper->newsItemHTML($title, $pubDateDisp, $bodyDisp, $newsUrl);
}
catch (Exception $e) {
Expand Down Expand Up @@ -947,9 +995,6 @@ function eventsRangeView($fromDate, $toDate, $type, $type_title, $utmCampaign)

// $location = empty($enode->location[0]) ? '' : $enode->location[0]->view('rollup_list');
// $locationDisp = \Drupal::service('renderer')->renderPlain($location);
if ($eCount > 1) {
$listDisplay = $listDisplay;
}

if ($recur_type == 'weekly_recurring_date') {
$weekly_id[$series_id][] = [
Expand Down Expand Up @@ -1052,6 +1097,37 @@ function accessNewsCronShouldRun() {
return FALSE;
}

/**
* Helper function to build a URL for an entity using a specific domain.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity (node, eventseries, or eventinstance) to get the URL for.
* @param string $domain_id
* The domain ID to use for building the URL.
*
* @return string
* The full URL with domain.
*/
function _access_news_build_url_for_domain($entity, $domain_id) {
try {
$domain = \Drupal::entityTypeManager()
->getStorage('domain')
->load($domain_id);

if ($domain) {
return $domain->buildUrl($entity->toUrl()->toString());
}

// Fallback to ACCESS Support URL if domain load fails.
return ACCESS_SUPPPORT_URL . $entity->toUrl()->toString();
}
catch (\Exception $e) {
\Drupal::logger('access_news')
->error('Error building URL for domain: ' . $e->getMessage());
return ACCESS_SUPPPORT_URL . $entity->toUrl()->toString();
}
}

/**
* Implements hook_theme().
*/
Expand Down