From d637a570f17cf19aada769f4a1f3485570375295 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Thu, 25 Mar 2021 15:52:51 +0100 Subject: [PATCH] LSattr_html::valueWithUnit: fix handling zero value --- .../class.LSformElement_valueWithUnit.php | 25 +++++++++++++------ .../LSformElement_valueWithUnit_field.tpl | 2 +- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/includes/class/class.LSformElement_valueWithUnit.php b/src/includes/class/class.LSformElement_valueWithUnit.php index 9f3aa681..0b140396 100644 --- a/src/includes/class/class.LSformElement_valueWithUnit.php +++ b/src/includes/class/class.LSformElement_valueWithUnit.php @@ -90,16 +90,25 @@ class LSformElement_valueWithUnit extends LSformElement { if (!$details) return $infos['value']; $units = $this -> getUnits(); - if ($units) { - foreach($units as $sill => $label) { - if ($infos['value'] >= $sill) { - $infos['valueWithUnit'] = $this -> formatNumber($infos['value']/$sill); - $infos['unitSill'] = $sill; - $infos['unitLabel'] = $label; - break; - } + if (!$units) + return $infos; + if ($infos['value'] == 0) { + ksort($units); + $infos['valueWithUnit'] = $this -> formatNumber(0); + $infos['unitSill'] = key($units); + $infos['unitLabel'] = $units[$infos['unitSill']]; + return $infos; + } + + foreach($units as $sill => $label) { + if ($infos['value'] >= $sill) { + $infos['valueWithUnit'] = $this -> formatNumber($infos['value']/$sill); + $infos['unitSill'] = $sill; + $infos['unitLabel'] = $label; + break; } } + return $infos; } return false; diff --git a/src/templates/default/LSformElement_valueWithUnit_field.tpl b/src/templates/default/LSformElement_valueWithUnit_field.tpl index 87f26c17..77b3cb4e 100644 --- a/src/templates/default/LSformElement_valueWithUnit_field.tpl +++ b/src/templates/default/LSformElement_valueWithUnit_field.tpl @@ -13,7 +13,7 @@ {$noValueTxt|escape:"htmlall"} {/if} {else} - {if $values_and_units[$value].valueWithUnit || !$values_and_units[$value]} + {if isset($values_and_units[$value].valueWithUnit) || !$values_and_units[$value]}