email.inc 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. 'mandatory' => 0,
  17. 'extra' => array(
  18. 'width' => '',
  19. 'unique' => 0,
  20. 'disabled' => 0,
  21. 'title_display' => 0,
  22. 'description' => '',
  23. 'attributes' => array(),
  24. 'private' => FALSE,
  25. ),
  26. );
  27. }
  28. /**
  29. * Implements _webform_theme_component().
  30. */
  31. function _webform_theme_email() {
  32. return array(
  33. 'webform_email' => array(
  34. 'render element' => 'element',
  35. 'file' => 'components/email.inc',
  36. ),
  37. 'webform_display_email' => array(
  38. 'render element' => 'element',
  39. 'file' => 'components/email.inc',
  40. ),
  41. );
  42. }
  43. /**
  44. * Implements _webform_edit_component().
  45. */
  46. function _webform_edit_email($component) {
  47. $form['value'] = array(
  48. '#type' => 'textfield',
  49. '#title' => t('Default value'),
  50. '#default_value' => $component['value'],
  51. '#description' => t('The default value of the field.') . theme('webform_token_help'),
  52. '#size' => 60,
  53. '#maxlength' => 127,
  54. '#weight' => 0,
  55. '#attributes' => ($component['value'] == '%useremail' && count(form_get_errors()) == 0) ? array('disabled' => TRUE) : array(),
  56. '#id' => 'email-value',
  57. );
  58. $form['user_email'] = array(
  59. '#type' => 'checkbox',
  60. '#title' => t('User email as default'),
  61. '#default_value' => $component['value'] == '%useremail' ? 1 : 0,
  62. '#description' => t('Set the default value of this field to the user email, if he/she is logged in.'),
  63. '#attributes' => array('onclick' => 'getElementById("email-value").value = (this.checked ? "%useremail" : ""); getElementById("email-value").disabled = this.checked;'),
  64. '#weight' => 0,
  65. '#element_validate' => array('_webform_edit_email_validate'),
  66. );
  67. $form['display']['width'] = array(
  68. '#type' => 'textfield',
  69. '#title' => t('Width'),
  70. '#default_value' => $component['extra']['width'],
  71. '#description' => t('Width of the textfield.') . ' ' . t('Leaving blank will use the default size.'),
  72. '#size' => 5,
  73. '#maxlength' => 10,
  74. '#parents' => array('extra', 'width'),
  75. );
  76. $form['display']['disabled'] = array(
  77. '#type' => 'checkbox',
  78. '#title' => t('Disabled'),
  79. '#return_value' => 1,
  80. '#description' => t('Make this field non-editable. Useful for setting an unchangeable default value.'),
  81. '#weight' => 11,
  82. '#default_value' => $component['extra']['disabled'],
  83. '#parents' => array('extra', 'disabled'),
  84. );
  85. $form['validation']['unique'] = array(
  86. '#type' => 'checkbox',
  87. '#title' => t('Unique'),
  88. '#return_value' => 1,
  89. '#description' => t('Check that all entered values for this field are unique. The same value is not allowed to be used twice.'),
  90. '#weight' => 1,
  91. '#default_value' => $component['extra']['unique'],
  92. '#parents' => array('extra', 'unique'),
  93. );
  94. return $form;
  95. }
  96. /**
  97. * Element validation function for the email edit form.
  98. */
  99. function _webform_edit_email_validate($element, &$form_state) {
  100. if ($form_state['values']['user_email']) {
  101. $form_state['values']['value'] = '%useremail';
  102. }
  103. }
  104. /**
  105. * Implements _webform_render_component().
  106. */
  107. function _webform_render_email($component, $value = NULL, $filter = TRUE) {
  108. global $user;
  109. $node = isset($component['nid']) ? node_load($component['nid']) : NULL;
  110. $element = array(
  111. '#type' => 'webform_email',
  112. '#title' => $filter ? _webform_filter_xss($component['name']) : $component['name'],
  113. '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
  114. '#default_value' => $filter ? _webform_filter_values($component['value'], $node) : $component['value'],
  115. '#required' => $component['mandatory'],
  116. '#weight' => $component['weight'],
  117. '#description' => $filter ? _webform_filter_descriptions($component['extra']['description'], $node) : $component['extra']['description'],
  118. '#attributes' => $component['extra']['attributes'],
  119. '#element_validate' => array('_webform_validate_email'),
  120. '#theme_wrappers' => array('webform_element'),
  121. '#translatable' => array('title', 'description'),
  122. );
  123. // Add an e-mail class for identifying the difference from normal textfields.
  124. $element['#attributes']['class'][] = 'email';
  125. // Enforce uniqueness.
  126. if ($component['extra']['unique']) {
  127. $element['#element_validate'][] = 'webform_validate_unique';
  128. }
  129. if (isset($value)) {
  130. $element['#default_value'] = $value[0];
  131. }
  132. if ($component['extra']['disabled']) {
  133. if ($filter) {
  134. $element['#attributes']['readonly'] = 'readonly';
  135. }
  136. else {
  137. $element['#disabled'] = TRUE;
  138. }
  139. }
  140. // Change the 'width' option to the correct 'size' option.
  141. if ($component['extra']['width'] > 0) {
  142. $element['#size'] = $component['extra']['width'];
  143. }
  144. return $element;
  145. }
  146. /**
  147. * Theme function to render an email component.
  148. */
  149. function theme_webform_email($variables) {
  150. $element = $variables['element'];
  151. // This IF statement is mostly in place to allow our tests to set type="text"
  152. // because SimpleTest does not support type="email".
  153. if (!isset($element['#attributes']['type'])) {
  154. $element['#attributes']['type'] = 'email';
  155. }
  156. // Convert properties to attributes on the element if set.
  157. foreach (array('id', 'name', 'value', 'size') as $property) {
  158. if (isset($element['#' . $property]) && $element['#' . $property] !== '') {
  159. $element['#attributes'][$property] = $element['#' . $property];
  160. }
  161. }
  162. _form_set_class($element, array('form-text', 'form-email'));
  163. return '<input' . drupal_attributes($element['#attributes']) . ' />';
  164. }
  165. /**
  166. * A Drupal Form API Validation function. Validates the entered values from
  167. * email components on the client-side form.
  168. *
  169. * @param $form_element
  170. * The e-mail form element.
  171. * @param $form_state
  172. * The full form state for the webform.
  173. * @return
  174. * None. Calls a form_set_error if the e-mail is not valid.
  175. */
  176. function _webform_validate_email($form_element, &$form_state) {
  177. $component = $form_element['#webform_component'];
  178. $value = trim($form_element['#value']);
  179. if ($value !== '' && !valid_email_address($value)) {
  180. form_error($form_element, t('%value is not a valid email address.', array('%value' => $value)));
  181. }
  182. else {
  183. form_set_value($form_element, $value, $form_state);
  184. }
  185. }
  186. /**
  187. * Implements _webform_display_component().
  188. */
  189. function _webform_display_email($component, $value, $format = 'html') {
  190. return array(
  191. '#title' => $component['name'],
  192. '#weight' => $component['weight'],
  193. '#theme' => 'webform_display_email',
  194. '#theme_wrappers' => $format == 'html' ? array('webform_element') : array('webform_element_text'),
  195. '#format' => $format,
  196. '#value' => isset($value[0]) ? $value[0] : '',
  197. '#translatable' => array('title'),
  198. );
  199. }
  200. /**
  201. * Format the text output for this component.
  202. */
  203. function theme_webform_display_email($variables) {
  204. $element = $variables['element'];
  205. $element['#value'] = empty($element['#value']) ? ' ' : $element['#value'];
  206. return $element['#format'] == 'html' ? check_plain($element['#value']) : $element['#value'];
  207. }
  208. /**
  209. * Implements _webform_analysis_component().
  210. */
  211. function _webform_analysis_email($component, $sids = array()) {
  212. $query = db_select('webform_submitted_data', 'wsd', array('fetch' => PDO::FETCH_ASSOC))
  213. ->fields('wsd', array('no', 'data'))
  214. ->condition('nid', $component['nid'])
  215. ->condition('cid', $component['cid']);
  216. if (count($sids)) {
  217. $query->condition('sid', $sids, 'IN');
  218. }
  219. $nonblanks = 0;
  220. $submissions = 0;
  221. $wordcount = 0;
  222. $result = $query->execute();
  223. foreach ($result as $data) {
  224. if (drupal_strlen(trim($data['data'])) > 0) {
  225. $nonblanks++;
  226. $wordcount += str_word_count(trim($data['data']));
  227. }
  228. $submissions++;
  229. }
  230. $rows[0] = array(t('Left Blank'), ($submissions - $nonblanks));
  231. $rows[1] = array(t('User entered value'), $nonblanks);
  232. $rows[2] = array(t('Average submission length in words (ex blanks)'), ($nonblanks != 0 ? number_format($wordcount/$nonblanks, 2) : '0'));
  233. return $rows;
  234. }
  235. /**
  236. * Implements _webform_table_component().
  237. */
  238. function _webform_table_email($component, $value) {
  239. return check_plain(empty($value[0]) ? '' : $value[0]);
  240. }
  241. /**
  242. * Implements _webform_csv_headers_component().
  243. */
  244. function _webform_csv_headers_email($component, $export_options) {
  245. $header = array();
  246. $header[0] = '';
  247. $header[1] = '';
  248. $header[2] = $component['name'];
  249. return $header;
  250. }
  251. /**
  252. * Implements _webform_csv_data_component().
  253. */
  254. function _webform_csv_data_email($component, $export_options, $value) {
  255. return empty($value[0]) ? '' : $value[0];
  256. }