Révision ca0757b9
Ajouté par Assos Assos il y a plus de 9 ans
drupal7/sites/all/modules/webform/components/number.inc | ||
---|---|---|
276 | 276 |
'#max' => $component['extra']['max'], |
277 | 277 |
'#step' => $component['extra']['step'] ? abs($component['extra']['step']) : '', |
278 | 278 |
'#integer' => $component['extra']['integer'], |
279 |
'#translatable' => array('title', 'description'), |
|
279 |
'#point' => $component['extra']['point'], |
|
280 |
'#separator' => $component['extra']['separator'], |
|
281 |
'#decimals' => $component['extra']['decimals'], |
|
282 |
'#translatable' => array('title', 'description', 'field_prefix', 'field_suffix'), |
|
280 | 283 |
); |
281 | 284 |
|
285 |
// Set the decimal count to zero for integers. |
|
286 |
if ($element['#integer'] && $element['#decimals'] === '') { |
|
287 |
$element['#decimals'] = 0; |
|
288 |
} |
|
289 |
|
|
282 | 290 |
// Flip the min and max properties to make min less than max if needed. |
283 | 291 |
if ($element['#min'] !== '' && $element['#max'] !== '' && $element['#min'] > $element['#max']) { |
284 | 292 |
$max = $element['#min']; |
... | ... | |
318 | 326 |
|
319 | 327 |
// Set user-entered values. |
320 | 328 |
if (isset($value[0])) { |
321 |
$element['#default_value'] = $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 |
} |
|
322 | 338 |
} |
323 | 339 |
|
324 | 340 |
// Enforce uniqueness. |
... | ... | |
385 | 401 |
|
386 | 402 |
$population = array(); |
387 | 403 |
$submissions = 0; |
388 |
$nonzero = 0; |
|
389 |
$not_empty = 0;
|
|
404 |
$non_zero = 0;
|
|
405 |
$non_empty = 0;
|
|
390 | 406 |
$sum = 0; |
391 | 407 |
|
392 | 408 |
$result = $query->execute(); |
393 | 409 |
foreach ($result as $data) { |
394 | 410 |
$value = trim($data['data']); |
395 |
if ($value == '') { |
|
396 |
$number = 0.0; |
|
397 |
} |
|
398 |
else { |
|
399 |
$number = $value * 1.0; |
|
400 |
} |
|
401 |
|
|
402 |
if ($value !== '') { |
|
403 |
$not_empty++; |
|
404 |
} |
|
405 |
|
|
406 |
if ($number > 0) { |
|
407 |
$nonzero++; |
|
408 |
$sum += $number; |
|
409 |
} |
|
411 |
$number = (float)$value; |
|
412 |
$non_empty += (integer)($value !== ''); |
|
413 |
$non_zero += (integer)($number != 0.0); |
|
414 |
$sum += $number; |
|
410 | 415 |
$population[] = $number; |
411 | 416 |
$submissions++; |
412 | 417 |
} |
... | ... | |
414 | 419 |
|
415 | 420 |
// Average and population count. |
416 | 421 |
if ($component['extra']['excludezero']) { |
417 |
$average = $nonzero ? ($sum / $nonzero) : 0;
|
|
422 |
$average = $non_zero ? ($sum / $non_zero) : 0;
|
|
418 | 423 |
$average_title = t('Average !mu excluding zeros/blanks', array('!mu' => $advanced_stats ? '(μ)' : '')); |
419 | 424 |
// Sample (sub-set of total population). |
420 |
$population_count = $nonzero - 1; |
|
425 |
$population_count = $non_zero - 1;
|
|
421 | 426 |
$sigma = 'sd'; |
422 | 427 |
$description = t('sample'); |
423 | 428 |
} |
... | ... | |
434 | 439 |
$average = _webform_number_format($component, $average); |
435 | 440 |
$sum = _webform_number_format($component, $sum); |
436 | 441 |
|
437 |
$rows[0] = array(t('Zero/blank'), ($submissions - $nonzero)); |
|
438 |
$rows[1] = array(t('User entered value'), $not_empty);
|
|
442 |
$rows[0] = array(t('Zero/blank'), ($submissions - $non_zero));
|
|
443 |
$rows[1] = array(t('User entered value'), $non_empty);
|
|
439 | 444 |
$rows[2] = array(t('Sum') . ($advanced_stats ? ' (Σ)' : ''), $sum); |
440 | 445 |
$rows[3] = array($average_title, $average); |
441 | 446 |
|
... | ... | |
561 | 566 |
* None. Calls a form_set_error if the number is not valid. |
562 | 567 |
*/ |
563 | 568 |
function _webform_validate_number($element, &$form_state) { |
569 |
// Trim spaces for basic cleanup. |
|
564 | 570 |
$value = trim($element['#value']); |
565 | 571 |
form_set_value($element, $value, $form_state); |
566 | 572 |
|
567 | 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 |
|
|
568 | 580 |
// Numeric test. |
569 |
if (is_numeric($value)) { |
|
581 |
$numeric_value = webform_number_standardize($value, $element['#point']); |
|
582 |
if (is_numeric($numeric_value)) { |
|
570 | 583 |
// Range test. |
571 | 584 |
if ($element['#min'] != '' && $element['#max'] != '') { |
572 | 585 |
// Flip minimum and maximum if needed. |
... | ... | |
578 | 591 |
$min = $element['#max']; |
579 | 592 |
$max = $element['#min']; |
580 | 593 |
} |
581 |
if ($value > $max || $value < $min) {
|
|
582 |
form_error($element, t('%name field value of @value should be in the range @min to @max.', array('%name' => $element['#title'], '@value' => $value, '@min' => $min, '@max' => $max)));
|
|
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'])));
|
|
583 | 596 |
} |
584 | 597 |
} |
585 |
elseif ($element['#max'] != '' && $value > $element['#max']) { |
|
598 |
elseif ($element['#max'] != '' && $numeric_value > $element['#max']) {
|
|
586 | 599 |
form_error($element, t('%name field value must be less than @max.', array('%name' => $element['#title'], '@max' => $element['#max']))); |
587 | 600 |
} |
588 |
elseif ($element['#min'] != '' && $value < $element['#min']) { |
|
601 |
elseif ($element['#min'] != '' && $numeric_value < $element['#min']) {
|
|
589 | 602 |
form_error($element, t('%name field value must be greater than @min.', array('%name' => $element['#title'], '@min' => $element['#min']))); |
590 | 603 |
} |
591 | 604 |
|
592 | 605 |
// Integer test. |
593 |
if ($element['#integer'] && !is_int($value * 1)) { |
|
606 |
if ($element['#integer'] && !is_int($numeric_value * 1)) {
|
|
594 | 607 |
form_error($element, t('%name field value of @value must be an integer.', array('%name' => $element['#title'], '@value' => $value))); |
595 | 608 |
} |
596 | 609 |
|
597 | 610 |
// Step test. |
598 | 611 |
$starting_number = $element['#min'] ? $element['#min'] : 0; |
599 |
if ($element['#step'] != 0 && webform_modulo($element['#value'] - $starting_number, $element['#step']) != 0) {
|
|
612 |
if ($element['#step'] != 0 && webform_modulo($numeric_value - $starting_number, $element['#step']) != 0) {
|
|
600 | 613 |
$samples = array( |
601 | 614 |
$starting_number, |
602 | 615 |
$starting_number + ($element['#step'] * 1), |
... | ... | |
615 | 628 |
form_error($element, t('%name field value of @value must be numeric.', array('%name' => $element['#title'], '@value' => $value))); |
616 | 629 |
} |
617 | 630 |
} |
631 |
} |
|
618 | 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; |
|
619 | 645 |
} |
620 | 646 |
|
621 | 647 |
/** |
... | ... | |
721 | 747 |
} |
722 | 748 |
|
723 | 749 |
/** |
724 |
* Apply number format. |
|
750 |
* Apply number format based on a component and number value.
|
|
725 | 751 |
*/ |
726 | 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 = ',') { |
|
727 | 776 |
if (!is_numeric($value)) { |
728 | 777 |
return ''; |
729 | 778 |
} |
779 |
|
|
730 | 780 |
// If no decimal places are specified, do a best guess length of decimals. |
731 |
$decimals = $component['extra']['decimals']; |
|
732 |
if ($decimals === '') { |
|
781 |
if (is_null($decimals) || $decimals === '') { |
|
733 | 782 |
// If it's an integer, no decimals needed. |
734 | 783 |
if (is_int(($value . '') * 1)) { |
735 | 784 |
$decimals = 0; |
... | ... | |
742 | 791 |
} |
743 | 792 |
} |
744 | 793 |
|
745 |
return number_format($value, $decimals, $component['extra']['point'], $component['extra']['separator']); |
|
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; |
|
746 | 811 |
} |
747 | 812 |
|
748 | 813 |
/** |
Formats disponibles : Unified diff
Weekly update of contrib modules