Project

General

Profile

Paste
Download (11.9 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / webform / components / email.inc @ 01f36513

1
<?php
2

    
3
/**
4
 * @file
5
 * Webform module email component.
6
 */
7

    
8
/**
9
 * Implements _webform_defaults_component().
10
 */
11
function _webform_defaults_email() {
12
  return array(
13
    'name' => '',
14
    'form_key' => NULL,
15
    'pid' => 0,
16
    'weight' => 0,
17
    'value' => '',
18
    'required' => 0,
19
    'extra' => array(
20
      'multiple' => 0,
21
      'format' => 'short',
22
      'width' => '',
23
      'unique' => 0,
24
      'disabled' => 0,
25
      'title_display' => 0,
26
      'description' => '',
27
      'description_above' => FALSE,
28
      'placeholder' => '',
29
      'attributes' => array(),
30
      'private' => FALSE,
31
      'analysis' => FALSE,
32
    ),
33
  );
34
}
35

    
36
/**
37
 * Implements _webform_theme_component().
38
 */
39
function _webform_theme_email() {
40
  return array(
41
    'webform_email' => array(
42
      'render element' => 'element',
43
      'file' => 'components/email.inc',
44
    ),
45
    'webform_display_email' => array(
46
      'render element' => 'element',
47
      'file' => 'components/email.inc',
48
    ),
49
  );
50
}
51

    
52
/**
53
 * Implements _webform_edit_component().
54
 */
55
function _webform_edit_email($component) {
56
  $form['value'] = array(
57
    '#type' => 'textfield',
58
    '#title' => t('Default value'),
59
    '#default_value' => $component['value'],
60
    '#description' => t('The default value of the field.') . ' ' . theme('webform_token_help'),
61
    '#size' => 60,
62
    '#maxlength' => 127,
63
    '#weight' => 0,
64
    '#attributes' => ($component['value'] == '[current-user:mail]' && count(form_get_errors()) == 0) ? array('disabled' => TRUE) : array(),
65
    '#id' => 'email-value',
66
  );
67
  $form['user_email'] = array(
68
    '#type' => 'checkbox',
69
    '#title' => t('User email as default'),
70
    '#default_value' => $component['value'] == '[current-user:mail]' ? 1 : 0,
71
    '#description' => t('Set the default value of this field to the user email, if he/she is logged in.'),
72
    '#attributes' => array('onclick' => 'getElementById("email-value").value = (this.checked ? "[current-user:mail]" : ""); getElementById("email-value").disabled = this.checked;'),
73
    '#weight' => 0,
74
    '#element_validate' => array('_webform_edit_email_validate'),
75
  );
76
  $form['extra']['multiple'] = array(
77
    '#type' => 'checkbox',
78
    '#title' => t('Multiple'),
79
    '#default_value' => $component['extra']['multiple'],
80
    '#description' => t('Allow multiple e-mail addresses, separated by commas.'),
81
    '#weight' => 0,
82
  );
83
  if (webform_variable_get('webform_email_address_format') == 'long') {
84
    $form['extra']['format'] = array(
85
      '#type' => 'radios',
86
      '#title' => t('Format'),
87
      '#options' => array(
88
        'long' => t('Allow long format: "Example Name" &lt;name@example.com&gt;'),
89
        'short' => t('Short format only: name@example.com'),
90
      ),
91
      '#default_value' => $component['extra']['format'],
92
      '#description' => t('Not all servers support the "long" format.'),
93
    );
94
  }
95
  $form['display']['width'] = array(
96
    '#type' => 'textfield',
97
    '#title' => t('Width'),
98
    '#default_value' => $component['extra']['width'],
99
    '#description' => t('Width of the textfield.') . ' ' . t('Leaving blank will use the default size.'),
100
    '#size' => 5,
101
    '#maxlength' => 10,
102
    '#parents' => array('extra', 'width'),
103
  );
104
  $form['display']['placeholder'] = array(
105
    '#type' => 'textfield',
106
    '#title' => t('Placeholder'),
107
    '#default_value' => $component['extra']['placeholder'],
108
    '#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".'),
109
    '#parents' => array('extra', 'placeholder'),
110
  );
111
  $form['display']['disabled'] = array(
112
    '#type' => 'checkbox',
113
    '#title' => t('Disabled'),
114
    '#return_value' => 1,
115
    '#description' => t('Make this field non-editable. Useful for displaying default value. Changeable via JavaScript or developer tools.'),
116
    '#weight' => 11,
117
    '#default_value' => $component['extra']['disabled'],
118
    '#parents' => array('extra', 'disabled'),
119
  );
120
  $form['validation']['unique'] = array(
121
    '#type' => 'checkbox',
122
    '#title' => t('Unique'),
123
    '#return_value' => 1,
124
    '#description' => t('Check that all entered values for this field are unique. The same value is not allowed to be used twice.'),
125
    '#weight' => 1,
126
    '#default_value' => $component['extra']['unique'],
127
    '#parents' => array('extra', 'unique'),
128
  );
129
  return $form;
130
}
131

    
132
/**
133
 * Element validation function for the email edit form.
134
 */
