Part-DB.Part-DB-server/src/Controller/LabelController.php

148 lines
5.3 KiB
PHP
Raw Normal View History

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-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;
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;
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-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;
protected $rangeParser;
2020-04-14 11:07:07 +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;
$this->rangeParser = $rangeParser;
2020-04-14 11:07:07 +02:00
}
/**
* @Route("/{profile}/{part}/view")
*/
public function view(LabelProfile $profile, Part $part)
{
$label = $this->labelGenerator->generateLabel($profile->getOptions(), $part);
$response = new LabelResponse($label);
$response->setContentDisposition(ResponseHeaderBag::DISPOSITION_INLINE, 'label.pdf');
return $response;
}
2020-04-16 19:56:30 +02:00
/**
* @Route("/dialog", name="label_dialog")
* @Route("/{profile}/dialog", name="label_dialog_profile")
2020-04-16 19:56:30 +02:00
*/
public function generator(Request $request, ?LabelProfile $profile = null)
{
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);
$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);
}
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';
//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)) {
$target_id = (string) $form->get('target_id')->getData();
$targets = $this->findObjects($form_options->getSupportedElement(), $target_id);
$pdf_data = $this->labelGenerator->generateLabel($form_options, $targets);
$filename = $this->getLabelName($targets[0], $profile);
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';
}
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!');
}
$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
}