1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* part-db version 0.1 |
4
|
|
|
* Copyright (C) 2005 Christoph Lechner |
5
|
|
|
* http://www.cl-projects.de/. |
6
|
|
|
* |
7
|
|
|
* part-db version 0.2+ |
8
|
|
|
* Copyright (C) 2009 K. Jacobs and others (see authors.php) |
9
|
|
|
* http://code.google.com/p/part-db/ |
10
|
|
|
* |
11
|
|
|
* Part-DB Version 0.4+ |
12
|
|
|
* Copyright (C) 2016 - 2019 Jan Böhmer |
13
|
|
|
* https://github.com/jbtronics |
14
|
|
|
* |
15
|
|
|
* This program is free software; you can redistribute it and/or |
16
|
|
|
* modify it under the terms of the GNU General Public License |
17
|
|
|
* as published by the Free Software Foundation; either version 2 |
18
|
|
|
* of the License, or (at your option) any later version. |
19
|
|
|
* |
20
|
|
|
* This program is distributed in the hope that it will be useful, |
21
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
22
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23
|
|
|
* GNU General Public License for more details. |
24
|
|
|
* |
25
|
|
|
* You should have received a copy of the GNU General Public License |
26
|
|
|
* along with this program; if not, write to the Free Software |
27
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
28
|
|
|
*/ |
29
|
|
|
|
30
|
|
|
namespace App\Twig; |
31
|
|
|
|
32
|
|
|
use App\Entity\Attachments\Attachment; |
33
|
|
|
use App\Entity\Base\DBElement; |
34
|
|
|
use App\Entity\Parts\MeasurementUnit; |
35
|
|
|
use App\Services\AmountFormatter; |
36
|
|
|
use App\Services\EntityURLGenerator; |
37
|
|
|
use App\Services\MoneyFormatter; |
38
|
|
|
use App\Services\SIFormatter; |
39
|
|
|
use App\Services\TreeBuilder; |
40
|
|
|
use Symfony\Component\Cache\Adapter\AdapterInterface; |
41
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
42
|
|
|
use Twig\Extension\AbstractExtension; |
43
|
|
|
use Twig\TwigFilter; |
44
|
|
|
use s9e\TextFormatter\Bundles\Forum as TextFormatter; |
45
|
|
|
use Twig\TwigFunction; |
46
|
|
|
use Twig\TwigTest; |
47
|
|
|
|
48
|
|
|
class AppExtension extends AbstractExtension |
49
|
|
|
{ |
50
|
|
|
protected $entityURLGenerator; |
51
|
|
|
protected $cache; |
52
|
|
|
protected $serializer; |
53
|
|
|
protected $treeBuilder; |
54
|
|
|
protected $moneyFormatter; |
55
|
|
|
protected $siformatter; |
56
|
|
|
protected $amountFormatter; |
57
|
|
|
|
58
|
|
|
public function __construct(EntityURLGenerator $entityURLGenerator, AdapterInterface $cache, |
59
|
|
|
SerializerInterface $serializer, TreeBuilder $treeBuilder, |
60
|
|
|
MoneyFormatter $moneyFormatter, |
61
|
|
|
SIFormatter $SIFormatter, AmountFormatter $amountFormatter) |
62
|
|
|
{ |
63
|
|
|
$this->entityURLGenerator = $entityURLGenerator; |
64
|
|
|
$this->cache = $cache; |
65
|
|
|
$this->serializer = $serializer; |
66
|
|
|
$this->treeBuilder = $treeBuilder; |
67
|
|
|
$this->moneyFormatter = $moneyFormatter; |
68
|
|
|
$this->siformatter = $SIFormatter; |
69
|
|
|
$this->amountFormatter = $amountFormatter; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function getFilters() |
73
|
|
|
{ |
74
|
|
|
return [ |
75
|
|
|
new TwigFilter('entityURL', [$this, 'generateEntityURL']), |
76
|
|
|
new TwigFilter('bbCode', [$this, 'parseBBCode'], ['pre_escape' => 'html', 'is_safe' => ['html']]), |
77
|
|
|
new TwigFilter('moneyFormat', [$this, 'formatCurrency']), |
78
|
|
|
new TwigFilter('siFormat', [$this, 'siFormat']), |
79
|
|
|
new TwigFilter('amountFormat', [$this, 'amountFormat']) |
80
|
|
|
]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function getTests() |
84
|
|
|
{ |
85
|
|
|
return [ |
86
|
|
|
new TwigTest('instanceof', function ($var, $instance) { |
87
|
|
|
return $var instanceof $instance; |
88
|
|
|
} ) |
89
|
|
|
]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function getFunctions() |
93
|
|
|
{ |
94
|
|
|
return [ |
95
|
|
|
new TwigFunction('generateTreeData', [$this, 'treeData']) |
96
|
|
|
]; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function treeData(DBElement $element, string $type = 'newEdit') : string |
100
|
|
|
{ |
101
|
|
|
$tree = $this->treeBuilder->typeToTree(get_class($element), $type, $element); |
102
|
|
|
return $this->serializer->serialize($tree, 'json', ['skip_null_values' => true]); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function generateEntityURL(DBElement $entity, string $method = 'info'): string |
106
|
|
|
{ |
107
|
|
|
return $this->entityURLGenerator->getURL($entity, $method); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function parseBBCode(string $bbcode): string |
111
|
|
|
{ |
112
|
|
|
if ('' === $bbcode) { |
113
|
|
|
return ''; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$item = $this->cache->getItem('bbcode_'.md5($bbcode)); |
117
|
|
|
if (!$item->isHit()) { |
118
|
|
|
$xml = TextFormatter::parse($bbcode); |
119
|
|
|
$item->set(TextFormatter::render($xml)); |
120
|
|
|
$this->cache->save($item); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $item->get(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function formatCurrency($amount, $currency = "") |
127
|
|
|
{ |
128
|
|
|
return $this->moneyFormatter->format($amount, $currency); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function siFormat($value, $unit, $decimals = 2) |
132
|
|
|
{ |
133
|
|
|
return $this->siformatter->format($value, $unit, $decimals); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function amountFormat($value, ?MeasurementUnit $unit, array $options = []) |
137
|
|
|
{ |
138
|
|
|
return $this->amountFormatter->format($value, $unit, $options); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|