135
function _webform_edit_email_validate($element, &$form_state) {
136
  if ($form_state['values']['user_email']) {
137
    $form_state['values']['value'] = '[current-user:mail]';
138
  }
139
}
140

    
141
/**
142
 * Implements _webform_render_component().
143
 */
144
function _webform_render_email($component, $value = NULL, $filter = TRUE, $submission = NULL) {
145
  $node = isset($component['nid']) ? node_load($component['nid']) : NULL;
146

    
147
  $element = array(
148
    '#type' => 'webform_email',
149
    '#title' => $filter ? webform_filter_xss($component['name']) : $component['name'],
150
    '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
151
    '#default_value' => $filter ? webform_replace_tokens($component['value'], $node) : $component['value'],
152
    '#required' => $component['required'],
153
    '#weight' => $component['weight'],
154
    '#description' => $filter ? webform_filter_descriptions($component['extra']['description'], $node) : $component['extra']['description'],
155
    '#attributes' => $component['extra']['attributes'],
156
    '#element_validate'  => array('_webform_validate_email'),
157
    '#theme_wrappers' => array('webform_element'),
158
    '#translatable' => array('title', 'description'),
159
  );
160

    
161
  if ($component['required']) {
162
    $element['#attributes']['required'] = 'required';
163
  }
164

    
165
  // Add an e-mail class for identifying the difference from normal textfields.
166
  $element['#attributes']['class'][] = 'email';
167

    
168
  // Enforce uniqueness.
169
  if ($component['extra']['unique']) {
170
    $element['#element_validate'][] = 'webform_validate_unique';
171
  }
172

    
173
  if ($component['extra']['format'] == 'long') {
174
    // html5 email elements enforce short-form email validation in addition to
175
    // pattern validation. This means that long format email addresses must be
176
    // rendered as text.
177
    $element['#attributes']['type'] = 'text';
178

    
179
    // html5 patterns have implied delimiters and start and end patterns.
180
    // The are also case sensitive, not global, and not multi-line.
181
    // See https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/HTML5/Constraint_validation
182
    // See http://stackoverflow.com/questions/19605773/html5-email-validation
183
    $address = '[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*';
184
    $name = '("[^<>"]*?"|[^<>",]*?)';
185
    $short_long = "($name *<$address>|$address)";
186
    $element['#attributes']['pattern'] = $component['extra']['multiple']
187
                                            ? "$short_long(, *$short_long)*"
188
                                            : $short_long;
189
  }
190
  elseif ($component['extra']['multiple']) {
191
    $element['#attributes']['multiple'] = 'multiple';
192
  }
193

    
194
  if (isset($value[0])) {
195
    $element['#default_value'] = $value[0];
196
  }
197

    
198
  if ($component['extra']['placeholder']) {
199
    $element['#attributes']['placeholder'] = $component['extra']['placeholder'];
200
  }
201

    
202
  if ($component['extra']['disabled']) {
203
    if ($filter) {
204
      $element['#attributes']['readonly'] = 'readonly';
205
    }
206
    else {
207
      $element['#disabled'] = TRUE;
208
    }
209
  }
210

    
211
  // Change the 'width' option to the correct 'size' option.
212
  if ($component['extra']['width'] > 0) {
213
    $element['#size'] = $component['extra']['width'];
214
  }
215

    
216
  return $element;
217
}
218

    
219
/**
220
 * Theme function to render an email component.
221
 */
222
function theme_webform_email($variables) {
223
  $element = $variables['element'];
224

    
225
  // This IF statement is mostly in place to allow our tests to set type="text"
226
  // because SimpleTest does not support type="email".
227
  if (!isset($element['#attributes']['type'])) {
228
    $element['#attributes']['type'] = 'email';
229
  }
230

    
231
  // Convert properties to attributes on the element if set.
232
  foreach (array('id', 'name', 'value', 'size') as $property) {
233
    if (isset($element['#' . $property]) && $element['#' . $property] !== '') {
234
      $element['#attributes'][$property] = $element['#' . $property];
235
    }
236
  }
237
  _form_set_class($element, array('form-text', 'form-email'));
238

    
239
  return '<input' . drupal_attributes($element['#attributes']) . ' />';
240
}
241

    
242
/**
243
 * A Drupal Form API Validation function.
244
 *
245
 * Validates the entered values from email components on the client-side form.
246
 * Calls a form_set_error if the e-mail is not valid.
247
 *
248
 * @param $form_element
249
 *   The e-mail form element.
250
 * @param $form_state
251
 *   The full form state for the webform.
252
 */
