webform.emails.inc 20 KB

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