-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtmlmail_views.module
More file actions
373 lines (327 loc) · 10.9 KB
/
htmlmail_views.module
File metadata and controls
373 lines (327 loc) · 10.9 KB
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
<?php
/**
* For custom views fields
* Implements hook_views_api().
*/
function htmlmail_views_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'htmlmail_views') . '/views',
);
}
/**
* Ajax menu callback.
*/
function htmlmail_views_callback($ajax, $view_name, $display) {
if ($ajax) {
ctools_include('ajax');
ctools_include('modal');
$main_view = views_get_view($view_name);
if (isset($main_view->display[$display]->display_options['header']['enable_htmlmail_views'])) {
$arr_view = $main_view->display[$display];
$a_user = $arr_view->display_options['header']['enable_htmlmail_views']['htmlmail_views_from_user'];
$a_from = $arr_view->display_options['header']['enable_htmlmail_views']['htmlmail_views_from_alias'];
$a_link_text = $arr_view->display_options['header']['enable_htmlmail_views']['htmlmail_views_link_text'];
$a_to = $arr_view->display_options['header']['enable_htmlmail_views']['htmlmail_views_def_recipient'];
$a_body = $arr_view->display_options['header']['enable_htmlmail_views']['htmlmail_views_body_msg'];
$a_css = $arr_view->display_options['header']['enable_htmlmail_views']['htmlmail_views_global_css'];
$a_subject = $arr_view->display_options['header']['enable_htmlmail_views']['htmlmail_views_def_subject'];
$other_view = $arr_view->display_options['header']['enable_htmlmail_views']['htmlmail_views_other_view'];
//If other_view is populated, update the view_name and display variables
if (!empty($other_view)) {
$arr_view = explode("|", $other_view);
if (views_get_view($arr_view[0], $arr_view[1])) {
$view_name = $arr_view[0];
$display = $arr_view[1];
}
}
}
if ($a_user == TRUE) {
global $user;
$a_from = $user->mail;
}
$form_state = array(
'ajax' => TRUE,
'title' => t('Email View'),
'view_name' => $view_name,
'display_name' => $display,
'from_alias' => $a_from,
'def_recipient' => $a_to,
'body_msg' => $a_body,
'globalcss' => $a_css,
'subject' => $a_subject,
);
// Use ctools to generate ajax instructions for the browser to create
// a form in a modal popup.
$output = ctools_modal_form_wrapper('htmlmail_views_form', $form_state);
// If the form has been submitted, there may be additional instructions
// such as dismissing the modal popup.
if (!empty($form_state['ajax_commands'])) {
$output = $form_state['ajax_commands'];
}
// Return the ajax instructions to the browser via ajax_render().
print ajax_render($output);
drupal_exit();
} else {
return drupal_get_form('htmlmail_views_form', $view_name);
}
}
function htmlmail_views_form($form, &$form_state, $view_name) {
global $user;
drupal_add_css(drupal_get_path('module', 'htmlmail_views') .'/htmlmail_views.css');
$form['#id'] = 'htmlmail_views_form';
$form['email_to'] = array(
'#weight' => 0,
'#title' => t('Email To:'),
'#id' => 'htmlmail_views_email_to',
'#type' => 'textfield',
'#size'=> 30,
'#default_value' => $form_state['def_recipient'],
);
$form['email_from'] = array(
'#weight' => 1,
'#title' => t('Email (alias) From:'),
'#id' => 'htmlmail_views_email_from',
'#type' => 'textfield',
'#size'=> 30,
'#default_value' => $form_state['from_alias'],
);
$form['email_subject'] = array(
'#weight' => 2,
'#title' => t('Subject:'),
'#id' => 'htmlmail_views_email_subject',
'#type' => 'textfield',
'#size'=> 30,
'#default_value' => $form_state['subject'],
);
$form['global_css'] = array(
'#type' => 'hidden',
'#default_value' => $form_state['globalcss'],
);
$form['body_msg'] = array(
'#weight' => 3,
'#title' => t('Message'),
'#description' => t('The view will be embedded at the end of this email.'),
'#type' => 'textarea',
'#id' => 'htmlmail_views_body_msg',
'#size'=> 30,
'#default_value' => $form_state['body_msg'],
);
$form['submit'] = array(
'#type' => 'submit',
'#weight' => 4,
'#value' => t('Send'),
);
$form['cancel'] = array(
'#type' => 'submit',
'#value' => t('Cancel'),
'#weight' => 5,
);
return $form;
}
/**
* Drupal form submit handler.
*/
function htmlmail_views_form_submit(&$form, &$form_state) {
// Tell the browser to close the modal.
if ($form_state['values']['op'] == 'Cancel') {
$form_state['ajax_commands'][] = ctools_modal_command_dismiss();
return;
}
global $user;
$to = trim($form_state['values']['email_to']);
$site_email = variable_get('site_mail', '');
$from = trim($form_state['values']['email_from']);
$css = $form_state['values']['global_css'];
$body = $form_state['values']['body_msg'];
$view_name = $form_state['view_name'];
$display = $form_state['display_name'];
$subject = trim($form_state['values']['email_subject']);
if (empty($form_state['values']['email_from'])) {
$from = $site_email;
}
drupal_mail
(
// Module
'htmlmail_views',
// Mail Key
'email_view',
// The destination email address
$to,
// The language that the mail should be sent in (requires translation if not English)
$user->language,
// parameters to use in hook_mail()
array
(
'to' => $to,
'from' => $from,
'body' => $body,
'css' => $css,
'subject' => $subject,
'viewname' => $view_name,
'display' => $display,
),
$from
);
$form_state['ajax_commands'][] = ctools_modal_command_dismiss();
$form_state['ajax_commands'][] = ajax_command_insert('#htmlmail-views-link', '<div class="ajax-messages"><span class="pos">×</span>Email sent!</div>');
}
/**
* Drupal form validate.
*/
function htmlmail_views_form_validate(&$form, &$form_state) {
if ($form_state['values']['op'] == 'Cancel') {
$form_state['ajax_commands'][] = ctools_modal_command_dismiss();
return;
}
$to = $form_state['values']['email_to'];
$site_email = variable_get('site_mail', '');
$from = $form_state['values']['email_from'];
$css = $form_state['values']['global_css'];
$body = $form_state['values']['body_msg'];
$view_name = $form_state['view_name'];
$display = $form_state['display_name'];
$subject = $form_state['values']['email_subject'];
//{+} VAL TO EMAIL
$email_raw = $to;
$elist = explode(",", $email_raw);
foreach ($elist as $key => $email) {
$email = trim($email);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
form_set_error('email_to', t('Invalid email(s). Should be name@domain.com, name2@domain.com'));
$form_state['rebuild'] = TRUE;
$form_state['flag'] = 1;
}
}
//{+} VAL FROM EMAIL
$arr = explode(' ',trim($from));
if (!filter_var($arr[0], FILTER_VALIDATE_EMAIL) && !empty($from)) {
form_set_error('email_from', t('Invalid From email. Should be name@domain.com'));
$form_state['rebuild'] = TRUE;
$form_state['flag'] = 1;
}
//{+} VAL SUBJECT
if (strlen(trim($from)) > 100) {
form_set_error('email_subject', t('C\'mon...subject is too long.'));
$form_state['rebuild'] = TRUE;
$form_state['flag'] = 1;
}
}
/**
* Implements hook_ctools_plugin_type().
*/
function htmlmail_views_admin() {
//--> BACKEND CONFIGURATION Form
$form = array();
$markup = 'test markup';
$form['htmlmail_views_version'] = array(
'#markup' => $markup,
'#weight' => -100,
);
$form['htmlmail_views_contenttype'] = array(
'#type' => 'textfield',
'#title' => t('Event Content Type'),
'#default_value' => variable_get('htmlmail_views_contenttype', 'event'),
'#size' => 60,
'#maxlength' => 60,
'#description' => t("The machine name of the content type to react on"),
'#required' => TRUE,
);
$form['#submit'][] = 'htmlmail_views_admin_submit';
return system_settings_form($form);
}
/**
* Implements submit handler
*/
function htmlmail_views_admin_submit($form, &$form_state) {
}
/**
* Implements hook_menu().
*/
function htmlmail_views_menu() {
$items = array();
$items['admin/config/htmlmail-views-settings'] = array(
'title' => 'HTML Mail Settings',
'description' => 'HTML Mail Settings',
'page callback' => 'drupal_get_form',
'page arguments' => array('htmlmail_views_admin'),
'access arguments' => array('access administration pages'),
'type' => MENU_NORMAL_ITEM,
);
$items['email_view/%ctools_js/%/%'] = array(
'title' => t('Email this'),
'page callback' => 'htmlmail_views_callback',
'page arguments' => array(1, 2, 3),
'access callback' => TRUE,
);
return $items;
}
/**
* Implements hook_views_pre_render().
*/
function htmlmail_views_views_pre_render(&$view) {
if (isset($view->header['enable_htmlmail_views']->options['htmlmail_views_option']) == 1) {
$view_name = $view->name;
$display = $view->current_display;
$a_link_text = $view->query->pager->display->handler->handlers['header']['enable_htmlmail_views']->options['htmlmail_views_link_text'];
ctools_include('ajax');
ctools_include('modal');
ctools_modal_add_js();
$href = 'email_view/nojs/' . $view_name;
$href .= '/'.$display;
$view->query->pager->display->handler->handlers['header']['enable_htmlmail_views']->options['htmlmail_views_link_text'] = ctools_modal_text_button($a_link_text, $href, 'Email this View', 'ctools-modal-happy-modal-style modal-invoke');
}
}
/**
* Implements hook_mail().
*
*/
function htmlmail_views_mail ($key, &$message, $params) {
if($key == 'email_view') {
//set headers to HTML - Requires MIMEMAIL
$message['headers']['MIME-Version'] = '1.0';
$message['headers']['Content-Type'] = 'multipart/mixed;';
$message['headers']['Content-Type'] = 'text/html;';
$message['subject'] = $params['subject'];
//load the view - attach it to the end of the body
$output = views_embed_view($params['viewname'], $params['display']);
$message['body'][] = '<style>'.$params['css'].'</style>';
$message['body'][] = $params['body'];
$message['body'][] = $output;
}
}
function htmlmail_views_mail_alter(&$message) {
//experimental code used for exporting a view and sending it as an attachment.
//parts of this work perfectly except it also prompts the sending user to download the file :(
// if ($message['key'] == 'email_view') {
// $view = views_get_view('accounts');
// $display = $view->preview('views_data_export_1');
// $xml_name = 'public://exports/test4.csv'; // xml,pdf,doc etc as you needed
// $file = file_save_data($display,$xml_name,FILE_EXISTS_REPLACE);
// dpm($file);
// $acct = $message['params']['account_name'];
// $output = views_embed_view('projects', 'page_4');
// dpm($output);
// dpm($display);
//file_unmanaged_save_data($output, 'public://exports/test5.csv', FILE_EXISTS_REPLACE);
// $message['body'][] = $output;
// $file_content = file_get_contents('public://exports/test5.csv');
// $attachments = array(
// 'filecontent' => $file_content,
// 'filename' => 'test5.csv',
// 'filepath' => 'public://exports/test5.csv',
// 'filemime' => 'text/csv',
// );
// $message['params']['attachments'][] = $attachments;
// }
}
/**
* Implements hook_node_view().
*/
function htmlmail_views_node_view($node, $view_mode, $langcode) {
}
/**
* Implements hook_form_alter().
*/
function htmlmail_views_form_alter(&$form, &$form_state, $form_id) {
}