2020-04-14 11:07:07 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* 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\Controller;
|
|
|
|
|
|
|
|
|
2020-04-16 19:56:30 +02:00
|
|
|
use App\Entity\Base\AbstractDBElement;
|
|
|
|
use App\Entity\Contracts\NamedElementInterface;
|
|
|
|
use App\Entity\LabelSystem\LabelOptions;
|
2020-04-14 11:07:07 +02:00
|
|
|
use App\Entity\LabelSystem\LabelProfile;
|
|
|
|
use App\Entity\Parts\Part;
|
2020-05-06 22:44:21 +02:00
|
|
|
use App\Exceptions\TwigModeException;
|
2020-04-16 19:56:30 +02:00
|
|
|
use App\Form\LabelOptionsType;
|
|
|
|
use App\Form\LabelSystem\LabelDialogType;
|
2020-04-14 11:07:07 +02:00
|
|
|
use App\Helpers\LabelResponse;
|
2020-04-21 00:00:29 +02:00
|
|
|
use App\Repository\DBElementRepository;
|
2020-04-16 19:56:30 +02:00
|
|
|
use App\Services\ElementTypeNameGenerator;
|
2020-04-14 11:07:07 +02:00
|
|
|
use App\Services\LabelSystem\LabelGenerator;
|
2020-04-21 00:00:29 +02:00
|
|
|
use App\Services\Misc\RangeParser;
|
2020-04-16 19:56:30 +02:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2020-04-14 11:07:07 +02:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
2020-05-06 22:44:21 +02:00
|
|
|
use Symfony\Component\Form\FormError;
|
2020-04-16 19:56:30 +02:00
|
|
|
use Symfony\Component\Form\SubmitButton;
|
2020-04-14 11:07:07 +02:00
|
|
|
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
2020-04-16 19:56:30 +02:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2020-04-14 11:07:07 +02:00
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
|
|
|
|
use Symfony\Component\Routing\Annotation\Route;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Route("/label")
|
|
|
|
* @package App\Controller
|
|
|
|
*/
|
|
|
|
class LabelController extends AbstractController
|
|
|
|
{
|
|
|
|
protected $labelGenerator;
|
2020-04-16 19:56:30 +02:00
|
|
|
protected $em;
|
|
|
|
protected $elementTypeNameGenerator;
|
2020-04-21 00:00:29 +02:00
|
|
|
protected $rangeParser;
|
2020-04-14 11:07:07 +02:00
|
|
|
|
2020-05-06 22:44:21 +02:00
|
|
|
public function __construct(LabelGenerator $labelGenerator, EntityManagerInterface $em, ElementTypeNameGenerator $elementTypeNameGenerator,
|
|
|
|
RangeParser $rangeParser)
|
2020-04-14 11:07:07 +02:00
|
|
|
{
|
|
|
|
$this->labelGenerator = $labelGenerator;
|
2020-04-16 19:56:30 +02:00
|
|
|
$this->em = $em;
|
|
|
|
$this->elementTypeNameGenerator = $elementTypeNameGenerator;
|
2020-04-21 00:00:29 +02:00
|
|
|
$this->rangeParser = $rangeParser;
|
2020-04-14 11:07:07 +02:00
|
|
|
}
|
|
|
|
|
2020-04-16 19:56:30 +02:00
|
|
|
/**
|
|
|
|
* @Route("/dialog", name="label_dialog")
|
2020-05-01 20:29:18 +02:00
|
|
|
* @Route("/{profile}/dialog", name="label_dialog_profile")
|
2020-04-16 19:56:30 +02:00
|
|
|
*/
|
|
|
|
public function generator(Request $request, ?LabelProfile $profile = null)
|
|
|
|
{
|
2020-05-04 23:21:58 +02:00
|
|
|
$this->denyAccessUnlessGranted('@labels.create_labels');
|
|
|
|
|
|
|
|
//If we inherit a LabelProfile, the user need to have access to it...
|
|
|
|
if ($profile !== null) {
|
|
|
|
$this->denyAccessUnlessGranted('read', $profile);
|
|
|
|
}
|
|
|
|
|
2020-04-16 19:56:30 +02:00
|
|
|
if ($profile) {
|
|
|
|
$label_options = $profile->getOptions();
|
|
|
|
} else {
|
|
|
|
$label_options = new LabelOptions();
|
|
|
|
}
|
|
|
|
|
|
|
|
$form = $this->createForm(LabelDialogType::class);
|
|
|
|
|
|
|
|
//Try to parse given target_type and target_id
|
|
|
|
$target_type = $request->query->get('target_type', null);
|
|
|
|
$target_id = $request->query->get('target_id', null);
|
2020-05-01 20:29:18 +02:00
|
|
|
$generate = $request->query->getBoolean('generate', false);
|
|
|
|
|
2020-04-16 19:56:30 +02:00
|
|
|
if ($profile === null && is_string($target_type)) {
|
|
|
|
$label_options->setSupportedElement($target_type);
|
|
|
|
}
|
2020-04-21 00:00:29 +02:00
|
|
|
if (is_string($target_id)) {
|
|
|
|
$form['target_id']->setData($target_id);
|
2020-04-16 19:56:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$form['options']->setData($label_options);
|
|
|
|
$form->handleRequest($request);
|
|
|
|
|
|
|
|
/** @var LabelOptions $form_options */
|
|
|
|
$form_options = $form['options']->getData();
|
|
|
|
|
|
|
|
$pdf_data = null;
|
|
|
|
$filename = 'invalid.pdf';
|
|
|
|
|
2020-05-01 20:29:18 +02:00
|
|
|
//Generate PDF either when the form is submitted and valid, or the form was not submit yet, and generate is set
|
|
|
|
if (($form->isSubmitted() && $form->isValid()) || ($generate && !$form->isSubmitted() && $profile !== null)) {
|
2020-04-21 00:00:29 +02:00
|
|
|
$target_id = (string) $form->get('target_id')->getData();
|
|
|
|
$targets = $this->findObjects($form_options->getSupportedElement(), $target_id);
|
2020-05-05 19:55:31 +02:00
|
|
|
if (!empty($targets)) {
|
2020-05-06 22:44:21 +02:00
|
|
|
try {
|
|
|
|
$pdf_data = $this->labelGenerator->generateLabel($form_options, $targets);
|
|
|
|
$filename = $this->getLabelName($targets[0], $profile);
|
|
|
|
} catch (TwigModeException $exception) {
|
|
|
|
$form->get('options')->get('lines')->addError(new FormError($exception->getMessage()));
|
|
|
|
}
|
|
|
|
|
2020-05-05 19:55:31 +02:00
|
|
|
} else {
|
|
|
|
$this->addFlash('warning', 'label_generator.no_entities_found');
|
|
|
|
}
|
2020-04-16 19:56:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->render('LabelSystem/dialog.html.twig', [
|
|
|
|
'form' => $form->createView(),
|
|
|
|
'pdf_data' => $pdf_data,
|
|
|
|
'filename' => $filename,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getLabelName(AbstractDBElement $element, ?LabelProfile $profile = null): string
|
|
|
|
{
|
|
|
|
$ret = 'label_' . $this->elementTypeNameGenerator->getLocalizedTypeLabel($element);
|
|
|
|
$ret .= $element->getID();
|
|
|
|
|
|
|
|
return $ret . '.pdf';
|
|
|
|
}
|
|
|
|
|
2020-04-21 00:00:29 +02:00
|
|
|
protected function findObjects(string $type, string $ids): array
|
2020-04-16 19:56:30 +02:00
|
|
|
{
|
|
|
|
if(!isset(LabelGenerator::CLASS_SUPPORT_MAPPING[$type])) {
|
|
|
|
throw new \InvalidArgumentException('The given type is not known and can not be mapped to a class!');
|
|
|
|
}
|
2020-04-21 00:00:29 +02:00
|
|
|
|
|
|
|
$id_array = $this->rangeParser->parse($ids);
|
|
|
|
|
|
|
|
/** @var DBElementRepository $repo */
|
|
|
|
$repo = $this->em->getRepository(LabelGenerator::CLASS_SUPPORT_MAPPING[$type]);
|
|
|
|
return $repo->getElementsFromIDArray($id_array);
|
2020-04-16 19:56:30 +02:00
|
|
|
}
|
2020-04-14 11:07:07 +02:00
|
|
|
}
|