|
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
|
|
|
namespace App\Services\LabelSystem; |
|
22
|
|
|
|
|
23
|
|
|
use App\Entity\Contracts\NamedElementInterface; |
|
24
|
|
|
use App\Entity\LabelSystem\LabelOptions; |
|
25
|
|
|
use App\Exceptions\TwigModeException; |
|
26
|
|
|
use App\Services\ElementTypeNameGenerator; |
|
27
|
|
|
use App\Services\LabelSystem\Barcodes\BarcodeContentGenerator; |
|
28
|
|
|
use Symfony\Component\Security\Core\Security; |
|
29
|
|
|
use Twig\Environment; |
|
30
|
|
|
use Twig\Error\Error; |
|
31
|
|
|
use Twig\Error\SyntaxError; |
|
32
|
|
|
|
|
33
|
|
|
final class LabelHTMLGenerator |
|
34
|
|
|
{ |
|
35
|
|
|
private $twig; |
|
36
|
|
|
private $elementTypeNameGenerator; |
|
37
|
|
|
private $replacer; |
|
38
|
|
|
private $barcodeGenerator; |
|
39
|
|
|
private $sandboxedTwigProvider; |
|
40
|
|
|
private $partdb_title; |
|
41
|
|
|
private $security; |
|
42
|
|
|
|
|
43
|
|
|
public function __construct(ElementTypeNameGenerator $elementTypeNameGenerator, LabelTextReplacer $replacer, Environment $twig, |
|
44
|
|
|
BarcodeGenerator $barcodeGenerator, SandboxedTwigProvider $sandboxedTwigProvider, Security $security, string $partdb_title) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->twig = $twig; |
|
47
|
|
|
$this->elementTypeNameGenerator = $elementTypeNameGenerator; |
|
48
|
|
|
$this->replacer = $replacer; |
|
49
|
|
|
$this->barcodeGenerator = $barcodeGenerator; |
|
50
|
|
|
$this->sandboxedTwigProvider = $sandboxedTwigProvider; |
|
51
|
|
|
$this->security = $security; |
|
52
|
|
|
$this->partdb_title = $partdb_title; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function getLabelHTML(LabelOptions $options, array $elements): string |
|
56
|
|
|
{ |
|
57
|
|
|
if (empty($elements)) { |
|
58
|
|
|
throw new \InvalidArgumentException('$elements must not be empty'); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$twig_elements = []; |
|
62
|
|
|
|
|
63
|
|
|
if ($options->getLinesMode() === 'twig') { |
|
64
|
|
|
$sandboxed_twig = $this->sandboxedTwigProvider->getTwig($options); |
|
65
|
|
|
$current_user = $this->security->getUser(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$page = 1; |
|
69
|
|
|
foreach ($elements as $element) { |
|
70
|
|
|
if ($options->getLinesMode() === 'twig' && isset($sandboxed_twig) && isset($current_user)) { |
|
71
|
|
|
try { |
|
72
|
|
|
$lines = $sandboxed_twig->render( |
|
73
|
|
|
'lines', |
|
74
|
|
|
[ |
|
75
|
|
|
'element' => $element, |
|
76
|
|
|
'page' => $page, |
|
77
|
|
|
'user' => $current_user, |
|
78
|
|
|
'install_title' => $this->partdb_title, |
|
79
|
|
|
] |
|
80
|
|
|
); |
|
81
|
|
|
} catch (Error $exception) { |
|
82
|
|
|
throw new TwigModeException($exception); |
|
83
|
|
|
} |
|
84
|
|
|
} else { |
|
85
|
|
|
$lines = $this->replacer->replace($options->getLines(), $element); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$twig_elements[] = [ |
|
89
|
|
|
'element' => $element, |
|
90
|
|
|
'lines' => $lines, |
|
91
|
|
|
'barcode' => $this->barcodeGenerator->generateSVG($options, $element), |
|
92
|
|
|
'barcode_content' => $this->barcodeGenerator->getContent($options, $element), |
|
93
|
|
|
]; |
|
94
|
|
|
|
|
95
|
|
|
$page++; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
return $this->twig->render('LabelSystem/labels/base_label.html.twig', [ |
|
100
|
|
|
'meta_title' => $this->getPDFTitle($options, $elements[0]), |
|
101
|
|
|
'elements' => $twig_elements, |
|
102
|
|
|
'options' => $options, |
|
103
|
|
|
]); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
protected function getPDFTitle(LabelOptions $options, object $element) |
|
|
|
|
|
|
108
|
|
|
{ |
|
109
|
|
|
if ($element instanceof NamedElementInterface) { |
|
110
|
|
|
return $this->elementTypeNameGenerator->getTypeNameCombination($element, false); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return 'Part-DB label'; |
|
114
|
|
|
} |
|
115
|
|
|
} |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.