webform.emails.inc 20 KB

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