Projet

Général

Profil

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

root / drupal7 / sites / all / modules / webform / components / textfield.inc @ 8d02775b

1
<?php
2

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

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

    
38
/**
39
 * Implements _webform_theme_component().
40
 */
41
function _webform_theme_textfield() {
42
  return array(
43
    'webform_display_textfield' => array(
44
      'render element' => 'element',
45
      'file' => 'components/textfield.inc',
46
    ),
47
  );
48
}
49

    
50
/**
51
 * Implements _webform_edit_component().
52
 */
53
function _webform_edit_textfield($component) {
54
  $form = array();
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' => 1024,
62
    '#weight' => 0,
63
  );
64
  $form['display']['width'] = array(
65
    '#type' => 'textfield',
66
    '#title' => t('Width'),
67
    '#default_value' => $component['extra']['width'],
68
    '#description' => t('Width of the textfield.') . ' ' . t('Leaving blank will use the default size.'),
69
    '#size' => 5,
70
    '#maxlength' => 10,
71
    '#weight' => 0,
72
    '#parents' => array('extra', 'width'),
73
  );
74
  $form['display']['placeholder'] = array(
75
    '#type' => 'textfield',
76
    '#title' => t('Placeholder'),
77
    '#default_value' => $component['extra']['placeholder'],
78
    '#description' => t('The placeholder will be shown in the field until the user starts entering a value.'),
79
    '#weight' => 1,
80
    '#parents' => array('extra', 'placeholder'),
81
  );
82
  $form['display']['field_prefix'] = array(
83
    '#type' => 'textfield',
84
    '#title' => t('Prefix text placed to the left of the textfield'),
85
    '#default_value' => $component['extra']['field_prefix'],
86
    '#description' => t('Examples: $, #, -.'),
87
    '#size' => 20,
88
    '#maxlength' => 127,
89
    '#weight' => 2.1,
90
    '#parents' => array('extra', 'field_prefix'),
91
  );
92
  $form['display']['field_suffix'] = array(
93
    '#type' => 'textfield',
94
    '#title' => t('Postfix text placed to the right of the textfield'),
95
    '#default_value' => $component['extra']['field_suffix'],
96
    '#description' => t('Examples: lb, kg, %.'),
97
    '#size' => 20,
98
    '#maxlength' => 127,
99
    '#weight' => 2.2,
100
    '#parents' => array('extra', 'field_suffix'),
101
  );
102
  $form['display']['disabled'] = array(
103
    '#type' => 'checkbox',
104
    '#title' => t('Disabled'),
105
    '#return_value' => 1,
106
    '#description' => t('Make this field non-editable. Useful for displaying default value. Changeable via JavaScript or developer tools.'),
107
    '#weight' => 11,
108
    '#default_value' => $component['extra']['disabled'],
109
    '#parents' => array('extra', 'disabled'),
110
  );
111
  $form['validation']['unique'] = array(
112
    '#type' => 'checkbox',
113
    '#title' => t('Unique'),
114
    '#return_value' => 1,
115
    '#description' => t('Check that all entered values for this field are unique. The same value is not allowed to be used twice.'),
116
    '#weight' => 1,
117
    '#default_value' => $component['extra']['unique'],
118
    '#parents' => array('extra', 'unique'),
119
  );
120
  $form['validation']['maxlength'] = array(
121
    '#type' => 'textfield',
122
    '#title' => t('Maxlength'),
123
    '#default_value' => $component['extra']['maxlength'],
124
    '#description' => t('Maximum length of the textfield value.'),
125
    '#size' => 5,
126
    '#maxlength' => 10,
127
    '#weight' => 2,
128
    '#parents' => array('extra', 'maxlength'),
129
  );
130
  $form['validation']['minlength'] = array(
131
    '#type' => 'textfield',
132
    '#title' => t('Minlength'),
133
    '#default_value' => $component['extra']['minlength'],
134
    '#description' => t('Minimum length of the textfield value. The component may still be empty unless it is set as Required.'),
135
    '#size' => 5,
136
    '#maxlength' => 10,
137
    '#weight' => 3,
138
    '#parents' => array('extra', 'minlength'),
139
  );
140
  return $form;
141
}
142

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

    
149
  $element = array(
150
    '#type' => 'textfield',
151
    '#title' => $filter ? webform_filter_xss($component['name']) : $component['name'],
152
    '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
153
    '#default_value' => $filter ? webform_replace_tokens($component['value'], $node) : $component['value'],
154
    '#required' => $component['required'],
155
    '#weight' => $component['weight'],
156
    '#field_prefix' => empty($component['extra']['field_prefix']) ? NULL : ($filter ? webform_filter_xss($component['extra']['field_prefix']) : $component['extra']['field_prefix']),
157
    '#field_suffix' => empty($component['extra']['field_suffix']) ? NULL : ($filter ? webform_filter_xss($component['extra']['field_suffix']) : $component['extra']['field_suffix']),
158
    '#description' => $filter ? webform_filter_descriptions($component['extra']['description'], $node) : $component['extra']['description'],
159
    '#attributes' => $component['extra']['attributes'],
160
    '#theme_wrappers' => array('webform_element'),
161
    '#translatable' => array(
162
      'title',
163
      'description',
164
      'field_prefix',
165
      'field_suffix',
166
      'placeholder',
167
    ),
168
  );
