Projet

Général

Profil

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

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

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
      'field_prefix' => '',
23
      'field_suffix' => '',
24
      'disabled' => 0,
25
      'unique' => 0,
26
      'title_display' => 0,
27
      'description' => '',
28
      'description_above' => FALSE,
29
      'placeholder' => '',
30
      'attributes' => array(),
31
      'private' => FALSE,
32
      'analysis' => FALSE,
33
    ),
34
  );
35
}
36

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

    
49
/**
50
 * Implements _webform_edit_component().
51
 */
52
function _webform_edit_textfield($component) {
53
  $form = array();
54
  $form['value'] = array(
55
    '#type' => 'textfield',
56
    '#title' => t('Default value'),
57
    '#default_value' => $component['value'],
58
    '#description' => t('The default value of the field.') . ' ' . theme('webform_token_help'),
59
    '#size' => 60,
60
    '#maxlength' => 1024,
61
    '#weight' => 0,
62
  );
63
  $form['display']['width'] = array(
64
    '#type' => 'textfield',
65
    '#title' => t('Width'),
66
    '#default_value' => $component['extra']['width'],
67
    '#description' => t('Width of the textfield.') . ' ' . t('Leaving blank will use the default size.'),
68
    '#size' => 5,
69
    '#maxlength' => 10,
70
    '#weight' => 0,
71
    '#parents' => array('extra', 'width'),
72
  );
73
  $form['display']['placeholder'] = array(
74
    '#type' => 'textfield',
75
    '#title' => t('Placeholder'),
76
    '#default_value' => $component['extra']['placeholder'],
77
    '#description' => t('The placeholder will be shown in the field until the user starts entering a value.'),
78
    '#weight' => 1,
79
    '#parents' => array('extra', 'placeholder'),
80
  );
81
  $form['display']['field_prefix'] = array(
82
    '#type' => 'textfield',
83
    '#title' => t('Prefix text placed to the left of the textfield'),
84
    '#default_value' => $component['extra']['field_prefix'],
85
    '#description' => t('Examples: $, #, -.'),
86
    '#size' => 20,
87
    '#maxlength' => 127,
88
    '#weight' => 2.1,
89
    '#parents' => array('extra', 'field_prefix'),
90
  );
91
  $form['display']['field_suffix'] = array(
92
    '#type' => 'textfield',
93
    '#title' => t('Postfix text placed to the right of the textfield'),
94
    '#default_value' => $component['extra']['field_suffix'],
95
    '#description' => t('Examples: lb, kg, %.'),
96
    '#size' => 20,
97
    '#maxlength' => 127,
98
    '#weight' => 2.2,
99
    '#parents' => array('extra', 'field_suffix'),
100
  );
101
  $form['display']['disabled'] = array(
102
    '#type' => 'checkbox',
103
    '#title' => t('Disabled'),
104
    '#return_value' => 1,
105
    '#description' => t('Make this field non-editable. Useful for setting an unchangeable default value.'),
106
    '#weight' => 11,
107
    '#default_value' => $component['extra']['disabled'],
108
    '#parents' => array('extra', 'disabled'),
109
  );
110
  $form['validation']['unique'] = array(
111
    '#type' => 'checkbox',
112
    '#title' => t('Unique'),
113
    '#return_value' => 1,
114
    '#description' => t('Check that all entered values for this field are unique. The same value is not allowed to be used twice.'),
115
    '#weight' => 1,
116
    '#default_value' => $component['extra']['unique'],
117
    '#parents' => array('extra', 'unique'),
118
  );
119
  $form['validation']['maxlength'] = array(
120
    '#type' => 'textfield',
121
    '#title' => t('Maxlength'),
122
    '#default_value' => $component['extra']['maxlength'],
123
    '#description' => t('Maximum length of the textfield value.'),
124
    '#size' => 5,
125
    '#maxlength' => 10,
126
    '#weight' => 2,
127
    '#parents' => array('extra', 'maxlength'),
128
  );
129
  return $form;
130
}
131

    
132
/**
133
 * Implements _webform_render_component().
134
 */
