2020-04-14 11:07:07 +02:00
|
|
|
<?php
|
2020-05-10 21:39:31 +02:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-04-14 11:07:07 +02:00
|
|
|
/**
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Services\LabelSystem;
|
|
|
|
|
|
|
|
use App\Entity\Contracts\NamedElementInterface;
|
|
|
|
use App\Entity\LabelSystem\LabelOptions;
|
2020-05-06 22:44:21 +02:00
|
|
|
use App\Exceptions\TwigModeException;
|
2020-04-14 11:07:07 +02:00
|
|
|
use App\Services\ElementTypeNameGenerator;
|
2020-05-06 22:44:21 +02:00
|
|
|
use Symfony\Component\Security\Core\Security;
|
2020-04-14 11:07:07 +02:00
|
|
|
use Twig\Environment;
|
2020-05-06 22:44:21 +02:00
|
|
|
use Twig\Error\Error;
|
2020-04-14 11:07:07 +02:00
|
|
|
|
2020-05-06 22:44:21 +02:00
|
|
|
final class LabelHTMLGenerator
|
2020-04-14 11:07:07 +02:00
|
|
|
{
|
2020-05-09 18:17:23 +02:00
|
|
|
private $twig;
|
|
|
|
private $elementTypeNameGenerator;
|
|
|
|
private $replacer;
|
|
|
|
private $barcodeGenerator;
|
|
|
|
private $sandboxedTwigProvider;
|
|
|
|
private $partdb_title;
|
|
|
|
private $security;
|
2020-04-14 11:07:07 +02:00
|
|
|
|
2020-04-24 22:10:49 +02:00
|
|
|
public function __construct(ElementTypeNameGenerator $elementTypeNameGenerator, LabelTextReplacer $replacer, Environment $twig,
|
2020-05-06 22:44:21 +02:00
|
|
|
BarcodeGenerator $barcodeGenerator, SandboxedTwigProvider $sandboxedTwigProvider, Security $security, string $partdb_title)
|
2020-04-14 11:07:07 +02:00
|
|
|
{
|
|
|
|
$this->twig = $twig;
|
|
|
|
$this->elementTypeNameGenerator = $elementTypeNameGenerator;
|
2020-04-14 17:21:30 +02:00
|
|
|
$this->replacer = $replacer;
|
2020-04-24 22:10:49 +02:00
|
|
|
$this->barcodeGenerator = $barcodeGenerator;
|
2020-05-06 22:44:21 +02:00
|
|
|
$this->sandboxedTwigProvider = $sandboxedTwigProvider;
|
|
|
|
$this->security = $security;
|
|
|
|
$this->partdb_title = $partdb_title;
|
2020-04-14 11:07:07 +02:00
|
|
|
}
|
|
|
|
|
2020-04-21 00:00:29 +02:00
|
|
|
public function getLabelHTML(LabelOptions $options, array $elements): string
|
2020-04-14 11:07:07 +02:00
|
|
|
{
|
2020-04-21 00:00:29 +02:00
|
|
|
if (empty($elements)) {
|
|
|
|
throw new \InvalidArgumentException('$elements must not be empty');
|
|
|
|
}
|
|
|
|
|
|
|
|
$twig_elements = [];
|
2020-05-06 22:44:21 +02:00
|
|
|
|
2020-05-10 21:39:31 +02:00
|
|
|
if ('twig' === $options->getLinesMode()) {
|
2020-05-06 22:44:21 +02:00
|
|
|
$sandboxed_twig = $this->sandboxedTwigProvider->getTwig($options);
|
|
|
|
$current_user = $this->security->getUser();
|
|
|
|
}
|
|
|
|
|
|
|
|
$page = 1;
|
2020-04-21 00:00:29 +02:00
|
|
|
foreach ($elements as $element) {
|
2020-05-10 21:39:31 +02:00
|
|
|
if ('twig' === $options->getLinesMode() && isset($sandboxed_twig) && isset($current_user)) {
|
2020-05-06 22:44:21 +02:00
|
|
|
try {
|
|
|
|
$lines = $sandboxed_twig->render(
|
|
|
|
'lines',
|
|
|
|
[
|
|
|
|
'element' => $element,
|
|
|
|
'page' => $page,
|
|
|
|
'user' => $current_user,
|
|
|
|
'install_title' => $this->partdb_title,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
} catch (Error $exception) {
|
|
|
|
throw new TwigModeException($exception);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$lines = $this->replacer->replace($options->getLines(), $element);
|
|
|
|
}
|
|
|
|
|
2020-04-21 00:00:29 +02:00
|
|
|
$twig_elements[] = [
|
|
|
|
'element' => $element,
|
2020-05-06 22:44:21 +02:00
|
|
|
'lines' => $lines,
|
2020-04-24 22:10:49 +02:00
|
|
|
'barcode' => $this->barcodeGenerator->generateSVG($options, $element),
|
|
|
|
'barcode_content' => $this->barcodeGenerator->getContent($options, $element),
|
2020-04-21 00:00:29 +02:00
|
|
|
];
|
2020-05-06 22:44:21 +02:00
|
|
|
|
2020-05-10 21:39:31 +02:00
|
|
|
++$page;
|
2020-04-21 00:00:29 +02:00
|
|
|
}
|
|
|
|
|
2020-04-16 19:56:30 +02:00
|
|
|
return $this->twig->render('LabelSystem/labels/base_label.html.twig', [
|
2020-04-21 00:00:29 +02:00
|
|
|
'meta_title' => $this->getPDFTitle($options, $elements[0]),
|
|
|
|
'elements' => $twig_elements,
|
2020-04-24 22:10:49 +02:00
|
|
|
'options' => $options,
|
2020-04-14 11:07:07 +02:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2020-05-10 21:39:31 +02:00
|
|
|
private function getPDFTitle(LabelOptions $options, object $element)
|
2020-04-14 11:07:07 +02:00
|
|
|
{
|
|
|
|
if ($element instanceof NamedElementInterface) {
|
|
|
|
return $this->elementTypeNameGenerator->getTypeNameCombination($element, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'Part-DB label';
|
|
|
|
}
|
2020-05-10 21:39:31 +02:00
|
|
|
}
|