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\Attachments\Attachment; |
33
|
|
|
use App\Entity\Attachments\AttachmentType; |
34
|
|
|
use App\Entity\Parts\Category; |
35
|
|
|
use App\Entity\Devices\Device; |
36
|
|
|
use App\Entity\Parts\Footprint; |
37
|
|
|
use App\Entity\Parts\Manufacturer; |
38
|
|
|
use App\Entity\Base\NamedDBElement; |
39
|
|
|
use App\Entity\Parts\MeasurementUnit; |
40
|
|
|
use App\Entity\Parts\Part; |
41
|
|
|
use App\Entity\Parts\Storelocation; |
42
|
|
|
use App\Entity\Parts\Supplier; |
43
|
|
|
use App\Entity\PriceInformations\Currency; |
44
|
|
|
use App\Entity\UserSystem\User; |
45
|
|
|
use App\Exceptions\EntityNotSupported; |
46
|
|
|
use Symfony\Component\HttpKernel\HttpCache\Store; |
47
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
48
|
|
|
|
49
|
|
|
class EntityURLGenerator |
50
|
|
|
{ |
51
|
|
|
/** |
52
|
|
|
* @var UrlGeneratorInterface |
53
|
|
|
*/ |
54
|
|
|
protected $urlGenerator; |
55
|
|
|
|
56
|
|
|
public function __construct(UrlGeneratorInterface $urlGenerator) |
57
|
|
|
{ |
58
|
|
|
$this->urlGenerator = $urlGenerator; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Generates an URL to the page using the given page type and element. |
63
|
|
|
* For the given types, the [type]URL() functions are called (e.g. infoURL()). |
64
|
|
|
* Not all entity class and $type combinations are supported. |
65
|
|
|
* |
66
|
|
|
* @param $entity mixed The element for which the page should be generated. |
67
|
|
|
* @param string $type The page type. Currently supported: 'info', 'edit', 'create', 'clone', 'list'/'list_parts' |
68
|
|
|
* @return string The link to the desired page. |
69
|
|
|
* @throws EntityNotSupported Thrown if the entity is not supported for the given type. |
70
|
|
|
* @throws \InvalidArgumentException Thrown if the givent type is not existing. |
71
|
|
|
*/ |
72
|
|
|
public function getURL($entity, string $type) |
73
|
|
|
{ |
74
|
|
|
switch ($type) { |
75
|
|
|
case 'info': |
76
|
|
|
return $this->infoURL($entity); |
77
|
|
|
case 'edit': |
78
|
|
|
return $this->editURL($entity); |
79
|
|
|
case 'create': |
80
|
|
|
return $this->createURL($entity); |
81
|
|
|
case 'clone': |
82
|
|
|
return $this->cloneURL($entity); |
83
|
|
|
case 'list': |
84
|
|
|
case 'list_parts': |
85
|
|
|
return $this->listPartsURL($entity); |
86
|
|
|
case 'delete': |
87
|
|
|
return $this->deleteURL($entity); |
88
|
|
|
case 'file_download': |
89
|
|
|
return $this->downloadURL($entity); |
90
|
|
|
case 'file_view': |
91
|
|
|
return $this->viewURL($entity); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
throw new \InvalidArgumentException('Method is not supported!'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function viewURL($entity) : string |
98
|
|
|
{ |
99
|
|
|
if ($entity instanceof Attachment) { |
100
|
|
|
if ($entity->isExternal()) { //For external attachments, return the link to external path |
101
|
|
|
return $entity->getURL(); |
|
|
|
|
102
|
|
|
} |
103
|
|
|
return $this->urlGenerator->generate('attachment_view', ['id' => $entity->getID()]); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
//Otherwise throw an error |
107
|
|
|
throw new EntityNotSupported('The given entity is not supported yet!'); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function downloadURL($entity) : string |
111
|
|
|
{ |
112
|
|
|
if ($entity instanceof Attachment) { |
113
|
|
|
if ($entity->isExternal()) { //For external attachments, return the link to external path |
114
|
|
|
return $entity->getURL(); |
|
|
|
|
115
|
|
|
} |
116
|
|
|
return $this->urlGenerator->generate('attachment_download', ['id' => $entity->getID()]); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
//Otherwise throw an error |
120
|
|
|
throw new EntityNotSupported('The given entity is not supported yet!'); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Generates an URL to a page, where info about this entity can be viewed. |
125
|
|
|
* |
126
|
|
|
* @param $entity mixed The entity for which the info should be generated. |
127
|
|
|
* @return string The URL to the info page |
128
|
|
|
* @throws EntityNotSupported If the method is not supported for the given Entity |
129
|
|
|
*/ |
130
|
|
|
public function infoURL($entity): string |
131
|
|
|
{ |
132
|
|
|
if ($entity instanceof Part) { |
133
|
|
|
return $this->urlGenerator->generate('part_info', ['id' => $entity->getID()]); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
//Otherwise throw an error |
137
|
|
|
throw new EntityNotSupported('The given entity is not supported yet!'); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Generates an URL to a page, where this entity can be edited. |
142
|
|
|
* |
143
|
|
|
* @param $entity mixed The entity for which the edit link should be generated. |
144
|
|
|
* @return string The URL to the edit page. |
145
|
|
|
* @throws EntityNotSupported If the method is not supported for the given Entity |
146
|
|
|
*/ |
147
|
|
|
public function editURL($entity): string |
148
|
|
|
{ |
149
|
|
|
if ($entity instanceof Part) { |
150
|
|
|
return $this->urlGenerator->generate('part_edit', ['id' => $entity->getID()]); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
if ($entity instanceof AttachmentType) { |
154
|
|
|
return $this->urlGenerator->generate('attachment_type_edit', ['id' => $entity->getID()]); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
if ($entity instanceof Category) { |
158
|
|
|
return $this->urlGenerator->generate("category_edit", ['id' => $entity->getID()]); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
if ($entity instanceof Device) { |
162
|
|
|
return $this->urlGenerator->generate("device_edit", ['id' => $entity->getID()]); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
if ($entity instanceof Supplier) { |
166
|
|
|
return $this->urlGenerator->generate("supplier_edit", ['id' => $entity->getID()]); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
if ($entity instanceof Manufacturer) { |
170
|
|
|
return $this->urlGenerator->generate("manufacturer_edit", ['id' => $entity->getID()]); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
if ($entity instanceof Storelocation) { |
174
|
|
|
return $this->urlGenerator->generate("store_location_edit", ['id' => $entity->getID()]); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
if ($entity instanceof Footprint) { |
178
|
|
|
return $this->urlGenerator->generate("footprint_edit", ['id' => $entity->getID()]); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
if ($entity instanceof User) { |
182
|
|
|
return $this->urlGenerator->generate('user_edit', ['id' => $entity->getID()]); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
if ($entity instanceof Currency) { |
186
|
|
|
return $this->urlGenerator->generate('currency_edit', ['id' => $entity->getID()]); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
if ($entity instanceof MeasurementUnit) { |
190
|
|
|
return $this->urlGenerator->generate('measurement_unit_edit', ['id' => $entity->getID()]); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
//Otherwise throw an error |
194
|
|
|
throw new EntityNotSupported('The given entity is not supported yet!'); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Generates an URL to a page, where a entity of this type can be created. |
199
|
|
|
* |
200
|
|
|
* @param $entity mixed The entity for which the link should be generated. |
201
|
|
|
* @return string The URL to the page. |
202
|
|
|
* @throws EntityNotSupported If the method is not supported for the given Entity |
203
|
|
|
*/ |
204
|
|
|
public function createURL($entity): string |
205
|
|
|
{ |
206
|
|
|
if ($entity instanceof Part) { |
207
|
|
|
return $this->urlGenerator->generate('part_new'); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
if ($entity instanceof AttachmentType) { |
211
|
|
|
return $this->urlGenerator->generate('attachment_type_new'); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
if ($entity instanceof Category) { |
215
|
|
|
return $this->urlGenerator->generate('category_new'); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
if ($entity instanceof Device) { |
219
|
|
|
return $this->urlGenerator->generate('device_new'); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
if ($entity instanceof Supplier) { |
223
|
|
|
return $this->urlGenerator->generate('supplier_new'); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
if ($entity instanceof Manufacturer) { |
227
|
|
|
return $this->urlGenerator->generate('manufacturer_new'); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
if ($entity instanceof Storelocation) { |
231
|
|
|
return $this->urlGenerator->generate('store_location_new'); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
if ($entity instanceof Footprint) { |
235
|
|
|
return $this->urlGenerator->generate('footprint_new'); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
if ($entity instanceof User) { |
239
|
|
|
return $this->urlGenerator->generate('user_new'); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
if ($entity instanceof Currency) { |
243
|
|
|
return $this->urlGenerator->generate('currency_new'); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
if ($entity instanceof MeasurementUnit) { |
247
|
|
|
return $this->urlGenerator->generate('measurement_unit_new'); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
throw new EntityNotSupported('The given entity is not supported yet!'); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Generates an URL to a page, where a new entity can be created, that has the same informations as the |
255
|
|
|
* given entity (element cloning) |
256
|
|
|
* |
257
|
|
|
* @param $entity mixed The entity for which the link should be generated. |
258
|
|
|
* @return string The URL to the page. |
259
|
|
|
* @throws EntityNotSupported If the method is not supported for the given Entity |
260
|
|
|
*/ |
261
|
|
|
public function cloneURL($entity): string |
262
|
|
|
{ |
263
|
|
|
if ($entity instanceof Part) { |
264
|
|
|
return $this->urlGenerator->generate('part_clone', ['id' => $entity->getID()]); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
throw new EntityNotSupported('The given entity is not supported yet!'); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Generates an URL to a page, where all parts are listed, which are contained in the given element. |
272
|
|
|
* |
273
|
|
|
* @param $entity mixed The entity for which the link should be generated. |
274
|
|
|
* @return string The URL to the page. |
275
|
|
|
* @throws EntityNotSupported If the method is not supported for the given Entity |
276
|
|
|
*/ |
277
|
|
|
public function listPartsURL($entity) : string |
278
|
|
|
{ |
279
|
|
|
if ($entity instanceof Category) { |
280
|
|
|
return $this->urlGenerator->generate('app_partlists_showcategory', ['id' => $entity->getID()]); |
281
|
|
|
} |
282
|
|
|
throw new EntityNotSupported('The given entity is not supported yet!'); |
283
|
|
|
|
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
public function deleteURL($entity) : string |
287
|
|
|
{ |
288
|
|
|
if ($entity instanceof Part) { |
289
|
|
|
return $this->urlGenerator->generate('part_delete', ['id' => $entity->getID()]); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
if ($entity instanceof AttachmentType) { |
293
|
|
|
return $this->urlGenerator->generate('attachment_type_delete', ['id' => $entity->getID()]); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
if ($entity instanceof Category) { |
297
|
|
|
return $this->urlGenerator->generate('category_delete', ['id' => $entity->getID()]); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
if ($entity instanceof Device) { |
301
|
|
|
return $this->urlGenerator->generate('device_delete', ['id' => $entity->getID()]); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
if ($entity instanceof Supplier) { |
305
|
|
|
return $this->urlGenerator->generate('supplier_delete', ['id' => $entity->getID()]); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
if ($entity instanceof Manufacturer) { |
309
|
|
|
return $this->urlGenerator->generate('manufacturer_delete', ['id' => $entity->getID()]); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
if ($entity instanceof Storelocation) { |
313
|
|
|
return $this->urlGenerator->generate('store_location_delete', ['id' => $entity->getID()]); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
if ($entity instanceof Footprint) { |
317
|
|
|
return $this->urlGenerator->generate('footprint_delete', ['id' => $entity->getID()]); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
if ($entity instanceof User) { |
321
|
|
|
return $this->urlGenerator->generate('user_delete', ['id' => $entity->getID()]); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
if ($entity instanceof Currency) { |
325
|
|
|
return $this->urlGenerator->generate('currency_delete', ['id' => $entity->getID()]); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
if ($entity instanceof MeasurementUnit) { |
329
|
|
|
return $this->urlGenerator->generate('measurement_unit_delete', ['id' => $entity->getID()]); |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
throw new EntityNotSupported('The given entity is not supported yet!'); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* Generates an HTML link to the info page about the given entity. |
337
|
|
|
* |
338
|
|
|
* @param $entity mixed The entity for which the info link should be generated. |
339
|
|
|
* |
340
|
|
|
* @return string The HTML of the info page link |
341
|
|
|
* |
342
|
|
|
* @throws EntityNotSupported |
343
|
|
|
*/ |
344
|
|
|
public function infoHTML($entity): string |
345
|
|
|
{ |
346
|
|
|
$href = $this->infoURL($entity); |
347
|
|
|
|
348
|
|
|
if ($entity instanceof NamedDBElement) { |
|
|
|
|
349
|
|
|
return sprintf('<a href="%s">%s</a>', $href, $entity->getName()); |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
throw new EntityNotSupported('The given entity is not supported yet!'); |
353
|
|
|
} |
354
|
|
|
} |
355
|
|
|
|