Projet

Général

Profil

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

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

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
    'mandatory' => 0,
19
    'extra' => array(
20
      'width' => '',
21
      'unique' => 0,
22
      'disabled' => 0,
23
      'title_display' => 0,
24
      'description' => '',
25
      'attributes' => array(),
26
      'private' => FALSE,
27
    ),
28
  );
29
}
30

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

    
47
/**
48
 * Implements _webform_edit_component().
49
 */
50
function _webform_edit_email($component) {
51
  $form['value'] = array(
52
    '#type' => 'textfield',
53
    '#title' => t('Default value'),
54
    '#default_value' => $component['value'],
55
    '#description' => t('The default value of the field.') . theme('webform_token_help'),
56
    '#size' => 60,
57
    '#maxlength' => 127,
58
    '#weight' => 0,
59
    '#attributes' => ($component['value'] == '%useremail' && count(form_get_errors()) == 0) ? array('disabled' => TRUE) : array(),
60
    '#id' => 'email-value',
61
  );
62
  $form['user_email'] = array(
63
    '#type' => 'checkbox',
64
    '#title' => t('User email as default'),
65
    '#default_value' => $component['value'] == '%useremail' ? 1 : 0,
66
    '#description' => t('Set the default value of this field to the user email, if he/she is logged in.'),
67
    '#attributes' => array('onclick' => 'getElementById("email-value").value = (this.checked ? "%useremail" : ""); getElementById("email-value").disabled = this.checked;'),
68
    '#weight' => 0,
69
    '#element_validate' => array('_webform_edit_email_validate'),
70
  );
71
  $form['display']['width'] = array(
72
    '#type' => 'textfield',
73
    '#title' => t('Width'),
74
    '#default_value' => $component['extra']['width'],
75
    '#description' => t('Width of the textfield.') . ' ' . t('Leaving blank will use the default size.'),
76
    '#size' => 5,
77
    '#maxlength' => 10,
78
    '#parents' => array('extra', 'width'),
79
  );
80
  $form['display']['disabled'] = array(
81
    '#type' => 'checkbox',
82
    '#title' => t('Disabled'),
83
    '#return_value' => 1,
84
    '#description' => t('Make this field non-editable. Useful for setting an unchangeable default value.'),
85
    '#weight' => 11,
86
    '#default_value' => $component['extra']['disabled'],
87
    '#parents' => array('extra', 'disabled'),
88
  );
89
  $form['validation']['unique'] = array(
90
    '#type' => 'checkbox',
91
    '#title' => t('Unique'),
92
    '#return_value' => 1,
93
    '#description' => t('Check that all entered values for this field are unique. The same value is not allowed to be used twice.'),
94
    '#weight' => 1,
95
    '#default_value' => $component['extra']['unique'],
96
    '#parents' => array('extra', 'unique'),
97
  );
98
  return $form;
99
}
100

    
101
/**
102
 * Element validation function for the email edit form.
103
 */
104
function _webform_edit_email_validate($element, &$form_state) {
105
  if ($form_state['values']['user_email']) {
106
    $form_state['values']['value'] = '%useremail';
107
  }
108
}
109

    
110
/**
111
 * Implements _webform_render_component().
112
 */
113
function _webform_render_email($component, $value = NULL, $filter = TRUE) {
114
  global $user;
115
  $node = isset($component['nid']) ? node_load($component['nid']) : NULL;
116

    
117
  $element = array(
118
    '#type' => 'webform_email',
119
    '#title' => $filter ? _webform_filter_xss($component['name']) : $component['name'],
120
    '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
121
    '#default_value' => $filter ? _webform_filter_values($component['value'], $node) : $component['value'],
122
    '#required' => $component['mandatory'],
123
    '#weight' => $component['weight'],
124
    '#description' => $filter ? _webform_filter_descriptions($component['extra']['description'], $node) : $component['extra']['description'],
125
    '#attributes' => $component['extra']['attributes'],
126
    '#element_validate'  => array('_webform_validate_email'),
127
    '#theme_wrappers' => array('webform_element'),
128
    '#translatable' => array('title', 'description'),
129
  );
130

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

    
134
  // Enforce uniqueness.
135
  if ($component['extra']['unique']) {
136
    $element['#element_validate'][] = 'webform_validate_unique';
137
  }
138

    
139
  if (isset($value)) {
140
    $element['#default_value'] = $value[0];
141
  }
142

    
143
  if ($component['extra']['disabled']) {
144
    if ($filter) {
145
      $element['#attributes']['readonly'] = 'readonly';
146
    }
147
    else {
148
      $element['#disabled'] = TRUE;
149
    }
150
  }
151

    
152
  // Change the 'width' option to the correct 'size' option.
153
  if ($component['extra']['width'] > 0) {
154
    $element['#size'] = $component['extra']['width'];
155
  }
156

    
157
  return $element;
158
}
159

    
160
/**
161
 * Theme function to render an email component.
162
 */
