webform.emails.inc

Tracking 7.x-4.x branch
  1. drupal
    1. 6 contributions/webform/includes/webform.emails.inc
    2. 7 contributions/webform/includes/webform.emails.inc

Provides interface and database handling for e-mail settings of a webform.

@author Nathan Haug <nate@lullabot.com>

Functions & methods

NameDescription
theme_webform_emails_formTheme the node components form. Use a table to organize the components.
theme_webform_email_add_formTheme the add new e-mail settings form on the node/x/webform/emails page.
theme_webform_email_edit_formTheme the Webform mail settings section of the node form.
webform_emails_formOverview form of all components for this webform.
webform_emails_form_submitSubmit handler for webform_emails_form().
webform_email_address_validateValidate handler for webform_email_edit_form() and webform_emails_form().
webform_email_deleteDelete an e-mail setting.
webform_email_delete_formForm for deleting an e-mail setting.
webform_email_delete_form_submitSubmit handler for webform_email_delete_form().
webform_email_edit_formForm for configuring an e-mail setting and template.
webform_email_edit_form_submitSubmit handler for webform_email_edit_form().
webform_email_edit_form_validateValidate handler for webform_email_edit_form().
webform_email_insertInsert a new e-mail setting into the database.
webform_email_loadLoad an e-mail setting from the database or initialize a new e-mail.
webform_email_updateUpdate an existing e-mail setting with new values.

File

