1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
4
|
|
|
* |
5
|
|
|
* Copyright (C) 2019 - 2020 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
|
|
|
namespace App\Controller; |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
use App\Entity\Base\AbstractStructuralDBElement; |
25
|
|
|
use App\Entity\Parts\Category; |
26
|
|
|
use App\Entity\Parts\Footprint; |
27
|
|
|
use App\Entity\Parts\Manufacturer; |
28
|
|
|
use App\Entity\Parts\MeasurementUnit; |
29
|
|
|
use App\Services\Trees\NodesListBuilder; |
30
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
31
|
|
|
use Symfony\Component\HttpFoundation\Response; |
32
|
|
|
use Symfony\Component\HttpKernel\Event\ResponseEvent; |
33
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
34
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @Route("/select_api") |
38
|
|
|
* @package App\Controller |
39
|
|
|
*/ |
40
|
|
|
class SelectAPIController extends AbstractController |
41
|
|
|
{ |
42
|
|
|
private $nodesListBuilder; |
43
|
|
|
private $translator; |
44
|
|
|
|
45
|
|
|
public function __construct(NodesListBuilder $nodesListBuilder, TranslatorInterface $translator) |
46
|
|
|
{ |
47
|
|
|
$this->nodesListBuilder = $nodesListBuilder; |
48
|
|
|
$this->translator = $translator; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @Route("/category", name="select_category") |
53
|
|
|
*/ |
54
|
|
|
public function category(): Response |
55
|
|
|
{ |
56
|
|
|
return $this->getResponseForClass(Category::class); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @Route("/footprint", name="select_footprint") |
61
|
|
|
*/ |
62
|
|
|
public function footprint(): Response |
63
|
|
|
{ |
64
|
|
|
return $this->getResponseForClass(Footprint::class, true); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @Route("/manufacturer", name="select_manufacturer") |
69
|
|
|
*/ |
70
|
|
|
public function manufacturer(): Response |
71
|
|
|
{ |
72
|
|
|
return $this->getResponseForClass(Manufacturer::class, true); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @Route("/measurement_unit", name="select_measurement_unit") |
77
|
|
|
*/ |
78
|
|
|
public function measurement_unit(): Response |
79
|
|
|
{ |
80
|
|
|
return $this->getResponseForClass(MeasurementUnit::class, true); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
protected function getResponseForClass(string $class, bool $include_empty = false): Response |
84
|
|
|
{ |
85
|
|
|
$test_obj = new $class; |
86
|
|
|
$this->denyAccessUnlessGranted('read', $test_obj); |
87
|
|
|
|
88
|
|
|
$nodes = $this->nodesListBuilder->typeToNodesList($class); |
89
|
|
|
|
90
|
|
|
$json = $this->buildJSONStructure($nodes); |
91
|
|
|
|
92
|
|
|
if ($include_empty) { |
93
|
|
|
array_unshift($json, [ |
94
|
|
|
'text' => '', |
95
|
|
|
'value' => null, |
96
|
|
|
'data-subtext' => $this->translator->trans('part_list.action.select_null'), |
97
|
|
|
]); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $this->json($json); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
protected function buildJSONStructure(array $nodes_list): array |
104
|
|
|
{ |
105
|
|
|
$entries = []; |
106
|
|
|
|
107
|
|
|
foreach ($nodes_list as $node) { |
108
|
|
|
/** @var AbstractStructuralDBElement $node */ |
109
|
|
|
$entry = [ |
110
|
|
|
'text' => str_repeat(' ', $node->getLevel()) . htmlspecialchars($node->getName()), |
111
|
|
|
'value' => $node->getID(), |
112
|
|
|
'data-subtext' => $node->getParent() ? $node->getParent()->getFullPath() : null, |
113
|
|
|
]; |
114
|
|
|
|
115
|
|
|
$entries[] = $entry; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $entries; |
119
|
|
|
} |
120
|
|
|
} |