169

    
170
  if ($component['required']) {
171
    $element['#attributes']['required'] = 'required';
172
  }
173

    
174
  if ($component['extra']['placeholder']) {
175
    $element['#attributes']['placeholder'] = $component['extra']['placeholder'];
176
  }
177

    
178
  if ($component['extra']['disabled']) {
179
    if ($filter) {
180
      $element['#attributes']['readonly'] = 'readonly';
181
    }
182
    else {
183
      $element['#disabled'] = TRUE;
184
    }
185
  }
186

    
187
  // Enforce uniqueness.
188
  if ($component['extra']['unique']) {
189
    $element['#element_validate'][] = 'webform_validate_unique';
190
  }
191

    
192
  // Change the 'width' option to the correct 'size' option.
193
  if ($component['extra']['width'] > 0) {
194
    $element['#size'] = $component['extra']['width'];
195
  }
196
  if ($component['extra']['maxlength'] > 0) {
197
    $element['#maxlength'] = $component['extra']['maxlength'];
198
  }
199
  if ($component['extra']['minlength'] > 0) {
200
    $element['#minlength'] = $component['extra']['minlength'];
201
  }
202

    
203
  if (isset($value[0])) {
204
    $element['#default_value'] = $value[0];
205
  }
206

    
207
  return $element;
208
}
209

    
210
/**
211
 * Implements _webform_display_component().
212
 */
213
function _webform_display_textfield($component, $value, $format = 'html', $submission = array()) {
214
  return array(
215
    '#title' => $component['name'],
216
    '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
217
    '#weight' => $component['weight'],
218
    '#theme' => 'webform_display_textfield',
219
    '#theme_wrappers' => $format == 'html' ? array('webform_element') : array('webform_element_text'),
220
    '#field_prefix' => $component['extra']['field_prefix'],
221
    '#field_suffix' => $component['extra']['field_suffix'],
222
    '#format' => $format,
223
    '#value' => isset($value[0]) ? $value[0] : '',
224
    '#translatable' => array('title', 'field_prefix', 'field_suffix', 'placeholder'),
225
  );
226
}
227

    
228
/**
229
 * Format the output of data for this component.
230
 */
231
function theme_webform_display_textfield($variables) {
232
  $element = $variables['element'];
233
  $prefix = $element['#format'] == 'html' ? '' : $element['#field_prefix'];
234
  $suffix = $element['#format'] == 'html' ? '' : $element['#field_suffix'];
235
  $value = $element['#format'] == 'html' ? check_plain($element['#value']) : $element['#value'];
236
  return $value !== '' ? ($prefix . $value . $suffix) : ' ';
237
}
238

    
239
/**
240
 * Implements _webform_analysis_component().
241
 */
242
function _webform_analysis_textfield($component, $sids = array(), $single = FALSE, $join = NULL) {
243
  $query = db_select('webform_submitted_data', 'wsd', array('fetch' => PDO::FETCH_ASSOC))
244
    ->fields('wsd', array('data'))
245
    ->condition('wsd.nid', $component['nid'])
246
    ->condition('wsd.cid', $component['cid']);
247

    
248
  if (count($sids)) {
249
    $query->condition('wsd.sid', $sids, 'IN');
250
  }
251

    
252
  if ($join) {
253
    $query->innerJoin($join, 'ws2_', 'wsd.sid = ws2_.sid');
254
  }
255

    
256
  $nonblanks = 0;
257
  $submissions = 0;
258
  $wordcount = 0;
259

    
260
  $result = $query->execute();
261
  foreach ($result as $data) {
262
    if (drupal_strlen(trim($data['data'])) > 0) {
263
      $nonblanks++;
264
      $wordcount += str_word_count(trim($data['data']));
265
    }
266
    $submissions++;
267
  }
268

    
269
  $rows[0] = array(t('Left Blank'), ($submissions - $nonblanks));
270
  $rows[1] = array(t('User entered value'), $nonblanks);
271

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

    
277
  return array(
278
    'table_rows' => $rows,
279
    'other_data' => $other,
280
  );
281
}
282

    
283
/**
284
 * Implements _webform_table_component().
285
 */
286
function _webform_table_textfield($component, $value) {
287
  return check_plain(empty($value[0]) ? '' : $value[0]);
288
}
289

    
290
/**
291
 * Implements _webform_action_set_component().
292
 */
293
function _webform_action_set_textfield($component, &$element, &$form_state, $value) {
294
  $element['#value'] = $value;
295
  form_set_value($element, $value, $form_state);
296
}
297

    
298
/**
299
 * Implements _webform_csv_headers_component().
300
 */
301
function _webform_csv_headers_textfield($component, $export_options) {
302
  $header = array();
303
  $header[0] = '';
304
  $header[1] = '';
305
  $header[2] = $export_options['header_keys'] ? $component['form_key'] : $component['name'];
306
  return $header;
307
}
308

    
309
/**
310
 * Implements _webform_csv_data_component().
311
 */
312
function _webform_csv_data_textfield($component, $export_options, $value) {
313
  return !isset($value[0]) ? '' : $value[0];
314
}