-
Notifications
You must be signed in to change notification settings - Fork 1
/
views_civimail.module
348 lines (330 loc) · 12.3 KB
/
views_civimail.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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<?php
/**
* @file
* Contains views_civimail.module.
*
* The views style plugin sets a theme of views_view_civimail.
*
* The module alters the suggestions for other templates when the style is in use.
* Those templates are defined in views_civimail_theme
*
* It also provides tokens to civicrm for views displays using this plugin.
*/
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function views_civimail_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the views_civimail module.
case 'help.page.views_civimail':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('CiviMail views output display plugin') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_theme().
*
* Theme implementations that strip out default drupal wrapping.
*
* views_container_civimail, used to alter the theme suggestions of the container.
* views_civimail, used to alter the theme suggestions of the view itself.
* views_view_civimail, used by the style plugin
*/
function views_civimail_theme() {
return [
'views_container_civimail' => [
'template' => 'views-container-civimail',
'base hook' => 'container',
],
'views_civimail' => [
'template' => 'views-civimail',
'base hook' => 'views_view',
],
'views_view_civimail' => [
'file' => 'views_civimail.theme.inc',
'template' => 'views-view-civimail',
'base hook' => 'views_view_civimail',
],
'views_view_fields_civimail_sidearticle' => [
'template' => 'views-view-fields-civimail-sidearticle',
'base hook' => 'views_view_fields',
],
'views_view_fields_civimail_singlearticle' => [
'template' => 'views-view-fields-civimail-singlearticle',
'base hook' => 'views_view_fields',
],
'views_view_fields_civimail_blog' => [
'template' => 'views-view-fields-civimail-blog',
'base hook' => 'views_view_fields',
],
];
}
/**
* Implement hook_preprocess_views_view_fields
*
* If I'm working with a civimail style for my display,
* and I've chosen a mosaico block option
* then assign the selected fields to the specially named mosaico
* fields.
*/
function views_civimail_preprocess_views_view_fields(&$variables) {
/** @var \Drupal\views\ViewExecutable $view */
$view = $variables['view'];
if (_views_civimail_is_civimail_style($view)) {
$style = $view->style_plugin;
$options = $style->options;
$mosaico_block = $options['mosaico_block'];
if ($mosaico_block) {
foreach(['mosaico_title','mosaico_image','mosaico_longText','mosaico_link'] as $key) {
$value = $options[$key];
if ($value && !empty($variables['fields'][$value])) {
$variables['fields'][strtolower($key)] = $variables['fields'][$value];
}
}
}
}
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*
* Add views "view" template suggestion override when using the civimail style.
*
* @inheritdoc
*/
function views_civimail_theme_suggestions_views_view_alter(array &$suggestions, array $variables) {
if (_views_civimail_is_civimail_style($variables['view'])) {
$suggestions[] = 'views_civimail';
}
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*
* Add views "views_view_fields" template suggestion override when using the civimail style and a mosaico option.
*
* @inheritdoc
*/
function views_civimail_theme_suggestions_views_view_fields_alter(array &$suggestions, array &$variables) {
if (_views_civimail_is_civimail_style($variables['view'])) {
$style = $variables['view']->style_plugin;
$options = $style->options;
$mosaico_block = $options['mosaico_block'];
switch($mosaico_block) {
case 'sideArticleBlock':
$suggestions[] = 'views_view_fields_civimail_sidearticle';
break;
case 'singleArticleBlock':
$suggestions[] = 'views_view_fields_civimail_singlearticle';
break;
case 'singleBlogBlock':
$suggestions[] = 'views_view_fields_civimail_blog';
break;
}
}
}
/**
* Implements hook_theme_suggestions_HOOK() for container templates.
*
* Add a suggestion for a view container when the display style is 'civimail'
*/
function views_civimail_theme_suggestions_container_alter(array &$suggestions, array $variables) {
$element = $variables['element'];
if (isset($element['#type']) && $element['#type'] == 'view') {
if (!empty($element['#view'])) {
if (_views_civimail_is_civimail_style($element['#view'])) {
$suggestions[] = 'views_container_civimail';
}
}
}
}
/*
* Get a list of views/displays using my civimail style
*/
function _views_civimail_displays() {
static $displays;
if (is_array($displays)) {
return $displays;
}
$displays = [];
// this should always be true, but let's avoid unexpected errors
if (method_exists('\Drupal\views\Views','getEnabledViews')) {
$views_list = \Drupal\views\Views::getEnabledViews();
foreach($views_list as $name => $view) {
foreach($view->get('display') as $display_name => $display) {
if (($display['display_options']['style']['type'] ?? '') == 'civimail') {
$displays[$name.'__'.$display_name] = $view->get('label') . ', ' . $display['display_title'];
}
}
}
}
return $displays;
}
/*
* Test a view object with display for whether it uses my "Civimail" style
* @var \Drupal\views\ViewExecutable $view
*/
function _views_civimail_is_civimail_style($view) {
if (!empty($view) && method_exists($view, 'getStyle')) {
$style_class = get_class($view->getStyle());
return ('Drupal\views_civimail\Plugin\views\style\Civimail' == $style_class) ? TRUE : FALSE;
}
}
/*
* Get the output to be used in civimail for a view display
* @var string $key
* Where key is {view name}__{display name}
* TODO: see if it's worth statically saving the output?
*/
function _views_civimail_view_output($key) {
list($view_name,$display) = explode('__', $key, 2);
$view_renderable = views_embed_view($view_name, $display);
$output = \Drupal::service('renderer')->renderPlain($view_renderable);
$output = trim($output);
// now convert src and href links to absolute links
// Fortunately, we can use a civicrm constant that we know points to the base url
$base_url = rtrim(CIVICRM_UF_BASEURL,"/");
// I'm not sure why this first one is necessary, perhaps because it's running via cron
$output = str_replace('="http://:/usr/local/bin/','="'.$base_url.'/', $output);
$output = str_replace(' src="/',' src="'.$base_url.'/', $output);
$output = str_replace(' href="/',' href="'.$base_url.'/', $output);
return $output;
}
function views_civimail_civicrm_config(&$config) {
// \CRM_Core_Error::debug_log_message('in hook config as drupal user ' . \Drupal::currentUser()->id() . ' and civi user ' . \CRM_Core_Session::getLoggedInContactID());
\Civi::dispatcher()->addListener('civi.token.list', 'views_civimail_civicrm_register_tokens');
\Civi::dispatcher()->addListener('civi.token.eval', 'views_civimail_civicrm_evaluate_tokens');
}
function views_civimail_civicrm_container($container) {
$container->addResource(new \Symfony\Component\Config\Resource\FileResource(__FILE__));
// $container->findDefinition('dispatcher')->addMethodCall('addListener',
// ['civi.token.list', 'views_civimail_civicrm_register_tokens']
// );
// $container->findDefinition('dispatcher')->addMethodCall('addListener',
// ['civi.token.eval', 'views_civimail_civicrm_evaluate_tokens']
// );
}
function views_civimail_civicrm_register_tokens(\Civi\Token\Event\TokenRegisterEvent $e) {
// \CRM_Core_Error::debug_log_message('in register tokens');
$displays = _views_civimail_displays();
$e_views = $e->entity('views');
foreach($displays as $name => $label) {
$e_views->register($name,$label);
}
}
function views_civimail_civicrm_evaluate_tokens(\Civi\Token\Event\TokenValueEvent $e) {
// \CRM_Core_Error::debug_log_message('in evaluate tokens');
$tokens = $e->getTokenProcessor()->getMessageTokens();
if (!empty($tokens['views'])) {
// ensure I've loaded all my Drupal modules (like views!)
\Drupal::getContainer()->get('module_handler')->loadAll();
$display_filter = _views_civimail_displays();
foreach($tokens['views'] as $key) {
if (!empty($display_filter[$key])) {
$output = _views_civimail_view_output($key);
// convert links to trackable links
$matched = preg_match_all('/ href="([^#].+?)"/', $output, $matches);
// CRM_Core_Error::debug('matched',$matched, 1, 1);
$trackable_urls = array();
// TODO: can job_id be fetched from some context value?
if (!empty($job_id) && !empty($output) && $matched) {
// job details to get mailing_id
$job = civicrm_api3('MailingJob', 'getsingle', array('id' => $job_id));
// $result = $xml->xpath("//@href");
$list = array();
// while(list( , $url) = each($result)) {
foreach($matches[1] as $url) {
$list[$url] = 1;
}
$trackable_urls = array_keys($list);
}
else {
// watchdog('view values', "$view_name $display_name $job_id ".$output);
}
foreach ($e->getRows() as $row) {
/** @var TokenRow $row */
$row->format('text/html');
$row->tokens('views', $key, $output);
}
/*
foreach ($cids as $cid) {
if (count($trackable_urls)) {
// get event_queue_id
$queue = civicrm_api3('MailingEventQueue', 'getsingle', array('job_id' => $job_id, 'contact_id' => $cid));
$replace = array();
$search = array();
foreach($trackable_urls as $url) {
$search[] = '"'.$url.'"';
$replace[] = '"'.CRM_Mailing_BAO_TrackableURL::getTrackerURL($url, $job['mailing_id'], $queue['id']).'"';
}
$values[$cid]['views.'.$key] = str_replace($search,$replace,$output);
}
else {
$values[$cid]['views.'.$key] = $output;
}
} */
}
}
}
}
/*
* Provide tokens for civicrm
*/
function views_civimail_civicrm_tokens(&$tokens) {
// get all my views/displays that are using the Civimail style
$displays = _views_civimail_displays();
$token_name = 'views';
$views_tokens = [];
foreach($displays as $name => $label) {
$views_tokens[$token_name.'.'.$name] = $label;
}
$tokens[$token_name] = $views_tokens;
}
function views_civimail_civicrm_tokenValues(&$values, $cids, $job_id = null, $tokens = array(), $context = null) {
if (!empty($tokens['views'])) {
\Drupal::getContainer()->get('module_handler')->loadAll();
// for security, only allow my "Civimail style" view/displays
$display_filter = _views_civimail_displays();
foreach(array_keys($tokens['views']) as $key) {
if (!empty($display_filter[$key])) {
$output = _views_civimail_view_output($key);
// convert links to trackable links
$matched = preg_match_all('/ href="([^#].+?)"/', $output, $matches);
// CRM_Core_Error::debug('matched',$matched, 1, 1);
$trackable_urls = array();
if (!empty($job_id) && !empty($output) && $matched) {
// job details to get mailing_id
$job = civicrm_api3('MailingJob', 'getsingle', array('id' => $job_id));
// $result = $xml->xpath("//@href");
$list = array();
// while(list( , $url) = each($result)) {
foreach($matches[1] as $url) {
$list[$url] = 1;
}
$trackable_urls = array_keys($list);
}
else {
// watchdog('view values', "$view_name $display_name $job_id ".$output);
}
foreach ($cids as $cid) {
if (count($trackable_urls)) {
// get event_queue_id
$queue = civicrm_api3('MailingEventQueue', 'getsingle', array('job_id' => $job_id, 'contact_id' => $cid));
$replace = array();
$search = array();
foreach($trackable_urls as $url) {
$search[] = '"'.$url.'"';
$replace[] = '"'.CRM_Mailing_BAO_TrackableURL::getTrackerURL($url, $job['mailing_id'], $queue['id']).'"';
}
$values[$cid]['views.'.$key] = str_replace($search,$replace,$output);
}
else {
$values[$cid]['views.'.$key] = $output;
}
}
}
}
}
}