253
function _webform_validate_email($form_element, &$form_state) {
254
  $component = $form_element['#webform_component'];
255
  $format = webform_variable_get('webform_email_address_format') == 'long' ? $component['extra']['format'] : 'short';
256
  webform_email_validate($form_element['#value'],
257
    implode('][', $form_element['#parents']),
258
    // Required validation is done elsewhere.
259
    TRUE,
260
    $component['extra']['multiple'],
261
    // No tokens are allowed in user input.
262
    FALSE,
263
    $format);
264
}
265

    
266
/**
267
 * Implements _webform_display_component().
268
 */
269
function _webform_display_email($component, $value, $format = 'html', $submission = array()) {
270
  return array(
271
    '#title' => $component['name'],
272
    '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
273
    '#weight' => $component['weight'],
274
    '#theme' => 'webform_display_email',
275
    '#theme_wrappers' => $format == 'html' ? array('webform_element') : array('webform_element_text'),
276
    '#format' => $format,
277
    '#value' => isset($value[0]) ? $value[0] : '',
278
    '#translatable' => array('title'),
279
  );
280
}
281

    
282
/**
283
 * Format the text output for this component.
284
 */
285
function theme_webform_display_email($variables) {
286
  $element = $variables['element'];
287
  $element['#value'] = empty($element['#value']) ? ' ' : $element['#value'];
288
  return $element['#format'] == 'html' ? check_plain($element['#value']) : $element['#value'];
289
}
290

    
291
/**
292
 * Implements _webform_analysis_component().
293
 */
294
function _webform_analysis_email($component, $sids = array(), $single = FALSE, $join = NULL) {
295
  $query = db_select('webform_submitted_data', 'wsd', array('fetch' => PDO::FETCH_ASSOC))
296
    ->fields('wsd', array('no', 'data'))
297
    ->condition('wsd.nid', $component['nid'])
298
    ->condition('wsd.cid', $component['cid']);
299

    
300
  if (count($sids)) {
301
    $query->condition('wsd.sid', $sids, 'IN');
302
  }
303

    
304
  if ($join) {
305
    $query->innerJoin($join, 'ws2_', 'wsd.sid = ws2_.sid');
306
  }
307

    
308
  $nonblanks = 0;
309
  $submissions = 0;
310
  $wordcount = 0;
311

    
312
  $result = $query->execute();
313
  foreach ($result as $data) {
314
    if (drupal_strlen(trim($data['data'])) > 0) {
315
      $nonblanks++;
316
      $wordcount += str_word_count(trim($data['data']));
317
    }
318
    $submissions++;
319
  }
320

    
321
  $rows[0] = array(t('Left Blank'), ($submissions - $nonblanks));
322
  $rows[1] = array(t('User entered value'), $nonblanks);
323

    
324
  $other[0] = array(
325
    t('Average submission length in words (ex blanks)'),
326
    ($nonblanks != 0 ? number_format($wordcount / $nonblanks, 2) : '0'),
327
  );
328

    
329
  return array(
330
    'table_rows' => $rows,
331
    'other_data' => $other,
332
  );
333
}
334

    
335
/**
336
 * Implements _webform_table_component().
337
 */
338
function _webform_table_email($component, $value) {
339
  return check_plain(empty($value[0]) ? '' : $value[0]);
340
}
341

    
342
/**
343
 * Implements _webform_action_set_component().
344
 */
345
function _webform_action_set_email($component, &$element, &$form_state, $value) {
346
  $element['#value'] = $value;
347
  form_set_value($element, $value, $form_state);
348
}
349

    
350
/**
351
 * Implements _webform_csv_headers_component().
352
 */
353
function _webform_csv_headers_email($component, $export_options) {
354
  $header = array();
355
  $header[0] = '';
356
  $header[1] = '';
357
  $header[2] = $export_options['header_keys'] ? $component['form_key'] : $component['name'];
358
  return $header;
359
}
360

    
361
/**
362
 * Implements _webform_csv_data_component().
363
 */
364
function _webform_csv_data_email($component, $export_options, $value) {
365
  return empty($value[0]) ? '' : $value[0];
366
}