-
-
Notifications
You must be signed in to change notification settings - Fork 522
/
Extensions.php
250 lines (226 loc) · 6.54 KB
/
Extensions.php
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
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Twig;
use App\Constants;
use App\Entity\Timesheet;
use App\Utils\Duration;
use App\Utils\LocaleSettings;
use NumberFormatter;
use Symfony\Component\Intl\Intl;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
/**
* Multiple Twig extensions: filters and functions
*/
class Extensions extends AbstractExtension
{
/**
* @var LocaleSettings
*/
protected $localeSettings;
/**
* @var string
*/
protected $locale;
/**
* @var Duration
*/
protected $durationFormatter;
/**
* @var NumberFormatter
*/
protected $numberFormatter;
/**
* @var NumberFormatter
*/
protected $moneyFormatter;
/**
* @var string[]
*/
protected static $icons = [
'activity' => 'fas fa-tasks',
'admin' => 'fas fa-wrench',
'calendar' => 'far fa-calendar-alt',
'customer' => 'fas fa-users',
'copy' => 'far fa-copy',
'create' => 'far fa-plus-square',
'dashboard' => 'fas fa-tachometer-alt',
'delete' => 'far fa-trash-alt',
'download' => 'fas fa-download',
'duration' => 'far fa-hourglass',
'edit' => 'far fa-edit',
'filter' => 'fas fa-filter',
'help' => 'far fa-question-circle',
'invoice' => 'fas fa-file-invoice',
'list' => 'fas fa-list',
'logout' => 'fas fa-sign-out-alt',
'manual' => 'fas fa-book',
'money' => 'far fa-money-bill-alt',
'print' => 'fas fa-print',
'project' => 'fas fa-project-diagram',
'repeat' => 'fas fa-redo-alt',
'start' => 'fas fa-play-circle',
'start-small' => 'far fa-play-circle',
'stop' => 'fas fa-stop',
'stop-small' => 'far fa-stop-circle',
'timesheet' => 'fas fa-clock',
'trash' => 'far fa-trash-alt',
'user' => 'fas fa-user',
'visibility' => 'far fa-eye',
'settings' => 'fas fa-cog',
'export' => 'fas fa-file-export',
'pdf' => 'fas fa-file-pdf',
'csv' => 'fas fa-table',
'ods' => 'fas fa-table',
'xlsx' => 'fas fa-file-excel',
'on' => 'fas fa-toggle-on',
'off' => 'fas fa-toggle-off',
'audit' => 'fas fa-history',
'home' => 'fas fa-home',
'shop' => 'fas fa-shopping-cart',
'about' => 'fas fa-info-circle',
'debug' => 'far fa-file-alt',
'profile-stats' => 'far fa-chart-bar',
'profile' => 'fas fa-user-edit',
'warning' => 'fas fa-exclamation-triangle',
];
/**
* @param LocaleSettings $localeSettings
*/
public function __construct(LocaleSettings $localeSettings)
{
$this->localeSettings = $localeSettings;
$this->durationFormatter = new Duration();
}
/**
* {@inheritdoc}
*/
public function getFilters()
{
return [
new TwigFilter('duration', [$this, 'duration']),
new TwigFilter('money', [$this, 'money']),
new TwigFilter('currency', [$this, 'currency']),
new TwigFilter('country', [$this, 'country']),
new TwigFilter('icon', [$this, 'icon']),
new TwigFilter('docu_link', [$this, 'documentationLink']),
];
}
/**
* {@inheritdoc}
*/
public function getFunctions()
{
return [
new TwigFunction('locales', [$this, 'getLocales']),
new TwigFunction('class_name', [$this, 'getClassName']),
];
}
/**
* @param $object
* @return null|string
*/
public function getClassName($object)
{
if (!is_object($object)) {
return null;
}
return get_class($object);
}
/**
* Transforms seconds into a duration string.
*
* @param int|Timesheet $duration
* @param string $format
* @return string
*/
public function duration($duration, $format = null)
{
$seconds = $duration;
if ($duration instanceof Timesheet) {
$seconds = $duration->getDuration();
if (null === $duration->getEnd()) {
$seconds = time() - $duration->getBegin()->getTimestamp();
}
}
if ($seconds < 0) {
return '?';
}
if (null === $format) {
$format = $this->localeSettings->getDurationFormat();
}
return $this->durationFormatter->format($seconds, $format);
}
/**
* @param string $currency
* @return string
*/
public function currency($currency)
{
return Intl::getCurrencyBundle()->getCurrencySymbol($currency);
}
/**
* @param string $country
* @return string
*/
public function country($country)
{
return Intl::getRegionBundle()->getCountryName($country);
}
/**
* @param string $name
* @param string $default
* @return string
*/
public function icon($name, $default = '')
{
return self::$icons[$name] ?? $default;
}
/**
* @param string $url
* @return string
*/
public function documentationLink($url = '')
{
return Constants::HOMEPAGE . '/documentation/' . $url;
}
/**
* @param float $amount
* @param string $currency
* @return string
*/
public function money($amount, $currency = null)
{
$locale = $this->localeSettings->getLocale();
if ($this->locale !== $locale) {
$this->locale = $locale;
$this->numberFormatter = new NumberFormatter($locale, NumberFormatter::DECIMAL);
$this->moneyFormatter = new NumberFormatter($locale, NumberFormatter::CURRENCY);
}
if (null !== $currency) {
return $this->moneyFormatter->formatCurrency($amount, $currency);
}
return $this->numberFormatter->format($amount);
}
/**
* Takes the list of codes of the locales (languages) enabled in the
* application and returns an array with the name of each locale written
* in its own language (e.g. English, Français, Español, etc.)
*
* @return array
*/
public function getLocales()
{
$locales = [];
foreach ($this->localeSettings->getAvailableLanguages() as $locale) {
$locales[] = ['code' => $locale, 'name' => Intl::getLocaleBundle()->getLocaleName($locale, $locale)];
}
return $locales;
}
}