-
Notifications
You must be signed in to change notification settings - Fork 5
/
DashboardPanelNumber.module
executable file
·104 lines (91 loc) · 3.03 KB
/
DashboardPanelNumber.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php namespace Daun\Dashboard;
use NumberFormatter;
use function ProcessWire\__;
// Include abstract panel base class
require_once dirname(__DIR__).'/Dashboard/DashboardPanel.class.php';
class DashboardPanelNumber extends DashboardPanel
{
public static function getModuleInfo()
{
return array_merge(
parent::getModuleInfo(),
[
'title' => __('Dashboard Panel: Number', __FILE__),
'summary' => __('Display a single number with trend indicator', __FILE__),
'author' => 'Philipp Daun',
'version' => '1.5.6',
]
);
}
public function getIcon()
{
return 'bar-chart';
}
public function getContent()
{
return $this->view('panels/number', [
'number' => $this->number,
'detail' => $this->detail,
'trend' => $this->trend,
]);
}
public function setup()
{
parent::setup();
$this->locale = $this->data['locale'] ?? setlocale(LC_ALL, 0);
$this->detail = $this->data['detail'] ?? '';
$this->number = $this->data['number'] ?? null;
$this->trend = $this->data['trend'] ?? null;
if (is_int($this->number) || is_float($this->number)) {
$this->number = $this->formatNumber($this->number);
}
}
private function formatNumber($number)
{
$formatted = $number;
try {
$style = NumberFormatter::DECIMAL;
$formatter = new NumberFormatter($this->locale, $style);
$formatted = $formatter->format($number);
} catch (\Throwable $th) {
$this->setTemporaryLocale();
$locale = localeconv();
$decimals = $this->getNumberOfDecimals($number);
$formatted = number_format($number, $decimals, $locale['decimal_point'], $locale['thousands_sep']);
$this->restoreLocales();
}
return $formatted;
}
private function getNumberOfDecimals($value)
{
if ((int) $value == $value) {
return 0;
} elseif (!is_numeric($value)) {
return 0;
}
$str = rtrim(number_format($value, 14 - log10($value)), '0');
return strlen($str) - strrpos($str, '.') - 1;
}
private function setTemporaryLocale()
{
if ($this->locale) {
$this->originalLocales = explode(';', setlocale(LC_ALL, 0));
setlocale(LC_ALL, $this->locale);
}
}
private function restoreLocales()
{
if ($this->locale && $this->originalLocales) {
foreach ($this->originalLocales as $localeSetting) {
if (strpos($localeSetting, '=') !== false) {
list($category, $locale) = explode('=', $localeSetting);
$category = (int) $category;
} else {
$category = LC_ALL;
$locale = $localeSetting;
}
setlocale($category, $locale);
}
}
}
}