email.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. <?php
  2. /**
  3. * @file
  4. * Webform module email component.
  5. */
  6. /**
  7. * Implements _webform_defaults_component().
  8. */
  9. function _webform_defaults_email() {
  10. return array(
  11. 'name' => '',
  12. 'form_key' => NULL,
  13. 'pid' => 0,
  14. 'weight' => 0,
  15. 'value' => '',
  16. 'required' => 0,
  17. 'extra' => array(
  18. 'multiple' => 0,
  19. 'format' => 'short',
  20. 'width' => '',
  21. 'unique' => 0,
  22. 'disabled' => 0,
  23. 'title_display' => 0,
  24. 'description' => '',
  25. 'description_above' => FALSE,
  26. 'placeholder' => '',
  27. 'attributes' => array(),
  28. 'private' => FALSE,
  29. 'analysis' => FALSE,
  30. ),
  31. );
  32. }
  33. /**
  34. * Implements _webform_theme_component().
  35. */
  36. function _webform_theme_email() {
  37. return array(
  38. 'webform_email' => array(
  39. 'render element' => 'element',
  40. 'file' => 'components/email.inc',
  41. ),
  42. 'webform_display_email' => array(
  43. 'render element' => 'element',
  44. 'file' => 'components/email.inc',
  45. ),
  46. );
  47. }
  48. /**
  49. * Implements _webform_edit_component().
  50. */
  51. function _webform_edit_email($component) {
  52. $form['value'] = array(
  53. '#type' => 'textfield',
  54. '#title' => t('Default value'),
  55. '#default_value' => $component['value'],
  56. '#description' => t('The default value of the field.') . ' ' . theme('webform_token_help'),
  57. '#size' => 60,
  58. '#maxlength' => 127,
  59. '#weight' => 0,
  60. '#attributes' => ($component['value'] == '[current-user:mail]' && !form_get_errors()) ? array('disabled' => TRUE) : array(),
  61. '#id' => 'email-value',
  62. );
  63. $form['user_email'] = array(
  64. '#type' => 'checkbox',
  65. '#title' => t('User email as default'),
  66. '#default_value' => $component['value'] == '[current-user:mail]' ? 1 : 0,
  67. '#description' => t('Set the default value of this field to the user email, if he/she is logged in.'),
  68. '#attributes' => array('onclick' => 'getElementById("email-value").value = (this.checked ? "[current-user:mail]" : ""); getElementById("email-value").disabled = this.checked;'),
  69. '#weight' => 0,
  70. '#element_validate' => array('_webform_edit_email_validate'),
  71. );
  72. $form['extra']['multiple'] = array(
  73. '#type' => 'checkbox',
  74. '#title' => t('Multiple'),
  75. '#default_value' => $component['extra']['multiple'],
  76. '#description' => t('Allow multiple e-mail addresses, separated by commas.'),
  77. '#weight' => 0,
  78. );
  79. if (webform_variable_get('webform_email_address_format') == 'long') {
  80. $form['extra']['format'] = array(
  81. '#type' => 'radios',
  82. '#title' => t('Format'),
  83. '#options' => array(
  84. 'long' => t('Allow long format: "Example Name" &lt;name@example.com&gt;'),
  85. 'short' => t('Short format only: name@example.com'),
  86. ),
  87. '#default_value' => $component['extra']['format'],
  88. '#description' => t('Not all servers support the "long" format.'),
  89. );
  90. }
  91. $form['display']['width'] = array(
  92. '#type' => 'textfield',
  93. '#title' => t('Width'),
  94. '#default_value' => $component['extra']['width'],
  95. '#description' => t('Width of the textfield.') . ' ' . t('Leaving blank will use the default size.'),
  96. '#size' => 5,
  97. '#maxlength' => 10,
  98. '#parents' => array('extra', 'width'),
  99. );
  100. $form['display']['placeholder'] = array(
  101. '#type' => 'textfield',
  102. '#title' => t('Placeholder'),
  103. '#default_value' => $component['extra']['placeholder'],
  104. '#description' => t('The placeholder will be shown in the field until the user starts entering a value.') . ' ' . t('Often used for example values, such as "john@example.com".'),
  105. '#parents' => array('extra', 'placeholder'),
  106. );
  107. $form['display']['disabled'] = array(
  108. '#type' => 'checkbox',
  109. '#title' => t('Disabled'),
  110. '#return_value' => 1,
  111. '#description' => t('Make this field non-editable. Useful for displaying default value. Changeable via JavaScript or developer tools.'),
  112. '#weight' => 11,
  113. '#default_value' => $component['extra']['disabled'],
  114. '#parents' => array('extra', 'disabled'),
  115. );
  116. $form['validation']['unique'] = array(
  117. '#type' => 'checkbox',
  118. '#title' => t('Unique'),
  119. '#return_value' => 1,
  120. '#description' => t('Check that all entered values for this field are unique. The same value is not allowed to be used twice.'),
  121. '#weight' => 1,
  122. '#default_value' => $component['extra']['unique'],
  123. '#parents' => array('extra', 'unique'),
  124. );
  125. return $form;
  126. }
  127. /**
  128. * Element validation function for the email edit form.
  129. */
  130. function _webform_edit_email_validate($element, &$form_state) {
  131. if ($form_state['values']['user_email']) {
  132. $form_state['values']['value'] = '[current-user:mail]';
  133. }
  134. }
  135. /**
  136. * Implements _webform_render_component().
  137. */
  138. function _webform_render_email($component, $value = NULL, $filter = TRUE, $submission = NULL) {
  139. $node = isset($component['nid']) ? node_load($component['nid']) : NULL;
  140. $element = array(
  141. '#type' => 'webform_email',
  142. '#title' => $filter ? webform_filter_xss($component['name']) : $component['name'],
  143. '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
  144. '#default_value' => $filter ? webform_replace_tokens($component['value'], $node) : $component['value'],
  145. '#required' => $component['required'],
  146. '#weight' => $component['weight'],
  147. '#description' => $filter ? webform_filter_descriptions($component['extra']['description'], $node) : $component['extra']['description'],
  148. '#attributes' => $component['extra']['attributes'],
  149. '#element_validate' => array('_webform_validate_email'),
  150. '#theme_wrappers' => array('webform_element'),
  151. '#translatable' => array('title', 'description', 'placeholder'),
  152. );
  153. if ($component['required']) {
  154. $element['#attributes']['required'] = 'required';
  155. }
  156. // Add an e-mail class for identifying the difference from normal textfields.
  157. $element['#attributes']['class'][] = 'email';
  158. // Enforce uniqueness.
  159. if ($component['extra']['unique']) {
  160. $element['#element_validate'][] = 'webform_validate_unique';
  161. }
  162. if ($component['extra']['format'] == 'long') {
  163. // html5 email elements enforce short-form email validation in addition to
  164. // pattern validation. This means that long format email addresses must be
  165. // rendered as text.
  166. $element['#attributes']['type'] = 'text';
  167. // html5 patterns have implied delimiters and start and end patterns.
  168. // The are also case sensitive, not global, and not multi-line.
  169. // See https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/HTML5/Constraint_validation
  170. // See http://stackoverflow.com/questions/19605773/html5-email-validation
  171. $address = '[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*';
  172. $name = '("[^<>"]*?"|[^<>",]*?)';
  173. $short_long = "($name *<$address>|$address)";
  174. $element['#attributes']['pattern'] = $component['extra']['multiple']
  175. ? "$short_long(, *$short_long)*"
  176. : $short_long;
  177. }
  178. elseif ($component['extra']['multiple']) {
  179. $element['#attributes']['multiple'] = 'multiple';
  180. }
  181. if (isset($value[0])) {
  182. $element['#default_value'] = $value[0];
  183. }
  184. if ($component['extra']['placeholder']) {
  185. $element['#attributes']['placeholder'] = $component['extra']['placeholder'];
  186. }
  187. if ($component['extra']['disabled']) {
  188. if ($filter) {
  189. $element['#attributes']['readonly'] = 'readonly';
  190. }
  191. else {
  192. $element['#disabled'] = TRUE;
  193. }
  194. }
  195. // Change the 'width' option to the correct 'size' option.
  196. if ($component['extra']['width'] > 0) {
  197. $element['#size'] = $component['extra']['width'];
  198. }
  199. return $element;
  200. }
  201. /**
  202. * Theme function to render an email component.
  203. */
  204. function theme_webform_email($variables) {
  205. $element = $variables['element'];
  206. // This IF statement is mostly in place to allow our tests to set type="text"
  207. // because SimpleTest does not support type="email".
  208. if (!isset($element['#attributes']['type'])) {
  209. $element['#attributes']['type'] = 'email';
  210. }
  211. // Convert properties to attributes on the element if set.
  212. foreach (array('id', 'name', 'value', 'size') as $property) {
  213. if (isset($element['#' . $property]) && $element['#' . $property] !== '') {
  214. $element['#attributes'][$property] = $element['#' . $property];
  215. }
  216. }
  217. _form_set_class($element, array('form-text', 'form-email'));
  218. return '<input' . drupal_attributes($element['#attributes']) . ' />';
  219. }
  220. /**
  221. * A Drupal Form API Validation function.
  222. *
  223. * Validates the entered values from email components on the client-side form.
  224. * Calls a form_set_error if the e-mail is not valid.
  225. *
  226. * @param array $form_element
  227. * The e-mail form element.
  228. * @param array $form_state
  229. * The full form state for the webform.
  230. */
  231. function _webform_validate_email($form_element, &$form_state) {
  232. $component = $form_element['#webform_component'];
  233. $format = webform_variable_get('webform_email_address_format') == 'long' ? $component['extra']['format'] : 'short';
  234. webform_email_validate($form_element['#value'],
  235. implode('][', $form_element['#parents']),
  236. // Required validation is done elsewhere.
  237. TRUE,
  238. $component['extra']['multiple'],
  239. // No tokens are allowed in user input.
  240. FALSE,
  241. $format);
  242. }
  243. /**
  244. * Implements _webform_display_component().
  245. */
  246. function _webform_display_email($component, $value, $format = 'html', $submission = array()) {
  247. return array(
  248. '#title' => $component['name'],
  249. '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
  250. '#weight' => $component['weight'],
  251. '#theme' => 'webform_display_email',
  252. '#theme_wrappers' => $format == 'html' ? array('webform_element') : array('webform_element_text'),
  253. '#format' => $format,
  254. '#value' => isset($value[0]) ? $value[0] : '',
  255. '#translatable' => array('title', 'placeholder'),
  256. );
  257. }
  258. /**
  259. * Format the text output for this component.
  260. */
  261. function theme_webform_display_email($variables) {
  262. $element = $variables['element'];
  263. $element['#value'] = empty($element['#value']) ? ' ' : $element['#value'];
  264. return $element['#format'] == 'html' ? check_plain($element['#value']) : $element['#value'];
  265. }
  266. /**
  267. * Implements _webform_analysis_component().
  268. */
  269. function _webform_analysis_email($component, $sids = array(), $single = FALSE, $join = NULL) {
  270. $query = db_select('webform_submitted_data', 'wsd', array('fetch' => PDO::FETCH_ASSOC))
  271. ->fields('wsd', array('no', 'data'))
  272. ->condition('wsd.nid', $component['nid'])
  273. ->condition('wsd.cid', $component['cid']);
  274. if (count($sids)) {
  275. $query->condition('wsd.sid', $sids, 'IN');
  276. }
  277. if ($join) {
  278. $query->innerJoin($join, 'ws2_', 'wsd.sid = ws2_.sid');
  279. }
  280. $nonblanks = 0;
  281. $submissions = 0;
  282. $wordcount = 0;
  283. $result = $query->execute();
  284. foreach ($result as $data) {
  285. if (drupal_strlen(trim($data['data'])) > 0) {
  286. $nonblanks++;
  287. $wordcount += str_word_count(trim($data['data']));
  288. }
  289. $submissions++;
  290. }
  291. $rows[0] = array(t('Left Blank'), ($submissions - $nonblanks));
  292. $rows[1] = array(t('User entered value'), $nonblanks);
  293. $other[0] = array(
  294. t('Average submission length in words (ex blanks)'),
  295. ($nonblanks != 0 ? number_format($wordcount / $nonblanks, 2) : '0'),
  296. );
  297. return array(
  298. 'table_rows' => $rows,
  299. 'other_data' => $other,
  300. );
  301. }
  302. /**
  303. * Implements _webform_table_component().
  304. */
  305. function _webform_table_email($component, $value) {
  306. return check_plain(empty($value[0]) ? '' : $value[0]);
  307. }
  308. /**
  309. * Implements _webform_action_set_component().
  310. */
  311. function _webform_action_set_email($component, &$element, &$form_state, $value) {
  312. $element['#value'] = $value;
  313. form_set_value($element, $value, $form_state);
  314. }
  315. /**
  316. * Implements _webform_csv_headers_component().
  317. */
  318. function _webform_csv_headers_email($component, $export_options) {
  319. $header = array();
  320. $header[0] = '';
  321. $header[1] = '';
  322. $header[2] = $export_options['header_keys'] ? $component['form_key'] : $component['name'];
  323. return $header;
  324. }
  325. /**
  326. * Implements _webform_csv_data_component().
  327. */
  328. function _webform_csv_data_email($component, $export_options, $value) {
  329. return empty($value[0]) ? '' : $value[0];
  330. }