1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Webform module number component.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Implements _webform_defaults_component().
|
10
|
*/
|
11
|
function _webform_defaults_number() {
|
12
|
return array(
|
13
|
'name' => '',
|
14
|
'form_key' => NULL,
|
15
|
'pid' => 0,
|
16
|
'weight' => 0,
|
17
|
'value' => '',
|
18
|
'mandatory' => 0,
|
19
|
'extra' => array(
|
20
|
'type' => 'textfield',
|
21
|
'field_prefix' => '',
|
22
|
'field_suffix' => '',
|
23
|
'disabled' => 0,
|
24
|
'unique' => 0,
|
25
|
'title_display' => 0,
|
26
|
'description' => '',
|
27
|
'attributes' => array(),
|
28
|
'private' => FALSE,
|
29
|
'min' => '',
|
30
|
'max' => '',
|
31
|
'step' => '',
|
32
|
'decimals' => '',
|
33
|
'point' => '.',
|
34
|
'separator' => ',',
|
35
|
'integer' => 0,
|
36
|
'excludezero' => 0,
|
37
|
),
|
38
|
);
|
39
|
}
|
40
|
|
41
|
/**
|
42
|
* Implements _webform_theme_component().
|
43
|
*/
|
44
|
function _webform_theme_number() {
|
45
|
return array(
|
46
|
'webform_number' => array(
|
47
|
'render element' => 'element',
|
48
|
'file' => 'components/number.inc',
|
49
|
),
|
50
|
'webform_display_number' => array(
|
51
|
'render element' => 'element',
|
52
|
'file' => 'components/number.inc',
|
53
|
),
|
54
|
);
|
55
|
}
|
56
|
|
57
|
/**
|
58
|
* Implements _webform_edit_component().
|
59
|
*/
|
60
|
function _webform_edit_number($component) {
|
61
|
$form = array();
|
62
|
$form['value'] = array(
|
63
|
'#type' => 'textfield',
|
64
|
'#title' => t('Default value'),
|
65
|
'#default_value' => $component['value'],
|
66
|
'#description' => t('The default value of the field.') . theme('webform_token_help'),
|
67
|
'#size' => 60,
|
68
|
'#maxlength' => 1024,
|
69
|
'#weight' => 0,
|
70
|
);
|
71
|
$form['display']['type'] = array(
|
72
|
'#type' => 'radios',
|
73
|
'#title' => t('Element type'),
|
74
|
'#options' => array(
|
75
|
'textfield' => t('Text field'),
|
76
|
'select' => t('Select list'),
|
77
|
),
|
78
|
'#default_value' => $component['extra']['type'],
|
79
|
'#description' => t('A minimum and maximum value are required if displaying as a select.'),
|
80
|
'#weight' => -1,
|
81
|
'#parents' => array('extra', 'type'),
|
82
|
);
|
83
|
$form['display']['field_prefix'] = array(
|
84
|
'#type' => 'textfield',
|
85
|
'#title' => t('Prefix text placed to the left of the field'),
|
86
|
'#default_value' => $component['extra']['field_prefix'],
|
87
|
'#description' => t('Examples: $, #, -.'),
|
88
|
'#size' => 20,
|
89
|
'#maxlength' => 127,
|
90
|
'#weight' => 1.1,
|
91
|
'#parents' => array('extra', 'field_prefix'),
|
92
|
);
|
93
|
$form['display']['field_suffix'] = array(
|
94
|
'#type' => 'textfield',
|
95
|
'#title' => t('Postfix text placed to the right of the field'),
|
96
|
'#default_value' => $component['extra']['field_suffix'],
|
97
|
'#description' => t('Examples: lb, kg, %.'),
|
98
|
'#size' => 20,
|
99
|
'#maxlength' => 127,
|
100
|
'#weight' => 1.2,
|
101
|
'#parents' => array('extra', 'field_suffix'),
|
102
|
);
|
103
|
$form['display']['disabled'] = array(
|
104
|
'#type' => 'checkbox',
|
105
|
'#title' => t('Disabled'),
|
106
|
'#return_value' => 1,
|
107
|
'#description' => t('Make this field non-editable. Useful for setting an unchangeable default value.'),
|
108
|
'#weight' => 11,
|
109
|
'#default_value' => $component['extra']['disabled'],
|
110
|
'#parents' => array('extra', 'disabled'),
|
111
|
);
|
112
|
$form['display']['decimals'] = array(
|
113
|
'#type' => 'select',
|
114
|
'#title' => t('Decimal places'),
|
115
|
'#options' => array('' => t('Automatic')) + drupal_map_assoc(range(0, 10)),
|
116
|
'#description' => t('Automatic will display up to @count decimals places if needed. A value of "2" is common to format currency amounts.', array('@count' => '4')),
|
117
|
'#default_value' => $component['extra']['decimals'],
|
118
|
'#weight' => 2,
|
119
|
'#parents' => array('extra', 'decimals'),
|
120
|
'#element_validate' => array('_webform_edit_number_validate'),
|
121
|
);
|
122
|
$form['display']['separator'] = array(
|
123
|
'#type' => 'select',
|
124
|
'#title' => t('Thousands separator'),
|
125
|
'#options' => array(
|
126
|
',' => t('Comma (,)'),
|
127
|
'.' => t('Period (.)'),
|
128
|
' ' => t('Space ( )'),
|
129
|
'' => t('None'),
|
130
|
),
|
131
|
'#default_value' => $component['extra']['separator'],
|
132
|
'#weight' => 3,
|
133
|
'#parents' => array('extra', 'separator'),
|
134
|
'#element_validate' => array('_webform_edit_number_validate'),
|
135
|
);
|
136
|
$form['display']['point'] = array(
|
137
|
'#type' => 'select',
|
138
|
'#title' => t('Decimal point'),
|
139
|
'#options' => array(
|
140
|
',' => t('Comma (,)'),
|
141
|
'.' => t('Period (.)'),
|
142
|
),
|
143
|
'#default_value' => $component['extra']['point'],
|
144
|
'#weight' => 4,
|
145
|
'#parents' => array('extra', 'point'),
|
146
|
'#element_validate' => array('_webform_edit_number_validate'),
|
147
|
);
|
148
|
$form['validation']['unique'] = array(
|
149
|
'#type' => 'checkbox',
|
150
|
'#title' => t('Unique'),
|
151
|
'#return_value' => 1,
|
152
|
'#description' => t('Check that all entered values for this field are unique. The same value is not allowed to be used twice.'),
|
153
|
'#weight' => 1,
|
154
|
'#default_value' => $component['extra']['unique'],
|
155
|
'#parents' => array('extra', 'unique'),
|
156
|
);
|
157
|
$form['validation']['integer'] = array(
|
158
|
'#type' => 'checkbox',
|
159
|
'#title' => t('Integer'),
|
160
|
'#return_value' => 1,
|
161
|
'#description' => t('Permit only integer values as input. e.g. 12.34 would be invalid.'),
|
162
|
'#weight' => 1.5,
|
163
|
'#default_value' => $component['extra']['integer'],
|
164
|
'#parents' => array('extra', 'integer'),
|
165
|
);
|
166
|
$form['validation']['min'] = array(
|
167
|
'#type' => 'textfield',
|
168
|
'#title' => t('Minimum'),
|
169
|
'#default_value' => $component['extra']['min'],
|
170
|
'#description' => t('Minimum numeric value. e.g. 0 would ensure positive numbers.'),
|
171
|
'#size' => 5,
|
172
|
'#maxlength' => 10,
|
173
|
'#weight' => 2.1,
|
174
|
'#parents' => array('extra', 'min'),
|
175
|
'#element_validate' => array('_webform_edit_number_validate'),
|
176
|
);
|
177
|
$form['validation']['max'] = array(
|
178
|
'#type' => 'textfield',
|
179
|
'#title' => t('Maximum'),
|
180
|
'#default_value' => $component['extra']['max'],
|
181
|
'#description' => t('Maximum numeric value. This may also determine the display width of your field.'),
|
182
|
'#size' => 5,
|
183
|
'#maxlength' => 10,
|
184
|
'#weight' => 2.2,
|
185
|
'#parents' => array('extra', 'max'),
|
186
|
'#element_validate' => array('_webform_edit_number_validate'),
|
187
|
);
|
188
|
$form['validation']['step'] = array(
|
189
|
'#type' => 'textfield',
|
190
|
'#title' => t('Step'),
|
191
|
'#default_value' => $component['extra']['step'],
|
192
|
'#description' => t('Limit options to a specific increment. e.g. a step of "5" would allow values 5, 10, 15, etc.'),
|
193
|
'#size' => 5,
|
194
|
'#maxlength' => 10,
|
195
|
'#weight' => 3,
|
196
|
'#parents' => array('extra', 'step'),
|
197
|
'#element_validate' => array('_webform_edit_number_validate'),
|
198
|
);
|
199
|
// Analysis settings.
|
200
|
$form['analysis'] = array(
|
201
|
'#type' => 'fieldset',
|
202
|
'#title' => t('Analysis'),
|
203
|
'#collapsible' => TRUE,
|
204
|
'#collapsed' => FALSE,
|
205
|
'#weight' => 10,
|
206
|
);
|
207
|
$form['analysis']['excludezero'] = array(
|
208
|
'#type' => 'checkbox',
|
209
|
'#title' => t('Exclude zero'),
|
210
|
'#return_value' => 1,
|
211
|
'#description' => t('Exclude entries of zero (or blank) when counting submissions to calculate average and standard deviation.'),
|
212
|
'#weight' => 1.5,
|
213
|
'#default_value' => $component['extra']['excludezero'],
|
214
|
'#parents' => array('extra', 'excludezero'),
|
215
|
);
|
216
|
return $form;
|
217
|
}
|
218
|
|
219
|
/**
|
220
|
* Theme function to render a number component.
|
221
|
*/
|
222
|
function theme_webform_number($variables) {
|
223
|
$element = $variables['element'];
|
224
|
|
225
|
// This IF statement is mostly in place to allow our tests to set type="text"
|
226
|
// because SimpleTest does not support type="number".
|
227
|
if (!isset($element['#attributes']['type'])) {
|
228
|
$element['#attributes']['type'] = 'number';
|
229
|
}
|
230
|
|
231
|
// Step property *must* be a full number with 0 prefix if a decimal.
|
232
|
if (!empty($element['#step']) && !is_int($element['#step'] * 1)) {
|
233
|
$decimals = strlen($element['#step']) - strrpos($element['#step'], '.') - 1;
|
234
|
$element['#step'] = sprintf('%1.' . $decimals . 'F', $element['#step']);
|
235
|
}
|
236
|
|
237
|
// If the number is not an integer and step is undefined/empty, set the "any"
|
238
|
// value to allow any decimal.
|
239
|
if (empty($element['#integer']) && empty($element['#step'])) {
|
240
|
$element['#step'] = 'any';
|
241
|
}
|
242
|
elseif ($element['#integer'] && empty($element['#step'])) {
|
243
|
$element['#step'] = 1;
|
244
|
}
|
245
|
|
246
|
// Convert properties to attributes on the element if set.
|
247
|
foreach (array('id', 'name', 'value', 'size', 'min', 'max', 'step') as $property) {
|
248
|
if (isset($element['#' . $property]) && $element['#' . $property] !== '') {
|
249
|
$element['#attributes'][$property] = $element['#' . $property];
|
250
|
}
|
251
|
}
|
252
|
_form_set_class($element, array('form-text', 'form-number'));
|
253
|
|
254
|
return '<input' . drupal_attributes($element['#attributes']) . ' />';
|
255
|
}
|
256
|
|
257
|
/**
|
258
|
* Implements _webform_render_component().
|
259
|
*/
|
260
|
function _webform_render_number($component, $value = NULL, $filter = TRUE) {
|
261
|
$node = isset($component['nid']) ? node_load($component['nid']) : NULL;
|
262
|
|
263
|
$element = array(
|
264
|
'#title' => $filter ? _webform_filter_xss($component['name']) : $component['name'],
|
265
|
'#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
|
266
|
'#default_value' => $filter ? _webform_filter_values($component['value'], $node, NULL, NULL, FALSE) : $component['value'],
|
267
|
'#required' => $component['mandatory'],
|
268
|
'#weight' => $component['weight'],
|
269
|
'#field_prefix' => empty($component['extra']['field_prefix']) ? NULL : ($filter ? _webform_filter_xss($component['extra']['field_prefix']) : $component['extra']['field_prefix']),
|
270
|
'#field_suffix' => empty($component['extra']['field_suffix']) ? NULL : ($filter ? _webform_filter_xss($component['extra']['field_suffix']) : $component['extra']['field_suffix']),
|
271
|
'#description' => $filter ? _webform_filter_descriptions($component['extra']['description'], $node) : $component['extra']['description'],
|
272
|
'#attributes' => $component['extra']['attributes'],
|
273
|
'#element_validate' => array('_webform_validate_number'),
|
274
|
'#theme_wrappers' => array('webform_element'),
|
275
|
'#min' => $component['extra']['min'],
|
276
|
'#max' => $component['extra']['max'],
|
277
|
'#step' => $component['extra']['step'] ? abs($component['extra']['step']) : '',
|
278
|
'#integer' => $component['extra']['integer'],
|
279
|
'#point' => $component['extra']['point'],
|
280
|
'#separator' => $component['extra']['separator'],
|
281
|
'#decimals' => $component['extra']['decimals'],
|
282
|
'#translatable' => array('title', 'description', 'field_prefix', 'field_suffix'),
|
283
|
);
|
284
|
|
285
|
// Set the decimal count to zero for integers.
|
286
|
if ($element['#integer'] && $element['#decimals'] === '') {
|
287
|
$element['#decimals'] = 0;
|
288
|
}
|
289
|
|
290
|
// Flip the min and max properties to make min less than max if needed.
|
291
|
if ($element['#min'] !== '' && $element['#max'] !== '' && $element['#min'] > $element['#max']) {
|
292
|
$max = $element['#min'];
|
293
|
$element['#min'] = $element['#max'];
|
294
|
$element['#max'] = $max;
|
295
|
}
|
296
|
|
297
|
// Ensure #step starts with a zero if a decimal.
|
298
|
if (!is_int($element['#step'] * 1)) {
|
299
|
$decimals = strlen($element['#step']) - strrpos($element['#step'], '.') - 1;
|
300
|
$element['#step'] = sprintf('%1.' . $decimals . 'F', $element['#step']);
|
301
|
}
|
302
|
|
303
|
if ($component['extra']['type'] == 'textfield') {
|
304
|
// Render as textfield.
|
305
|
$element['#type'] = 'webform_number';
|
306
|
|
307
|
// Set the size property based on #max, to ensure consistent behavior for
|
308
|
// browsers that do not support type = number.
|
309
|
if ($element['#max']) {
|
310
|
$element['#size'] = strlen($element['#max']) + 1;
|
311
|
}
|
312
|
}
|
313
|
else {
|
314
|
// Render as select.
|
315
|
$element['#type'] = 'select';
|
316
|
|
317
|
// Create user-specified options list as an array.
|
318
|
$element['#options'] = _webform_number_select_options($component);
|
319
|
|
320
|
// Add default options if using a select list with no default. This trigger's
|
321
|
// Drupal 7's adding of the option for us. See form_process_select().
|
322
|
if ($component['extra']['type'] == 'select' && $element['#default_value'] === '') {
|
323
|
$element['#empty_value'] = '';
|
324
|
}
|
325
|
}
|
326
|
|
327
|
// Set user-entered values.
|
328
|
if (isset($value[0])) {
|
329
|
// If the value has been standardized, convert it to the expected format
|
330
|
// for display to the user.
|
331
|
if (webform_number_format_match($value[0], '.', '')) {
|
332
|
$element['#default_value'] = _webform_number_format($component, $value[0]);
|
333
|
}
|
334
|
// Otherwise use the user-defined input.
|
335
|
else {
|
336
|
$element['#default_value'] = $value[0];
|
337
|
}
|
338
|
}
|
339
|
|
340
|
// Enforce uniqueness.
|
341
|
if ($component['extra']['unique']) {
|
342
|
$element['#element_validate'][] = 'webform_validate_unique';
|
343
|
}
|
344
|
|
345
|
// Set readonly if disabled.
|
346
|
if ($component['extra']['disabled']) {
|
347
|
if ($filter) {
|
348
|
$element['#attributes']['readonly'] = 'readonly';
|
349
|
}
|
350
|
else {
|
351
|
$element['#disabled'] = TRUE;
|
352
|
}
|
353
|
}
|
354
|
|
355
|
return $element;
|
356
|
}
|
357
|
|
358
|
/**
|
359
|
* Implements _webform_display_component().
|
360
|
*/
|
361
|
function _webform_display_number($component, $value, $format = 'html') {
|
362
|
$empty = !isset($value[0]) || $value[0] === '';
|
363
|
return array(
|
364
|
'#title' => $component['name'],
|
365
|
'#weight' => $component['weight'],
|
366
|
'#theme' => 'webform_display_number',
|
367
|
'#theme_wrappers' => $format == 'html' ? array('webform_element') : array('webform_element_text'),
|
368
|
'#field_prefix' => $empty ? '' : $component['extra']['field_prefix'],
|
369
|
'#field_suffix' => $empty ? '' : $component['extra']['field_suffix'],
|
370
|
'#format' => $format,
|
371
|
'#value' => $empty ? '' : _webform_number_format($component, $value[0]),
|
372
|
'#translatable' => array('title'),
|
373
|
);
|
374
|
}
|
375
|
|
376
|
/**
|
377
|
* Format the output of data for this component.
|
378
|
*/
|
379
|
function theme_webform_display_number($variables) {
|
380
|
$element = $variables['element'];
|
381
|
$prefix = $element['#format'] == 'html' ? '' : $element['#field_prefix'];
|
382
|
$suffix = $element['#format'] == 'html' ? '' : $element['#field_suffix'];
|
383
|
$value = $element['#format'] == 'html' ? check_plain($element['#value']) : $element['#value'];
|
384
|
return $value !== '' ? ($prefix . $value . $suffix) : ' ';
|
385
|
}
|
386
|
|
387
|
/**
|
388
|
* Implements _webform_analysis_component().
|
389
|
*/
|
390
|
function _webform_analysis_number($component, $sids = array(), $single = FALSE) {
|
391
|
$advanced_stats = $single;
|
392
|
|
393
|
$query = db_select('webform_submitted_data', 'wsd', array('fetch' => PDO::FETCH_ASSOC))
|
394
|
->fields('wsd', array('data'))
|
395
|
->condition('nid', $component['nid'])
|
396
|
->condition('cid', $component['cid']);
|
397
|
|
398
|
if (count($sids)) {
|
399
|
$query->condition('sid', $sids, 'IN');
|
400
|
}
|
401
|
|
402
|
$population = array();
|
403
|
$submissions = 0;
|
404
|
$non_zero = 0;
|
405
|
$non_empty = 0;
|
406
|
$sum = 0;
|
407
|
|
408
|
$result = $query->execute();
|
409
|
foreach ($result as $data) {
|
410
|
$value = trim($data['data']);
|
411
|
$number = (float)$value;
|
412
|
$non_empty += (integer)($value !== '');
|
413
|
$non_zero += (integer)($number != 0.0);
|
414
|
$sum += $number;
|
415
|
$population[] = $number;
|
416
|
$submissions++;
|
417
|
}
|
418
|
sort($population, SORT_NUMERIC);
|
419
|
|
420
|
// Average and population count.
|
421
|
if ($component['extra']['excludezero']) {
|
422
|
$average = $non_zero ? ($sum / $non_zero) : 0;
|
423
|
$average_title = t('Average !mu excluding zeros/blanks', array('!mu' => $advanced_stats ? '(μ)' : ''));
|
424
|
// Sample (sub-set of total population).
|
425
|
$population_count = $non_zero - 1;
|
426
|
$sigma = 'sd';
|
427
|
$description = t('sample');
|
428
|
}
|
429
|
else {
|
430
|
$average = $submissions ? ($sum / $submissions) : 0;
|
431
|
$average_title = t('Average !mu including zeros/blanks', array('!mu' => $advanced_stats ? '(μ)' : ''));
|
432
|
// Population.
|
433
|
$population_count = $submissions;
|
434
|
$sigma = 'σ';
|
435
|
$description = t('population');
|
436
|
}
|
437
|
|
438
|
// Formatting.
|
439
|
$average = _webform_number_format($component, $average);
|
440
|
$sum = _webform_number_format($component, $sum);
|
441
|
|
442
|
$rows[0] = array(t('Zero/blank'), ($submissions - $non_zero));
|
443
|
$rows[1] = array(t('User entered value'), $non_empty);
|
444
|
$rows[2] = array(t('Sum') . ($advanced_stats ? ' (Σ)' : ''), $sum);
|
445
|
$rows[3] = array($average_title, $average);
|
446
|
|
447
|
if (!$advanced_stats && $sum != 0) {
|
448
|
$rows[4] = array('', l(t('More stats »'), 'node/' . $component['nid'] . '/webform-results/analysis/' . $component['cid']));
|
449
|
}
|
450
|
|
451
|
// Normal distribution information.
|
452
|
if ($advanced_stats && $population_count && $sum != 0) {
|
453
|
// Standard deviation.
|
454
|
$stddev = 0;
|
455
|
foreach($population as $value) {
|
456
|
// Obtain the total of squared variances.
|
457
|
$stddev += pow(($value - $average), 2);
|
458
|
}
|
459
|
if ($population_count > 0) {
|
460
|
$stddev = sqrt($stddev / $population_count);
|
461
|
}
|
462
|
else {
|
463
|
$stddev = sqrt($stddev);
|
464
|
}
|
465
|
|
466
|
// Skip the rest of the distribution rows if standard deviation is 0.
|
467
|
if (empty($stddev)) {
|
468
|
return $rows;
|
469
|
}
|
470
|
|
471
|
// Build normal distribution table rows.
|
472
|
$count = array();
|
473
|
$percent = array();
|
474
|
$limit = array();
|
475
|
$index = 0;
|
476
|
|
477
|
$count[] = 0;
|
478
|
$limit[] = $average - ($stddev * 4);
|
479
|
foreach ($population as $value) {
|
480
|
while ($value >= $limit[$index]) {
|
481
|
$percent[] = number_format($count[$index] / $population_count * 100, 2, '.', '');
|
482
|
$limit[] = $limit[$index] + $stddev;
|
483
|
$index += 1;
|
484
|
if ($limit[$index] == $average) {
|
485
|
$limit[$index] = $limit[$index] + $stddev;
|
486
|
}
|
487
|
$count[$index] = 0;
|
488
|
}
|
489
|
$count[$index] += 1;
|
490
|
}
|
491
|
$percent[] = number_format($count[$index] / $population_count * 100, 2, '.', '');
|
492
|
|
493
|
// Format normal distribution table output.
|
494
|
$stddev = _webform_number_format($component, $stddev);
|
495
|
$low = _webform_number_format($component, $population[0]);
|
496
|
$high = _webform_number_format($component, end($population));
|
497
|
foreach($limit as $key => $value) {
|
498
|
$limit[$key] = _webform_number_format($component, $value);
|
499
|
}
|
500
|
|
501
|
// Column headings (override potential theme uppercase, e.g. Seven in D7).
|
502
|
$header = array(
|
503
|
t('Normal Distribution'),
|
504
|
array('data' => '-4' . $sigma, 'style' => 'text-transform: lowercase;',),
|
505
|
array('data' => '-3' . $sigma, 'style' => 'text-transform: lowercase;',),
|
506
|
array('data' => '-2' . $sigma, 'style' => 'text-transform: lowercase;',),
|
507
|
array('data' => '-1' . $sigma, 'style' => 'text-transform: lowercase;',),
|
508
|
array('data' => '+1' . $sigma, 'style' => 'text-transform: lowercase;',),
|
509
|
array('data' => '+2' . $sigma, 'style' => 'text-transform: lowercase;',),
|
510
|
array('data' => '+3' . $sigma, 'style' => 'text-transform: lowercase;',),
|
511
|
array('data' => '+4' . $sigma, 'style' => 'text-transform: lowercase;',),
|
512
|
);
|
513
|
|
514
|
// Insert row labels.
|
515
|
array_unshift($limit, t('Boundary'));
|
516
|
array_unshift($count, t('Count'));
|
517
|
array_unshift($percent, t('% of !description', array('!description' => $description)));
|
518
|
|
519
|
$output = theme('table', array('header' => $header, 'rows' => array($limit, $count, $percent)));
|
520
|
|
521
|
$rows[4] = array(t('Range'), t('!low to !high', array('!low' => $low, '!high' => $high)));
|
522
|
$rows[5] = array(t('Standard deviation (!sigma)', array('!sigma' => $sigma)), $stddev);
|
523
|
$rows[6] = array(array('data' => $output, 'colspan' => 2));
|
524
|
}
|
525
|
|
526
|
return $rows;
|
527
|
}
|
528
|
|
529
|
/**
|
530
|
* Implements _webform_table_component().
|
531
|
*/
|
532
|
function _webform_table_number($component, $value) {
|
533
|
return isset($value[0]) ? _webform_number_format($component, $value[0]) : '';
|
534
|
}
|
535
|
|
536
|
/**
|
537
|
* Implements _webform_csv_headers_component().
|
538
|
*/
|
539
|
function _webform_csv_headers_number($component, $export_options) {
|
540
|
$header = array();
|
541
|
$header[0] = '';
|
542
|
$header[1] = '';
|
543
|
$header[2] = $component['name'];
|
544
|
return $header;
|
545
|
}
|
546
|
|
547
|
/**
|
548
|
* Implements _webform_csv_data_component().
|
549
|
*/
|
550
|
function _webform_csv_data_number($component, $export_options, $value) {
|
551
|
if (isset($value[0]) && is_numeric($value[0]) && $component['extra']['decimals'] !== '') {
|
552
|
$value[0] = number_format($value[0], $component['extra']['decimals'], '.', '');
|
553
|
}
|
554
|
return isset($value[0]) ? $value[0] : '';
|
555
|
}
|
556
|
|
557
|
/**
|
558
|
* A Drupal Form API Validation function. Validates the entered values from
|
559
|
* number components on the client-side form.
|
560
|
*
|
561
|
* @param $element
|
562
|
* The form element. May either be a select or a webform_number element.
|
563
|
* @param $form_state
|
564
|
* The full form state for the webform.
|
565
|
* @return
|
566
|
* None. Calls a form_set_error if the number is not valid.
|
567
|
*/
|
568
|
function _webform_validate_number($element, &$form_state) {
|
569
|
// Trim spaces for basic cleanup.
|
570
|
$value = trim($element['#value']);
|
571
|
form_set_value($element, $value, $form_state);
|
572
|
|
573
|
if ($value != '') {
|
574
|
// First check that the entered value matches the expected value.
|
575
|
if (!webform_number_format_match($value, $element['#point'], $element['#separator'])) {
|
576
|
form_error($element, t('%name field value must format numbers as "@example".', array('%name' => $element['#title'], '@example' => webform_number_format(12345.6789, $element['#decimals'], $element['#point'], $element['#separator']))));
|
577
|
return;
|
578
|
}
|
579
|
|
580
|
// Numeric test.
|
581
|
$numeric_value = webform_number_standardize($value, $element['#point']);
|
582
|
if (is_numeric($numeric_value)) {
|
583
|
// Range test.
|
584
|
if ($element['#min'] != '' && $element['#max'] != '') {
|
585
|
// Flip minimum and maximum if needed.
|
586
|
if ($element['#max'] > $element['#min']) {
|
587
|
$min = $element['#min'];
|
588
|
$max = $element['#max'];
|
589
|
}
|
590
|
else {
|
591
|
$min = $element['#max'];
|
592
|
$max = $element['#min'];
|
593
|
}
|
594
|
if ($numeric_value > $max || $numeric_value < $min) {
|
595
|
form_error($element, t('%name field value of @value should be in the range @min to @max.', array('%name' => $element['#title'], '@value' => $value, '@min' => $element['#min'], '@max' => $element['#max'])));
|
596
|
}
|
597
|
}
|
598
|
elseif ($element['#max'] != '' && $numeric_value > $element['#max']) {
|
599
|
form_error($element, t('%name field value must be less than @max.', array('%name' => $element['#title'], '@max' => $element['#max'])));
|
600
|
}
|
601
|
elseif ($element['#min'] != '' && $numeric_value < $element['#min']) {
|
602
|
form_error($element, t('%name field value must be greater than @min.', array('%name' => $element['#title'], '@min' => $element['#min'])));
|
603
|
}
|
604
|
|
605
|
// Integer test.
|
606
|
if ($element['#integer'] && !is_int($numeric_value * 1)) {
|
607
|
form_error($element, t('%name field value of @value must be an integer.', array('%name' => $element['#title'], '@value' => $value)));
|
608
|
}
|
609
|
|
610
|
// Step test.
|
611
|
$starting_number = $element['#min'] ? $element['#min'] : 0;
|
612
|
if ($element['#step'] != 0 && webform_modulo($numeric_value - $starting_number, $element['#step']) != 0) {
|
613
|
$samples = array(
|
614
|
$starting_number,
|
615
|
$starting_number + ($element['#step'] * 1),
|
616
|
$starting_number + ($element['#step'] * 2),
|
617
|
$starting_number + ($element['#step'] * 3),
|
618
|
);
|
619
|
if ($starting_number) {
|
620
|
form_error($element, t('%name field value must be @start plus a multiple of @step. i.e. @samples, etc.', array('%name' => $element['#title'], '@start' => $element['#min'], '@step' => $element['#step'], '@samples' => implode(', ', $samples))));
|
621
|
}
|
622
|
else {
|
623
|
form_error($element, t('%name field value must be a multiple of @step. i.e. @samples, etc.', array('%name' => $element['#title'], '@step' => $element['#step'], '@samples' => implode(', ', $samples))));
|
624
|
}
|
625
|
}
|
626
|
}
|
627
|
else {
|
628
|
form_error($element, t('%name field value of @value must be numeric.', array('%name' => $element['#title'], '@value' => $value)));
|
629
|
}
|
630
|
}
|
631
|
}
|
632
|
|
633
|
/**
|
634
|
* Implements _webform_submit_component().
|
635
|
*/
|
636
|
function _webform_submit_number($component, $value) {
|
637
|
// Because _webform_validate_number() ensures the format matches when moving
|
638
|
// forward through a form, this should always pass before saving into the
|
639
|
// database. When moving backwards in a form, do not adjust the value, since
|
640
|
// it has not yet been validated.
|
641
|
if (webform_number_format_match($value, $component['extra']['point'], $component['extra']['separator'])) {
|
642
|
$value = webform_number_standardize($value, $component['extra']['point']);
|
643
|
}
|
644
|
return $value;
|
645
|
}
|
646
|
|
647
|
/**
|
648
|
* Validation of number edit form items.
|
649
|
*/
|
650
|
function _webform_edit_number_validate($element, &$form_state) {
|
651
|
// Find the value of all related fields to this element.
|
652
|
$parents = $element['#parents'];
|
653
|
$key = array_pop($parents);
|
654
|
$values = $form_state['values'];
|
655
|
foreach ($parents as $parent) {
|
656
|
$values = $values[$parent];
|
657
|
}
|
658
|
|
659
|
switch ($key) {
|
660
|
case 'min':
|
661
|
if ($values['min'] == '') {
|
662
|
if (isset($values['type']) && $values['type'] === 'select') {
|
663
|
form_error($element, t('Minimum is required when using a select list element.'));
|
664
|
}
|
665
|
}
|
666
|
else {
|
667
|
if (!is_numeric($values['min'])) {
|
668
|
form_error($element, t('Minimum must be numeric.'));
|
669
|
}
|
670
|
if ($values['integer'] && !is_int($values['min'] * 1)) {
|
671
|
form_error($element, t('Minimum must have an integer value.'));
|
672
|
}
|
673
|
}
|
674
|
break;
|
675
|
case 'max':
|
676
|
if ($values['max'] == '') {
|
677
|
if (isset($values['type']) && $values['type'] === 'select') {
|
678
|
form_error($element, t('Maximum is required when using a select list element.'));
|
679
|
}
|
680
|
}
|
681
|
else {
|
682
|
if (!is_numeric($values['max'])) {
|
683
|
form_error($element, t('Maximum must be numeric.'));
|
684
|
}
|
685
|
if ($values['integer'] && !is_int($values['max'] * 1)) {
|
686
|
form_error($element, t('Maximum must have an integer value.'));
|
687
|
}
|
688
|
}
|
689
|
break;
|
690
|
case 'step':
|
691
|
if ($values['step'] !== '') {
|
692
|
if (!is_numeric($values['step'])) {
|
693
|
form_error($element, t('Step must be numeric.'));
|
694
|
}
|
695
|
else {
|
696
|
if ($values['integer'] && !is_int($values['step'] * 1)) {
|
697
|
form_error($element, t('Step must have an integer value.'));
|
698
|
}
|
699
|
}
|
700
|
}
|
701
|
break;
|
702
|
}
|
703
|
return TRUE;
|
704
|
}
|
705
|
|
706
|
/**
|
707
|
* Generate select list options.
|
708
|
*/
|
709
|
function _webform_number_select_options($component) {
|
710
|
$options = array();
|
711
|
$step = abs($component['extra']['step']);
|
712
|
|
713
|
// Step is optional and defaults to 1.
|
714
|
$step = empty($step) ? 1 : $step;
|
715
|
|
716
|
// Generate list in correct direction.
|
717
|
$min = $component['extra']['min'];
|
718
|
$max = $component['extra']['max'];
|
719
|
$flipped = FALSE;
|
720
|
if ($max < $min) {
|
721
|
$min = $component['extra']['max'];
|
722
|
$max = $component['extra']['min'];
|
723
|
$flipped = TRUE;
|
724
|
}
|
725
|
|
726
|
for ($f = $min; $f <= $max; $f += $step) {
|
727
|
$options[$f . ''] = $f . '';
|
728
|
}
|
729
|
|
730
|
// TODO: HTML5 browsers apparently do not include the max value if it does
|
731
|
// not line up with step. Restore this if needed in the future.
|
732
|
// Add end limit if it's been skipped due to step.
|
733
|
//if (end($options) != $max) {
|
734
|
// $options[$f] = $max;
|
735
|
//}
|
736
|
|
737
|
if ($flipped) {
|
738
|
$options = array_reverse($options, TRUE);
|
739
|
}
|
740
|
|
741
|
// Apply requisite number formatting.
|
742
|
foreach ($options as $key => $value) {
|
743
|
$options[$key] = _webform_number_format($component, $value);
|
744
|
}
|
745
|
|
746
|
return $options;
|
747
|
}
|
748
|
|
749
|
/**
|
750
|
* Apply number format based on a component and number value.
|
751
|
*/
|
752
|
function _webform_number_format($component, $value) {
|
753
|
return webform_number_format($value, $component['extra']['decimals'], $component['extra']['point'], $component['extra']['separator']);
|
754
|
}
|
755
|
|
756
|
/**
|
757
|
* Validates if a provided number string matches an expected format.
|
758
|
*
|
759
|
* This function allows the thousands separator to be optional, but decimal
|
760
|
* points must be in the right location.
|
761
|
*
|
762
|
* Based on http://stackoverflow.com/questions/5917082/regular-expression-to-match-numbers-with-or-without-commas-and-decimals-in-text.
|
763
|
*/
|
764
|
function webform_number_format_match($value, $point, $separator) {
|
765
|
return preg_match('/^(-? ?[1-9](?:\d{0,2})(?:' . ($separator ? (preg_quote($separator, '/') . '?') : '') . '\d{3})*(?:' . preg_quote($point, '/') . '\d*[0-9])?|0?' . preg_quote($point, '/') . '\d*[1-9]|0)$/', $value);
|
766
|
}
|
767
|
|
768
|
/**
|
769
|
* Format a number with thousands separator, decimal point, and decimal places.
|
770
|
*
|
771
|
* This function is a wrapper around PHP's native number_format(), but allows
|
772
|
* the decimal places parameter to be NULL or an empty string, resulting in a
|
773
|
* behavior of no change to the decimal places.
|
774
|
*/
|
775
|
function webform_number_format($value, $decimals = NULL, $point = '.', $separator = ',') {
|
776
|
if (!is_numeric($value)) {
|
777
|
return '';
|
778
|
}
|
779
|
|
780
|
// If no decimal places are specified, do a best guess length of decimals.
|
781
|
if (is_null($decimals) || $decimals === '') {
|
782
|
// If it's an integer, no decimals needed.
|
783
|
if (is_int(($value . '') * 1)) {
|
784
|
$decimals = 0;
|
785
|
}
|
786
|
else {
|
787
|
$decimals = strlen($value) - strrpos($value, '.') - 1;
|
788
|
}
|
789
|
if ($decimals > 4) {
|
790
|
$decimals = 4;
|
791
|
}
|
792
|
}
|
793
|
|
794
|
return number_format($value, $decimals, $point, $separator);
|
795
|
}
|
796
|
|
797
|
/**
|
798
|
* Given a number, convert it to string compatible with a PHP float.
|
799
|
*
|
800
|
* @param string $value
|
801
|
* The string value to be standardized into a numeric string.
|
802
|
* @param $point
|
803
|
* The point separator between the whole number and the decimals.
|
804
|
*/
|
805
|
function webform_number_standardize($value, $point) {
|
806
|
// For simplicity, strip everything that's not the decimal point.
|
807
|
$value = preg_replace('/[^\-0-9' . preg_quote($point, '/') . ']/', '', $value);
|
808
|
// Convert the decimal point to a period.
|
809
|
$value = str_replace($point, '.', $value);
|
810
|
return $value;
|
811
|
}
|
812
|
|
813
|
/**
|
814
|
* Custom modulo function that properly handles float division.
|
815
|
*
|
816
|
* See https://drupal.org/node/1601968.
|
817
|
*/
|
818
|
function webform_modulo($a, $b) {
|
819
|
return $a - $b * (($b < 0) ? ceil($a / $b) : floor($a / $b));
|
820
|
}
|