From 95bea8a4008087c4fd6261fc06542bd3137a0312 Mon Sep 17 00:00:00 2001 From: mattab Date: Mon, 12 Oct 2015 11:37:26 +1300 Subject: SMS report: percentage evolution are prefixed by '+' https://github.com/piwik/piwik/pull/8857 --- core/NumberFormatter.php | 21 +++++++++++++++++++-- core/Twig.php | 9 +++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) (limited to 'core') diff --git a/core/NumberFormatter.php b/core/NumberFormatter.php index f294508185..141bcdae8d 100644 --- a/core/NumberFormatter.php +++ b/core/NumberFormatter.php @@ -8,8 +8,6 @@ */ namespace Piwik; -use Piwik\Container\StaticContainer; - /** * Class NumberFormatter * @@ -148,6 +146,25 @@ class NumberFormatter extends Singleton return $this->formatNumberWithPattern($pattern, $newValue, $maximumFractionDigits, $minimumFractionDigits); } + + /** + * Formats given number as percent value, but keep the leading + sign if found + * + * @param $value + * @return string + */ + public function formatPercentEvolution($value) + { + $isPositiveEvolution = !empty($value) && ($value > 0 || $value[0] == '+'); + + $formatted = self::formatPercent($value); + + if($isPositiveEvolution) { + return '+' . $formatted; + } + return $formatted; + } + /** * Formats given number as percent value * @param string|int|float $value diff --git a/core/Twig.php b/core/Twig.php index 6d5b91a24c..c806dcf9bb 100755 --- a/core/Twig.php +++ b/core/Twig.php @@ -86,6 +86,7 @@ class Twig $this->addFilter_notification(); $this->addFilter_percentage(); $this->addFilter_percent(); + $this->addFilter_percentEvolution(); $this->addFilter_prettyDate(); $this->addFilter_safeDecodeRaw(); $this->addFilter_number(); @@ -288,6 +289,14 @@ class Twig $this->twig->addFilter($percentage); } + protected function addFilter_percentEvolution() + { + $percentage = new Twig_SimpleFilter('percentEvolution', function ($string) { + return NumberFormatter::getInstance()->formatPercentEvolution($string); + }); + $this->twig->addFilter($percentage); + } + protected function addFilter_number() { $formatter = new Twig_SimpleFilter('number', function ($string, $minFractionDigits = 0, $maxFractionDigits = 0) { -- cgit v1.2.3