Projet

Général

Profil

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

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

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
      '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_textfield() {
40
  return array(
41
    'webform_display_textfield' => array(
42
      'render element' => 'element',
43
      'file' => 'components/textfield.inc',
44
    ),
45
  );
46
}
47

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

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

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

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

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

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

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

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

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

    
186
  return $element;
187
}
188

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

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

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

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

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

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

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

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

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

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

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

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

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

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