Projet

Général

Profil

Paste
Télécharger (20,5 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / webform / includes / webform.emails.inc @ ca0757b9

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
  $form['nid'] = array(
23
    '#type' => 'value',
24
    '#value' => $node->nid,
25
  );
26

    
27
  foreach ($node->webform['emails'] as $eid => $email) {
28
    $email_addresses = array_filter(explode(',', check_plain($email['email'])));
29
    foreach ($email_addresses as $key => $email_address) {
30
      $email_addresses[$key] = webform_format_email_address($email_address, NULL, $node, NULL, FALSE);
31
    }
32

    
33
    $form['emails'][$eid]['email'] = array(
34
      '#markup' => implode('<br />', $email_addresses),
35
    );
36
    $form['emails'][$eid]['subject'] = array(
37
      '#markup' => check_plain(webform_format_email_subject($email['subject'], $node)),
38
    );
39
    $form['emails'][$eid]['from'] = array(
40
      '#markup' => check_plain(webform_format_email_address($email['from_address'], $email['from_name'], $node, NULL, FALSE)),
41
    );
42
  }
43

    
44
  $form['add'] = array(
45
    '#theme' => 'webform_email_add_form',
46
    '#tree' => FALSE,
47
  );
48

    
49
  $form['add']['email_option'] = array(
50
    '#type' => 'radios',
51
    '#options' => array(
52
      'custom' => t('Address'),
53
      'component' => t('Component value'),
54
    ),
55
    '#default_value' => 'custom',
56
  );
57

    
58
  $form['add']['email_custom'] = array(
59
    '#type' => 'textfield',
60
    '#size' => 24,
61
    '#maxlength' => 500,
62
  );
63

    
64
  $form['add']['email_component'] = array(
65
    '#type' => 'select',
66
    '#options' => webform_component_list($node, 'email_address', FALSE),
67
  );
68

    
69
  if (empty($form['add']['email_component']['#options'])) {
70
    $form['add']['email_component']['#options'][''] = t('No available components');
71
    $form['add']['email_component']['#disabled'] = TRUE;
72
  }
73

    
74
  $form['add_button'] = array(
75
    '#type' => 'submit',
76
    '#value' => t('Add'),
77
    '#weight' => 45,
78
  );
79

    
80
  $form['#validate'] = array('webform_email_address_validate');
81

    
82
  return $form;
83
}
84

    
85
/**
86
 * Theme the node components form. Use a table to organize the components.
87
 *
88
 * @param $form
89
 *   The form array.
90
 * @return
91
 *   Formatted HTML form, ready for display.
92
 */
93
function theme_webform_emails_form($variables) {
94
  $form = $variables['form'];
95
  $node = $form['#node'];
96

    
97
  $header = array(t('E-mail to'), t('Subject'), t('From'), array('data' => t('Operations'), 'colspan' => 2));
98
  $rows = array();
99

    
100
  if (!empty($form['emails'])) {
101
    foreach (element_children($form['emails']) as $eid) {
102
      // Add each component to a table row.
103
      $rows[] = array(
104
        drupal_render($form['emails'][$eid]['email']),
105
        drupal_render($form['emails'][$eid]['subject']),
106
        drupal_render($form['emails'][$eid]['from']),
107
        l(t('Edit'), 'node/' . $node->nid . '/webform/emails/' . $eid),
108
        l(t('Delete'), 'node/' . $node->nid . '/webform/emails/' . $eid . '/delete'),
109
      );
110
    }
111
  }
112
  else {
113
    $rows[] = array(array('data' => t('Currently not sending e-mails, add an e-mail recipient below.'), 'colspan' => 5));
114
  }
115

    
116
  // Add a row containing form elements for a new item.
117
  $row_data = array(
118
    array('colspan' => 3, 'data' => drupal_render($form['add'])),
119
    array('colspan' => 2, 'data' => drupal_render($form['add_button'])),
120
  );
121
  $rows[] = array('data' => $row_data, 'class' => array('webform-add-form'));
122

    
123
  $output = '';
124
  $output .= theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'webform-emails')));
125
  $output .= drupal_render_children($form);
126
  return $output;
127
}
128

    
129
/**
130
 * Theme the add new e-mail settings form on the node/x/webform/emails page.
131
 */
