-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilters.php
More file actions
255 lines (210 loc) · 7.14 KB
/
Copy pathFilters.php
File metadata and controls
255 lines (210 loc) · 7.14 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<?php
namespace ADT\FancyAdmin\Model;
use Contributte\Translation\Exceptions\InvalidArgument;
use Exception;
use Latte\ContentType;
use Latte\Runtime\FilterInfo;
use Nette\Localization\Translator;
use Nette\Utils\DateTime;
class Filters
{
public function __construct(protected readonly string $sharedDir, protected readonly Translator $translator)
{
}
public function setSharedDir(\Nette\Application\UI\Template $template)
{
$template->sharedDir = $this->sharedDir;
}
public function number(string|int|float $number, int $decimals = 0, string $decimalSymbol = ',', string $thousandsSeparator = ' '): string
{
// Pokud je vstup string, zkusíme detekovat počet desetinných míst
$actualDecimals = $decimals;
if (is_string($number)) {
$parts = explode('.', $number);
if (count($parts) == 2) {
// Použijeme menší z: skutečný počet desetinných míst vs. maximální limit
$actualDecimals = min(strlen($parts[1]), $decimals);
}
$number = floatval($number);
}
// Formátujeme číslo s určeným počtem desetinných míst
$formatted = number_format($number, $actualDecimals, $decimalSymbol, $thousandsSeparator);
// Pokud jsou desetinná místa, odstraníme koncové nuly
if ($actualDecimals > 0) {
// Rozdělíme podle desetinného symbolu
$parts = explode($decimalSymbol, $formatted);
if (count($parts) == 2) {
// Odstraníme koncové nuly z desetinné části
$decimalPart = rtrim($parts[1], '0');
// Pokud zůstala prázdná desetinná část, vrátíme jen celé číslo
if ($decimalPart === '') {
return $parts[0];
}
// Jinak spojíme zpět s desetinným symbolem
return $parts[0] . $decimalSymbol . $decimalPart;
}
}
return $formatted;
}
/**
* @throws Exception
*/
public function date($time, string $format = 'j. n. Y'): ?string
{
if ($time === null) {
return null;
}
$format = html_entity_decode($format);
return DateTime::from($time)->format($format);
}
public function datetime($time, string $format = 'j. n. Y H:i'): ?string
{
if ($time === null) {
return null;
}
$format = html_entity_decode($format);
return DateTime::from($time)->format($format);
}
/**
* @throws Exception
*/
public function time($time, string $format = 'H:i'): string
{
$format = html_entity_decode($format);
return DateTime::from($time)->format($format);
}
/**
* @throws InvalidArgument
*/
public function price(float|string $price, string $currency, int $decimals = 2, ?string $decimalSymbol = null, ?string $thousandsSeparator = null): string
{
if ($currency) {
$currency = html_entity_decode($currency);
}
if ($decimalSymbol === null) {
$decimalSymbol = $this->translator->translate('fcadmin.appGeneral.model.filters.decimalSeparator');
}
if ($thousandsSeparator === null) {
$thousandsSeparator = $this->translator->translate('fcadmin.appGeneral.model.filters.thousandsSeparator');
}
$price = $this->number($price, $decimals, $decimalSymbol, $thousandsSeparator);
return trim($price . ' ' . $currency);
}
public function priceNullable(null|float|string $price, string $currency, int $decimals = 2, ?string $decimalSymbol = null, ?string $thousandsSeparator = null): ?string
{
if ($price === null) {
return '---';
}
return $this->price($price, $currency, $decimals, $decimalSymbol, $thousandsSeparator);
}
public function ifEmpty(FilterInfo $info, $string)
{
if ((string)$string === '') {
$info->contentType = ContentType::Html;
return '<span class="empty">' . $this->translator->translate('fcadmin.appGeneral.model.filters.empty') . '</span>';
}
return $string;
}
public function boolIcon(FilterInfo $info, ?bool $value): string
{
$info->contentType = ContentType::Html;
return $value === null ? '<i class="fa-regular fa-square" style="color: #999999"></i>' : ($value ? '<i class="fa-solid fa-square-check" style="color: #28C885"></i>' : '<i class="fa-solid fa-square-xmark" style="color: #FF4242"></i>');
}
public function darken(string $hexColor, int $percent): string
{
// Odstranit "#" z barvy, pokud je přítomna
$hexColor = ltrim($hexColor, '#');
// Pokud je barva ve zkráceném formátu (#abc), rozšířit ji na plnou (#aabbcc)
if (strlen($hexColor) == 3) {
$hexColor = $hexColor[0] . $hexColor[0] . $hexColor[1] . $hexColor[1] . $hexColor[2] . $hexColor[2];
}
// Rozdělit barvu na jednotlivé složky RGB
$r = hexdec(substr($hexColor, 0, 2)) / 255;
$g = hexdec(substr($hexColor, 2, 2)) / 255;
$b = hexdec(substr($hexColor, 4, 2)) / 255;
// Převod z RGB do HSL
$max = max($r, $g, $b);
$min = min($r, $g, $b);
$l = ($max + $min) / 2; // Světlost
$s = $max == $min ? 0 : ($l > 0.5 ? ($max - $min) / (2 - $max - $min) : ($max - $min) / ($max + $min));
$h = $max == $r
? fmod((60 * (($g - $b) / ($max - $min)) + 360), 360)
: ($max == $g
? fmod((60 * (($b - $r) / ($max - $min)) + 120), 360)
: fmod((60 * (($r - $g) / ($max - $min)) + 240), 360));
// Snížit světlost
$l = max(0, $l - $percent / 100);
// Převod zpět z HSL do RGB
$c = (1 - abs(2 * $l - 1)) * $s;
$x = $c * (1 - abs(fmod($h / 60, 2) - 1));
$m = $l - $c / 2;
if ($h < 60) {
$r = $c;
$g = $x;
$b = 0;
} elseif ($h < 120) {
$r = $x;
$g = $c;
$b = 0;
} elseif ($h < 180) {
$r = 0;
$g = $c;
$b = $x;
} elseif ($h < 240) {
$r = 0;
$g = $x;
$b = $c;
} elseif ($h < 300) {
$r = $x;
$g = 0;
$b = $c;
} else {
$r = $c;
$g = 0;
$b = $x;
}
// Normalizace a převod zpět do hexadecimálního formátu
$r = ($r + $m) * 255;
$g = ($g + $m) * 255;
$b = ($b + $m) * 255;
return sprintf('#%02x%02x%02x', round($r), round($g), round($b));
}
public function lighten(string $hexColor, int $percent): string
{
// Odstranit "#" z barvy, pokud je přítomna
$hexColor = ltrim($hexColor, '#');
// Pokud je barva ve zkráceném formátu (#abc), rozšířit ji na plnou (#aabbcc)
if (strlen($hexColor) == 3) {
$hexColor = $hexColor[0] . $hexColor[0] . $hexColor[1] . $hexColor[1] . $hexColor[2] . $hexColor[2];
}
// Rozdělit barvu na jednotlivé složky RGB
$r = hexdec(substr($hexColor, 0, 2));
$g = hexdec(substr($hexColor, 2, 2));
$b = hexdec(substr($hexColor, 4, 2));
// Vypočítat zesvětlené hodnoty
$r = max(0, min(255, $r + ($r * $percent / 100)));
$g = max(0, min(255, $g + ($g * $percent / 100)));
$b = max(0, min(255, $b + ($b * $percent / 100)));
// Převést zpět na hexadecimální hodnotu
return sprintf('#%02x%02x%02x', $r, $g, $b);
}
public static function invertColor(string $hexColor): string
{
// Odstranit "#" z barvy, pokud je přítomna
$hexColor = ltrim($hexColor, '#');
// Pokud je barva ve zkráceném formátu (#abc), rozšířit ji na plnou (#aabbcc)
if (strlen($hexColor) == 3) {
$hexColor = $hexColor[0] . $hexColor[0] . $hexColor[1] . $hexColor[1] . $hexColor[2] . $hexColor[2];
}
// Rozdělit barvu na jednotlivé složky RGB
$r = hexdec(substr($hexColor, 0, 2));
$g = hexdec(substr($hexColor, 2, 2));
$b = hexdec(substr($hexColor, 4, 2));
// Inverze
$r = 255 - $r;
$g = 255 - $g;
$b = 255 - $b;
// nová hex hodnota
return sprintf("#%02x%02x%02x", $r, $g, $b);
}
}