Projet

Général

Profil

Paste
Télécharger (11,9 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / webform / components / email.inc @ 8c72e82a

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
  global $user;
146
  $node = isset($component['nid']) ? node_load($component['nid']) : NULL;
147

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

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

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

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

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

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

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

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

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

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

    
217
  return $element;
218
}
219

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
325
  return array(
326
    'table_rows' => $rows,
327
    'other_data' => $other,
328
  );
329

    
330
}
331

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

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

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

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