132
function theme_webform_email_add_form($variables) {
133
  $form = $variables['form'];
134

    
135
  // Add a default value to the custom e-mail textfield.
136
  $form['email_custom']['#attributes']['rel'] = t('email@example.com');
137
  $form['email_custom']['#attributes']['class'][] = 'webform-set-active';
138
  $form['email_custom']['#attributes']['class'][] = 'webform-default-value';
139
  $form['email_option']['custom']['#theme_wrappers'] = array('webform_inline_radio');
140
  $form['email_option']['custom']['#inline_element'] = drupal_render($form['email_custom']);
141

    
142
  // Render the component value.
143
  $form['email_component']['#attributes']['class'][] = 'webform-set-active';
144
  $form['email_option']['component']['#theme_wrappers'] = array('webform_inline_radio');
145
  $form['email_option']['component']['#inline_element'] = drupal_render($form['email_component']);
146

    
147
  return drupal_render_children($form);
148
}
149

    
150
/**
151
 * Submit handler for webform_emails_form().
152
 */
153
function webform_emails_form_submit($form, &$form_state) {
154
  if ($form_state['values']['email_option'] == 'custom') {
155
    $email = $form_state['values']['email_custom'];
156
  }
157
  else {
158
    $email = $form_state['values']['email_component'];
159
  }
160
  $form_state['redirect'] = array('node/' . $form['#node']->nid . '/webform/emails/new', array('query' => array('option' => $form_state['values']['email_option'], 'email' => trim($email))));
161
}
162

    
163
/**
164
 * Form for configuring an e-mail setting and template.
165
 */
