-
-
Notifications
You must be signed in to change notification settings - Fork 204
Expand file tree
/
Copy pathSelectAPIController.php
More file actions
234 lines (195 loc) · 7.98 KB
/
SelectAPIController.php
File metadata and controls
234 lines (195 loc) · 7.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
declare(strict_types=1);
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2022 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;
use App\Entity\Base\AbstractNamedDBElement;
use App\Entity\Base\AbstractStructuralDBElement;
use App\Entity\LabelSystem\LabelProfile;
use App\Entity\Parts\Category;
use App\Entity\Parts\Footprint;
use App\Entity\Parts\Manufacturer;
use App\Entity\Parts\MeasurementUnit;
use App\Entity\Parts\StorageLocation;
use App\Entity\ProjectSystem\Project;
use App\Form\Type\Helper\StructuralEntityChoiceHelper;
use App\Services\Tools\TagFinder;
use App\Services\Trees\NodesListBuilder;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* This endpoint is used by the select2 library to dynamically load data (used in the multiselect action helper in parts lists)
*/
#[Route(path: '/select_api')]
class SelectAPIController extends AbstractController
{
public function __construct(private readonly NodesListBuilder $nodesListBuilder, private readonly TranslatorInterface $translator, private readonly StructuralEntityChoiceHelper $choiceHelper)
{
}
#[Route(path: '/category', name: 'select_category')]
public function category(): Response
{
return $this->getResponseForClass(Category::class);
}
#[Route(path: '/footprint', name: 'select_footprint')]
public function footprint(): Response
{
return $this->getResponseForClass(Footprint::class, true);
}
#[Route(path: '/manufacturer', name: 'select_manufacturer')]
public function manufacturer(): Response
{
return $this->getResponseForClass(Manufacturer::class, true);
}
#[Route(path: '/measurement_unit', name: 'select_measurement_unit')]
public function measurement_unit(): Response
{
return $this->getResponseForClass(MeasurementUnit::class, true);
}
#[Route(path: '/project', name: 'select_project')]
public function projects(): Response
{
return $this->getResponseForClass(Project::class, false);
}
#[Route(path: '/storage_location', name: 'select_storage_location')]
public function locations(): Response
{
return $this->getResponseForClass(StorageLocation::class, true);
}
#[Route(path: '/export_level', name: 'select_export_level')]
public function exportLevel(): Response
{
$entries = [
1 => $this->translator->trans('export.level.simple'),
2 => $this->translator->trans('export.level.extended'),
3 => $this->translator->trans('export.level.full'),
];
return $this->json(array_map(static fn($key, $value) => [
'text' => $value,
'value' => $key,
], array_keys($entries), $entries));
}
#[Route(path: '/label_profiles', name: 'select_label_profiles')]
public function labelProfiles(EntityManagerInterface $entityManager): Response
{
$this->denyAccessUnlessGranted('@labels.create_labels');
if ($this->isGranted('@labels.read_profiles')) {
$profiles = $entityManager->getRepository(LabelProfile::class)->getPartLabelProfiles();
$nodes = $this->buildJSONStructure($profiles);
} else {
$nodes = [];
}
//Add the empty option
$this->addEmptyNode($nodes, 'part_list.action.generate_label.empty');
return $this->json($nodes);
}
#[Route(path: '/label_profiles_lot', name: 'select_label_profiles_lot')]
public function labelProfilesLot(EntityManagerInterface $entityManager): Response
{
$this->denyAccessUnlessGranted('@labels.create_labels');
if ($this->isGranted('@labels.read_profiles')) {
$profiles = $entityManager->getRepository(LabelProfile::class)->getPartLotsLabelProfiles();
$nodes = $this->buildJSONStructure($profiles);
} else {
$nodes = [];
}
//Add the empty option
$this->addEmptyNode($nodes, 'part_list.action.generate_label.empty');
return $this->json($nodes);
}
#[Route(path: '/tag', name: 'select_tag')]
public function getResponseForTags(EntityManagerInterface $entityManager): Response
{
$tf = new TagFinder($entityManager);
$list = $tf->listTags('__', ['min_keyword_length' => 2, 'query_limit' => 250]); // return every tag with at least two characters!
$entries = [];
foreach($list as $d)
{
//if ($entries[$d] === null)
$entries[$d['tags']] = $d['tags'];
}
return $this->json(array_map(static fn($key, $value) => [
'text' => $value,
'value' => $key,
], array_keys($entries), $entries));
}
protected function getResponseForClass(string $class, bool $include_empty = false): Response
{
$test_obj = new $class();
$this->denyAccessUnlessGranted('read', $test_obj);
$nodes = $this->nodesListBuilder->typeToNodesList($class);
$json = $this->buildJSONStructure($nodes);
if ($include_empty) {
$this->addEmptyNode($json);
}
return $this->json($json);
}
protected function addEmptyNode(array &$arr, string $text = 'part_list.action.select_null'): array
{
array_unshift($arr, [
'text' => $this->translator->trans($text),
'value' => "",
]);
return $arr;
}
protected function buildJSONStructure(array $nodes_list): array
{
$entries = [];
foreach ($nodes_list as $node) {
if ($node instanceof AbstractStructuralDBElement) {
$entry = [
'text' => $this->choiceHelper->generateChoiceLabel($node),
'value' => $this->choiceHelper->generateChoiceValue($node),
];
$data = $this->choiceHelper->generateChoiceAttr($node, [
'disable_not_selectable' => true,
]);
//Remove the data-* prefix for each key
$data = array_combine(
array_map(static function ($key) {
if (str_starts_with($key, 'data-')) {
return substr($key, 5);
}
return $key;
}, array_keys($data)),
$data
);
//Append the data to the entry
$entry += $data;
/*$entry = [
'text' => str_repeat(' ', $node->getLevel()).htmlspecialchars($node->getName()),
'value' => $node->getID(),
'data-subtext' => $node->getParent() ? $node->getParent()->getFullPath() : null,
];*/
} elseif ($node instanceof AbstractNamedDBElement) {
$entry = [
'text' => htmlspecialchars($node->getName()),
'value' => $node->getID(),
];
} else {
throw new \InvalidArgumentException('Invalid node type!');
}
$entries[] = $entry;
}
return $entries;
}
}