View source
  1. <?php
  2. /**
  3. * @file
  4. * Provides interface and database handling for e-mail settings of a webform.
  5. *
  6. * @author Nathan Haug <nate@lullabot.com>
  7. */
  8. /**
  9. * Overview form of all components for this webform.
  10. */
  11. function webform_emails_form($form, $form_state, $node) {
  12. module_load_include('inc', 'webform', 'includes/webform.components');
  13. $form['#attached']['library'][] = array('webform', 'admin');
  14. $form['#tree'] = TRUE;
  15. $form['#node'] = $node;
  16. $form['components'] = array();
  17. foreach ($node->webform['emails'] as $eid => $email) {
  18. $email_addresses = array_filter(explode(',', check_plain($email['email'])));
  19. foreach ($email_addresses as $key => $email_address) {
  20. $email_addresses[$key] = webform_format_email_address($email_address, NULL, $node, NULL, FALSE);
  21. }
  22. $form['emails'][$eid]['email'] = array(
  23. '#markup' => implode('<br />', $email_addresses),
  24. );
  25. $form['emails'][$eid]['subject'] = array(
  26. '#markup' => check_plain(webform_format_email_subject($email['subject'], $node)),
  27. );
  28. $form['emails'][$eid]['from'] = array(
  29. '#markup' => check_plain(webform_format_email_address($email['from_address'], $email['from_name'], $node, NULL, FALSE)),
  30. );
  31. }
  32. $form['add'] = array(
  33. '#theme' => 'webform_email_add_form',
  34. '#tree' => FALSE,
  35. );
  36. $form['add']['email_option'] = array(
  37. '#type' => 'radios',
  38. '#options' => array(
  39. 'custom' => t('Address'),
  40. 'component' => t('Component value'),
  41. ),
  42. '#default_value' => 'custom',
  43. );
  44. $form['add']['email_custom'] = array(
  45. '#type' => 'textfield',
  46. '#size' => 24,
  47. '#maxlength' => 500,
  48. );
  49. $form['add']['email_component'] = array(
  50. '#type' => 'select',
  51. '#options' => webform_component_list($node, 'email_address', FALSE),
  52. );
  53. if (empty($form['add']['email_component']['#options'])) {
  54. $form['add']['email_component']['#options'][''] = t('No available components');
  55. $form['add']['email_component']['#disabled'] = TRUE;
  56. }
  57. $form['add_button'] = array(
  58. '#type' => 'submit',
  59. '#value' => t('Add'),
  60. '#weight' => 45,
  61. );
  62. $form['#validate'] = array('webform_email_address_validate');
  63. return $form;
  64. }
  65. /**
  66. * Theme the node components form. Use a table to organize the components.
  67. *
  68. * @param $form
  69. * The form array.
  70. * @return
  71. * Formatted HTML form, ready for display.
  72. */
  73. function theme_webform_emails_form($variables) {
  74. $form = $variables['form'];
  75. $node = $form['#node'];
  76. $header = array(t('E-mail to'), t('Subject'), t('From'), array('data' => t('Operations'), 'colspan' => 2));
  77. $rows = array();
  78. if (!empty($form['emails'])) {
  79. foreach (element_children($form['emails']) as $eid) {
  80. // Add each component to a table row.
  81. $rows[] = array(
  82. drupal_render($form['emails'][$eid]['email']),
  83. drupal_render($form['emails'][$eid]['subject']),
  84. drupal_render($form['emails'][$eid]['from']),
  85. l(t('Edit'), 'node/' . $node->nid . '/webform/emails/' . $eid),
  86. l(t('Delete'), 'node/' . $node->nid . '/webform/emails/' . $eid . '/delete'),
  87. );
  88. }
  89. }
  90. else {
  91. $rows[] = array(array('data' => t('Currently not sending e-mails, add an e-mail recipient below.'), 'colspan' => 5));
  92. }
  93. // Add a row containing form elements for a new item.
  94. $row_data = array(
  95. array('colspan' => 3, 'data' => drupal_render($form['add'])),
  96. array('colspan' => 2, 'data' => drupal_render($form['add_button'])),
  97. );
  98. $rows[] = array('data' => $row_data, 'class' => array('webform-add-form'));
  99. $output = '';
  100. $output .= theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'webform-emails')));
  101. $output .= drupal_render_children($form);
  102. return $output;
  103. }
  104. /**
  105. * Theme the add new e-mail settings form on the node/x/webform/emails page.
  106. */
  107. function theme_webform_email_add_form($variables) {
  108. $form = $variables['form'];
  109. // Add a default value to the custom e-mail textfield.
  110. $form['email_custom']['#attributes']['rel'] = t('email@example.com');
  111. $form['email_custom']['#attributes']['class'] = array('webform-set-active', 'webform-default-value');
  112. $form['email_option']['custom']['#theme_wrappers'] = array('webform_inline_radio');
  113. $form['email_option']['custom']['#inline_element'] = drupal_render($form['email_custom']);
  114. // Render the component value.
  115. $form['email_component']['#attributes']['class'] = array('webform-set-active');
  116. $form['email_option']['component']['#theme_wrappers'] = array('webform_inline_radio');
  117. $form['email_option']['component']['#inline_element'] = drupal_render($form['email_component']);
  118. return drupal_render_children($form);
  119. }
  120. /**
  121. * Submit handler for webform_emails_form().
  122. */
  123. function webform_emails_form_submit($form, &$form_state) {
  124. if ($form_state['values']['email_option'] == 'custom') {
  125. $email = $form_state['values']['email_custom'];
  126. }
  127. else {
  128. $email = $form_state['values']['email_component'];
  129. }
  130. $form_state['redirect'] = array('node/' . $form['#node']->nid . '/webform/emails/new', array('query' => array('option' => $form_state['values']['email_option'], 'email' => trim($email))));
  131. }
  132. /**
  133. * Form for configuring an e-mail setting and template.
  134. */
  135. function webform_email_edit_form($form, $form_state, $node, $email = array()) {
  136. module_load_include('inc', 'webform', 'includes/webform.components');
  137. $form['#attached']['library'][] = array('webform', 'admin');
  138. $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');
  139. $form['#tree'] = TRUE;
  140. $form['#node'] = $node;
  141. $form['eid'] = array(
  142. '#type' => 'value',
  143. '#value' => isset($email['eid']) ? $email['eid'] : NULL,
  144. );
  145. // All these fields work essentially the same, with a radio button set,
  146. // a textfield for custom values, and a select list for a component.
  147. foreach (array('email', 'subject', 'from_address', 'from_name') as $field) {
  148. switch ($field) {
  149. case 'email':
  150. $default_value = NULL;
  151. $title = t('E-mail to address');
  152. $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.');
  153. break;
  154. case 'subject':
  155. $default_value = _webform_filter_values(webform_variable_get('webform_default_subject'), $node);
  156. $title = t('E-mail subject');
  157. $description = t('Any textfield, select, or hidden form element may be selected as the subject for e-mails.');
  158. break;
  159. case 'from_address':
  160. $default_value = _webform_filter_values(webform_variable_get('webform_default_from_address'), $node);
  161. $title = t('E-mail from address');
  162. $description = t('Any email, select, or hidden form element may be selected as the sender\'s e-mail address.');
  163. break;
  164. case 'from_name':
  165. $default_value = _webform_filter_values(webform_variable_get('webform_default_from_name'), $node);
  166. $title = t('E-mail from name');
  167. $description = t('Any textfield, select, or hidden form element may be selected as the sender\'s name for e-mails.');
  168. break;
  169. }
  170. $form[$field . '_option'] = array(
  171. '#title' => $title,
  172. '#type' => 'radios',
  173. '#default_value' => is_numeric($email[$field]) ? 'component' : ((empty($default_value) || ($email[$field] != 'default' && isset($email[$field]))) ? 'custom' : 'default'),
  174. '#description' => $description,
  175. );
  176. if (!empty($default_value)) {
  177. $form[$field . '_option']['#options']['default'] = t('Default: %value', array('%value' => $default_value));
  178. }
  179. $form[$field . '_option']['#options']['custom'] = t('Custom');
  180. $form[$field . '_option']['#options']['component'] = t('Component');
  181. $form[$field . '_custom'] = array(
  182. '#type' => 'textfield',
  183. '#size' => 40,
  184. '#default_value' => (!is_numeric($email[$field]) && $email[$field] != 'default') ? $email[$field] : NULL,
  185. '#maxlength' => $field == 'email' ? 500 : 255,
  186. );
  187. $options = webform_component_list($node, $field == 'from_address' || $field == 'email' ? 'email_address' : 'email_name', FALSE);
  188. $form[$field . '_component'] = array(
  189. '#type' => 'select',
  190. '#default_value' => is_numeric($email[$field]) ? $email[$field] : NULL,
  191. '#options' => empty($options) ? array('' => t('No available components')) : $options,
  192. '#disabled' => empty($options) ? TRUE : FALSE,
  193. '#weight' => 6,
  194. );
  195. }
  196. // Do not show the "E-mail from name" if using the short e-mail format.
  197. if (variable_get('webform_email_address_format', 'long') == 'short') {
  198. $form['from_name_option']['#access'] = FALSE;
  199. $form['from_name_custom']['#access'] = FALSE;
  200. $form['from_name_component']['#access'] = FALSE;
  201. }
  202. // Add the template fieldset.
  203. $form['template'] = array(
  204. '#type' => 'fieldset',
  205. '#title' => t('E-mail template'),
  206. '#collapsible' => TRUE,
  207. '#collapsed' => !empty($email['cid']) && empty($email['template']),
  208. '#description' => t('An e-mail template can customize the display of e-mails.'),
  209. '#weight' => 15,
  210. '#tree' => FALSE,
  211. '#attributes' => array('id' => 'webform-template-fieldset'),
  212. );
  213. $form['template']['template_option'] = array(
  214. '#type' => 'select',
  215. '#options' => array(
  216. 'default' => t('Default template'),
  217. 'custom' => t('Custom template'),
  218. ),
  219. '#default_value' => $email['template'] == 'default' ? 'default' : 'custom',
  220. );
  221. $default_template = theme(array('webform_mail_' . $node->nid, 'webform_mail', 'webform_mail_message'), array('node' => $node, 'email' => $email));
  222. $template = $email['template'] == 'default' ? $default_template : $email['template'];
  223. $form['template']['template'] = array(
  224. '#type' => 'textarea',
  225. '#rows' => max(10, min(20, count(explode("\n", $template)))),
  226. '#default_value' => $template,
  227. '#wysiwyg' => webform_email_html_capable() ? NULL : FALSE,
  228. );
  229. $form['template']['html'] = array(
  230. '#type' => 'checkbox',
  231. '#title' => t('Send e-mail as HTML'),
  232. '#default_value' => $email['html'],
  233. '#access' => webform_email_html_capable() && !variable_get('webform_format_override', 0),
  234. );
  235. $form['template']['attachments'] = array(
  236. '#type' => 'checkbox',
  237. '#title' => t('Include files as attachments'),
  238. '#default_value' => $email['attachments'],
  239. '#access' => webform_email_html_capable(),
  240. );
  241. $form['template']['tokens'] = array(
  242. '#markup' => theme('webform_token_help', array('groups' => array('node', 'submission'))),
  243. );
  244. $form['template']['components'] = array(
  245. '#type' => 'select',
  246. '#title' => t('Included e-mail values'),
  247. '#options' => webform_component_list($node, 'email', TRUE),
  248. '#default_value' => array_diff(array_keys($node->webform['components']), $email['excluded_components']),
  249. '#multiple' => TRUE,
  250. '#size' => 10,
  251. '#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.'),
  252. '#process' => array('webform_component_select'),
  253. );
  254. // TODO: Allow easy re-use of existing templates.
  255. $form['templates']['#tree'] = TRUE;
  256. $form['templates']['default'] = array(
  257. '#type' => 'textarea',
  258. '#value' => $default_template,
  259. '#resizable' => FALSE,
  260. '#weight' => 19,
  261. '#wysiwyg' => FALSE,
  262. );
  263. // Add the submit button.
  264. $form['submit'] = array(
  265. '#type' => 'submit',
  266. '#value' => t('Save e-mail settings'),
  267. '#weight' => 20,
  268. );
  269. $form['#validate'] = array('webform_email_address_validate', 'webform_email_edit_form_validate');
  270. return $form;
  271. }
  272. /**
  273. * Theme the Webform mail settings section of the node form.
  274. */
  275. function theme_webform_email_edit_form($variables) {
  276. $form = $variables['form'];
  277. // Loop through fields, rendering them into radio button options.
  278. foreach (array('email', 'subject', 'from_address', 'from_name') as $field) {
  279. foreach (array('custom', 'component') as $option) {
  280. $form[$field . '_' . $option]['#attributes']['class'] = array('webform-set-active');
  281. $form[$field . '_option'][$option]['#theme_wrappers'] = array('webform_inline_radio');
  282. $form[$field . '_option'][$option]['#inline_element'] = drupal_render($form[$field . '_' . $option]);
  283. }
  284. if (isset($form[$field . '_option']['#options']['default'])) {
  285. $form[$field . '_option']['default']['#theme_wrappers'] = array('webform_inline_radio');
  286. }
  287. }
  288. $details = '';
  289. $details .= drupal_render($form['subject_option']);
  290. $details .= drupal_render($form['from_address_option']);
  291. $details .= drupal_render($form['from_name_option']);
  292. $form['details'] = array(
  293. '#type' => 'fieldset',
  294. '#title' => t('E-mail header details'),
  295. '#weight' => 10,
  296. '#children' => $details,
  297. '#collapsible' => FALSE,
  298. '#parents' => array('details'),
  299. '#groups' => array('details' => array()),
  300. '#attributes' => array(),
  301. );
  302. // Ensure templates are completely hidden.
  303. $form['templates']['#prefix'] = '<div id="webform-email-templates" style="display: none">';
  304. $form['templates']['#suffix'] = '</div>';
  305. // Re-sort the elements since we added the details fieldset.
  306. $form['#sorted'] = FALSE;
  307. $children = element_children($form, TRUE);
  308. return drupal_render_children($form, $children);
  309. }
  310. /**
  311. * Validate handler for webform_email_edit_form() and webform_emails_form().
  312. */
  313. function webform_email_address_validate($form, &$form_state) {
  314. if ($form_state['values']['email_option'] == 'custom') {
  315. $email = trim($form_state['values']['email_custom']);
  316. if (empty($email)) {
  317. form_set_error('email_custom', t('When adding a new custom e-mail, the e-mail field is required.'));
  318. }
  319. else {
  320. $emails = array_filter(explode(',', $email));
  321. foreach ($emails as $email) {
  322. if (!valid_email_address(_webform_filter_values(trim($email), $form['#node']))) {
  323. form_set_error('email_custom', t('The entered e-mail address "@email" does not appear valid.', array('@email' => $email)));
  324. }
  325. }
  326. }
  327. }
  328. }
  329. /**
  330. * Validate handler for webform_email_edit_form().
  331. */
  332. function webform_email_edit_form_validate($form, &$form_state) {
  333. if ($form_state['values']['from_address_option'] == 'custom' && !valid_email_address(_webform_filter_values($form_state['values']['from_address_custom'], $form['#node']))) {
  334. 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'])));
  335. }
  336. }
  337. /**
  338. * Submit handler for webform_email_edit_form().
  339. */
  340. function webform_email_edit_form_submit($form, &$form_state) {
  341. // Ensure a webform record exists.
  342. $node = $form['#node'];
  343. webform_ensure_record($node);
  344. // Merge the e-mail, name, address, and subject options into single values.
  345. $email = array(
  346. 'eid' => $form_state['values']['eid'],
  347. 'nid' => $node->nid,
  348. );
  349. foreach (array('email', 'from_name', 'from_address', 'subject') as $field) {
  350. $option = $form_state['values'][$field . '_option'];
  351. if ($option == 'default') {
  352. $email[$field] = 'default';
  353. }
  354. else {
  355. $email[$field] = $form_state['values'][$field . '_' . $option];
  356. }
  357. }
  358. // Ensure templates are unaffected by differences in line breaks.
  359. $form_state['values']['template'] = str_replace(array("\r", "\n"), array('', "\n"), $form_state['values']['template']);
  360. $form_state['values']['templates']['default'] = str_replace(array("\r", "\n"), array('', "\n"), $form_state['values']['templates']['default']);
  361. // Set the template value.
  362. // TODO: Support reuse of templates.
  363. if (strcmp(trim($form_state['values']['templates']['default']), trim($form_state['values']['template'])) == 0) {
  364. $email['template'] = 'default';
  365. }
  366. else {
  367. $email['template'] = $form_state['values']['template'];
  368. }
  369. // Save the attachment and HTML options provided by MIME mail.
  370. $email['html'] = empty($form_state['values']['html']) ? 0 : 1;
  371. $email['attachments'] = empty($form_state['values']['attachments']) ? 0 : 1;
  372. // Save the list of included components.
  373. // We actually maintain an *exclusion* list, so any new components will
  374. // default to being included in the [submission:values] token until unchecked.
  375. $included = array_keys(array_filter((array) $form_state['values']['components']));
  376. $excluded = array_diff(array_keys($node->webform['components']), $included);
  377. $email['excluded_components'] = $excluded;
  378. if (empty($form_state['values']['eid'])) {
  379. drupal_set_message(t('Email settings added.'));
  380. $form_state['values']['eid'] = webform_email_insert($email);
  381. }
  382. else {
  383. drupal_set_message(t('Email settings updated.'));
  384. webform_email_update($email);
  385. }
  386. // Clear the entity cache if Entity Cache module is installed.
  387. if (module_exists('entitycache')) {
  388. cache_clear_all($node->nid, 'cache_entity_node');
  389. }
  390. $form_state['redirect'] = array('node/' . $node->nid . '/webform/emails');
  391. }
  392. /**
  393. * Form for deleting an e-mail setting.
  394. */
  395. function webform_email_delete_form($form, $form_state, $node, $email) {
  396. $eid = $email['eid'];
  397. $form['node'] = array(
  398. '#type' => 'value',
  399. '#value' => $node,
  400. );
  401. $form['email'] = array(
  402. '#type' => 'value',
  403. '#value' => $email,
  404. );
  405. $question = t('Delete e-mail settings?');
  406. if (is_numeric($email['email'])) {
  407. $description = t('This will immediately delete the e-mail settings based on the @component component.', array('@component' => $email['email']));
  408. }
  409. else {
  410. $description = t('This will immediately delete the e-mail settings sending to the @address address.', array('@address' => $email['email']));
  411. }
  412. return confirm_form($form, $question, 'node/' . $node->nid . '/webform/emails', $description, t('Delete'));
  413. }
  414. /**
  415. * Submit handler for webform_email_delete_form().
  416. */
  417. function webform_email_delete_form_submit($form, &$form_state) {
  418. // Delete the e-mail settings.
  419. $node = $form_state['values']['node'];
  420. $email = $form_state['values']['email'];
  421. webform_email_delete($node, $email);
  422. drupal_set_message(t('E-mail settings deleted.'));
  423. // Check if this webform still contains any information.
  424. unset($node->webform['emails'][$email['eid']]);
  425. webform_check_record($node);
  426. // Clear the entity cache if Entity Cache module is installed.
  427. if (module_exists('entitycache')) {
  428. cache_clear_all($node->nid, 'cache_entity_node');
  429. }
  430. $form_state['redirect'] = 'node/' . $node->nid . '/webform/emails';
  431. }
  432. /**
  433. * Load an e-mail setting from the database or initialize a new e-mail.
  434. */
  435. function webform_email_load($eid, $nid) {
  436. $node = node_load($nid);
  437. if ($eid == 'new') {
  438. $email = array(
  439. 'email' => '',
  440. 'subject' => 'default',
  441. 'from_name' => 'default',
  442. 'from_address' => 'default',
  443. 'template' => 'default',
  444. 'excluded_components' => array(),
  445. 'html' => variable_get('webform_default_format', 0),
  446. 'attachments' => 0,
  447. );
  448. }
  449. else {
  450. $email = isset($node->webform['emails'][$eid]) ? $node->webform['emails'][$eid] : FALSE;
  451. if (variable_get('webform_format_override', 0)) {
  452. $email['html'] = variable_get('webform_default_format', 0);
  453. }
  454. }
  455. return $email;
  456. }
  457. /**
  458. * Insert a new e-mail setting into the database.
  459. *
  460. * @param $email
  461. * An array of settings for sending an e-mail.
  462. */
  463. function webform_email_insert($email) {
  464. // TODO: This is not race-condition safe. Switch to using transactions?
  465. if (!isset($email['eid'])) {
  466. $next_id_query = db_select('webform_emails')->condition('nid', $email['nid']);
  467. $next_id_query->addExpression('MAX(eid) + 1', 'eid');
  468. $email['eid'] = $next_id_query->execute()->fetchField();
  469. if ($email['eid'] == NULL) {
  470. $email['eid'] = 1;
  471. }
  472. }
  473. $email['excluded_components'] = implode(',', $email['excluded_components']);
  474. $success = drupal_write_record('webform_emails', $email);
  475. return $success ? $email['eid'] : FALSE;
  476. }
  477. /**
  478. * Update an existing e-mail setting with new values.
  479. *
  480. * @param $email
  481. * An array of settings for sending an e-mail containing a nid, eid, and all
  482. * other fields from the e-mail form.
  483. */
  484. function webform_email_update($email) {
  485. $email['excluded_components'] = implode(',', $email['excluded_components']);
  486. return drupal_write_record('webform_emails', $email, array('nid', 'eid'));
  487. }
  488. /**
  489. * Delete an e-mail setting.
  490. */
  491. function webform_email_delete($node, $email) {
  492. db_delete('webform_emails')
  493. ->condition('nid', $node->nid)
  494. ->condition('eid', $email['eid'])
  495. ->execute();
  496. }