-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsympa.module
167 lines (157 loc) · 5.17 KB
/
sympa.module
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
<?php
/**
* @file
* Sympa module.
*/
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Entity\EntityInterface;
/**
* Implements hook_mail().
*/
function sympa_mail($key, &$message, $params) {
switch ($key) {
case 'subscriptions':
$message['from'] = \Drupal::config('system.site')->get('mail');
$message['headers'] = [
'reply-To' => '[email protected]',
];
$message['body'][] = $params['action'] . ' ' . $params['type'] . ' ' . $params['email'] . ' ' . $params['name'];
break;
case 'node-update':
$message['from'] = \Drupal::config('system.site')->get('mail');
$message['headers'] = [
'reply-To' => '[email protected]',
];
$message['subject'] = $params['title'];
$message['body'][] = $params['body'];
break;
}
}
/**
* Implements hook_entity_presave().
*/
function sympa_entity_presave(EntityInterface $entity) {
$type = $entity->bundle();
if ((!$entity->isNew()) && ($entity->getEntityType()->id() == 'node') && ($type == 'news' || $type == 'service_alert')) {
sympa_node_email_send($entity);
}
}
/**
* Implements hook_node_insert().
*/
function sympa_node_insert(EntityInterface $entity) {
$type = $entity->bundle();
if ($type == 'news' || $type == 'service_alert') {
sympa_node_email_send($entity);
}
}
function sympa_node_email_send($entity) {
$type = $entity->bundle();
$get_fields = $entity->getFields();
$sympa_send = $get_fields['field_sympa_send']->getValue();
if ($sympa_send[0]['value'] == 1) {
$sympa_body = $get_fields['body']->getValue();
$sympa_nid = $entity->id();
// Get entity type.
$type = $entity->bundle();
if ($type == 'service_alert') {
$sympa_title = $entity->get('field_service_alert_status')->getValue()[0]['value'] . ": " . $entity->gettitle();
} else {
$sympa_title = $entity->gettitle();
}
sympa_build_email($type, $sympa_nid, $sympa_title, $sympa_body, 'sympa');
sympa_build_email($type, $sympa_nid, $sympa_title, $sympa_body, 'distribution_list');
// See if checked set to the default of 0 below.
$entity->set('field_sympa_send', 0);
}
}
/**
* Build Sympa Email.
*/
function sympa_build_email($type, $sympa_nid, $sympa_title, $sympa_body, $service) {
$options = ['absolute' => TRUE];
$here = Url::fromRoute('entity.node.canonical', ['node' => $sympa_nid], $options);
$more_info = Url::fromRoute('entity.node.canonical', ['node' => 2514], $options);
$un_options = ['absolute' => TRUE, 'fragment' => 'unsubscribe'];
$unsubscribe_n = Url::fromRoute('entity.node.canonical', ['node' => 31540], $un_options);
$unsubscribe_sa = Url::fromRoute('entity.node.canonical', ['node' => 31539], $un_options);
$config = \Drupal::config('system.site');
$body_xss = isset($sympa_body[0]['value']) ? Xss::filterAdmin($sympa_body[0]['value']) : NULL;
$body_stripped = $body_xss ? preg_replace("/<img[^>]+\>/i", " ", $body_xss) : '';
$unsubscribe_news = $service == 'sympa' ? Link::fromTextAndUrl(t('Unsubscribe now'), $unsubscribe_n)->toString() : '';
$unsubscribe_service_alert = $service == 'sympa' ? Link::fromTextAndUrl(t('Unsubscribe now'), $unsubscribe_sa)->toString() : '';
$learn = $service == 'sympa' ? Link::fromTextAndUrl(t('Learn more about these alerts'), $more_info)->toString() : '';
$body_news = sprintf(
'%s
<p>%s</p>
<br /><br />
---------
<br /><br />
<p>%s %s</p>
<p>%s</p>
<p>%s</p>',
$body_stripped,
Link::fromTextAndUrl(t('View news story here'), $here)->toString(),
t('This is an automated message from'),
$config->get('name'),
$unsubscribe_news,
$learn
);
$body_sa = sprintf(
'<h2>%s</h2>
%s
<p>%s</p>
<br /><br />
---------
<br /><br />
<p>%s %s</p>
<p>%s</p>
<p>%s</p>',
$sympa_title,
$body_stripped,
Link::fromTextAndUrl(t('View full service alert here'), $here)->toString(),
t('This is an automated message from'),
$config->get('name'),
$unsubscribe_service_alert,
$learn
);
$config = \Drupal::config('sympa.settings');
$prod_email = $config->get('sympa_email_prod');
switch ($service) {
case "sympa":
if ($prod_email) {
$to_sa = '[email protected]';
$to_news = '[email protected]';
$to_email = $type == 'service_alert' ? $to_sa : $to_news;
} else {
$to_email = '[email protected]';
}
break;
case "distribution_list":
if ($prod_email) {
$to_email = "[email protected]";
} else {
$to_email = '[email protected]';
}
break;
}
$params = [];
$params['to'] = $to_email;
$params['body'] = $type == 'service_alert' ? $body_sa : $body_news;
$params['title'] = $sympa_title;
sympa_send('node-update', $params);
}
/**
* Send email.
*/
function sympa_send($key, $params) {
$to = $params['to'];
$langcode = \Drupal::currentUser()->getPreferredLangcode();
$send = TRUE;
$module = 'sympa';
$mailManager = \Drupal::service('plugin.manager.mail');
$result = $mailManager->mail($module, $key, $to, $langcode, $params, NULL, $send);
return $result;
}