166
function webform_email_edit_form($form, $form_state, $node, $email = array()) {
167
  module_load_include('inc', 'webform', 'includes/webform.components');
168

    
169
  $form['#attached']['library'][] = array('webform', 'admin');
170
  $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');
171

    
172
  $form['#tree'] = TRUE;
173
  $form['node'] = array(
174
    '#type' => 'value',
175
    '#value' => $node,
176
  );
177
  $form['eid'] = array(
178
    '#type' => 'value',
179
    '#value' => isset($email['eid']) ? $email['eid'] : NULL,
180
  );
181

    
182
  // All these fields work essentially the same, with a radio button set,
183
  // a textfield for custom values, and a select list for a component.
184
  foreach (array('email', 'subject', 'from_address', 'from_name') as $field) {
185
    switch ($field) {
186
      case 'email':
187
        $default_value = NULL;
188
        $title = t('E-mail to address');
189
        $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.');
190
        break;
191
      case 'subject':
192
        $default_value = _webform_filter_values(webform_variable_get('webform_default_subject'), $node);
193
        $title = t('E-mail subject');
194
        $description = t('Any textfield, select, or hidden form element may be selected as the subject for e-mails.');
195
        break;
196
      case 'from_address':
197
        $default_value = _webform_filter_values(webform_variable_get('webform_default_from_address'), $node);
198
        $title = t('E-mail from address');
199
        $description = t('Any email, select, or hidden form element may be selected as the sender\'s e-mail address.');
200
        break;
201
      case 'from_name':
202
        $default_value = _webform_filter_values(webform_variable_get('webform_default_from_name'), $node);
203
        $title = t('E-mail from name');
204
        $description = t('Any textfield, select, or hidden form element may be selected as the sender\'s name for e-mails.');
205
        break;
206
    }
207

    
208
    $form[$field . '_option'] = array(
209
      '#title' => $title,
210
      '#type' => 'radios',
211
      '#default_value' => is_numeric($email[$field]) ? 'component' : ((empty($default_value) || ($email[$field] != 'default' && isset($email[$field]))) ? 'custom' : 'default'),
212
      '#description' => $description,
213
    );
214
    if (!empty($default_value)) {
215
      $form[$field . '_option']['#options']['default'] = t('Default: %value', array('%value' => $default_value));
216
    }
217
    $form[$field . '_option']['#options']['custom'] = t('Custom');
218
    $form[$field . '_option']['#options']['component'] = t('Component');
219

    
220
    $form[$field . '_custom'] = array(
221
      '#type' => 'textfield',
222
      '#size' => 40,
223
      '#default_value' => (!is_numeric($email[$field]) && $email[$field] != 'default') ? $email[$field] : NULL,
224
      '#maxlength' => $field == 'email' ? 500 : 255,
225
    );
226
    $options = webform_component_list($node, $field == 'from_address' || $field == 'email' ? 'email_address' : 'email_name', FALSE);
227
    $form[$field . '_component'] = array(
228
      '#type' => 'select',
229
      '#default_value' =>  is_numeric($email[$field]) ? $email[$field] : NULL,
230
      '#options' => empty($options) ? array('' => t('No available components')) : $options,
231
      '#disabled' => empty($options) ? TRUE : FALSE,
232
      '#weight' => 6,
233
    );
234
  }
235

    
236
  // Do not show the "E-mail from name" if using the short e-mail format.
237
  if (variable_get('webform_email_address_format', 'long') == 'short') {
238
    $form['from_name_option']['#access'] = FALSE;
239
    $form['from_name_custom']['#access'] = FALSE;
240
    $form['from_name_component']['#access'] = FALSE;
241
  }
242

    
243
  // Add the template fieldset.
244
  $form['template'] = array(
245
    '#type' => 'fieldset',
246
    '#title' => t('E-mail template'),
247
    '#collapsible' => TRUE,
248
    '#collapsed' => !empty($email['cid']) && empty($email['template']),
249
    '#description' => t('An e-mail template can customize the display of e-mails.'),
250
    '#weight' => 15,
251
    '#tree' => FALSE,
252
    '#attributes' => array('id' => 'webform-template-fieldset'),
253
  );
254

    
255
  $form['template']['template_option'] = array(
256
    '#type' => 'select',
257
    '#options' => array(
258
      'default' => t('Default template'),
259
      'custom' => t('Custom template'),
260
    ),
261
    '#default_value' => $email['template'] == 'default' ? 'default' : 'custom',
262
  );
263

    
264
  $default_template = theme(array('webform_mail_' . $node->nid, 'webform_mail', 'webform_mail_message'), array('node' => $node, 'email' => $email));
265
  $template = $email['template'] == 'default' ? $default_template : $email['template'];
266
  $form['template']['template'] = array(
267
    '#type' => 'textarea',
268
    '#rows' => max(10, min(20, count(explode("\n", $template)))),
269
    '#default_value' => $template,
270
    '#wysiwyg' => webform_email_html_capable() ? NULL : FALSE,
271
  );
272

    
273
  $form['template']['html'] = array(
274
    '#type' => 'checkbox',
275
    '#title' => t('Send e-mail as HTML'),
276
    '#default_value' => $email['html'],
277
    '#access' => webform_email_html_capable() && !variable_get('webform_format_override', 0),
278
  );
279

    
280
  $form['template']['attachments'] = array(
281
    '#type' => 'checkbox',
282
    '#title' => t('Include files as attachments'),
283
    '#default_value' => $email['attachments'],
284
    '#access' => webform_email_html_capable(),
285
  );
286

    
287
  $form['template']['tokens'] = array(
288
    '#markup' => theme('webform_token_help', array('groups' => 'all')),
289
  );
290

    
291
  $form['template']['components'] = array(
292
    '#type' => 'select',
293
    '#title' => t('Included e-mail values'),
294
    '#options' => webform_component_list($node, 'email', TRUE),
295
    '#default_value' => array_diff(array_keys($node->webform['components']), $email['excluded_components']),
296
    '#multiple' => TRUE,
297
    '#size' => 10,
298
    '#description' => t('The selected components will be included in the %email_values token. Individual values may still be printed if explicitly specified as a %email[key] in the template.'),
299
    '#process' => array('webform_component_select'),
300
  );
301

    
302
  // TODO: Allow easy re-use of existing templates.
303
  $form['templates']['#tree'] = TRUE;
304
  $form['templates']['default'] = array(
305
    '#type' => 'textarea',
306
    '#value' => $default_template,
307
    '#resizable' => FALSE,
308
    '#weight' => 19,
309
    '#wysiwyg' => FALSE,
310
  );
311

    
312
  // Add the submit button.
313
  $form['actions'] = array('#type' => 'actions');
314
  $form['actions']['submit'] = array(
315
    '#type' => 'submit',
316
    '#value' => t('Save e-mail settings'),
317
    '#weight' => 20,
318
  );
319

    
320
  $form['#validate'] = array('webform_email_address_validate', 'webform_email_edit_form_validate');
321

    
322
  return $form;
323
}
324

    
325
/**
326
 * Theme the Webform mail settings section of the node form.
327
 */
