Projet

Général

Profil

Paste
Télécharger (7,96 ko) Statistiques
| Branche: | Révision:

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

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

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

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

    
121
/**
122
 * Implements _webform_render_component().
123
 */
124
function _webform_render_textfield($component, $value = NULL, $filter = TRUE) {
125
  $node = isset($component['nid']) ? node_load($component['nid']) : NULL;
126

    
127
  $element = array(
128
    '#type' => 'textfield',
129
    '#title' => $filter ? _webform_filter_xss($component['name']) : $component['name'],
130
    '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
131
    '#default_value' => $filter ? _webform_filter_values($component['value'], $node, NULL, NULL, FALSE) : $component['value'],
132
    '#required' => $component['mandatory'],
133
    '#weight' => $component['weight'],
134
    '#field_prefix' => empty($component['extra']['field_prefix']) ? NULL : ($filter ? _webform_filter_xss($component['extra']['field_prefix']) : $component['extra']['field_prefix']),
135
    '#field_suffix' => empty($component['extra']['field_suffix']) ? NULL : ($filter ? _webform_filter_xss($component['extra']['field_suffix']) : $component['extra']['field_suffix']),
136
    '#description' => $filter ? _webform_filter_descriptions($component['extra']['description'], $node) : $component['extra']['description'],
137
    '#attributes' => $component['extra']['attributes'],
138
    '#theme_wrappers' => array('webform_element'),
139
    '#translatable' => array('title', 'description', 'field_prefix', 'field_suffix'),
140
  );
141

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

    
151
  // Enforce uniqueness.
152
  if ($component['extra']['unique']) {
153
    $element['#element_validate'][] = 'webform_validate_unique';
154
  }
155

    
156
  // Change the 'width' option to the correct 'size' option.
157
  if ($component['extra']['width'] > 0) {
158
    $element['#size'] = $component['extra']['width'];
159
  }
160
  if ($component['extra']['maxlength'] > 0) {
161
    $element['#maxlength'] = $component['extra']['maxlength'];
162
  }
163

    
164
  if (isset($value)) {
165
    $element['#default_value'] = $value[0];
166
  }
167

    
168
  return $element;
169
}
170

    
171
/**
172
 * Implements _webform_display_component().
173
 */
174
function _webform_display_textfield($component, $value, $format = 'html') {
175
  return array(
176
    '#title' => $component['name'],
177
    '#weight' => $component['weight'],
178
    '#theme' => 'webform_display_textfield',
179
    '#theme_wrappers' => $format == 'html' ? array('webform_element') : array('webform_element_text'),
180
    '#field_prefix' => $component['extra']['field_prefix'],
181
    '#field_suffix' => $component['extra']['field_suffix'],
182
    '#format' => $format,
183
    '#value' => isset($value[0]) ? $value[0] : '',
184
    '#translatable' => array('title', 'field_prefix', 'field_suffix'),
185
  );
186
}
187

    
188
/**
189
 * Format the output of data for this component.
190
 */
191
function theme_webform_display_textfield($variables) {
192
  $element = $variables['element'];
193
  $prefix = $element['#format'] == 'html' ? '' : $element['#field_prefix'];
194
  $suffix = $element['#format'] == 'html' ? '' : $element['#field_suffix'];
195
  $value = $element['#format'] == 'html' ? check_plain($element['#value']) : $element['#value'];
196
  return $value !== '' ? ($prefix . $value . $suffix) : ' ';
197
}
198

    
199
/**
200
 * Implements _webform_analysis_component().
201
 */
202
function _webform_analysis_textfield($component, $sids = array()) {
203
  $query = db_select('webform_submitted_data', 'wsd', array('fetch' => PDO::FETCH_ASSOC))
204
    ->fields('wsd', array('data'))
205
    ->condition('nid', $component['nid'])
206
    ->condition('cid', $component['cid']);
207

    
208
  if (count($sids)) {
209
    $query->condition('sid', $sids, 'IN');
210
  }
211

    
212
  $nonblanks = 0;
213
  $submissions = 0;
214
  $wordcount = 0;
215

    
216
  $result = $query->execute();
217
  foreach ($result as $data) {
218
    if (drupal_strlen(trim($data['data'])) > 0) {
219
      $nonblanks++;
220
      $wordcount += str_word_count(trim($data['data']));
221
    }
222
    $submissions++;
223
  }
224

    
225
  $rows[0] = array(t('Left Blank'), ($submissions - $nonblanks));
226
  $rows[1] = array(t('User entered value'), $nonblanks);
227
  $rows[2] = array(t('Average submission length in words (ex blanks)'), ($nonblanks != 0 ? number_format($wordcount/$nonblanks, 2) : '0'));
228
  return $rows;
229
}
230

    
231
/**
232
 * Implements _webform_table_component().
233
 */
234
function _webform_table_textfield($component, $value) {
235
  return check_plain(empty($value[0]) ? '' : $value[0]);
236
}
237

    
238
/**
239
 * Implements _webform_csv_headers_component().
240
 */
241
function _webform_csv_headers_textfield($component, $export_options) {
242
  $header = array();
243
  $header[0] = '';
244
  $header[1] = '';
245
  $header[2] = $component['name'];
246
  return $header;
247
}
248

    
249
/**
250
 * Implements _webform_csv_data_component().
251
 */
252
function _webform_csv_data_textfield($component, $export_options, $value) {
253
  return !isset($value[0]) ? '' : $value[0];
254
}