diff options
author | diosmosis <benaka@piwik.pro> | 2014-11-14 05:22:49 +0300 |
---|---|---|
committer | diosmosis <benaka@piwik.pro> | 2014-11-14 05:22:49 +0300 |
commit | 545d316d4dad434edfe5048807bfbe686f856259 (patch) | |
tree | 2f82ab2c064539c0624b7496ccfd41398dee75db /plugins | |
parent | f57473c98324fa6026c4e7776f30c104d81d0989 (diff) |
Make MetricsFormatter a class with instance methods and move Html related functionality to derived class. Use FOrmatter in Metric::format calls so Metrics do not have to be aware of context of formatting, and so Metric::format methods will have less code redundancy.
Diffstat (limited to 'plugins')
36 files changed, 165 insertions, 104 deletions
diff --git a/plugins/API/ProcessedReport.php b/plugins/API/ProcessedReport.php index dcdf8aa09e..e31c938a18 100644 --- a/plugins/API/ProcessedReport.php +++ b/plugins/API/ProcessedReport.php @@ -18,7 +18,7 @@ use Piwik\DataTable\Row; use Piwik\DataTable\Simple; use Piwik\Date; use Piwik\Metrics; -use Piwik\MetricsFormatter; +use Piwik\Metrics\Formatter; use Piwik\Period; use Piwik\Piwik; use Piwik\Plugin\Report; @@ -647,6 +647,8 @@ class ProcessedReport $enhancedDataTable = new Simple(); } + $formatter = new Formatter(); + foreach ($simpleDataTable->getRows() as $row) { $rowMetrics = $row->getColumns(); @@ -673,7 +675,8 @@ class ProcessedReport $idSiteForRow = (int) $row->getMetadata('idsite'); } - $prettyValue = MetricsFormatter::getPrettyValue($idSiteForRow, $columnName, $columnValue, $htmlAllowed = false); + // TODO: should not call this for formatted processed metrics + $prettyValue = $this->getPrettyValue($formatter, $idSiteForRow, $columnName, $columnValue, $htmlAllowed = false); $enhancedRow->addColumn($columnName, $prettyValue); } // For example the Maps Widget requires the raw metrics to do advanced datavis elseif ($returnRawMetrics) { @@ -797,4 +800,35 @@ class ProcessedReport return $entry; }, $v)); } -} + + /** + * Prettifies a metric value based on the column name. + * + * @param int $idSite The ID of the site the metric is for (used if the column value is an amount of money). + * @param string $columnName The metric name. + * @param mixed $value The metric value. + * @param bool $isHtml If true, replaces all spaces with `' '`. + * @return string + */ + public function getPrettyValue(Formatter $formatter, $idSite, $columnName, $value) // TODO: is this method used? if not remove it + { + // Display time in human readable + if (strpos($columnName, 'time') !== false) { + return $formatter->getPrettyTimeFromSeconds($value); + } + + // Add revenue symbol to revenues + if (strpos($columnName, 'revenue') !== false && strpos($columnName, 'evolution') === false) { + return $formatter->getPrettyMoney($value, $idSite); + } + + // Add % symbol to rates + if (strpos($columnName, '_rate') !== false) { + if (strpos($value, "%") === false) { + return $value . "%"; + } + } + + return $value; + } +}
\ No newline at end of file diff --git a/plugins/Actions/Metrics/AveragePageGenerationTime.php b/plugins/Actions/Metrics/AveragePageGenerationTime.php index 0babe85007..50b6cc7227 100644 --- a/plugins/Actions/Metrics/AveragePageGenerationTime.php +++ b/plugins/Actions/Metrics/AveragePageGenerationTime.php @@ -10,7 +10,7 @@ namespace Piwik\Plugins\Actions\Metrics; use Piwik\DataTable; use Piwik\DataTable\Row; use Piwik\Metrics; -use Piwik\MetricsFormatter; +use Piwik\Metrics\Formatter; use Piwik\Piwik; use Piwik\Plugin\ProcessedMetric; use Piwik\Plugin\Report; @@ -48,10 +48,9 @@ class AveragePageGenerationTime extends ProcessedMetric return Piwik::getQuotientSafe($sumGenerationTime, $hitsWithTimeGeneration, $precision = 3); } - public function format($value) + public function format($value, Formatter $formatter) { - // TODO: isHtml needs to be true for this & for revenue columns. perhaps we can make MetricsFormatter an object. - return $value ? MetricsFormatter::getPrettyTimeFromSeconds($value, $displayAsSentence = true, $isHtml = false) : "-"; + return $value ? $formatter->getPrettyTimeFromSeconds($value, $displayAsSentence = true) : "-"; } public function beforeCompute($report, DataTable $table) diff --git a/plugins/Actions/Metrics/AverageTimeOnPage.php b/plugins/Actions/Metrics/AverageTimeOnPage.php index 30bfdb78cf..395f1d0a8e 100644 --- a/plugins/Actions/Metrics/AverageTimeOnPage.php +++ b/plugins/Actions/Metrics/AverageTimeOnPage.php @@ -8,7 +8,7 @@ namespace Piwik\Plugins\Actions\Metrics; use Piwik\DataTable\Row; -use Piwik\MetricsFormatter; +use Piwik\Metrics\Formatter; use Piwik\Piwik; use Piwik\Plugin\ProcessedMetric; @@ -39,9 +39,9 @@ class AverageTimeOnPage extends ProcessedMetric return Piwik::getQuotientSafe($sumTimeSpent, $visits, $precision = 0); } - public function format($value) + public function format($value, Formatter $formatter) { - return MetricsFormatter::getPrettyTimeFromSeconds($value); + return $formatter->getPrettyTimeFromSeconds($value); } public function getDependentMetrics() diff --git a/plugins/Actions/Metrics/BounceRate.php b/plugins/Actions/Metrics/BounceRate.php index 72e2cc08af..f19b701ada 100644 --- a/plugins/Actions/Metrics/BounceRate.php +++ b/plugins/Actions/Metrics/BounceRate.php @@ -8,6 +8,7 @@ namespace Piwik\Plugins\Actions\Metrics; use Piwik\DataTable\Row; +use Piwik\Metrics\Formatter; use Piwik\Piwik; use Piwik\Plugin\ProcessedMetric; @@ -38,9 +39,9 @@ class BounceRate extends ProcessedMetric return Piwik::getQuotientSafe($entryBounceCount, $entryVisits, $precision = 2); } - public function format($value) + public function format($value, Formatter $formatter) { - return ($value * 100) . '%'; // TODO: how does this affect the float/locale bug? + return $formatter->getPrettyPercentFromQuotient($value); } public function getDependentMetrics() diff --git a/plugins/Actions/Metrics/ExitRate.php b/plugins/Actions/Metrics/ExitRate.php index 89f50a7867..2b570b045e 100644 --- a/plugins/Actions/Metrics/ExitRate.php +++ b/plugins/Actions/Metrics/ExitRate.php @@ -8,6 +8,7 @@ namespace Piwik\Plugins\Actions\Metrics; use Piwik\DataTable\Row; +use Piwik\Metrics\Formatter; use Piwik\Piwik; use Piwik\Plugin\ProcessedMetric; @@ -38,9 +39,9 @@ class ExitRate extends ProcessedMetric return Piwik::getQuotientSafe($exitVisits, $visits, $precision = 2); } - public function format($value) + public function format($value, Formatter $formatter) { - return ($value * 100) . '%'; + return $formatter->getPrettyPercentFromQuotient($value); } public function getDependentMetrics() diff --git a/plugins/Actions/Reports/Base.php b/plugins/Actions/Reports/Base.php index 9685ea5cf3..84870baba8 100644 --- a/plugins/Actions/Reports/Base.php +++ b/plugins/Actions/Reports/Base.php @@ -9,7 +9,7 @@ namespace Piwik\Plugins\Actions\Reports; use Piwik\Common; -use Piwik\MetricsFormatter; +use Piwik\Metrics\Formatter; use Piwik\Piwik; use Piwik\Plugin\ViewDataTable; use Piwik\Plugins\Actions\Actions; @@ -61,8 +61,10 @@ abstract class Base extends \Piwik\Plugin\Report 'nb_visits' => Piwik::translate('General_ColumnUniquePageviews') )); + $formatter = new Formatter(); + // add avg_generation_time tooltip - $tooltipCallback = function ($hits, $min, $max) { + $tooltipCallback = function ($hits, $min, $max) use ($formatter) { if (!$hits) { return false; } @@ -70,8 +72,8 @@ abstract class Base extends \Piwik\Plugin\Report return Piwik::translate("Actions_AvgGenerationTimeTooltip", array( $hits, "<br />", - MetricsFormatter::getPrettyTimeFromSeconds($min), - MetricsFormatter::getPrettyTimeFromSeconds($max) + $formatter->getPrettyTimeFromSeconds($min), + $formatter->getPrettyTimeFromSeconds($max) )); }; $view->config->filters[] = array('ColumnCallbackAddMetadata', diff --git a/plugins/Contents/Metrics/InteractionRate.php b/plugins/Contents/Metrics/InteractionRate.php index 8d8500cf8a..6dfa09eefd 100644 --- a/plugins/Contents/Metrics/InteractionRate.php +++ b/plugins/Contents/Metrics/InteractionRate.php @@ -9,6 +9,7 @@ namespace Piwik\Plugins\Contents\Metrics; use Piwik\DataTable\Row; +use Piwik\Metrics\Formatter; use Piwik\Piwik; use Piwik\Plugin\ProcessedMetric; @@ -39,9 +40,9 @@ class InteractionRate extends ProcessedMetric return Piwik::getQuotientSafe($interactions, $impressions, $precision = 4); } - public function format($value) + public function format($value, Formatter $formatter) { - return ($value * 100) . '%'; + return $formatter->getPrettyPercentFromQuotient($value); } public function getDependentMetrics() diff --git a/plugins/CoreAdminHome/Controller.php b/plugins/CoreAdminHome/Controller.php index af45ccc327..da59ea9e66 100644 --- a/plugins/CoreAdminHome/Controller.php +++ b/plugins/CoreAdminHome/Controller.php @@ -218,7 +218,7 @@ class Controller extends \Piwik\Plugin\ControllerAdmin $view->idSite = Common::getRequestVar('idSite', $defaultIdSite, 'int'); $view->defaultReportSiteName = Site::getNameFor($view->idSite); - $view->defaultSiteRevenue = \Piwik\MetricsFormatter::getCurrencySymbol($view->idSite); + $view->defaultSiteRevenue = \Piwik\Metrics\Formatter::getCurrencySymbol($view->idSite); $view->maxCustomVariables = CustomVariables::getMaxCustomVariables(); $allUrls = APISitesManager::getInstance()->getSiteUrlsFromId($view->idSite); diff --git a/plugins/CoreHome/Metrics/AverageTimeOnSite.php b/plugins/CoreHome/Metrics/AverageTimeOnSite.php index 5b0acd652c..4a908d1a5f 100644 --- a/plugins/CoreHome/Metrics/AverageTimeOnSite.php +++ b/plugins/CoreHome/Metrics/AverageTimeOnSite.php @@ -8,6 +8,7 @@ namespace Piwik\Plugins\CoreHome\Metrics; use Piwik\DataTable\Row; +use Piwik\Metrics\Formatter; use Piwik\Piwik; use Piwik\Plugin\ProcessedMetric; @@ -35,6 +36,11 @@ class AverageTimeOnSite extends ProcessedMetric return Piwik::getQuotientSafe($sumVisitLength, $nbVisits, $precision = 0); } + public function format($value, Formatter $formatter) + { + return $formatter->getPrettyTimeFromSeconds($value); + } + public function getTranslatedName() { return Piwik::translate('General_ColumnAvgTimeOnSite'); diff --git a/plugins/CoreHome/Metrics/BounceRate.php b/plugins/CoreHome/Metrics/BounceRate.php index 3e45061589..d39e405cda 100644 --- a/plugins/CoreHome/Metrics/BounceRate.php +++ b/plugins/CoreHome/Metrics/BounceRate.php @@ -8,6 +8,7 @@ namespace Piwik\Plugins\CoreHome\Metrics; use Piwik\DataTable\Row; +use Piwik\Metrics\Formatter; use Piwik\Piwik; use Piwik\Plugin\ProcessedMetric; @@ -36,9 +37,9 @@ class BounceRate extends ProcessedMetric return array('bounce_count', 'nb_visits'); } - public function format($value) + public function format($value, Formatter $formatter) { - return ($value * 100) . '%'; + return $formatter->getPrettyPercentFromQuotient($value); } public function compute(Row $row) diff --git a/plugins/CoreHome/Metrics/ConversionRate.php b/plugins/CoreHome/Metrics/ConversionRate.php index 6a1e08bf5e..23bca997a0 100644 --- a/plugins/CoreHome/Metrics/ConversionRate.php +++ b/plugins/CoreHome/Metrics/ConversionRate.php @@ -8,6 +8,7 @@ namespace Piwik\Plugins\CoreHome\Metrics; use Piwik\DataTable\Row; +use Piwik\Metrics\Formatter; use Piwik\Piwik; use Piwik\Plugin\ProcessedMetric; @@ -35,9 +36,9 @@ class ConversionRate extends ProcessedMetric return array('nb_visits_converted', 'nb_visits'); } - public function format($value) + public function format($value, Formatter $formatter) { - return ($value * 100) . '%'; + return $formatter->getPrettyPercentFromQuotient($value); } public function compute(Row $row) diff --git a/plugins/CoreHome/Metrics/EvolutionMetric.php b/plugins/CoreHome/Metrics/EvolutionMetric.php index 29d136816d..8794604509 100644 --- a/plugins/CoreHome/Metrics/EvolutionMetric.php +++ b/plugins/CoreHome/Metrics/EvolutionMetric.php @@ -11,6 +11,7 @@ namespace Piwik\Plugins\CoreHome\Metrics; use Piwik\Common; use Piwik\DataTable; use Piwik\DataTable\Row; +use Piwik\Metrics\Formatter; use Piwik\Piwik; use Piwik\Plugin\Metric; use Piwik\Plugin\ProcessedMetric; @@ -98,11 +99,9 @@ class EvolutionMetric extends ProcessedMetric } } - public function format($value) + public function format($value, Formatter $formatter) { - $value = ($value * 100) . '%'; - $value = Common::forceDotAsSeparatorForDecimalPoint($value); // TODO: need to create helper function for this in Piwik and use in all percent classes? - return $value; + return $formatter->getPrettyPercentFromQuotient($value); } public function getDependentMetrics() diff --git a/plugins/CoreHome/Metrics/VisitsPercent.php b/plugins/CoreHome/Metrics/VisitsPercent.php index 24350c5cae..7d268091b1 100644 --- a/plugins/CoreHome/Metrics/VisitsPercent.php +++ b/plugins/CoreHome/Metrics/VisitsPercent.php @@ -10,6 +10,7 @@ namespace Piwik\Plugins\CoreHome\Metrics; use Piwik\DataTable; use Piwik\DataTable\Row; +use Piwik\Metrics\Formatter; use Piwik\Piwik; use Piwik\Plugin\ProcessedMetric; use Piwik\Plugin\Report; @@ -53,9 +54,9 @@ class VisitsPercent extends ProcessedMetric return Piwik::getQuotientSafe($visits, $this->cachedTotalVisits, $precision = 3); } - public function format($value) + public function format($value, Formatter $formatter) { - return ($value * 100) . '%'; + return $formatter->getPrettyPercentFromQuotient($value); } public function getDependentMetrics() diff --git a/plugins/CoreHome/Visitor.php b/plugins/CoreHome/Visitor.php index 35b64b3d30..b98ee8ead0 100644 --- a/plugins/CoreHome/Visitor.php +++ b/plugins/CoreHome/Visitor.php @@ -8,15 +8,18 @@ */ namespace Piwik\Plugins\CoreHome; +use Piwik\Metrics\Formatter; use Piwik\Plugins\CoreHome\Columns\VisitGoalBuyer; class Visitor { private $details = array(); + private $metricsFormatter = null; public function __construct($details) { $this->details = $details; + $this->metricsFormatter = new Formatter(); } function getTimestampFirstAction() @@ -96,7 +99,7 @@ class Visitor function getVisitLengthPretty() { - return \Piwik\MetricsFormatter::getPrettyTimeFromSeconds($this->details['visit_total_time']); + return $this->metricsFormatter->getPrettyTimeFromSeconds($this->details['visit_total_time']); } function getUserId() @@ -107,5 +110,4 @@ class Visitor } return null; } - }
\ No newline at end of file diff --git a/plugins/CoreVisualizations/Visualizations/HtmlTable/AllColumns.php b/plugins/CoreVisualizations/Visualizations/HtmlTable/AllColumns.php index 2ccd5a9e01..da9a82ac75 100644 --- a/plugins/CoreVisualizations/Visualizations/HtmlTable/AllColumns.php +++ b/plugins/CoreVisualizations/Visualizations/HtmlTable/AllColumns.php @@ -9,6 +9,7 @@ namespace Piwik\Plugins\CoreVisualizations\Visualizations\HtmlTable; +use Piwik\DataTable; use Piwik\Plugins\CoreVisualizations\Visualizations\HtmlTable; use Piwik\View; @@ -36,7 +37,7 @@ class AllColumns extends HtmlTable $properties = $this->config; - $this->dataTable->filter(function ($dataTable) use ($properties) { + $this->dataTable->filter(function (DataTable $dataTable) use ($properties) { $columnsToDisplay = array('label', 'nb_visits'); if (in_array('nb_uniq_visitors', $dataTable->getColumns())) { @@ -60,11 +61,4 @@ class AllColumns extends HtmlTable $properties->columns_to_display = $columnsToDisplay; }); } - - public function afterGenericFiltersAreAppliedToLoadedDataTable() - { - $prettifyTime = array('\Piwik\MetricsFormatter', 'getPrettyTimeFromSeconds'); - - $this->dataTable->filter('ColumnCallbackReplace', array('avg_time_on_site', $prettifyTime)); - } -} +}
\ No newline at end of file diff --git a/plugins/DBStats/Controller.php b/plugins/DBStats/Controller.php index de36313264..1e8aad1311 100644 --- a/plugins/DBStats/Controller.php +++ b/plugins/DBStats/Controller.php @@ -8,7 +8,7 @@ */ namespace Piwik\Plugins\DBStats; -use Piwik\MetricsFormatter; +use Piwik\Metrics\Formatter; use Piwik\Piwik; use Piwik\Plugins\DBStats\Reports\GetAdminDataSummary; use Piwik\Plugins\DBStats\Reports\GetDatabaseUsageSummary; @@ -42,9 +42,10 @@ class Controller extends \Piwik\Plugin\ControllerAdmin list($siteCount, $userCount, $totalSpaceUsed) = API::getInstance()->getGeneralInformation(); - $view->siteCount = MetricsFormatter::getPrettyNumber($siteCount); - $view->userCount = MetricsFormatter::getPrettyNumber($userCount); - $view->totalSpaceUsed = MetricsFormatter::getPrettySizeFromBytes($totalSpaceUsed); + $formatter = new Formatter(); + $view->siteCount = $formatter->getPrettyNumber($siteCount); + $view->userCount = $formatter->getPrettyNumber($userCount); + $view->totalSpaceUsed = $formatter->getPrettySizeFromBytes($totalSpaceUsed); return $view->render(); } diff --git a/plugins/DBStats/Reports/Base.php b/plugins/DBStats/Reports/Base.php index a37452a6a3..c32e2d7410 100644 --- a/plugins/DBStats/Reports/Base.php +++ b/plugins/DBStats/Reports/Base.php @@ -8,6 +8,7 @@ */ namespace Piwik\Plugins\DBStats\Reports; +use Piwik\Metrics\Formatter; use Piwik\Option; use Piwik\Piwik; use Piwik\Plugin\ViewDataTable; @@ -17,7 +18,6 @@ use Piwik\Plugins\DBStats\DBStats; abstract class Base extends \Piwik\Plugin\Report { - public function isEnabled() { return Piwik::hasUserSuperUserAccess(); @@ -119,7 +119,9 @@ abstract class Base extends \Piwik\Plugin\Report $view->config->selectable_rows = array(); } - $getPrettySize = array('\Piwik\MetricsFormatter', 'getPrettySizeFromBytes'); + $formatter = new Formatter(); + + $getPrettySize = array($formatter, 'getPrettySizeFromBytes'); $params = !isset($fixedMemoryUnit) ? array() : array($fixedMemoryUnit); $view->config->filters[] = function ($dataTable) use ($sizeColumns, $getPrettySize, $params) { @@ -135,7 +137,7 @@ abstract class Base extends \Piwik\Plugin\Report $view->config->filters[] = array('ColumnCallbackReplace', array($sizeColumns, $replaceSpaces)); } - $getPrettyNumber = array('\Piwik\MetricsFormatter', 'getPrettyNumber'); + $getPrettyNumber = array($formatter, 'getPrettyNumber'); $view->config->filters[] = array('ColumnCallbackReplace', array('row_count', $getPrettyNumber)); } diff --git a/plugins/Goals/Metrics/AverageOrderRevenue.php b/plugins/Goals/Metrics/AverageOrderRevenue.php index 76237b6ff5..fed8e7779b 100644 --- a/plugins/Goals/Metrics/AverageOrderRevenue.php +++ b/plugins/Goals/Metrics/AverageOrderRevenue.php @@ -9,7 +9,7 @@ namespace Piwik\Plugins\Goals\Metrics; use Piwik\DataTable; use Piwik\DataTable\Row; -use Piwik\MetricsFormatter; +use Piwik\Metrics\Formatter; use Piwik\Piwik; use Piwik\Plugin\ProcessedMetric; @@ -47,9 +47,9 @@ class AverageOrderRevenue extends ProcessedMetric return array('revenue', 'nb_conversions'); } - public function format($value) + public function format($value, Formatter $formatter) { - return MetricsFormatter::getPrettyMoney(sprintf("%.1f", $value), $this->idSite, $isHtml = false); + return $formatter->getPrettyMoney($value, $this->idSite); } public function beforeFormat($report, DataTable $table) diff --git a/plugins/Goals/Metrics/AveragePrice.php b/plugins/Goals/Metrics/AveragePrice.php index b22cca297b..1fb83c18bd 100644 --- a/plugins/Goals/Metrics/AveragePrice.php +++ b/plugins/Goals/Metrics/AveragePrice.php @@ -10,7 +10,7 @@ namespace Piwik\Plugins\Goals\Metrics; use Piwik\DataTable; use Piwik\DataTable\Row; -use Piwik\MetricsFormatter; +use Piwik\Metrics\Formatter; use Piwik\Piwik; use Piwik\Plugin\ProcessedMetric; use Piwik\Tracker\GoalManager; @@ -50,9 +50,9 @@ class AveragePrice extends ProcessedMetric return array('price', 'orders', 'abandoned_carts'); } - public function format($value) + public function format($value, Formatter $formatter) { - return MetricsFormatter::getPrettyMoney(sprintf("%.2f", $value), $this->idSite, $isHtml = false); + return $formatter->getPrettyMoney($value, $this->idSite); } public function beforeFormat($report, DataTable $table) diff --git a/plugins/Goals/Metrics/GoalSpecific/AverageOrderRevenue.php b/plugins/Goals/Metrics/GoalSpecific/AverageOrderRevenue.php index 144d8861bf..b946535580 100644 --- a/plugins/Goals/Metrics/GoalSpecific/AverageOrderRevenue.php +++ b/plugins/Goals/Metrics/GoalSpecific/AverageOrderRevenue.php @@ -10,7 +10,7 @@ namespace Piwik\Plugins\Goals\Metrics\GoalSpecific; use Piwik\DataTable; use Piwik\DataTable\Row; use Piwik\Metrics; -use Piwik\MetricsFormatter; +use Piwik\Metrics\Formatter; use Piwik\Piwik; use Piwik\Plugins\Goals\Metrics\GoalSpecificProcessedMetric; use Piwik\Tracker\GoalManager; @@ -54,9 +54,9 @@ class AverageOrderRevenue extends GoalSpecificProcessedMetric return Piwik::getQuotientSafe($goalRevenue, $conversions, GoalManager::REVENUE_PRECISION); } - public function format($value) + public function format($value, Formatter $formatter) { - return MetricsFormatter::getPrettyMoney(sprintf("%.1f", $value), $this->idSite, $isHtml = false); + return $formatter->getPrettyMoney($value, $this->idSite); } public function beforeFormat($report, DataTable $table) diff --git a/plugins/Goals/Metrics/GoalSpecific/ConversionRate.php b/plugins/Goals/Metrics/GoalSpecific/ConversionRate.php index 0019fb0e06..1a1bf80305 100644 --- a/plugins/Goals/Metrics/GoalSpecific/ConversionRate.php +++ b/plugins/Goals/Metrics/GoalSpecific/ConversionRate.php @@ -9,6 +9,7 @@ namespace Piwik\Plugins\Goals\Metrics\GoalSpecific; use Piwik\DataTable\Row; use Piwik\Metrics; +use Piwik\Metrics\Formatter; use Piwik\Piwik; use Piwik\Plugins\Goals\Metrics\GoalSpecificProcessedMetric; use Piwik\Tracker\GoalManager; @@ -43,9 +44,9 @@ class ConversionRate extends GoalSpecificProcessedMetric return array('goals'); } - public function format($value) + public function format($value, Formatter $formatter) { - return ($value * 100) . '%'; + return $formatter->getPrettyPercentFromQuotient($value); } public function compute(Row $row) diff --git a/plugins/Goals/Metrics/GoalSpecific/Revenue.php b/plugins/Goals/Metrics/GoalSpecific/Revenue.php index 5dcffb3236..814485309c 100644 --- a/plugins/Goals/Metrics/GoalSpecific/Revenue.php +++ b/plugins/Goals/Metrics/GoalSpecific/Revenue.php @@ -10,7 +10,7 @@ namespace Piwik\Plugins\Goals\Metrics\GoalSpecific; use Piwik\DataTable; use Piwik\DataTable\Row; use Piwik\Metrics; -use Piwik\MetricsFormatter; +use Piwik\Metrics\Formatter; use Piwik\Piwik; use Piwik\Plugins\Goals\Metrics\GoalSpecificProcessedMetric; @@ -47,9 +47,9 @@ class Revenue extends GoalSpecificProcessedMetric return (float) $this->getMetric($goalMetrics, 'revenue', $mappingFromNameToIdGoal); } - public function format($value) + public function format($value, Formatter $formatter) { - return MetricsFormatter::getPrettyMoney(sprintf("%.2f", $value), $this->idSite, $isHtml = false); + return $formatter->getPrettyMoney($value, $this->idSite); } public function beforeFormat($report, DataTable $table) diff --git a/plugins/Goals/Metrics/GoalSpecific/RevenuePerVisit.php b/plugins/Goals/Metrics/GoalSpecific/RevenuePerVisit.php index ce18172794..3abe443d71 100644 --- a/plugins/Goals/Metrics/GoalSpecific/RevenuePerVisit.php +++ b/plugins/Goals/Metrics/GoalSpecific/RevenuePerVisit.php @@ -10,7 +10,7 @@ namespace Piwik\Plugins\Goals\Metrics\GoalSpecific; use Piwik\DataTable; use Piwik\DataTable\Row; use Piwik\Metrics; -use Piwik\MetricsFormatter; +use Piwik\Metrics\Formatter; use Piwik\Piwik; use Piwik\Plugins\Goals\Metrics\GoalSpecificProcessedMetric; use Piwik\Tracker\GoalManager; @@ -62,9 +62,9 @@ class RevenuePerVisit extends GoalSpecificProcessedMetric return Piwik::getQuotientSafe($goalRevenue, $nbVisits == 0 ? $conversions : $nbVisits, GoalManager::REVENUE_PRECISION); } - public function format($value) + public function format($value, Formatter $formatter) { - return MetricsFormatter::getPrettyMoney(sprintf("%.1f", $value), $this->idSite, $isHtml = false); + return $formatter->getPrettyMoney($value, $this->idSite); } public function beforeFormat($report, DataTable $table) diff --git a/plugins/Goals/Metrics/ProductConversionRate.php b/plugins/Goals/Metrics/ProductConversionRate.php index f11e9d9085..09d9a4ba29 100644 --- a/plugins/Goals/Metrics/ProductConversionRate.php +++ b/plugins/Goals/Metrics/ProductConversionRate.php @@ -9,6 +9,7 @@ namespace Piwik\Plugins\Goals\Metrics; use Piwik\DataTable\Row; +use Piwik\Metrics\Formatter; use Piwik\Piwik; use Piwik\Plugin\ProcessedMetric; use Piwik\Tracker\GoalManager; @@ -32,9 +33,9 @@ class ProductConversionRate extends ProcessedMetric return Piwik::translate('General_ProductConversionRate'); } - public function format($value) + public function format($value, Formatter $formatter) { - return ($value * 100) . '%'; + return $formatter->getPrettyPercentFromQuotient($value); } public function compute(Row $row) diff --git a/plugins/Goals/Metrics/RevenuePerVisit.php b/plugins/Goals/Metrics/RevenuePerVisit.php index a307ef566a..825b6e1033 100644 --- a/plugins/Goals/Metrics/RevenuePerVisit.php +++ b/plugins/Goals/Metrics/RevenuePerVisit.php @@ -10,7 +10,7 @@ namespace Piwik\Plugins\Goals\Metrics; use Piwik\DataTable; use Piwik\DataTable\Row; use Piwik\Metrics; -use Piwik\MetricsFormatter; +use Piwik\Metrics\Formatter; use Piwik\Piwik; use Piwik\Plugin\ProcessedMetric; use Piwik\Tracker\GoalManager; @@ -70,10 +70,9 @@ class RevenuePerVisit extends ProcessedMetric return Piwik::getQuotientSafe($revenue, $nbVisits == 0 ? $conversions : $nbVisits, GoalManager::REVENUE_PRECISION); } - public function format($value) + public function format($value, Formatter $formatter) { - // TODO: is the sprintf necessary? - return MetricsFormatter::getPrettyMoney(sprintf("%.2f", $value), $this->idSite, $isHtml = false); + return $formatter->getPrettyMoney($value, $this->idSite); } public function beforeFormat($report, DataTable $table) diff --git a/plugins/Goals/Reports/BaseEcommerceItem.php b/plugins/Goals/Reports/BaseEcommerceItem.php index 5da30d154d..ae6d0e87a8 100644 --- a/plugins/Goals/Reports/BaseEcommerceItem.php +++ b/plugins/Goals/Reports/BaseEcommerceItem.php @@ -9,6 +9,7 @@ namespace Piwik\Plugins\Goals\Reports; use Piwik\Common; +use Piwik\Metrics\Formatter; use Piwik\Piwik; use Piwik\Plugin\Report; use Piwik\Plugin\ViewDataTable; @@ -69,7 +70,7 @@ abstract class BaseEcommerceItem extends BaseEcommerce $view->config->show_table_all_columns = false; $moneyColumns = array('revenue'); - $formatter = '\Piwik\MetricsFormatter::getPrettyMoney'; + $formatter = array(new Formatter(), 'getPrettyMoney'); $view->config->filters[] = array('ColumnCallbackReplace', array($moneyColumns, $formatter, array($idSite))); $view->requestConfig->filter_limit = 10; diff --git a/plugins/Live/API.php b/plugins/Live/API.php index 67f7d46cd4..935762c79d 100644 --- a/plugins/Live/API.php +++ b/plugins/Live/API.php @@ -15,7 +15,7 @@ use Piwik\DataTable; use Piwik\DataTable\Row; use Piwik\Date; use Piwik\Db; -use Piwik\MetricsFormatter; +use Piwik\Metrics\Formatter; use Piwik\Period\Range; use Piwik\Period; use Piwik\Piwik; @@ -346,7 +346,8 @@ class API extends \Piwik\Plugin\API round($pageGenerationTimeTotal / $result['totalPageViews'], $precision = 2); } - $result['totalVisitDurationPretty'] = MetricsFormatter::getPrettyTimeFromSeconds($result['totalVisitDuration']); + $formatter = new Formatter(); + $result['totalVisitDurationPretty'] = $formatter->getPrettyTimeFromSeconds($result['totalVisitDuration']); // use requested visits for first/last visit info $rows = $visits->getRows(); diff --git a/plugins/Live/Reports/GetSimpleLastVisitCount.php b/plugins/Live/Reports/GetSimpleLastVisitCount.php index 1b44e4ad69..34ba773fa9 100644 --- a/plugins/Live/Reports/GetSimpleLastVisitCount.php +++ b/plugins/Live/Reports/GetSimpleLastVisitCount.php @@ -9,7 +9,7 @@ namespace Piwik\Plugins\Live\Reports; use Piwik\Config; -use Piwik\MetricsFormatter; +use Piwik\Metrics\Formatter; use Piwik\Piwik; use Piwik\Plugin\Report; use Piwik\Plugins\Live\Controller; @@ -32,11 +32,13 @@ class GetSimpleLastVisitCount extends Base $lastNData = Request::processRequest('Live.getCounters', array('lastMinutes' => $lastMinutes)); + $formatter = new Formatter(); + $view = new View('@Live/getSimpleLastVisitCount'); $view->lastMinutes = $lastMinutes; - $view->visitors = MetricsFormatter::getPrettyNumber($lastNData[0]['visitors']); - $view->visits = MetricsFormatter::getPrettyNumber($lastNData[0]['visits']); - $view->actions = MetricsFormatter::getPrettyNumber($lastNData[0]['actions']); + $view->visitors = $formatter->getPrettyNumber($lastNData[0]['visitors']); + $view->visits = $formatter->getPrettyNumber($lastNData[0]['visits']); + $view->actions = $formatter->getPrettyNumber($lastNData[0]['actions']); $view->refreshAfterXSecs = Config::getInstance()->General['live_widget_refresh_after_seconds']; $view->translations = array( 'one_visitor' => Piwik::translate('Live_NbVisitor'), @@ -51,5 +53,4 @@ class GetSimpleLastVisitCount extends Base return $view->render(); } - -} +}
\ No newline at end of file diff --git a/plugins/Live/Visitor.php b/plugins/Live/Visitor.php index 27873ec2a1..c7c8a13e6d 100644 --- a/plugins/Live/Visitor.php +++ b/plugins/Live/Visitor.php @@ -13,6 +13,7 @@ use Piwik\DataAccess\LogAggregator; use Piwik\DataTable\Filter\ColumnDelete; use Piwik\Date; use Piwik\Db; +use Piwik\Metrics\Formatter; use Piwik\Network\IPUtils; use Piwik\Piwik; use Piwik\Plugins\CustomVariables\CustomVariables; @@ -282,6 +283,8 @@ class Visitor implements VisitorInterface "; $actionDetails = Db::fetchAll($sql, array($idVisit)); + $formatter = new Formatter(); + foreach ($actionDetails as $actionIdx => &$actionDetail) { $actionDetail =& $actionDetails[$actionIdx]; $customVariablesPage = array(); @@ -327,7 +330,7 @@ class Visitor implements VisitorInterface $actionDetail['eventValue'] = round($actionDetail['custom_float'], self::EVENT_VALUE_PRECISION); } } elseif ($actionDetail['custom_float'] > 0) { - $actionDetail['generationTime'] = \Piwik\MetricsFormatter::getPrettyTimeFromSeconds($actionDetail['custom_float'] / 1000); + $actionDetail['generationTime'] = $formatter->getPrettyTimeFromSeconds($actionDetail['custom_float'] / 1000); } unset($actionDetail['custom_float']); @@ -343,7 +346,7 @@ class Visitor implements VisitorInterface // Set the time spent for this action (which is the timeSpentRef of the next action) if (isset($actionDetails[$actionIdx + 1])) { $actionDetail['timeSpent'] = $actionDetails[$actionIdx + 1]['timeSpentRef']; - $actionDetail['timeSpentPretty'] = \Piwik\MetricsFormatter::getPrettyTimeFromSeconds($actionDetail['timeSpent']); + $actionDetail['timeSpentPretty'] = $formatter->getPrettyTimeFromSeconds($actionDetail['timeSpent']); } unset($actionDetails[$actionIdx]['timeSpentRef']); // not needed after timeSpent is added diff --git a/plugins/Overlay/Controller.php b/plugins/Overlay/Controller.php index 331be0a1f7..bbbb2a30c1 100644 --- a/plugins/Overlay/Controller.php +++ b/plugins/Overlay/Controller.php @@ -13,7 +13,6 @@ use Piwik\API\Request; use Piwik\Common; use Piwik\Config; use Piwik\Metrics; -use Piwik\MetricsFormatter; use Piwik\Piwik; use Piwik\Plugins\Actions\ArchivingHelper; use Piwik\Plugins\SitesManager\API as APISitesManager; @@ -77,6 +76,8 @@ class Controller extends \Piwik\Plugin\Controller ); $dataTable = $request->process(); + $formatter = new Metrics\Formatter(); + $data = array(); if ($dataTable->getRowsCount() > 0) { $row = $dataTable->getFirstRow(); @@ -92,7 +93,7 @@ class Controller extends \Piwik\Plugin\Controller continue; } if ($metric == 'avg_time_on_page') { - $value = MetricsFormatter::getPrettyTimeFromSeconds($value); + $value = $formatter->getPrettyTimeFromSeconds($value); } $data[] = array( 'name' => $translations[$metric], diff --git a/plugins/PrivacyManager/Controller.php b/plugins/PrivacyManager/Controller.php index c950d789ed..2cacd93c30 100644 --- a/plugins/PrivacyManager/Controller.php +++ b/plugins/PrivacyManager/Controller.php @@ -12,7 +12,7 @@ use Piwik\Common; use Piwik\Config as PiwikConfig; use Piwik\Date; use Piwik\Db; -use Piwik\MetricsFormatter; +use Piwik\Metrics\Formatter; use Piwik\Nonce; use Piwik\Notification; use Piwik\Option; @@ -196,8 +196,9 @@ class Controller extends \Piwik\Plugin\ControllerAdmin $totalBytes += $status['Data_length'] + $status['Index_length']; } + $formatter = new Formatter(); $result = array( - 'currentSize' => MetricsFormatter::getPrettySizeFromBytes($totalBytes) + 'currentSize' => $formatter->getPrettySizeFromBytes($totalBytes) ); // if the db size estimate feature is enabled, get the estimate @@ -224,8 +225,8 @@ class Controller extends \Piwik\Plugin\ControllerAdmin } } - $result['sizeAfterPurge'] = MetricsFormatter::getPrettySizeFromBytes($totalAfterPurge); - $result['spaceSaved'] = MetricsFormatter::getPrettySizeFromBytes($totalBytes - $totalAfterPurge); + $result['sizeAfterPurge'] = $formatter->getPrettySizeFromBytes($totalAfterPurge); + $result['spaceSaved'] = $formatter->getPrettySizeFromBytes($totalBytes - $totalAfterPurge); } return $result; @@ -287,7 +288,9 @@ class Controller extends \Piwik\Plugin\ControllerAdmin } } - $deleteDataInfos["nextRunPretty"] = MetricsFormatter::getPrettyTimeFromSeconds($deleteDataInfos["nextScheduleTime"] - time()); + $formatter = new Formatter(); + + $deleteDataInfos["nextRunPretty"] = $formatter->getPrettyTimeFromSeconds($deleteDataInfos["nextScheduleTime"] - time()); return $deleteDataInfos; } diff --git a/plugins/SEO/RankChecker.php b/plugins/SEO/RankChecker.php index 88010814a1..fa254070df 100644 --- a/plugins/SEO/RankChecker.php +++ b/plugins/SEO/RankChecker.php @@ -11,7 +11,7 @@ namespace Piwik\Plugins\SEO; use Exception; use Piwik\Http; use Piwik\Log; -use Piwik\MetricsFormatter; +use Piwik\Metrics\Formatter; /** * The functions below are derived/adapted from GetRank.org's @@ -25,10 +25,12 @@ class RankChecker { private $url; private $majesticInfo = null; + private $formatter = null; public function __construct($url) { $this->url = self::extractDomainFromUrl($url); + $this->formatter = new Formatter(); } /** @@ -171,7 +173,7 @@ class RankChecker } if ($maxAge) { - return MetricsFormatter::getPrettyTimeFromSeconds(time() - $maxAge); + return $this->formatter->getPrettyTimeFromSeconds(time() - $maxAge); } return false; } diff --git a/plugins/SitesManager/API.php b/plugins/SitesManager/API.php index 24bb750f12..b10d321fce 100644 --- a/plugins/SitesManager/API.php +++ b/plugins/SitesManager/API.php @@ -13,8 +13,7 @@ use Piwik\Access; use Piwik\Common; use Piwik\Date; use Piwik\Db; -use Piwik\IP; -use Piwik\MetricsFormatter; +use Piwik\Metrics\Formatter; use Piwik\Network\IPUtils; use Piwik\Option; use Piwik\Piwik; @@ -1157,7 +1156,7 @@ class API extends \Piwik\Plugin\API */ public function getCurrencyList() { - $currencies = MetricsFormatter::getCurrencyList(); + $currencies = Formatter::getCurrencyList(); return array_map(function ($a) { return $a[1] . " (" . $a[0] . ")"; }, $currencies); @@ -1170,7 +1169,7 @@ class API extends \Piwik\Plugin\API */ public function getCurrencySymbols() { - $currencies = MetricsFormatter::getCurrencyList(); + $currencies = Formatter::getCurrencyList(); return array_map(function ($a) { return $a[0]; }, $currencies); diff --git a/plugins/UsersManager/Controller.php b/plugins/UsersManager/Controller.php index a0b184ceb9..65f55b9a01 100644 --- a/plugins/UsersManager/Controller.php +++ b/plugins/UsersManager/Controller.php @@ -11,7 +11,7 @@ namespace Piwik\Plugins\UsersManager; use Exception; use Piwik\API\ResponseBuilder; use Piwik\Common; -use Piwik\MetricsFormatter; +use Piwik\Metrics\Formatter; use Piwik\Piwik; use Piwik\Plugins\LanguagesManager\API as APILanguagesManager; use Piwik\Plugins\LanguagesManager\LanguagesManager; @@ -83,6 +83,8 @@ class Controller extends \Piwik\Plugin\ControllerAdmin $superUsers = array(); $usersAliasByLogin = array(); + $formatter = new Formatter(); + if (Piwik::isUserHasSomeAdminAccess()) { $view->showLastSeen = true; @@ -92,7 +94,7 @@ class Controller extends \Piwik\Plugin\ControllerAdmin $lastSeen = LastSeenTimeLogger::getLastSeenTimeForUser($user['login']); $users[$index]['last_seen'] = $lastSeen == 0 - ? false : MetricsFormatter::getPrettyTimeFromSeconds(time() - $lastSeen); + ? false : $formatter->getPrettyTimeFromSeconds(time() - $lastSeen); } if (Piwik::hasUserSuperUserAccess()) { diff --git a/plugins/VisitFrequency/Metrics/ReturningMetric.php b/plugins/VisitFrequency/Metrics/ReturningMetric.php index 8a3f95b06c..6768523dad 100644 --- a/plugins/VisitFrequency/Metrics/ReturningMetric.php +++ b/plugins/VisitFrequency/Metrics/ReturningMetric.php @@ -8,6 +8,7 @@ namespace Piwik\Plugins\VisitFrequency\Metrics; use Piwik\DataTable\Row; +use Piwik\Metrics\Formatter; use Piwik\Piwik; use Piwik\Plugin\ProcessedMetric; @@ -46,9 +47,9 @@ class ReturningMetric extends ProcessedMetric return Piwik::translate(self::$translations[$this->getName()]); } - public function format($value) + public function format($value, Formatter $formatter) { - return $this->wrapped->format($value); + return $this->wrapped->format($value, $formatter); } public function compute(Row $row) diff --git a/plugins/VisitsSummary/API.php b/plugins/VisitsSummary/API.php index 0530fd7ae3..8207e23bb6 100644 --- a/plugins/VisitsSummary/API.php +++ b/plugins/VisitsSummary/API.php @@ -9,8 +9,7 @@ namespace Piwik\Plugins\VisitsSummary; use Piwik\Archive; -use Piwik\Common; -use Piwik\MetricsFormatter; +use Piwik\Metrics\Formatter; use Piwik\Piwik; use Piwik\Plugin\Report; use Piwik\SettingsPiwik; @@ -119,12 +118,14 @@ class API extends \Piwik\Plugin\API public function getSumVisitsLengthPretty($idSite, $period, $date, $segment = false) { + $formatter = new Formatter(); + $table = $this->getSumVisitsLength($idSite, $period, $date, $segment); if (is_object($table)) { $table->filter('ColumnCallbackReplace', - array('sum_visit_length', '\Piwik\MetricsFormatter::getPrettyTimeFromSeconds')); + array('sum_visit_length', array($formatter, 'getPrettyTimeFromSeconds'))); } else { - $table = MetricsFormatter::getPrettyTimeFromSeconds($table); + $table = $formatter->getPrettyTimeFromSeconds($table); } return $table; } |