Unverified Commit b7457cbb authored by Stanislas's avatar Stanislas Committed by GitHub
Browse files

fix(display): fix display number of days (#9131)



* fix(display): fix display number of days

* fix(core):do not replace empty space

* feat(core): implement rawnumber

* feat(core): change sprintf arg

* Fix decimal handling

* Fix non numeric handling
Co-authored-by: default avatarCédric Anne <cedric.anne@gmail.com>
parent 397781b4
......@@ -4556,13 +4556,13 @@ class CommonDBTM extends CommonGLPI {
return $searchoptions['toadd'][$value];
}
if ($options['html']) {
return Dropdown::getValueWithUnit(Html::formatNumber($value, false, 0), $unit);
return Dropdown::getValueWithUnit($value, $unit);
}
return $value;
case "decimal" :
if ($options['html']) {
return Dropdown::getValueWithUnit(Html::formatNumber($value), $unit);
return Dropdown::getValueWithUnit($value, $unit, $CFG_GLPI["decimal_number"]);
}
return $value;
......
......@@ -1511,53 +1511,57 @@ class Dropdown {
*
* @since 0.84
*
* @param $value integer number of item
* @param $unit string of unit (maybe year, month, day, hour, % for standard management)
* @param integer $value numeric value
* @param string $unit unit (maybe year, month, day, hour, % for standard management)
* @param integer $decimals number of decimal
**/
static function getValueWithUnit($value, $unit) {
static function getValueWithUnit($value, $unit, $decimals = 0) {
$formatted_number = is_numeric($value)
? Html::formatNumber($value, false, $decimals)
: $value;
if (strlen($unit) == 0) {
return $value;
return $formatted_number;
}
switch ($unit) {
case 'year' :
//TRANS: %d is a number of years
return sprintf(_n('%d year', '%d years', $value), $value);
//TRANS: %s is a number of years
return sprintf(_n('%s year', '%s years', $value), $formatted_number);
case 'month' :
//TRANS: %d is a number of months
return sprintf(_n('%d month', '%d months', $value), $value);
//TRANS: %s is a number of months
return sprintf(_n('%s month', '%s months', $value), $formatted_number);
case 'day' :
//TRANS: %d is a number of days
return sprintf(_n('%d day', '%d days', $value), $value);
//TRANS: %s is a number of days
return sprintf(_n('%s day', '%s days', $value), $formatted_number);
case 'hour' :
//TRANS: %d is a number of hours
return sprintf(_n('%d hour', '%d hours', $value), $value);
//TRANS: %s is a number of hours
return sprintf(_n('%s hour', '%s hours', $value), $formatted_number);
case 'minute' :
//TRANS: %d is a number of minutes
return sprintf(_n('%d minute', '%d minutes', $value), $value);
//TRANS: %s is a number of minutes
return sprintf(_n('%s minute', '%s minutes', $value), $formatted_number);
case 'second' :
//TRANS: %d is a number of seconds
return sprintf(_n('%d second', '%d seconds', $value), $value);
//TRANS: %s is a number of seconds
return sprintf(_n('%s second', '%s seconds', $value), $formatted_number);
case 'millisecond' :
//TRANS: %d is a number of milliseconds
return sprintf(_n('%d millisecond', '%d milliseconds', $value), $value);
//TRANS: %s is a number of milliseconds
return sprintf(_n('%s millisecond', '%s milliseconds', $value), $formatted_number);
case 'auto':
$value = str_replace([' ', '&nbsp;'], ['', ''], $value); // unformat value
return Toolbox::getSize($value*1024*1024);
return Toolbox::getSize($value*1024*1024);
case '%' :
return sprintf(__('%d%%'), $value);
return sprintf(__('%s%%'), $formatted_number);
default :
return sprintf(__('%1$s %2$s'), $value, $unit);
return sprintf(__('%1$s %2$s'), $formatted_number, $unit);
}
}
......
......@@ -6412,9 +6412,7 @@ JAVASCRIPT;
&& isset($so['toadd'][$data[$ID][$k]['name']])) {
$out .= $so['toadd'][$data[$ID][$k]['name']];
} else {
$number = str_replace(' ', '&nbsp;',
Html::formatNumber($data[$ID][$k]['name'], false, 0));
$out .= Dropdown::getValueWithUnit($number, $unit);
$out .= Dropdown::getValueWithUnit($data[$ID][$k]['name'], $unit);
}
}
}
......@@ -6434,9 +6432,7 @@ JAVASCRIPT;
&& isset($so['toadd'][$data[$ID][$k]['name']])) {
$out .= $so['toadd'][$data[$ID][$k]['name']];
} else {
$number = str_replace(' ', '&nbsp;',
Html::formatNumber($data[$ID][$k]['name']));
$out .= Dropdown::getValueWithUnit($number, $unit);
$out .= Dropdown::getValueWithUnit($data[$ID][$k]['name'], $unit, $CFG_GLPI["decimal_number"]);
}
}
}
......
......@@ -275,34 +275,43 @@ class Dropdown extends DbTestCase {
public function dataGetValueWithUnit() {
return [
[1, 'auto', '1024 Kio'],
[1025, 'auto', '1 Gio'],
['1 025', 'auto', '1 Gio'],
[1, 'year', '1 year'],
[2, 'year', '2 years'],
[3, '%', '3%'],
['foo', 'bar', 'foo bar'],
[1, 'month', '1 month'],
[2, 'month', '2 months'],
['any', '', 'any'],
[1, 'day', '1 day'],
[2, 'day', '2 days'],
[1, 'hour', '1 hour'],
[2, 'hour', '2 hours'],
[1, 'minute', '1 minute'],
[2, 'minute', '2 minutes'],
[1, 'second', '1 second'],
[2, 'second', '2 seconds'],
[1, 'millisecond', '1 millisecond'],
[2, 'millisecond', '2 milliseconds'],
[1, 'auto', null, '1024 Kio'],
[1, 'auto', null, '1024 Kio'],
[1025, 'auto', null, '1 Gio'],
[1, 'year', null, '1 year'],
[2, 'year', null, '2 years'],
[3, '%', null, '3%'],
['foo', 'bar', null, 'foo bar'],
[1, 'month', null, '1 month'],
[2, 'month', null, '2 months'],
['any', '', null, 'any'],
[1, 'day', null, '1 day'],
[2, 'day', null, '2 days'],
[1, 'hour', null, '1 hour'],
[2, 'hour', null, '2 hours'],
[1, 'minute', null, '1 minute'],
[2, 'minute', null, '2 minutes'],
[1, 'second', null, '1 second'],
[2, 'second', null, '2 seconds'],
[1, 'millisecond', null, '1 millisecond'],
[2, 'millisecond', null, '2 milliseconds'],
[10, 'bar', null, '10 bar'],
[3.3597, '%', 0, '3%'],
[3.3597, '%', 2, '3.36%'],
[3.3597, '%', 6, '3.359700%'],
[3579, 'day', 0, '3&nbsp;579 days'],
];
}
/**
* @dataProvider dataGetValueWithUnit
*/
public function testGetValueWithUnit($input, $unit, $expected) {
$this->string(\Dropdown::getValueWithUnit($input, $unit))->isIdenticalTo($expected);
public function testGetValueWithUnit($input, $unit, $decimals, $expected) {
$value = $decimals !== null
? \Dropdown::getValueWithUnit($input, $unit, $decimals)
: \Dropdown::getValueWithUnit($input, $unit);
$this->string($value)->isIdenticalTo($expected);
}
protected function getDropdownValueProvider() {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment