webform.emails.inc 28 KB

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