1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Provides interface and database handling for e-mail settings of a webform.
|
6
|
*
|
7
|
* @author Nathan Haug <nate@lullabot.com>
|
8
|
*/
|
9
|
|
10
|
/**
|
11
|
* Overview form of all components for this webform.
|
12
|
*/
|
13
|
function webform_emails_form($form, $form_state, $node) {
|
14
|
module_load_include('inc', 'webform', 'includes/webform.components');
|
15
|
|
16
|
$form['#attached']['library'][] = array('webform', 'admin');
|
17
|
|
18
|
$form['#tree'] = TRUE;
|
19
|
$form['#node'] = $node;
|
20
|
$form['components'] = array();
|
21
|
|
22
|
foreach ($node->webform['emails'] as $eid => $email) {
|
23
|
$form['emails'][$eid]['status'] = array(
|
24
|
'#type' => 'checkbox',
|
25
|
'#default_value' => $email['status'],
|
26
|
);
|
27
|
$form['emails'][$eid]['email'] = array(
|
28
|
'#markup' => nl2br(check_plain(implode("\n", webform_format_email_address($email['email'], NULL, $node, NULL, FALSE, FALSE)))),
|
29
|
);
|
30
|
$form['emails'][$eid]['subject'] = array(
|
31
|
'#markup' => check_plain(webform_format_email_subject($email['subject'], $node)),
|
32
|
);
|
33
|
$form['emails'][$eid]['from'] = array(
|
34
|
'#markup' => check_plain(webform_format_email_address($email['from_address'], $email['from_name'], $node, NULL, FALSE)),
|
35
|
);
|
36
|
}
|
37
|
|
38
|
$form['add'] = array(
|
39
|
'#theme' => 'webform_email_add_form',
|
40
|
'#tree' => FALSE,
|
41
|
);
|
42
|
|
43
|
$form['add']['status'] = array(
|
44
|
'#type' => 'checkbox',
|
45
|
'#default_value' => TRUE,
|
46
|
);
|
47
|
|
48
|
$form['add']['email_option'] = array(
|
49
|
'#type' => 'radios',
|
50
|
'#options' => array(
|
51
|
'custom' => t('Address'),
|
52
|
'component' => t('Component value'),
|
53
|
),
|
54
|
'#default_value' => 'custom',
|
55
|
);
|
56
|
|
57
|
$form['add']['email_custom'] = array(
|
58
|
'#type' => 'textfield',
|
59
|
'#size' => 24,
|
60
|
'#maxlength' => 500,
|
61
|
);
|
62
|
|
63
|
$form['add']['email_component'] = array(
|
64
|
'#type' => 'select',
|
65
|
'#options' => webform_component_list($node, 'email_address', FALSE),
|
66
|
);
|
67
|
|
68
|
if (empty($form['add']['email_component']['#options'])) {
|
69
|
$form['add']['email_component']['#options'][''] = t('No available components');
|
70
|
$form['add']['email_component']['#disabled'] = TRUE;
|
71
|
}
|
72
|
|
73
|
$form['add']['add'] = array(
|
74
|
'#type' => 'submit',
|
75
|
'#value' => t('Add'),
|
76
|
'#validate' => array('webform_email_address_validate'),
|
77
|
'#submit' => array('webform_emails_form_submit'),
|
78
|
);
|
79
|
|
80
|
$form['actions'] = array(
|
81
|
'#type' => 'actions',
|
82
|
'#weight' => 45,
|
83
|
);
|
84
|
$form['actions']['save'] = array(
|
85
|
'#type' => 'submit',
|
86
|
'#value' => t('Save'),
|
87
|
'#submit' => array('webform_emails_form_status_save'),
|
88
|
'#access' => count($node->webform['emails']) > 0,
|
89
|
);
|
90
|
|
91
|
return $form;
|
92
|
}
|
93
|
|
94
|
/**
|
95
|
* Theme the node components form. Use a table to organize the components.
|
96
|
*
|
97
|
* @param array $variables
|
98
|
* Array with key "form" containing the form array.
|
99
|
*
|
100
|
* @return string
|
101
|
* Formatted HTML form, ready for display.
|
102
|
*
|
103
|
* @throws Exception
|
104
|
*/
|
105
|
function theme_webform_emails_form(array $variables) {
|
106
|
$form = $variables['form'];
|
107
|
$node = $form['#node'];
|
108
|
|
109
|
$header = array(t('Send'), t('E-mail to'), t('Subject'), t('From'), array('data' => t('Operations'), 'colspan' => 3));
|
110
|
$rows = array();
|
111
|
|
112
|
if (!empty($form['emails'])) {
|
113
|
foreach (element_children($form['emails']) as $eid) {
|
114
|
// Add each component to a table row.
|
115
|
$rows[] = array(
|
116
|
array('data' => drupal_render($form['emails'][$eid]['status']), 'class' => array('webform-email-status', 'checkbox')),
|
117
|
drupal_render($form['emails'][$eid]['email']),
|
118
|
drupal_render($form['emails'][$eid]['subject']),
|
119
|
drupal_render($form['emails'][$eid]['from']),
|
120
|
l(t('Edit'), 'node/' . $node->nid . '/webform/emails/' . $eid),
|
121
|
l(t('Clone'), 'node/' . $node->nid . '/webform/emails/' . $eid . '/clone'),
|
122
|
l(t('Delete'), 'node/' . $node->nid . '/webform/emails/' . $eid . '/delete'),
|
123
|
);
|
124
|
}
|
125
|
}
|
126
|
else {
|
127
|
$rows[] = array(array('data' => t('Currently not sending e-mails, add an e-mail recipient below.'), 'colspan' => 7));
|
128
|
}
|
129
|
|
130
|
// Add a row containing form elements for a new item.
|
131
|
$add_button = drupal_render($form['add']['add']);
|
132
|
$row_data = array(
|
133
|
array('data' => drupal_render($form['add']['status']), 'class' => array('webform-email-status', 'checkbox')),
|
134
|
array('colspan' => 3, 'data' => drupal_render($form['add'])),
|
135
|
array('colspan' => 3, 'data' => $add_button),
|
136
|
);
|
137
|
$rows[] = array('data' => $row_data, 'class' => array('webform-add-form'));
|
138
|
|
139
|
$output = '';
|
140
|
$output .= theme('table', array('header' => $header, 'rows' => $rows, 'sticky' => FALSE, 'attributes' => array('id' => 'webform-emails')));
|
141
|
$output .= drupal_render_children($form);
|
142
|
return $output;
|
143
|
}
|
144
|
|
145
|
/**
|
146
|
* Theme the add new e-mail settings form on the node/x/webform/emails page.
|
147
|
*/
|
148
|
function theme_webform_email_add_form($variables) {
|
149
|
$form = $variables['form'];
|
150
|
|
151
|
// Add a default value to the custom e-mail textfield.
|
152
|
$form['email_custom']['#attributes']['placeholder'] = t('email@example.com');
|
153
|
$form['email_custom']['#attributes']['class'][] = 'webform-set-active';
|
154
|
$form['email_custom']['#theme_wrappers'] = array();
|
155
|
$form['email_option']['custom']['#theme_wrappers'] = array('webform_inline_radio');
|
156
|
$form['email_option']['custom']['#title'] = t('Address: !email', array('!email' => drupal_render($form['email_custom'])));
|
157
|
|
158
|
// Render the component value.
|
159
|
$form['email_component']['#theme_wrappers'] = array();
|
160
|
$form['email_component']['#attributes']['class'][] = 'webform-set-active';
|
161
|
$form['email_option']['component']['#theme_wrappers'] = array('webform_inline_radio');
|
162
|
$form['email_option']['component']['#title'] = t('Component value: !component', array('!component' => drupal_render($form['email_component'])));
|
163
|
|
164
|
return drupal_render_children($form);
|
165
|
}
|
166
|
|
167
|
/**
|
168
|
* Submit handler for webform_emails_form().
|
169
|
*/
|
170
|
function webform_emails_form_submit($form, &$form_state) {
|
171
|
if ($form_state['values']['email_option'] == 'custom') {
|
172
|
$email = $form_state['values']['email_custom'];
|
173
|
}
|
174
|
else {
|
175
|
$email = $form_state['values']['email_component'];
|
176
|
}
|
177
|
$form_state['redirect'] = array('node/' . $form['#node']->nid . '/webform/emails/new', array('query' => array('status' => $form_state['values']['status'], 'option' => $form_state['values']['email_option'], 'email' => trim($email))));
|
178
|
}
|
179
|
|
180
|
/**
|
181
|
* Submit handler for status update.
|
182
|
*/
|
183
|
function webform_emails_form_status_save($form, &$form_state) {
|
184
|
foreach ($form_state['values']['emails'] as $eid => $status) {
|
185
|
db_update('webform_emails')->fields(array(
|
186
|
'status' => $status['status'],
|
187
|
))
|
188
|
->condition('eid', $eid)
|
189
|
->condition('nid', $form['#node']->nid)
|
190
|
->execute();
|
191
|
}
|
192
|
|
193
|
// Refresh the entity cache, should it be cached in persistent storage.
|
194
|
entity_get_controller('node')->resetCache(array($form['#node']->nid));
|
195
|
}
|
196
|
|
197
|
/**
|
198
|
* Form for configuring an e-mail setting and template.
|
199
|
*/
|
200
|
function webform_email_edit_form($form, $form_state, $node, $email = array(), $clone = FALSE) {
|
201
|
module_load_include('inc', 'webform', 'includes/webform.components');
|
202
|
|
203
|
$form['#attached']['library'][] = array('webform', 'admin');
|
204
|
$form['#attached']['js'][] = array('data' => array('webform' => array('revertConfirm' => t('Are you sure you want to revert any changes to your template back to the default?'))), 'type' => 'setting');
|
205
|
|
206
|
$form['#tree'] = TRUE;
|
207
|
$form['#node'] = $node;
|
208
|
$form['eid'] = array(
|
209
|
'#type' => 'value',
|
210
|
'#value' => isset($email['eid']) ? $email['eid'] : NULL,
|
211
|
);
|
212
|
$form['clone'] = array(
|
213
|
'#type' => 'value',
|
214
|
'#value' => $clone,
|
215
|
);
|
216
|
|
217
|
// All these fields work essentially the same, with a radio button set,
|
218
|
// a textfield for custom values, and a select list for a component.
|
219
|
foreach (array('email', 'subject', 'from_address', 'from_name') as $field) {
|
220
|
switch ($field) {
|
221
|
case 'email':
|
222
|
$default_value = NULL;
|
223
|
$title = t('E-mail to address');
|
224
|
$description = t('Form submissions will be e-mailed to this address. Any email, select, or hidden form element may be selected as the recipient address. Multiple e-mail addresses may be separated by commas.');
|
225
|
break;
|
226
|
|
227
|
case 'subject':
|
228
|
$default_value = webform_replace_tokens(webform_variable_get('webform_default_subject'), $node);
|
229
|
$title = t('E-mail subject');
|
230
|
$description = t('Any textfield, select, or hidden form element may be selected as the subject for e-mails.');
|
231
|
break;
|
232
|
|
233
|
case 'from_address':
|
234
|
$default_value = webform_replace_tokens(webform_variable_get('webform_default_from_address'), $node);
|
235
|
$title = t('E-mail from address');
|
236
|
$description = t("Any email, select, or hidden form element may be selected as the sender's e-mail address.");
|
237
|
break;
|
238
|
|
239
|
case 'from_name':
|
240
|
$default_value = webform_replace_tokens(webform_variable_get('webform_default_from_name'), $node);
|
241
|
$title = t('E-mail from name');
|
242
|
$description = t("Any textfield, select, or hidden form element may be selected as the sender's name for e-mails.");
|
243
|
break;
|
244
|
}
|
245
|
|
246
|
$form[$field . '_option'] = array(
|
247
|
'#title' => $title,
|
248
|
'#type' => 'radios',
|
249
|
'#default_value' => is_numeric($email[$field]) ? 'component' : ((empty($default_value) || ($email[$field] != 'default' && isset($email[$field]))) ? 'custom' : 'default'),
|
250
|
'#description' => $description,
|
251
|
);
|
252
|
if (!empty($default_value)) {
|
253
|
$form[$field . '_option']['#options']['default'] = t('Default: %value', array('%value' => $default_value));
|
254
|
}
|
255
|
$form[$field . '_option']['#options']['custom'] = t('Custom');
|
256
|
$form[$field . '_option']['#options']['component'] = t('Component');
|
257
|
|
258
|
$form[$field . '_custom'] = array(
|
259
|
'#type' => 'textfield',
|
260
|
'#size' => 40,
|
261
|
'#default_value' => (!is_numeric($email[$field]) && $email[$field] != 'default') ? $email[$field] : NULL,
|
262
|
'#maxlength' => 500,
|
263
|
);
|
264
|
$field_type = $field === 'from_address' || $field === 'email' ? 'email_address' : 'email_name';
|
265
|
$component_options = webform_component_list($node, $field_type, FALSE);
|
266
|
$form[$field . '_component'] = array(
|
267
|
'#type' => 'select',
|
268
|
'#default_value' => is_numeric($email[$field]) ? $email[$field] : NULL,
|
269
|
'#options' => empty($component_options) ? array('' => t('No available components')) : $component_options,
|
270
|
'#disabled' => empty($component_options) ? TRUE : FALSE,
|
271
|
'#weight' => 6,
|
272
|
);
|
273
|
|
274
|
// If this component is being used as an e-mail address and has multiple
|
275
|
// options (such as a select list or checkboxes), we provide text fields to
|
276
|
// map each option to a user-defined e-mail.
|
277
|
if ($field_type === 'email_address') {
|
278
|
foreach ($component_options as $cid => $component_label) {
|
279
|
$component = $node->webform['components'][$cid];
|
280
|
$options = webform_component_invoke($component['type'], 'options', $component, TRUE);
|
281
|
// For components that don't provide multiple options (hidden or email).
|
282
|
if (empty($options)) {
|
283
|
continue;
|
284
|
}
|
285
|
|
286
|
// To avoid flooding the form with hundreds of textfields, skip select
|
287
|
// lists that have huge numbers of options.
|
288
|
if (count($options) > webform_variable_get('webform_email_select_max')) {
|
289
|
unset($form[$field . '_component']['#options'][$cid]);
|
290
|
continue;
|
291
|
}
|
292
|
$form[$field . '_mapping'][$cid] = array(
|
293
|
'#title' => check_plain($component['name']),
|
294
|
'#theme' => 'webform_email_component_mapping',
|
295
|
'#attributes' => array('class' => array('webform-email-mapping')),
|
296
|
'#webform_allow_empty' => $field === 'email',
|
297
|
'#type' => 'container',
|
298
|
'#states' => array(
|
299
|
'visible' => array(
|
300
|
'input[name=' . $field . '_option' . ']' => array('value' => 'component'),
|
301
|
'select[name=' . $field . '_component' . ']' => array('value' => (string) $cid),
|
302
|
),
|
303
|
),
|
304
|
);
|
305
|
foreach ($options as $key => $label) {
|
306
|
$form[$field . '_mapping'][$cid][$key] = array(
|
307
|
'#type' => 'textfield',
|
308
|
'#title' => $label,
|
309
|
'#default_value' => is_numeric($email[$field]) && $email[$field] == $cid && is_array($email['extra']) && isset($email['extra'][$field . '_mapping'][$key]) ? $email['extra'][$field . '_mapping'][$key] : '',
|
310
|
'#attributes' => array('placeholder' => t('email@example.com')),
|
311
|
'#maxlength' => 500,
|
312
|
);
|
313
|
}
|
314
|
}
|
315
|
}
|
316
|
}
|
317
|
|
318
|
// Do not show the "E-mail from name" if using the short e-mail format.
|
319
|
if (webform_variable_get('webform_email_address_format') == 'short') {
|
320
|
$form['from_name_option']['#access'] = FALSE;
|
321
|
$form['from_name_custom']['#access'] = FALSE;
|
322
|
$form['from_name_component']['#access'] = FALSE;
|
323
|
}
|
324
|
|
325
|
// Add the checkbox to disable the email for current recipients.
|
326
|
$form['status'] = array(
|
327
|
'#title' => t('Enable sending'),
|
328
|
'#description' => t('Uncheck to disable sending this email.'),
|
329
|
'#type' => 'checkbox',
|
330
|
'#default_value' => $email['status'],
|
331
|
);
|
332
|
|
333
|
// Add the template fieldset.
|
334
|
$form['template'] = array(
|
335
|
'#type' => 'fieldset',
|
336
|
'#title' => t('E-mail template'),
|
337
|
'#collapsible' => TRUE,
|
338
|
'#collapsed' => !empty($email['cid']) && empty($email['template']),
|
339
|
'#description' => t('An e-mail template can customize the display of e-mails.'),
|
340
|
'#weight' => 15,
|
341
|
'#tree' => FALSE,
|
342
|
'#attributes' => array('id' => 'webform-template-fieldset'),
|
343
|
);
|
344
|
|
345
|
$form['template']['template_option'] = array(
|
346
|
'#type' => 'select',
|
347
|
'#options' => array(
|
348
|
'default' => t('Default template'),
|
349
|
'custom' => t('Custom template'),
|
350
|
),
|
351
|
'#default_value' => $email['template'] == 'default' ? 'default' : 'custom',
|
352
|
);
|
353
|
|
354
|
$default_template = theme(array('webform_mail_' . $node->nid, 'webform_mail', 'webform_mail_message'), array('node' => $node, 'email' => $email));
|
355
|
$template = $email['template'] == 'default' ? $default_template : $email['template'];
|
356
|
$form['template']['template'] = array(
|
357
|
'#type' => 'textarea',
|
358
|
'#rows' => max(10, min(20, count(explode("\n", $template)))),
|
359
|
'#default_value' => $template,
|
360
|
'#wysiwyg' => webform_variable_get('webform_email_html_capable') ? NULL : FALSE,
|
361
|
'#description' => theme('webform_token_help', array('groups' => array('node', 'submission'))),
|
362
|
);
|
363
|
|
364
|
$form['template']['html'] = array(
|
365
|
'#type' => 'checkbox',
|
366
|
'#title' => t('Send e-mail as HTML'),
|
367
|
'#default_value' => $email['html'],
|
368
|
'#access' => webform_variable_get('webform_email_html_capable') && !webform_variable_get('webform_format_override'),
|
369
|
);
|
370
|
|
371
|
$form['template']['attachments'] = array(
|
372
|
'#type' => 'checkbox',
|
373
|
'#title' => t('Include files as attachments'),
|
374
|
'#default_value' => $email['attachments'],
|
375
|
'#access' => webform_variable_get('webform_email_html_capable'),
|
376
|
);
|
377
|
|
378
|
$form['template']['components'] = array(
|
379
|
'#type' => 'select',
|
380
|
'#title' => t('Included e-mail values'),
|
381
|
'#options' => webform_component_list($node, 'email', TRUE),
|
382
|
'#default_value' => array_diff(array_keys($node->webform['components']), $email['excluded_components']),
|
383
|
'#multiple' => TRUE,
|
384
|
'#size' => 10,
|
385
|
'#description' => t('The selected components will be included in the [submission:values] token. Individual values may still be printed if explicitly specified as a [submission:values:?] in the template.'),
|
386
|
'#process' => array('webform_component_select'),
|
387
|
);
|
388
|
|
389
|
$form['template']['components']['suffix']['exclude_empty'] = array(
|
390
|
'#type' => 'checkbox',
|
391
|
'#title' => t('Exclude empty components'),
|
392
|
'#default_value' => $email['exclude_empty'],
|
393
|
);
|
394
|
|
395
|
// @todo: Allow easy re-use of existing templates.
|
396
|
$form['templates']['#tree'] = TRUE;
|
397
|
$form['templates']['default'] = array(
|
398
|
'#type' => 'textarea',
|
399
|
'#value' => $default_template,
|
400
|
'#resizable' => FALSE,
|
401
|
'#weight' => 19,
|
402
|
'#wysiwyg' => FALSE,
|
403
|
);
|
404
|
|
405
|
// Add the submit button.
|
406
|
$form['actions'] = array(
|
407
|
'#type' => 'actions',
|
408
|
'#weight' => 20,
|
409
|
);
|
410
|
$form['actions']['submit'] = array(
|
411
|
'#type' => 'submit',
|
412
|
'#value' => t('Save e-mail settings'),
|
413
|
);
|
414
|
|
415
|
$form['#validate'] = array('webform_email_address_validate', 'webform_email_edit_form_validate');
|
416
|
|
417
|
return $form;
|
418
|
}
|
419
|
|
420
|
/**
|
421
|
* Theme the Webform mail settings section of the node form.
|
422
|
*/
|
423
|
function theme_webform_email_edit_form($variables) {
|
424
|
$form = $variables['form'];
|
425
|
|
426
|
// Loop through fields, rendering them into radio button options.
|
427
|
foreach (array('email', 'subject', 'from_address', 'from_name') as $field) {
|
428
|
foreach (array('custom', 'component') as $option) {
|
429
|
$form[$field . '_' . $option]['#attributes']['class'][] = 'webform-set-active';
|
430
|
$form[$field . '_' . $option]['#theme_wrappers'] = array();
|
431
|
$form[$field . '_option'][$option]['#theme_wrappers'] = array('webform_inline_radio');
|
432
|
$form[$field . '_option'][$option]['#title'] = t('!title: !field', array('!title' => $form[$field . '_option'][$option]['#title'], '!field' => drupal_render($form[$field . '_' . $option])));
|
433
|
}
|
434
|
if (isset($form[$field . '_option']['#options']['default'])) {
|
435
|
$form[$field]['#theme_wrappers'] = array();
|
436
|
$form[$field . '_option']['default']['#theme_wrappers'] = array('webform_inline_radio');
|
437
|
}
|
438
|
}
|
439
|
|
440
|
$details = '';
|
441
|
$details .= drupal_render($form['subject_option']);
|
442
|
$details .= drupal_render($form['from_address_option']);
|
443
|
$details .= drupal_render($form['from_address_mapping']);
|
444
|
$details .= drupal_render($form['from_name_option']);
|
445
|
|
446
|
$form['details'] = array(
|
447
|
'#type' => 'fieldset',
|
448
|
'#title' => t('E-mail header details'),
|
449
|
'#weight' => 10,
|
450
|
'#children' => $details,
|
451
|
'#collapsible' => FALSE,
|
452
|
'#parents' => array('details'),
|
453
|
'#groups' => array('details' => array()),
|
454
|
'#attributes' => array(),
|
455
|
);
|
456
|
|
457
|
// Ensure templates are completely hidden.
|
458
|
$form['templates']['#prefix'] = '<div id="webform-email-templates" style="display: none">';
|
459
|
$form['templates']['#suffix'] = '</div>';
|
460
|
|
461
|
// Re-sort the elements since we added the details fieldset.
|
462
|
$form['#sorted'] = FALSE;
|
463
|
$children = element_children($form, TRUE);
|
464
|
return drupal_render_children($form, $children);
|
465
|
}
|
466
|
|
467
|
/**
|
468
|
* Theme the presentation of select list option to e-mail address mappings.
|
469
|
*/
|
470
|
function theme_webform_email_component_mapping($variables) {
|
471
|
$element = $variables['element'];
|
472
|
|
473
|
$header = array(t('Option'), t('E-mail address'));
|
474
|
$rows = array();
|
475
|
foreach (element_children($element) as $key) {
|
476
|
$element[$key]['#theme_wrappers'] = array();
|
477
|
$row = array();
|
478
|
$row[] = array(
|
479
|
'data' => theme('form_element_label', array('element' => $element[$key])),
|
480
|
'class' => array('webform-email-option'),
|
481
|
);
|
482
|
$row[] = drupal_render($element[$key]);
|
483
|
$rows[] = $row;
|
484
|
}
|
485
|
|
486
|
$empty = t('This component has no options defined yet.');
|
487
|
$table = theme('table', array('header' => $header, 'rows' => $rows, 'sticky' => FALSE, 'empty' => $empty));
|
488
|
$description = t('The selected component %name has multiple options. You may enter an e-mail address for each choice.', array('%name' => $element['#title']));
|
489
|
if ($element['#webform_allow_empty']) {
|
490
|
$description .= ' ' . t('When that choice is selected, an e-mail will be sent to the corresponding address. If a field is left blank, no e-mail will be sent for that option.');
|
491
|
}
|
492
|
else {
|
493
|
$description .= ' ' . t('When that choice is selected, an e-mail will be sent from the corresponding address.');
|
494
|
}
|
495
|
|
496
|
$wrapper_element = array(
|
497
|
'#title' => t('Component e-mail options'),
|
498
|
'#children' => $table,
|
499
|
'#description' => $description,
|
500
|
'#theme_wrappers' => array('form_element'),
|
501
|
'#id' => NULL,
|
502
|
);
|
503
|
|
504
|
return render($wrapper_element);
|
505
|
}
|
506
|
|
507
|
/**
|
508
|
* Validate handler for webform_email_edit_form() and webform_emails_form().
|
509
|
*/
|
510
|
function webform_email_address_validate($form, &$form_state) {
|
511
|
if ($form_state['values']['email_option'] == 'custom') {
|
512
|
webform_email_validate($form_state['values']['email_custom'], 'email_custom', FALSE, TRUE, TRUE);
|
513
|
}
|
514
|
}
|
515
|
|
516
|
/**
|
517
|
* Validate handler for webform_email_edit_form().
|
518
|
*/
|
519
|
function webform_email_edit_form_validate($form, &$form_state) {
|
520
|
if ($form_state['values']['from_address_option'] == 'custom') {
|
521
|
webform_email_validate($form_state['values']['from_address_custom'], 'from_address_custom', FALSE, FALSE, TRUE);
|
522
|
}
|
523
|
|
524
|
// Validate component-based values for the TO and FROM address.
|
525
|
foreach (array('email', 'from_address') as $field_name) {
|
526
|
if ($form_state['values'][$field_name . '_option'] == 'component') {
|
527
|
$cid = $form_state['values'][$field_name . '_component'];
|
528
|
if (isset($form_state['values'][$field_name . '_mapping'][$cid])) {
|
529
|
$empty_allowed = $field_name === 'email';
|
530
|
$multiple_allowed = $field_name === 'email';
|
531
|
foreach ($form_state['values'][$field_name . '_mapping'][$cid] as $key => &$value) {
|
532
|
webform_email_validate($value, "{$field_name}_mapping][$cid][$key", $empty_allowed, $multiple_allowed, TRUE);
|
533
|
}
|
534
|
}
|
535
|
}
|
536
|
}
|
537
|
|
538
|
}
|
539
|
|
540
|
/**
|
541
|
* Submit handler for webform_email_edit_form().
|
542
|
*/
|
543
|
function webform_email_edit_form_submit($form, &$form_state) {
|
544
|
// Ensure a webform record exists.
|
545
|
$node = $form['#node'];
|
546
|
webform_ensure_record($node);
|
547
|
|
548
|
// Remove duplicate email To: addresses.
|
549
|
$form_state['values']['email_custom'] = implode(',', array_unique(array_map('trim', explode(',', $form_state['values']['email_custom']))));
|
550
|
|
551
|
// Merge the e-mail, name, address, and subject options into single values.
|
552
|
$email = array(
|
553
|
'eid' => $form_state['values']['eid'],
|
554
|
'nid' => $node->nid,
|
555
|
);
|
556
|
|
557
|
foreach (array('email', 'from_name', 'from_address', 'subject') as $field) {
|
558
|
$option = $form_state['values'][$field . '_option'];
|
559
|
if ($option == 'default') {
|
560
|
$email[$field] = 'default';
|
561
|
}
|
562
|
else {
|
563
|
$email[$field] = $form_state['values'][$field . '_' . $option];
|
564
|
|
565
|
// Merge the email mapping(s) into single value(s)
|
566
|
$cid = $form_state['values'][$field . '_' . $option];
|
567
|
if (is_numeric($cid) && isset($form_state['values'][$field . '_mapping'][$cid])) {
|
568
|
$email['extra'][$field . '_mapping'] = $form_state['values'][$field . '_mapping'][$cid];
|
569
|
}
|
570
|
}
|
571
|
}
|
572
|
|
573
|
// Ensure templates are unaffected by differences in line breaks.
|
574
|
$form_state['values']['template'] = str_replace(array("\r", "\n"), array('', "\n"), $form_state['values']['template']);
|
575
|
$form_state['values']['templates']['default'] = str_replace(array("\r", "\n"), array('', "\n"), $form_state['values']['templates']['default']);
|
576
|
|
577
|
// Set the template value.
|
578
|
// @todo: Support reuse of templates.
|
579
|
if (strcmp(trim($form_state['values']['templates']['default']), trim($form_state['values']['template'])) == 0) {
|
580
|
$email['template'] = 'default';
|
581
|
}
|
582
|
else {
|
583
|
$email['template'] = $form_state['values']['template'];
|
584
|
}
|
585
|
|
586
|
// Save the attachment and HTML options provided by MIME mail.
|
587
|
$email['html'] = empty($form_state['values']['html']) ? 0 : 1;
|
588
|
$email['attachments'] = empty($form_state['values']['attachments']) ? 0 : 1;
|
589
|
|
590
|
// Save the list of included components.
|
591
|
// We actually maintain an *exclusion* list, so any new components will
|
592
|
// default to being included in the [submission:values] token until unchecked.
|
593
|
$included = array_keys(array_filter((array) $form_state['values']['components']));
|
594
|
$excluded = array_diff(array_keys($node->webform['components']), $included);
|
595
|
$email['excluded_components'] = $excluded;
|
596
|
|
597
|
$email['exclude_empty'] = empty($form_state['values']['exclude_empty']) ? 0 : 1;
|
598
|
|
599
|
$email['status'] = empty($form_state['values']['status']) ? 0 : 1;
|
600
|
|
601
|
if ($form_state['values']['clone']) {
|
602
|
drupal_set_message(t('Email settings cloned.'));
|
603
|
$form_state['values']['eid'] = webform_email_clone($email);
|
604
|
}
|
605
|
elseif (empty($form_state['values']['eid'])) {
|
606
|
drupal_set_message(t('Email settings added.'));
|
607
|
$form_state['values']['eid'] = webform_email_insert($email);
|
608
|
}
|
609
|
else {
|
610
|
drupal_set_message(t('Email settings updated.'));
|
611
|
webform_email_update($email);
|
612
|
}
|
613
|
|
614
|
// Refresh the entity cache, should it be cached in persistent storage.
|
615
|
entity_get_controller('node')->resetCache(array($node->nid));
|
616
|
|
617
|
$form_state['redirect'] = array('node/' . $node->nid . '/webform/emails');
|
618
|
}
|
619
|
|
620
|
/**
|
621
|
* Form for deleting an e-mail setting.
|
622
|
*/
|
623
|
function webform_email_delete_form($form, $form_state, $node, $email) {
|
624
|
$form['node'] = array(
|
625
|
'#type' => 'value',
|
626
|
'#value' => $node,
|
627
|
);
|
628
|
$form['email'] = array(
|
629
|
'#type' => 'value',
|
630
|
'#value' => $email,
|
631
|
);
|
632
|
|
633
|
$question = t('Delete e-mail settings?');
|
634
|
if (is_numeric($email['email'])) {
|
635
|
$description = t('This will immediately delete the e-mail settings based on the @component component.', array('@component' => $email['email']));
|
636
|
}
|
637
|
else {
|
638
|
$description = t('This will immediately delete the e-mail settings sending to the @address address.', array('@address' => $email['email']));
|
639
|
}
|
640
|
|
641
|
return confirm_form($form, $question, 'node/' . $node->nid . '/webform/emails', $description, t('Delete'));
|
642
|
}
|
643
|
|
644
|
/**
|
645
|
* Submit handler for webform_email_delete_form().
|
646
|
*/
|
647
|
function webform_email_delete_form_submit($form, &$form_state) {
|
648
|
// Delete the e-mail settings.
|
649
|
$node = $form_state['values']['node'];
|
650
|
$email = $form_state['values']['email'];
|
651
|
webform_email_delete($node, $email);
|
652
|
drupal_set_message(t('E-mail settings deleted.'));
|
653
|
|
654
|
// Check if this webform still contains any information.
|
655
|
unset($node->webform['emails'][$email['eid']]);
|
656
|
webform_check_record($node);
|
657
|
|
658
|
// Refresh the entity cache, should it be cached in persistent storage.
|
659
|
entity_get_controller('node')->resetCache(array($node->nid));
|
660
|
|
661
|
$form_state['redirect'] = 'node/' . $node->nid . '/webform/emails';
|
662
|
}
|
663
|
|
664
|
/**
|
665
|
* Load an e-mail setting from the database or initialize a new e-mail.
|
666
|
*/
|
667
|
function webform_email_load($eid, $nid) {
|
668
|
$node = node_load($nid);
|
669
|
if ($eid == 'new') {
|
670
|
$email = array(
|
671
|
'email' => '',
|
672
|
'subject' => 'default',
|
673
|
'from_name' => 'default',
|
674
|
'from_address' => 'default',
|
675
|
'template' => 'default',
|
676
|
'excluded_components' => array(),
|
677
|
'exclude_empty' => 0,
|
678
|
'html' => webform_variable_get('webform_default_format'),
|
679
|
'attachments' => 0,
|
680
|
'extra' => '',
|
681
|
'status' => 1,
|
682
|
);
|
683
|
}
|
684
|
else {
|
685
|
$email = isset($node->webform['emails'][$eid]) ? $node->webform['emails'][$eid] : FALSE;
|
686
|
if ($email && webform_variable_get('webform_format_override')) {
|
687
|
$email['html'] = webform_variable_get('webform_default_format');
|
688
|
}
|
689
|
}
|
690
|
|
691
|
return $email;
|
692
|
}
|
693
|
|
694
|
/**
|
695
|
* Insert a new e-mail setting into the database.
|
696
|
*
|
697
|
* @param $email
|
698
|
* An array of settings for sending an e-mail.
|
699
|
*
|
700
|
* @return int|false
|
701
|
* The e-mail identifier for this row's settings on success else false.
|
702
|
*/
|
703
|
function webform_email_insert($email) {
|
704
|
// @todo: This is not race-condition safe. Switch to using transactions?
|
705
|
if (!isset($email['eid'])) {
|
706
|
$next_id_query = db_select('webform_emails')->condition('nid', $email['nid']);
|
707
|
$next_id_query->addExpression('MAX(eid) + 1', 'eid');
|
708
|
$email['eid'] = $next_id_query->execute()->fetchField();
|
709
|
if ($email['eid'] == NULL) {
|
710
|
$email['eid'] = 1;
|
711
|
}
|
712
|
}
|
713
|
|
714
|
$email['excluded_components'] = implode(',', $email['excluded_components']);
|
715
|
$email['extra'] = empty($email['extra']) ? '' : serialize($email['extra']);
|
716
|
$success = drupal_write_record('webform_emails', $email);
|
717
|
|
718
|
return $success ? $email['eid'] : FALSE;
|
719
|
}
|
720
|
|
721
|
/**
|
722
|
* Clone an existing e-mail setting.
|
723
|
*
|
724
|
* @param $email
|
725
|
* An array of settings for sending an e-mail.
|
726
|
*
|
727
|
* @return false|int
|
728
|
* The e-mail identifier for this row's settings on success else false.
|
729
|
*/
|
730
|
function webform_email_clone($email) {
|
731
|
$email['eid'] = NULL;
|
732
|
return webform_email_insert($email);
|
733
|
}
|
734
|
|
735
|
/**
|
736
|
* Update an existing e-mail setting with new values.
|
737
|
*
|
738
|
* @param $email
|
739
|
* An array of settings for sending an e-mail containing a nid, eid, and all
|
740
|
* other fields from the e-mail form.
|
741
|
*
|
742
|
* @return false|int
|
743
|
* On success SAVED_NEW or SAVED_UPDATED, depending on the operation
|
744
|
* performed, FALSE on failure.
|
745
|
*/
|
746
|
function webform_email_update($email) {
|
747
|
$email['excluded_components'] = implode(',', $email['excluded_components']);
|
748
|
$email['extra'] = empty($email['extra']) ? '' : serialize($email['extra']);
|
749
|
return drupal_write_record('webform_emails', $email, array('nid', 'eid'));
|
750
|
}
|
751
|
|
752
|
/**
|
753
|
* Delete an e-mail setting.
|
754
|
*/
|
755
|
function webform_email_delete($node, $email) {
|
756
|
db_delete('webform_emails')
|
757
|
->condition('nid', $node->nid)
|
758
|
->condition('eid', $email['eid'])
|
759
|
->execute();
|
760
|
}
|