328
function theme_webform_email_edit_form($variables) {
329
  $form = $variables['form'];
330

    
331
  // Loop through fields, rendering them into radio button options.
332
  foreach (array('email', 'subject', 'from_address', 'from_name') as $field) {
333
    foreach (array('custom', 'component') as $option) {
334
      $form[$field . '_' . $option]['#attributes']['class'][] = 'webform-set-active';
335
      $form[$field . '_option'][$option]['#theme_wrappers'] = array('webform_inline_radio');
336
      $form[$field . '_option'][$option]['#inline_element'] = drupal_render($form[$field . '_' . $option]);
337
    }
338
    if (isset($form[$field . '_option']['#options']['default'])) {
339
      $form[$field . '_option']['default']['#theme_wrappers'] = array('webform_inline_radio');
340
    }
341
  }
342

    
343
  $details = '';
344
  $details .= drupal_render($form['subject_option']);
345
  $details .= drupal_render($form['from_address_option']);
346
  $details .= drupal_render($form['from_name_option']);
347
  $form['details'] = array(
348
    '#type' => 'fieldset',
349
    '#title' => t('E-mail header details'),
350
    '#weight' => 10,
351
    '#children' => $details,
352
    '#collapsible' => FALSE,
353
    '#parents' => array('details'),
354
    '#groups' => array('details' => array()),
355
    '#attributes' => array(),
356
  );
357

    
358
  // Ensure templates are completely hidden.
359
  $form['templates']['#prefix'] = '<div id="webform-email-templates" style="display: none">';
360
  $form['templates']['#suffix'] = '</div>';
361

    
362
  // Re-sort the elements since we added the details fieldset.
363
  $form['#sorted'] = FALSE;
364
  $children = element_children($form, TRUE);
365
  return drupal_render_children($form, $children);
366
}
367

    
368
/**
369
 * Validate handler for webform_email_edit_form() and webform_emails_form().
370
 */
371
function webform_email_address_validate($form, &$form_state) {
372
  if ($form_state['values']['email_option'] == 'custom') {
373
    $email = trim($form_state['values']['email_custom']);
374
    if (empty($email)) {
375
      form_set_error('email_custom', t('When adding a new custom e-mail, the e-mail field is required.'));
376
    }
377
    else {
378
      $emails = array_filter(explode(',', $email));
379
      foreach ($emails as $email) {
380
        if (!valid_email_address(trim($email))) {
381
          form_set_error('email_custom', t('The entered e-mail address "@email" does not appear valid.', array('@email' => $email)));
382
        }
383
      }
384
    }
385
  }
386
}
387

    
388
/**
389
 * Validate handler for webform_email_edit_form().
390
 */
391
function webform_email_edit_form_validate($form, &$form_state) {
392
  if ($form_state['values']['from_address_option'] == 'custom' && !valid_email_address($form_state['values']['from_address_custom'])) {
393
    form_set_error('from_address_custom', t('The entered e-mail address "@email" does not appear valid.', array('@email' => $form_state['values']['from_address_custom'])));
394
  }
395
}
396

    
397
/**
398
 * Submit handler for webform_email_edit_form().
399
 */
400
function webform_email_edit_form_submit($form, &$form_state) {
401
  // Ensure a webform record exists.
402
  $node = $form_state['values']['node'];
403
  webform_ensure_record($node);
404

    
405
  // Merge the e-mail, name, address, and subject options into single values.
406
  $email = array(
407
    'eid' => $form_state['values']['eid'],
408
    'nid' => $node->nid,
409
  );
410

    
411
  foreach (array('email', 'from_name', 'from_address', 'subject') as $field) {
412
    $option = $form_state['values'][$field . '_option'];
413
    if ($option == 'default') {
414
      $email[$field] = 'default';
415
    }
416
    else {
417
      $email[$field] = $form_state['values'][$field . '_' . $option];
418
    }
419
  }
420

    
421
  // Ensure templates are unaffected by differences in line breaks.
422
  $form_state['values']['template'] = str_replace(array("\r", "\n"), array('', "\n"), $form_state['values']['template']);
423
  $form_state['values']['templates']['default'] = str_replace(array("\r", "\n"), array('', "\n"), $form_state['values']['templates']['default']);
424

    
425
  // Set the template value.
426
  // TODO: Support reuse of templates.
427
  if (strcmp(trim($form_state['values']['templates']['default']), trim($form_state['values']['template'])) == 0) {
428
    $email['template'] = 'default';
429
  }
430
  else {
431
    $email['template'] = $form_state['values']['template'];
432
  }
433

    
434
  // Save the attachment and HTML options provided by MIME mail.
435
  $email['html'] = empty($form_state['values']['html']) ? 0 : 1;
436
  $email['attachments'] = empty($form_state['values']['attachments']) ? 0 : 1;
437

    
438
  // Save the list of included components.
439
  // We actually maintain an *exclusion* list, so any new components will
440
  // default to being included in the %email_values token until unchecked.
441
  $included = array_keys(array_filter((array) $form_state['values']['components']));
442
  $excluded = array_diff(array_keys($node->webform['components']), $included);
443
  $email['excluded_components'] = $excluded;
444

    
445
  if (empty($form_state['values']['eid'])) {
446
    drupal_set_message(t('Email settings added.'));
447
    $form_state['values']['eid'] = webform_email_insert($email);
448
  }
449
  else {
450
    drupal_set_message(t('Email settings updated.'));
451
    webform_email_update($email);
452
  }
453

    
454
  // Clear the entity cache if Entity Cache module is installed.
455
  if (module_exists('entitycache')) {
456
    cache_clear_all($node->nid, 'cache_entity_node');
457
  }
458

    
459
  $form_state['redirect'] = array('node/' . $node->nid . '/webform/emails');
460
}
461

    
462
/**
463
 * Form for deleting an e-mail setting.
464
 */