135
function _webform_render_textfield($component, $value = NULL, $filter = TRUE, $submission = NULL) {
136
  $node = isset($component['nid']) ? node_load($component['nid']) : NULL;
137

    
138
  $element = array(
139
    '#type' => 'textfield',
140
    '#title' => $filter ? webform_filter_xss($component['name']) : $component['name'],
141
    '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
142
    '#default_value' => $filter ? webform_replace_tokens($component['value'], $node) : $component['value'],
143
    '#required' => $component['required'],
144
    '#weight' => $component['weight'],
145
    '#field_prefix' => empty($component['extra']['field_prefix']) ? NULL : ($filter ? webform_filter_xss($component['extra']['field_prefix']) : $component['extra']['field_prefix']),
146
    '#field_suffix' => empty($component['extra']['field_suffix']) ? NULL : ($filter ? webform_filter_xss($component['extra']['field_suffix']) : $component['extra']['field_suffix']),
147
    '#description' => $filter ? webform_filter_descriptions($component['extra']['description'], $node) : $component['extra']['description'],
148
    '#attributes' => $component['extra']['attributes'],
149
    '#theme_wrappers' => array('webform_element'),
150
    '#translatable' => array('title', 'description', 'field_prefix', 'field_suffix'),
151
  );
152

    
153
  if ($component['required']) {
154
    $element['#attributes']['required'] = 'required';
155
  }
156

    
157
  if ($component['extra']['placeholder']) {
158
    $element['#attributes']['placeholder'] = $component['extra']['placeholder'];
159
  }
160

    
161
  if ($component['extra']['disabled']) {
162
    if ($filter) {
163
      $element['#attributes']['readonly'] = 'readonly';
164
    }
165
    else {
166
      $element['#disabled'] = TRUE;
167
    }
168
  }
169

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

    
175
  // Change the 'width' option to the correct 'size' option.
176
  if ($component['extra']['width'] > 0) {
177
    $element['#size'] = $component['extra']['width'];
178
  }
179
  if ($component['extra']['maxlength'] > 0) {
180
    $element['#maxlength'] = $component['extra']['maxlength'];
181
  }
182

    
183
  if (isset($value[0])) {
184
    $element['#default_value'] = $value[0];
185
  }
186

    
187
  return $element;
188
}
189

    
190
/**
191
 * Implements _webform_display_component().
192
 */
193
function _webform_display_textfield($component, $value, $format = 'html', $submission = array()) {
194
  return array(
195
    '#title' => $component['name'],
196
    '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
197
    '#weight' => $component['weight'],
198
    '#theme' => 'webform_display_textfield',
199
    '#theme_wrappers' => $format == 'html' ? array('webform_element') : array('webform_element_text'),
200
    '#field_prefix' => $component['extra']['field_prefix'],
201
    '#field_suffix' => $component['extra']['field_suffix'],
202
    '#format' => $format,
203
    '#value' => isset($value[0]) ? $value[0] : '',
204
    '#translatable' => array('title', 'field_prefix', 'field_suffix'),
205
  );
206
}
207

    
208
/**
209
 * Format the output of data for this component.
210
 */
211
function theme_webform_display_textfield($variables) {
212
  $element = $variables['element'];
213
  $prefix = $element['#format'] == 'html' ? '' : $element['#field_prefix'];
214
  $suffix = $element['#format'] == 'html' ? '' : $element['#field_suffix'];
215
  $value = $element['#format'] == 'html' ? check_plain($element['#value']) : $element['#value'];
216
  return $value !== '' ? ($prefix . $value . $suffix) : ' ';
217
}
218

    
219
/**
220
 * Implements _webform_analysis_component().
221
 */
222
function _webform_analysis_textfield($component, $sids = array(), $single = FALSE, $join = NULL) {
223
  $query = db_select('webform_submitted_data', 'wsd', array('fetch' => PDO::FETCH_ASSOC))
224
    ->fields('wsd', array('data'))
225
    ->condition('wsd.nid', $component['nid'])
226
    ->condition('wsd.cid', $component['cid']);
227

    
228
  if (count($sids)) {
229
    $query->condition('wsd.sid', $sids, 'IN');
230
  }
231

    
232
  if ($join) {
233
    $query->innerJoin($join, 'ws2_', 'wsd.sid = ws2_.sid');
234
  }
235

    
236
  $nonblanks = 0;
237
  $submissions = 0;
238
  $wordcount = 0;
239

    
240
  $result = $query->execute();
241
  foreach ($result as $data) {
242
    if (drupal_strlen(trim($data['data'])) > 0) {
243
      $nonblanks++;
244
      $wordcount += str_word_count(trim($data['data']));
245
    }
246
    $submissions++;
247
  }
248

    
249
  $rows[0] = array(t('Left Blank'), ($submissions - $nonblanks));
250
  $rows[1] = array(t('User entered value'), $nonblanks);
251

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

    
254
  return array(
255
    'table_rows' => $rows,
256
    'other_data' => $other,
257
  );
258
}
259

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

    
267
/**
268
 * Implements _webform_action_set_component().
269
 */
270
function _webform_action_set_textfield($component, &$element, &$form_state, $value) {
271
  $element['#value'] = $value;
272
  form_set_value($element, $value, $form_state);
273
}
274

    
275
/**
276
 * Implements _webform_csv_headers_component().
277
 */
278
function _webform_csv_headers_textfield($component, $export_options) {
279
  $header = array();
280
  $header[0] = '';
281
  $header[1] = '';
282
  $header[2] = $export_options['header_keys'] ? $component['form_key'] : $component['name'];
283
  return $header;
284
}
285

    
286
/**
287
 * Implements _webform_csv_data_component().
288
 */
289
function _webform_csv_data_textfield($component, $export_options, $value) {
290
  return !isset($value[0]) ? '' : $value[0];
291
}