Issues (257)

src/Controller/LabelController.php (1 issue)

Severity
1
<?php
2
/*
3
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4
 *
5
 *  Copyright (C) 2019 - 2022 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
declare(strict_types=1);
22
23
/**
24
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
25
 *
26
 * Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
27
 *
28
 * This program is free software: you can redistribute it and/or modify
29
 * it under the terms of the GNU Affero General Public License as published
30
 * by the Free Software Foundation, either version 3 of the License, or
31
 * (at your option) any later version.
32
 *
33
 * This program is distributed in the hope that it will be useful,
34
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36
 * GNU Affero General Public License for more details.
37
 *
38
 * You should have received a copy of the GNU Affero General Public License
39
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
40
 */
41
42
namespace App\Controller;
43
44
use App\Entity\Base\AbstractDBElement;
45
use App\Entity\LabelSystem\LabelOptions;
46
use App\Entity\LabelSystem\LabelProfile;
47
use App\Exceptions\TwigModeException;
48
use App\Form\LabelSystem\LabelDialogType;
49
use App\Repository\DBElementRepository;
50
use App\Services\ElementTypeNameGenerator;
51
use App\Services\LabelSystem\LabelGenerator;
52
use App\Services\Misc\RangeParser;
53
use Doctrine\ORM\EntityManagerInterface;
54
use InvalidArgumentException;
55
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
56
use Symfony\Component\Form\FormError;
57
use Symfony\Component\HttpFoundation\Request;
58
use Symfony\Component\HttpFoundation\Response;
59
use Symfony\Component\Routing\Annotation\Route;
60
use Symfony\Contracts\Translation\TranslatorInterface;
61
62
/**
63
 * @Route("/label")
64
 */
65
class LabelController extends AbstractController
66
{
67
    protected LabelGenerator $labelGenerator;
68
    protected EntityManagerInterface $em;
69
    protected ElementTypeNameGenerator $elementTypeNameGenerator;
70
    protected RangeParser $rangeParser;
71
    protected TranslatorInterface $translator;
72
73
    public function __construct(LabelGenerator $labelGenerator, EntityManagerInterface $em, ElementTypeNameGenerator $elementTypeNameGenerator,
74
        RangeParser $rangeParser, TranslatorInterface $translator)
75
    {
76
        $this->labelGenerator = $labelGenerator;
77
        $this->em = $em;
78
        $this->elementTypeNameGenerator = $elementTypeNameGenerator;
79
        $this->rangeParser = $rangeParser;
80
        $this->translator = $translator;
81
    }
82
83
    /**
84
     * @Route("/dialog", name="label_dialog")
85
     * @Route("/{profile}/dialog", name="label_dialog_profile")
86
     */
87
    public function generator(Request $request, ?LabelProfile $profile = null): Response
88
    {
89
        $this->denyAccessUnlessGranted('@labels.create_labels');
90
91
        //If we inherit a LabelProfile, the user need to have access to it...
92
        if (null !== $profile) {
93
            $this->denyAccessUnlessGranted('read', $profile);
94
        }
95
96
        if ($profile) {
97
            $label_options = $profile->getOptions();
98
        } else {
99
            $label_options = new LabelOptions();
100
        }
101
102
        //We have to disable the options, if twig mode is selected and user is not allowed to use it.
103
        $disable_options = 'twig' === $label_options->getLinesMode() && !$this->isGranted('@labels.use_twig');
104
105
        $form = $this->createForm(LabelDialogType::class, null, [
106
            'disable_options' => $disable_options,
107
        ]);
108
109
        //Try to parse given target_type and target_id
110
        $target_type = $request->query->get('target_type', null);
111
        $target_id = $request->query->get('target_id', null);
112
        $generate = $request->query->getBoolean('generate', false);
113
114
        if (null === $profile && is_string($target_type)) {
115
            $label_options->setSupportedElement($target_type);
116
        }
117
        if (is_string($target_id)) {
118
            $form['target_id']->setData($target_id);
119
        }
120
121
        $form['options']->setData($label_options);
122
        $form->handleRequest($request);
123
124
        /** @var LabelOptions $form_options */
125
        $form_options = $form['options']->getData();
126
127
        $pdf_data = null;
128
        $filename = 'invalid.pdf';
129
130
        //Generate PDF either when the form is submitted and valid, or the form  was not submit yet, and generate is set
131
        if (($form->isSubmitted() && $form->isValid()) || ($generate && !$form->isSubmitted() && null !== $profile)) {
132
            $target_id = (string) $form->get('target_id')->getData();
133
            $targets = $this->findObjects($form_options->getSupportedElement(), $target_id);
134
            if (!empty($targets)) {
135
                try {
136
                    $pdf_data = $this->labelGenerator->generateLabel($form_options, $targets);
137
                    $filename = $this->getLabelName($targets[0], $profile);
138
                } catch (TwigModeException $exception) {
139
                    $form->get('options')->get('lines')->addError(new FormError($exception->getMessage()));
140
                }
141
            } else {
142
                //$this->addFlash('warning', 'label_generator.no_entities_found');
143
                $form->get('target_id')->addError(
144
                    new FormError($this->translator->trans('label_generator.no_entities_found'))
145
                );
146
            }
147
        }
148
149
        return $this->renderForm('label_system/dialog.html.twig', [
150
            'form' => $form,
151
            'pdf_data' => $pdf_data,
152
            'filename' => $filename,
153
            'profile' => $profile,
154
        ]);
155
    }
156
157
    protected function getLabelName(AbstractDBElement $element, ?LabelProfile $profile = null): string
0 ignored issues
show
The parameter $profile is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

157
    protected function getLabelName(AbstractDBElement $element, /** @scrutinizer ignore-unused */ ?LabelProfile $profile = null): string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
158
    {
159
        $ret = 'label_'.$this->elementTypeNameGenerator->getLocalizedTypeLabel($element);
160
        $ret .= $element->getID();
161
162
        return $ret.'.pdf';
163
    }
164
165
    protected function findObjects(string $type, string $ids): array
166
    {
167
        if (!isset(LabelGenerator::CLASS_SUPPORT_MAPPING[$type])) {
168
            throw new InvalidArgumentException('The given type is not known and can not be mapped to a class!');
169
        }
170
171
        $id_array = $this->rangeParser->parse($ids);
172
173
        /** @var DBElementRepository $repo */
174
        $repo = $this->em->getRepository(LabelGenerator::CLASS_SUPPORT_MAPPING[$type]);
175
176
        return $repo->getElementsFromIDArray($id_array);
177
    }
178
}
179