163
function theme_webform_email($variables) {
164
  $element = $variables['element'];
165

    
166
  // This IF statement is mostly in place to allow our tests to set type="text"
167
  // because SimpleTest does not support type="email".
168
  if (!isset($element['#attributes']['type'])) {
169
    $element['#attributes']['type'] = 'email';
170
  }
171

    
172
  // Convert properties to attributes on the element if set.
173
  foreach (array('id', 'name', 'value', 'size') as $property) {
174
    if (isset($element['#' . $property]) && $element['#' . $property] !== '') {
175
      $element['#attributes'][$property] = $element['#' . $property];
176
    }
177
  }
178
  _form_set_class($element, array('form-text', 'form-email'));
179

    
180
  return '<input' . drupal_attributes($element['#attributes']) . ' />';
181
}
182

    
183
/**
184
 * A Drupal Form API Validation function. Validates the entered values from
185
 * email components on the client-side form.
186
 *
187
 * @param $form_element
188
 *   The e-mail form element.
189
 * @param $form_state
190
 *   The full form state for the webform.
191
 * @return
192
 *   None. Calls a form_set_error if the e-mail is not valid.
193
 */
194
function _webform_validate_email($form_element, &$form_state) {
195
  $component = $form_element['#webform_component'];
196
  $value = trim($form_element['#value']);
197
  if ($value !== '' && !valid_email_address($value)) {
198
    form_error($form_element, t('%value is not a valid email address.', array('%value' => $value)));
199
  }
200
  else {
201
    form_set_value($form_element, $value, $form_state);
202
  }
203
}
204

    
205
/**
206
 * Implements _webform_display_component().
207
 */
208
function _webform_display_email($component, $value, $format = 'html') {
209
  return array(
210
    '#title' => $component['name'],
211
    '#weight' => $component['weight'],
212
    '#theme' => 'webform_display_email',
213
    '#theme_wrappers' => $format == 'html' ? array('webform_element') : array('webform_element_text'),
214
    '#format' => $format,
215
    '#value' => isset($value[0]) ? $value[0] : '',
216
    '#translatable' => array('title'),
217
  );
218
}
219

    
220
/**
221
 * Format the text output for this component.
222
 */
223
function theme_webform_display_email($variables) {
224
  $element = $variables['element'];
225
  $element['#value'] = empty($element['#value']) ? ' ' : $element['#value'];
226
  return $element['#format'] == 'html' ? check_plain($element['#value']) : $element['#value'];
227
}
228

    
229
/**
230
 * Implements _webform_analysis_component().
231
 */
232
function _webform_analysis_email($component, $sids = array()) {
233
  $query = db_select('webform_submitted_data', 'wsd', array('fetch' => PDO::FETCH_ASSOC))
234
    ->fields('wsd', array('no', 'data'))
235
    ->condition('nid', $component['nid'])
236
    ->condition('cid', $component['cid']);
237

    
238
  if (count($sids)) {
239
    $query->condition('sid', $sids, 'IN');
240
  }
241

    
242
  $nonblanks = 0;
243
  $submissions = 0;
244
  $wordcount = 0;
245

    
246
  $result = $query->execute();
247
  foreach ($result as $data) {
248
    if (drupal_strlen(trim($data['data'])) > 0) {
249
      $nonblanks++;
250
      $wordcount += str_word_count(trim($data['data']));
251
    }
252
    $submissions++;
253
  }
254

    
255
  $rows[0] = array(t('Left Blank'), ($submissions - $nonblanks));
256
  $rows[1] = array(t('User entered value'), $nonblanks);
257
  $rows[2] = array(t('Average submission length in words (ex blanks)'), ($nonblanks != 0 ? number_format($wordcount/$nonblanks, 2) : '0'));
258
  return $rows;
259
}
260

    
261
/**
262
 * Implements _webform_table_component().
263
 */
264
function _webform_table_email($component, $value) {
265
  return check_plain(empty($value[0]) ? '' : $value[0]);
266
}
267

    
268

    
269
/**
270
 * Implements _webform_csv_headers_component().
271
 */
272
function _webform_csv_headers_email($component, $export_options) {
273
  $header = array();
274
  $header[0] = '';
275
  $header[1] = '';
276
  $header[2] = $component['name'];
277
  return $header;
278
}
279

    
280
/**
281
 * Implements _webform_csv_data_component().
282
 */
283
function _webform_csv_data_email($component, $export_options, $value) {
284
  return empty($value[0]) ? '' : $value[0];
285
}