1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
4
|
|
|
* |
5
|
|
|
* Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics) |
6
|
|
|
* |
7
|
|
|
* This program is free software; you can redistribute it and/or |
8
|
|
|
* modify it under the terms of the GNU General Public License |
9
|
|
|
* as published by the Free Software Foundation; either version 2 |
10
|
|
|
* of the License, or (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 General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU General Public License |
18
|
|
|
* along with this program; if not, write to the Free Software |
19
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace App\Services\Trees; |
23
|
|
|
|
24
|
|
|
use App\Entity\Attachments\AttachmentType; |
25
|
|
|
use App\Entity\Attachments\PartAttachment; |
26
|
|
|
use App\Entity\Devices\Device; |
27
|
|
|
use App\Entity\Parts\Category; |
28
|
|
|
use App\Entity\Parts\Footprint; |
29
|
|
|
use App\Entity\Parts\Manufacturer; |
30
|
|
|
use App\Entity\Parts\MeasurementUnit; |
31
|
|
|
use App\Entity\Parts\Part; |
32
|
|
|
use App\Entity\Parts\Storelocation; |
33
|
|
|
use App\Entity\Parts\Supplier; |
34
|
|
|
use App\Entity\PriceInformations\Currency; |
35
|
|
|
use App\Entity\UserSystem\Group; |
36
|
|
|
use App\Entity\UserSystem\User; |
37
|
|
|
use App\Helpers\Trees\TreeViewNode; |
38
|
|
|
use App\Services\UserCacheKeyGenerator; |
39
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
40
|
|
|
use Symfony\Component\Security\Core\Security; |
41
|
|
|
use Symfony\Contracts\Cache\ItemInterface; |
42
|
|
|
use Symfony\Contracts\Cache\TagAwareCacheInterface; |
43
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* This Service generates the tree structure for the tools. |
47
|
|
|
* Whenever you change something here, you has to clear the cache, because the results are cached for performance reasons. |
48
|
|
|
*/ |
49
|
|
|
class ToolsTreeBuilder |
50
|
|
|
{ |
51
|
|
|
protected $translator; |
52
|
|
|
protected $urlGenerator; |
53
|
|
|
protected $keyGenerator; |
54
|
|
|
protected $cache; |
55
|
|
|
protected $security; |
56
|
|
|
|
57
|
|
|
public function __construct(TranslatorInterface $translator, UrlGeneratorInterface $urlGenerator, |
58
|
|
|
TagAwareCacheInterface $treeCache, UserCacheKeyGenerator $keyGenerator, |
59
|
|
|
Security $security) |
60
|
|
|
{ |
61
|
|
|
$this->translator = $translator; |
62
|
|
|
$this->urlGenerator = $urlGenerator; |
63
|
|
|
|
64
|
|
|
$this->cache = $treeCache; |
65
|
|
|
|
66
|
|
|
$this->keyGenerator = $keyGenerator; |
67
|
|
|
|
68
|
|
|
$this->security = $security; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Generates the tree for the tools menu. |
73
|
|
|
* The result is cached. |
74
|
|
|
* |
75
|
|
|
* @return TreeViewNode[] The array containing all Nodes for the tools menu. |
76
|
|
|
*/ |
77
|
|
|
public function getTree(): array |
78
|
|
|
{ |
79
|
|
|
$key = 'tree_tools_'.$this->keyGenerator->generateKey(); |
80
|
|
|
|
81
|
|
|
return $this->cache->get($key, function (ItemInterface $item) { |
82
|
|
|
//Invalidate tree, whenever group or the user changes |
83
|
|
|
$item->tag(['tree_tools', 'groups', $this->keyGenerator->generateKey()]); |
84
|
|
|
|
85
|
|
|
$tree = []; |
86
|
|
|
$tree[] = new TreeViewNode($this->translator->trans('tree.tools.edit'), null, $this->getEditNodes()); |
87
|
|
|
$tree[] = new TreeViewNode($this->translator->trans('tree.tools.show'), null, $this->getShowNodes()); |
88
|
|
|
$tree[] = new TreeViewNode($this->translator->trans('tree.tools.system'), null, $this->getSystemNodes()); |
89
|
|
|
|
90
|
|
|
return $tree; |
91
|
|
|
}); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* This functions creates a tree entries for the "edit" node of the tool's tree. |
96
|
|
|
* |
97
|
|
|
* @return TreeViewNode[] |
98
|
|
|
*/ |
99
|
|
|
protected function getEditNodes(): array |
100
|
|
|
{ |
101
|
|
|
$nodes = []; |
102
|
|
|
|
103
|
|
|
if ($this->security->isGranted('read', new AttachmentType())) { |
104
|
|
|
$nodes[] = new TreeViewNode( |
105
|
|
|
$this->translator->trans('tree.tools.edit.attachment_types'), |
106
|
|
|
$this->urlGenerator->generate('attachment_type_new') |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
if ($this->security->isGranted('read', new Category())) { |
110
|
|
|
$nodes[] = new TreeViewNode( |
111
|
|
|
$this->translator->trans('tree.tools.edit.categories'), |
112
|
|
|
$this->urlGenerator->generate('category_new') |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
if ($this->security->isGranted('read', new Device())) { |
116
|
|
|
$nodes[] = new TreeViewNode( |
117
|
|
|
$this->translator->trans('tree.tools.edit.devices'), |
118
|
|
|
$this->urlGenerator->generate('device_new') |
119
|
|
|
); |
120
|
|
|
} |
121
|
|
|
if ($this->security->isGranted('read', new Supplier())) { |
122
|
|
|
$nodes[] = new TreeViewNode( |
123
|
|
|
$this->translator->trans('tree.tools.edit.suppliers'), |
124
|
|
|
$this->urlGenerator->generate('supplier_new') |
125
|
|
|
); |
126
|
|
|
} |
127
|
|
|
if ($this->security->isGranted('read', new Manufacturer())) { |
128
|
|
|
$nodes[] = new TreeViewNode( |
129
|
|
|
$this->translator->trans('tree.tools.edit.manufacturer'), |
130
|
|
|
$this->urlGenerator->generate('manufacturer_new') |
131
|
|
|
); |
132
|
|
|
} |
133
|
|
|
if ($this->security->isGranted('read', new Storelocation())) { |
134
|
|
|
$nodes[] = new TreeViewNode( |
135
|
|
|
$this->translator->trans('tree.tools.edit.storelocation'), |
136
|
|
|
$this->urlGenerator->generate('store_location_new') |
137
|
|
|
); |
138
|
|
|
} |
139
|
|
|
if ($this->security->isGranted('read', new Footprint())) { |
140
|
|
|
$nodes[] = new TreeViewNode( |
141
|
|
|
$this->translator->trans('tree.tools.edit.footprint'), |
142
|
|
|
$this->urlGenerator->generate('footprint_new') |
143
|
|
|
); |
144
|
|
|
} |
145
|
|
|
if ($this->security->isGranted('read', new Currency())) { |
146
|
|
|
$nodes[] = new TreeViewNode( |
147
|
|
|
$this->translator->trans('tree.tools.edit.currency'), |
148
|
|
|
$this->urlGenerator->generate('currency_new') |
149
|
|
|
); |
150
|
|
|
} |
151
|
|
|
if ($this->security->isGranted('read', new MeasurementUnit())) { |
152
|
|
|
$nodes[] = new TreeViewNode( |
153
|
|
|
$this->translator->trans('tree.tools.edit.measurement_unit'), |
154
|
|
|
$this->urlGenerator->generate('measurement_unit_new') |
155
|
|
|
); |
156
|
|
|
} |
157
|
|
|
if ($this->security->isGranted('create', new Part())) { |
158
|
|
|
$nodes[] = new TreeViewNode( |
159
|
|
|
$this->translator->trans('tree.tools.edit.part'), |
160
|
|
|
$this->urlGenerator->generate('part_new') |
161
|
|
|
); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return $nodes; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* This function creates the tree entries for the "show" node of the tools tree. |
169
|
|
|
* |
170
|
|
|
* @return TreeViewNode[] |
171
|
|
|
*/ |
172
|
|
|
protected function getShowNodes(): array |
173
|
|
|
{ |
174
|
|
|
$show_nodes = []; |
175
|
|
|
$show_nodes[] = new TreeViewNode( |
176
|
|
|
$this->translator->trans('tree.tools.show.all_parts'), |
177
|
|
|
$this->urlGenerator->generate('parts_show_all') |
178
|
|
|
); |
179
|
|
|
|
180
|
|
|
if ($this->security->isGranted('read', new PartAttachment())) { |
181
|
|
|
$show_nodes[] = new TreeViewNode( |
182
|
|
|
$this->translator->trans('tree.tools.show.all_attachments'), |
183
|
|
|
$this->urlGenerator->generate('attachment_list') |
184
|
|
|
); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return $show_nodes; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* This function creates the tree entries for the "system" node of the tools tree. |
192
|
|
|
* |
193
|
|
|
* @return array |
194
|
|
|
*/ |
195
|
|
|
protected function getSystemNodes(): array |
196
|
|
|
{ |
197
|
|
|
$nodes = []; |
198
|
|
|
|
199
|
|
|
if ($this->security->isGranted('read', new User())) { |
200
|
|
|
$nodes[] = new TreeViewNode( |
201
|
|
|
$this->translator->trans('tree.tools.system.users'), |
202
|
|
|
$this->urlGenerator->generate('user_new') |
203
|
|
|
); |
204
|
|
|
} |
205
|
|
|
if ($this->security->isGranted('read', new Group())) { |
206
|
|
|
$nodes[] = new TreeViewNode( |
207
|
|
|
$this->translator->trans('tree.tools.system.groups'), |
208
|
|
|
$this->urlGenerator->generate('group_new') |
209
|
|
|
); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
return $nodes; |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|