|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics) |
|
6
|
|
|
* |
|
7
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
8
|
|
|
* it under the terms of the GNU Affero General Public License as published |
|
9
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
|
10
|
|
|
* (at your option) any later version. |
|
11
|
|
|
* |
|
12
|
|
|
* This program is distributed in the hope that it will be useful, |
|
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15
|
|
|
* GNU Affero General Public License for more details. |
|
16
|
|
|
* |
|
17
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
18
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
declare(strict_types=1); |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
|
25
|
|
|
* |
|
26
|
|
|
* Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics) |
|
27
|
|
|
* |
|
28
|
|
|
* This program is free software; you can redistribute it and/or |
|
29
|
|
|
* modify it under the terms of the GNU General Public License |
|
30
|
|
|
* as published by the Free Software Foundation; either version 2 |
|
31
|
|
|
* of the License, or (at your option) any later version. |
|
32
|
|
|
* |
|
33
|
|
|
* This program is distributed in the hope that it will be useful, |
|
34
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
35
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
36
|
|
|
* GNU General Public License for more details. |
|
37
|
|
|
* |
|
38
|
|
|
* You should have received a copy of the GNU General Public License |
|
39
|
|
|
* along with this program; if not, write to the Free Software |
|
40
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
|
41
|
|
|
*/ |
|
42
|
|
|
|
|
43
|
|
|
namespace App\Twig; |
|
44
|
|
|
|
|
45
|
|
|
use App\Entity\Base\AbstractDBElement; |
|
46
|
|
|
use App\Entity\Parts\MeasurementUnit; |
|
47
|
|
|
use App\Entity\PriceInformations\Currency; |
|
48
|
|
|
use App\Services\AmountFormatter; |
|
49
|
|
|
use App\Services\Attachments\AttachmentURLGenerator; |
|
50
|
|
|
use App\Services\EntityURLGenerator; |
|
51
|
|
|
use App\Services\FAIconGenerator; |
|
52
|
|
|
use App\Services\MarkdownParser; |
|
53
|
|
|
use App\Services\MoneyFormatter; |
|
54
|
|
|
use App\Services\SIFormatter; |
|
55
|
|
|
use App\Services\Trees\TreeViewGenerator; |
|
56
|
|
|
use Brick\Math\BigDecimal; |
|
57
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
|
58
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
|
59
|
|
|
use Twig\Extension\AbstractExtension; |
|
60
|
|
|
use Twig\TwigFilter; |
|
61
|
|
|
use Twig\TwigFunction; |
|
62
|
|
|
use Twig\TwigTest; |
|
63
|
|
|
|
|
64
|
|
|
class AppExtension extends AbstractExtension |
|
65
|
|
|
{ |
|
66
|
|
|
protected $entityURLGenerator; |
|
67
|
|
|
protected $markdownParser; |
|
68
|
|
|
protected $serializer; |
|
69
|
|
|
protected $treeBuilder; |
|
70
|
|
|
protected $moneyFormatter; |
|
71
|
|
|
protected $siformatter; |
|
72
|
|
|
protected $amountFormatter; |
|
73
|
|
|
protected $attachmentURLGenerator; |
|
74
|
|
|
protected $FAIconGenerator; |
|
75
|
|
|
protected $translator; |
|
76
|
|
|
|
|
77
|
|
|
public function __construct(EntityURLGenerator $entityURLGenerator, MarkdownParser $markdownParser, |
|
78
|
|
|
SerializerInterface $serializer, TreeViewGenerator $treeBuilder, |
|
79
|
|
|
MoneyFormatter $moneyFormatter, |
|
80
|
|
|
SIFormatter $SIFormatter, AmountFormatter $amountFormatter, |
|
81
|
|
|
AttachmentURLGenerator $attachmentURLGenerator, |
|
82
|
|
|
FAIconGenerator $FAIconGenerator, TranslatorInterface $translator) |
|
83
|
|
|
{ |
|
84
|
|
|
$this->entityURLGenerator = $entityURLGenerator; |
|
85
|
|
|
$this->markdownParser = $markdownParser; |
|
86
|
|
|
$this->serializer = $serializer; |
|
87
|
|
|
$this->treeBuilder = $treeBuilder; |
|
88
|
|
|
$this->moneyFormatter = $moneyFormatter; |
|
89
|
|
|
$this->siformatter = $SIFormatter; |
|
90
|
|
|
$this->amountFormatter = $amountFormatter; |
|
91
|
|
|
$this->attachmentURLGenerator = $attachmentURLGenerator; |
|
92
|
|
|
$this->FAIconGenerator = $FAIconGenerator; |
|
93
|
|
|
$this->translator = $translator; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function getFilters() |
|
97
|
|
|
{ |
|
98
|
|
|
return [ |
|
99
|
|
|
new TwigFilter('entityURL', [$this, 'generateEntityURL']), |
|
100
|
|
|
new TwigFilter('markdown', [$this->markdownParser, 'markForRendering'], [ |
|
101
|
|
|
'pre_escape' => 'html', |
|
102
|
|
|
'is_safe' => ['html'], |
|
103
|
|
|
]), |
|
104
|
|
|
new TwigFilter('moneyFormat', [$this, 'formatCurrency']), |
|
105
|
|
|
new TwigFilter('siFormat', [$this, 'siFormat']), |
|
106
|
|
|
new TwigFilter('amountFormat', [$this, 'amountFormat']), |
|
107
|
|
|
new TwigFilter('loginPath', [$this, 'loginPath']), |
|
108
|
|
|
]; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function getTests() |
|
112
|
|
|
{ |
|
113
|
|
|
return [ |
|
114
|
|
|
new TwigTest('instanceof', function ($var, $instance) { |
|
115
|
|
|
return $var instanceof $instance; |
|
116
|
|
|
}), |
|
117
|
|
|
]; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function getFunctions() |
|
121
|
|
|
{ |
|
122
|
|
|
return [ |
|
123
|
|
|
new TwigFunction('generateTreeData', [$this, 'treeData']), |
|
124
|
|
|
new TwigFunction('attachment_thumbnail', [$this->attachmentURLGenerator, 'getThumbnailURL']), |
|
125
|
|
|
new TwigFunction('ext_to_fa_icon', [$this->FAIconGenerator, 'fileExtensionToFAType']), |
|
126
|
|
|
]; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public function treeData(AbstractDBElement $element, string $type = 'newEdit'): string |
|
130
|
|
|
{ |
|
131
|
|
|
$tree = $this->treeBuilder->getTreeView(\get_class($element), null, $type, $element); |
|
132
|
|
|
|
|
133
|
|
|
return json_encode($tree); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* This function/filter generates an path. |
|
138
|
|
|
* |
|
139
|
|
|
* @return string |
|
140
|
|
|
*/ |
|
141
|
|
|
public function loginPath(string $path): string |
|
142
|
|
|
{ |
|
143
|
|
|
$parts = explode('/', $path); |
|
144
|
|
|
//Remove the part with |
|
145
|
|
|
unset($parts[1]); |
|
146
|
|
|
|
|
147
|
|
|
return implode('/', $parts); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
public function generateEntityURL(AbstractDBElement $entity, string $method = 'info'): string |
|
151
|
|
|
{ |
|
152
|
|
|
return $this->entityURLGenerator->getURL($entity, $method); |
|
|
|
|
|
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
public function formatCurrency($amount, ?Currency $currency = null, int $decimals = 5) |
|
156
|
|
|
{ |
|
157
|
|
|
if ($amount instanceof BigDecimal) { |
|
158
|
|
|
$amount = (string) $amount; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
return $this->moneyFormatter->format($amount, $currency, $decimals); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
public function siFormat($value, $unit, $decimals = 2, bool $show_all_digits = false) |
|
165
|
|
|
{ |
|
166
|
|
|
return $this->siformatter->format($value, $unit, $decimals, $show_all_digits); |
|
|
|
|
|
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
public function amountFormat($value, ?MeasurementUnit $unit, array $options = []) |
|
170
|
|
|
{ |
|
171
|
|
|
return $this->amountFormatter->format($value, $unit, $options); |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|