1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* part-db version 0.1 |
4
|
|
|
* Copyright (C) 2005 Christoph Lechner |
5
|
|
|
* http://www.cl-projects.de/. |
6
|
|
|
* |
7
|
|
|
* part-db version 0.2+ |
8
|
|
|
* Copyright (C) 2009 K. Jacobs and others (see authors.php) |
9
|
|
|
* http://code.google.com/p/part-db/ |
10
|
|
|
* |
11
|
|
|
* Part-DB Version 0.4+ |
12
|
|
|
* Copyright (C) 2016 - 2019 Jan Böhmer |
13
|
|
|
* https://github.com/jbtronics |
14
|
|
|
* |
15
|
|
|
* This program is free software; you can redistribute it and/or |
16
|
|
|
* modify it under the terms of the GNU General Public License |
17
|
|
|
* as published by the Free Software Foundation; either version 2 |
18
|
|
|
* of the License, or (at your option) any later version. |
19
|
|
|
* |
20
|
|
|
* This program is distributed in the hope that it will be useful, |
21
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
22
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23
|
|
|
* GNU General Public License for more details. |
24
|
|
|
* |
25
|
|
|
* You should have received a copy of the GNU General Public License |
26
|
|
|
* along with this program; if not, write to the Free Software |
27
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
28
|
|
|
*/ |
29
|
|
|
|
30
|
|
|
namespace App\Services; |
31
|
|
|
|
32
|
|
|
use App\Entity\Attachment; |
33
|
|
|
use App\Entity\AttachmentType; |
34
|
|
|
use App\Entity\Category; |
35
|
|
|
use App\Entity\Device; |
36
|
|
|
use App\Entity\Footprint; |
37
|
|
|
use App\Entity\Manufacturer; |
38
|
|
|
use App\Entity\NamedDBElement; |
39
|
|
|
use App\Entity\Part; |
40
|
|
|
use App\Entity\Storelocation; |
41
|
|
|
use App\Entity\Supplier; |
42
|
|
|
use App\Entity\User; |
43
|
|
|
use App\Exceptions\EntityNotSupported; |
44
|
|
|
use Symfony\Component\HttpKernel\HttpCache\Store; |
45
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
46
|
|
|
|
47
|
|
|
class EntityURLGenerator |
48
|
|
|
{ |
49
|
|
|
/** |
50
|
|
|
* @var UrlGeneratorInterface |
51
|
|
|
*/ |
52
|
|
|
protected $urlGenerator; |
53
|
|
|
|
54
|
|
|
public function __construct(UrlGeneratorInterface $urlGenerator) |
55
|
|
|
{ |
56
|
|
|
$this->urlGenerator = $urlGenerator; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Generates an URL to the page using the given page type and element. |
61
|
|
|
* For the given types, the [type]URL() functions are called (e.g. infoURL()). |
62
|
|
|
* Not all entity class and $type combinations are supported. |
63
|
|
|
* |
64
|
|
|
* @param $entity mixed The element for which the page should be generated. |
65
|
|
|
* @param string $type The page type. Currently supported: 'info', 'edit', 'create', 'clone', 'list'/'list_parts' |
66
|
|
|
* @return string The link to the desired page. |
67
|
|
|
* @throws EntityNotSupported Thrown if the entity is not supported for the given type. |
68
|
|
|
* @throws \InvalidArgumentException Thrown if the givent type is not existing. |
69
|
|
|
*/ |
70
|
|
|
public function getURL($entity, string $type) |
71
|
|
|
{ |
72
|
|
|
switch ($type) { |
73
|
|
|
case 'info': |
74
|
|
|
return $this->infoURL($entity); |
75
|
|
|
case 'edit': |
76
|
|
|
return $this->editURL($entity); |
77
|
|
|
case 'create': |
78
|
|
|
return $this->createURL($entity); |
79
|
|
|
case 'clone': |
80
|
|
|
return $this->cloneURL($entity); |
81
|
|
|
case 'list': |
82
|
|
|
case 'list_parts': |
83
|
|
|
return $this->listPartsURL($entity); |
84
|
|
|
case 'delete': |
85
|
|
|
return $this->deleteURL($entity); |
86
|
|
|
case 'file_download': |
87
|
|
|
return $this->downloadURL($entity); |
88
|
|
|
case 'file_view': |
89
|
|
|
return $this->viewURL($entity); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
throw new \InvalidArgumentException('Method is not supported!'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function viewURL($entity) : string |
96
|
|
|
{ |
97
|
|
|
if ($entity instanceof Attachment) { |
98
|
|
|
if ($entity->isExternal()) { //For external attachments, return the link to external path |
99
|
|
|
return $entity->getURL(); |
|
|
|
|
100
|
|
|
} |
101
|
|
|
return $this->urlGenerator->generate('attachment_view', ['id' => $entity->getID()]); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
//Otherwise throw an error |
105
|
|
|
throw new EntityNotSupported('The given entity is not supported yet!'); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function downloadURL($entity) : string |
109
|
|
|
{ |
110
|
|
|
if ($entity instanceof Attachment) { |
111
|
|
|
if ($entity->isExternal()) { //For external attachments, return the link to external path |
112
|
|
|
return $entity->getURL(); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
return $this->urlGenerator->generate('attachment_download', ['id' => $entity->getID()]); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
//Otherwise throw an error |
118
|
|
|
throw new EntityNotSupported('The given entity is not supported yet!'); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Generates an URL to a page, where info about this entity can be viewed. |
123
|
|
|
* |
124
|
|
|
* @param $entity mixed The entity for which the info should be generated. |
125
|
|
|
* @return string The URL to the info page |
126
|
|
|
* @throws EntityNotSupported If the method is not supported for the given Entity |
127
|
|
|
*/ |
128
|
|
|
public function infoURL($entity): string |
129
|
|
|
{ |
130
|
|
|
if ($entity instanceof Part) { |
131
|
|
|
return $this->urlGenerator->generate('part_info', ['id' => $entity->getID()]); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
//Otherwise throw an error |
135
|
|
|
throw new EntityNotSupported('The given entity is not supported yet!'); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Generates an URL to a page, where this entity can be edited. |
140
|
|
|
* |
141
|
|
|
* @param $entity mixed The entity for which the edit link should be generated. |
142
|
|
|
* @return string The URL to the edit page. |
143
|
|
|
* @throws EntityNotSupported If the method is not supported for the given Entity |
144
|
|
|
*/ |
145
|
|
|
public function editURL($entity): string |
146
|
|
|
{ |
147
|
|
|
if ($entity instanceof Part) { |
148
|
|
|
return $this->urlGenerator->generate('part_edit', ['id' => $entity->getID()]); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
if ($entity instanceof AttachmentType) { |
152
|
|
|
return $this->urlGenerator->generate('attachment_type_edit', ['id' => $entity->getID()]); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
if ($entity instanceof Category) { |
156
|
|
|
return $this->urlGenerator->generate("category_edit", ['id' => $entity->getID()]); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
if ($entity instanceof Device) { |
160
|
|
|
return $this->urlGenerator->generate("device_edit", ['id' => $entity->getID()]); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
if ($entity instanceof Supplier) { |
164
|
|
|
return $this->urlGenerator->generate("supplier_edit", ['id' => $entity->getID()]); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
if ($entity instanceof Manufacturer) { |
168
|
|
|
return $this->urlGenerator->generate("manufacturer_edit", ['id' => $entity->getID()]); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
if ($entity instanceof Storelocation) { |
172
|
|
|
return $this->urlGenerator->generate("store_location_edit", ['id' => $entity->getID()]); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
if ($entity instanceof Footprint) { |
176
|
|
|
return $this->urlGenerator->generate("footprint_edit", ['id' => $entity->getID()]); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
if($entity instanceof User) { |
180
|
|
|
return $this->urlGenerator->generate('user_edit', ['id' => $entity->getID()]); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
//Otherwise throw an error |
184
|
|
|
throw new EntityNotSupported('The given entity is not supported yet!'); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Generates an URL to a page, where a entity of this type can be created. |
189
|
|
|
* |
190
|
|
|
* @param $entity mixed The entity for which the link should be generated. |
191
|
|
|
* @return string The URL to the page. |
192
|
|
|
* @throws EntityNotSupported If the method is not supported for the given Entity |
193
|
|
|
*/ |
194
|
|
|
public function createURL($entity): string |
195
|
|
|
{ |
196
|
|
|
if ($entity instanceof Part) { |
197
|
|
|
return $this->urlGenerator->generate('part_new'); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
if ($entity instanceof AttachmentType) { |
201
|
|
|
return $this->urlGenerator->generate('attachment_type_new'); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
if ($entity instanceof Category) { |
205
|
|
|
return $this->urlGenerator->generate('category_new'); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
if ($entity instanceof Device) { |
209
|
|
|
return $this->urlGenerator->generate('device_new'); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
if ($entity instanceof Supplier) { |
213
|
|
|
return $this->urlGenerator->generate('supplier_new'); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
if ($entity instanceof Manufacturer) { |
217
|
|
|
return $this->urlGenerator->generate('manufacturer_new'); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
if ($entity instanceof Storelocation) { |
221
|
|
|
return $this->urlGenerator->generate('store_location_new'); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
if ($entity instanceof Footprint) { |
225
|
|
|
return $this->urlGenerator->generate('footprint_new'); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
if ($entity instanceof User) { |
229
|
|
|
return $this->urlGenerator->generate('user_new'); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
throw new EntityNotSupported('The given entity is not supported yet!'); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Generates an URL to a page, where a new entity can be created, that has the same informations as the |
237
|
|
|
* given entity (element cloning) |
238
|
|
|
* |
239
|
|
|
* @param $entity mixed The entity for which the link should be generated. |
240
|
|
|
* @return string The URL to the page. |
241
|
|
|
* @throws EntityNotSupported If the method is not supported for the given Entity |
242
|
|
|
*/ |
243
|
|
|
public function cloneURL($entity): string |
244
|
|
|
{ |
245
|
|
|
if ($entity instanceof Part) { |
246
|
|
|
return $this->urlGenerator->generate('part_clone', ['id' => $entity->getID()]); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
throw new EntityNotSupported('The given entity is not supported yet!'); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Generates an URL to a page, where all parts are listed, which are contained in the given element. |
254
|
|
|
* |
255
|
|
|
* @param $entity mixed The entity for which the link should be generated. |
256
|
|
|
* @return string The URL to the page. |
257
|
|
|
* @throws EntityNotSupported If the method is not supported for the given Entity |
258
|
|
|
*/ |
259
|
|
|
public function listPartsURL($entity) : string |
260
|
|
|
{ |
261
|
|
|
if ($entity instanceof Category) { |
262
|
|
|
return $this->urlGenerator->generate('app_partlists_showcategory', ['id' => $entity->getID()]); |
263
|
|
|
} |
264
|
|
|
throw new EntityNotSupported('The given entity is not supported yet!'); |
265
|
|
|
|
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
public function deleteURL($entity) : string |
269
|
|
|
{ |
270
|
|
|
if ($entity instanceof AttachmentType) { |
271
|
|
|
return $this->urlGenerator->generate('attachment_type_delete', ['id' => $entity->getID()]); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
if ($entity instanceof Category) { |
275
|
|
|
return $this->urlGenerator->generate('category_delete', ['id' => $entity->getID()]); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
if ($entity instanceof Device) { |
279
|
|
|
return $this->urlGenerator->generate('device_delete', ['id' => $entity->getID()]); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
if ($entity instanceof Supplier) { |
283
|
|
|
return $this->urlGenerator->generate('supplier_delete', ['id' => $entity->getID()]); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
if ($entity instanceof Manufacturer) { |
287
|
|
|
return $this->urlGenerator->generate('manufacturer_delete', ['id' => $entity->getID()]); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
if ($entity instanceof Storelocation) { |
291
|
|
|
return $this->urlGenerator->generate('store_location_delete', ['id' => $entity->getID()]); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
if ($entity instanceof Footprint) { |
295
|
|
|
return $this->urlGenerator->generate('footprint_delete', ['id' => $entity->getID()]); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
if ($entity instanceof User) { |
299
|
|
|
return $this->urlGenerator->generate('user_delete', ['id' => $entity->getID()]); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
throw new EntityNotSupported('The given entity is not supported yet!'); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Generates an HTML link to the info page about the given entity. |
307
|
|
|
* |
308
|
|
|
* @param $entity mixed The entity for which the info link should be generated. |
309
|
|
|
* |
310
|
|
|
* @return string The HTML of the info page link |
311
|
|
|
* |
312
|
|
|
* @throws EntityNotSupported |
313
|
|
|
*/ |
314
|
|
|
public function infoHTML($entity): string |
315
|
|
|
{ |
316
|
|
|
$href = $this->infoURL($entity); |
317
|
|
|
|
318
|
|
|
if ($entity instanceof NamedDBElement) { |
|
|
|
|
319
|
|
|
return sprintf('<a href="%s">%s</a>', $href, $entity->getName()); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
throw new EntityNotSupported('The given entity is not supported yet!'); |
323
|
|
|
} |
324
|
|
|
} |
325
|
|
|
|