465
function webform_email_delete_form($form, $form_state, $node, $email) {
466
  $eid = $email['eid'];
467

    
468
  $form['node'] = array(
469
    '#type' => 'value',
470
    '#value' => $node,
471
  );
472
  $form['email'] = array(
473
    '#type' => 'value',
474
    '#value' => $email,
475
  );
476

    
477
  $question = t('Delete e-mail settings?');
478
  if (is_numeric($email['email'])) {
479
    $description = t('This will immediately delete the e-mail settings based on the @component component.', array('@component' => $email['email']));
480
  }
481
  else {
482
    $description = t('This will immediately delete the e-mail settings sending to the @address address.', array('@address' => $email['email']));
483
  }
484

    
485
  return confirm_form($form, $question, 'node/' . $node->nid . '/webform/emails', $description, t('Delete'));
486
}
487

    
488
/**
489
 * Submit handler for webform_email_delete_form().
490
 */
491
function webform_email_delete_form_submit($form, &$form_state) {
492
  // Delete the e-mail settings.
493
  $node = $form_state['values']['node'];
494
  $email = $form_state['values']['email'];
495
  webform_email_delete($node, $email);
496
  drupal_set_message(t('E-mail settings deleted.'));
497

    
498
  // Check if this webform still contains any information.
499
  unset($node->webform['emails'][$email['eid']]);
500
  webform_check_record($node);
501

    
502
  // Clear the entity cache if Entity Cache module is installed.
503
  if (module_exists('entitycache')) {
504
    cache_clear_all($node->nid, 'cache_entity_node');
505
  }
506

    
507
  $form_state['redirect'] = 'node/' . $node->nid . '/webform/emails';
508
}
509

    
510
/**
511
 * Load an e-mail setting from the database or initialize a new e-mail.
512
 */
513
function webform_email_load($eid, $nid) {
514
  $node = node_load($nid);
515
  if ($eid == 'new') {
516
    $email = array(
517
      'email' => '',
518
      'subject' => 'default',
519
      'from_name' => 'default',
520
      'from_address' => 'default',
521
      'template' => 'default',
522
      'excluded_components' => array(),
523
      'html' => variable_get('webform_default_format', 0),
524
      'attachments' => 0,
525
    );
526
  }
527
  else {
528
    $email = isset($node->webform['emails'][$eid]) ? $node->webform['emails'][$eid] : FALSE;
529
    if (variable_get('webform_format_override', 0)) {
530
      $email['html'] = variable_get('webform_default_format', 0);
531
    }
532
  }
533

    
534
  return $email;
535
}
536

    
537
/**
538
 * Insert a new e-mail setting into the database.
539
 *
540
 * @param $email
541
 *   An array of settings for sending an e-mail.
542
 */
543
function webform_email_insert($email) {
544
  // TODO: This is not race-condition safe. Switch to using transactions?
545
  if (!isset($email['eid'])) {
546
    $next_id_query = db_select('webform_emails')->condition('nid', $email['nid']);
547
    $next_id_query->addExpression('MAX(eid) + 1', 'eid');
548
    $email['eid'] = $next_id_query->execute()->fetchField();
549
    if ($email['eid'] == NULL) {
550
      $email['eid'] = 1;
551
    }
552
  }
553

    
554
  $email['excluded_components'] = implode(',', $email['excluded_components']);
555
  $success = drupal_write_record('webform_emails', $email);
556

    
557
  return $success ? $email['eid'] : FALSE;
558
}
559

    
560
/**
561
 * Update an existing e-mail setting with new values.
562
 *
563
 * @param $email
564
 *   An array of settings for sending an e-mail containing a nid, eid, and all
565
 *   other fields from the e-mail form.
566
 */
567
function webform_email_update($email) {
568
  $email['excluded_components'] = implode(',', $email['excluded_components']);
569
  return drupal_write_record('webform_emails', $email, array('nid', 'eid'));
570
}
571

    
572
/**
573
 * Delete an e-mail setting.
574
 */
575
function webform_email_delete($node, $email) {
576
  db_delete('webform_emails')
577
    ->condition('nid', $node->nid)
578
    ->condition('eid', $email['eid'])
579
    ->execute();
580
}