Projet

Général

Profil

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

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

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
      'placeholder' => '',
28
      'attributes' => array(),
29
      'private' => FALSE,
30
      'analysis' => FALSE,
31
    ),
32
  );
33
}
34

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

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

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

    
140
/**
141
 * Implements _webform_render_component().
142
 */
143
function _webform_render_email($component, $value = NULL, $filter = TRUE, $submission = NULL) {
144
  global $user;
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 delimters 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. Validates the entered values from
244
 * email components on the client-side form.
245
 *
246
 * @param $form_element
247
 *   The e-mail form element.
248
 * @param $form_state
249
 *   The full form state for the webform.
250
 * @return
251
 *   None. Calls a form_set_error if the e-mail is not valid.
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
                         TRUE,  // Required validation is done elsewhere.
259
                         $component['extra']['multiple'],
260
                         FALSE, // No tokens are allowed in user input.
261
                         $format);
262
}
263

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

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

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

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

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

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

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

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

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

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

    
329
}
330

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

    
338

    
339
/**
340
 * Implements _webform_csv_headers_component().
341
 */
342
function _webform_csv_headers_email($component, $export_options) {
343
  $header = array();
344
  $header[0] = '';
345
  $header[1] = '';
346
  $header[2] = $export_options['header_keys'] ? $component['form_key'] : $component['name'];
347
  return $header;
348
}
349

    
350
/**
351
 * Implements _webform_csv_data_component().
352
 */
353
function _webform_csv_data_email($component, $export_options, $value) {
354
  return empty($value[0]) ? '' : $value[0];
355
}