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\AttachmentType; |
33
|
|
|
use App\Entity\Category; |
34
|
|
|
use App\Entity\Device; |
35
|
|
|
use App\Entity\Footprint; |
36
|
|
|
use App\Entity\Manufacturer; |
37
|
|
|
use App\Entity\NamedDBElement; |
38
|
|
|
use App\Entity\Part; |
39
|
|
|
use App\Entity\Storelocation; |
40
|
|
|
use App\Entity\Supplier; |
41
|
|
|
use App\Exceptions\EntityNotSupported; |
42
|
|
|
use Symfony\Component\HttpKernel\HttpCache\Store; |
43
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
44
|
|
|
|
45
|
|
|
class EntityURLGenerator |
46
|
|
|
{ |
47
|
|
|
/** |
48
|
|
|
* @var UrlGeneratorInterface |
49
|
|
|
*/ |
50
|
|
|
protected $urlGenerator; |
51
|
|
|
|
52
|
|
|
public function __construct(UrlGeneratorInterface $urlGenerator) |
53
|
|
|
{ |
54
|
|
|
$this->urlGenerator = $urlGenerator; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Generates an URL to the page using the given page type and element. |
59
|
|
|
* For the given types, the [type]URL() functions are called (e.g. infoURL()). |
60
|
|
|
* Not all entity class and $type combinations are supported. |
61
|
|
|
* |
62
|
|
|
* @param $entity mixed The element for which the page should be generated. |
63
|
|
|
* @param string $type The page type. Currently supported: 'info', 'edit', 'create', 'clone', 'list'/'list_parts' |
64
|
|
|
* @return string The link to the desired page. |
65
|
|
|
* @throws EntityNotSupported Thrown if the entity is not supported for the given type. |
66
|
|
|
* @throws \InvalidArgumentException Thrown if the givent type is not existing. |
67
|
|
|
*/ |
68
|
|
|
public function getURL($entity, string $type) |
69
|
|
|
{ |
70
|
|
|
switch ($type) { |
71
|
|
|
case 'info': |
72
|
|
|
return $this->infoURL($entity); |
73
|
|
|
case 'edit': |
74
|
|
|
return $this->editURL($entity); |
75
|
|
|
case 'create': |
76
|
|
|
return $this->createURL($entity); |
77
|
|
|
case 'clone': |
78
|
|
|
return $this->cloneURL($entity); |
79
|
|
|
case 'list': |
80
|
|
|
case 'list_parts': |
81
|
|
|
return $this->listPartsURL($entity); |
82
|
|
|
case 'delete': |
83
|
|
|
return $this->deleteURL($entity); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
throw new \InvalidArgumentException('Method is not supported!'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Generates an URL to a page, where info about this entity can be viewed. |
91
|
|
|
* |
92
|
|
|
* @param $entity mixed The entity for which the info should be generated. |
93
|
|
|
* @return string The URL to the info page |
94
|
|
|
* @throws EntityNotSupported If the method is not supported for the given Entity |
95
|
|
|
*/ |
96
|
|
|
public function infoURL($entity): string |
97
|
|
|
{ |
98
|
|
|
if ($entity instanceof Part) { |
99
|
|
|
return $this->urlGenerator->generate('part_info', ['id' => $entity->getID()]); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
//Otherwise throw an error |
103
|
|
|
throw new EntityNotSupported('The given entity is not supported yet!'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Generates an URL to a page, where this entity can be edited. |
108
|
|
|
* |
109
|
|
|
* @param $entity mixed The entity for which the edit link should be generated. |
110
|
|
|
* @return string The URL to the edit page. |
111
|
|
|
* @throws EntityNotSupported If the method is not supported for the given Entity |
112
|
|
|
*/ |
113
|
|
|
public function editURL($entity): string |
114
|
|
|
{ |
115
|
|
|
if ($entity instanceof Part) { |
116
|
|
|
return $this->urlGenerator->generate('part_edit', ['id' => $entity->getID()]); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
if ($entity instanceof AttachmentType) { |
120
|
|
|
return $this->urlGenerator->generate('attachment_type_edit', ['id' => $entity->getID()]); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if ($entity instanceof Category) { |
124
|
|
|
return $this->urlGenerator->generate("category_edit", ['id' => $entity->getID()]); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
if ($entity instanceof Device) { |
128
|
|
|
return $this->urlGenerator->generate("device_edit", ['id' => $entity->getID()]); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
if ($entity instanceof Supplier) { |
132
|
|
|
return $this->urlGenerator->generate("supplier_edit", ['id' => $entity->getID()]); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
if ($entity instanceof Manufacturer) { |
136
|
|
|
return $this->urlGenerator->generate("manufacturer_edit", ['id' => $entity->getID()]); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
if ($entity instanceof Storelocation) { |
140
|
|
|
return $this->urlGenerator->generate("store_location_edit", ['id' => $entity->getID()]); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
if ($entity instanceof Footprint) { |
144
|
|
|
return $this->urlGenerator->generate("footprint_edit", ['id' => $entity->getID()]); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
//Otherwise throw an error |
148
|
|
|
throw new EntityNotSupported('The given entity is not supported yet!'); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Generates an URL to a page, where a entity of this type can be created. |
153
|
|
|
* |
154
|
|
|
* @param $entity mixed The entity for which the link should be generated. |
155
|
|
|
* @return string The URL to the page. |
156
|
|
|
* @throws EntityNotSupported If the method is not supported for the given Entity |
157
|
|
|
*/ |
158
|
|
|
public function createURL($entity): string |
159
|
|
|
{ |
160
|
|
|
if ($entity instanceof Part) { |
161
|
|
|
return $this->urlGenerator->generate('part_new'); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
if ($entity instanceof AttachmentType) { |
165
|
|
|
return $this->urlGenerator->generate('attachment_type_new'); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
if ($entity instanceof Category) { |
169
|
|
|
return $this->urlGenerator->generate('category_new'); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
if ($entity instanceof Device) { |
173
|
|
|
return $this->urlGenerator->generate('device_new'); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
if ($entity instanceof Supplier) { |
177
|
|
|
return $this->urlGenerator->generate('supplier_new'); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
if ($entity instanceof Manufacturer) { |
181
|
|
|
return $this->urlGenerator->generate('manufacturer_new'); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
if ($entity instanceof Storelocation) { |
185
|
|
|
return $this->urlGenerator->generate('store_location_new'); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
if ($entity instanceof Footprint) { |
189
|
|
|
return $this->urlGenerator->generate('footprint_new'); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
throw new EntityNotSupported('The given entity is not supported yet!'); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Generates an URL to a page, where a new entity can be created, that has the same informations as the |
197
|
|
|
* given entity (element cloning) |
198
|
|
|
* |
199
|
|
|
* @param $entity mixed The entity for which the link should be generated. |
200
|
|
|
* @return string The URL to the page. |
201
|
|
|
* @throws EntityNotSupported If the method is not supported for the given Entity |
202
|
|
|
*/ |
203
|
|
|
public function cloneURL($entity): string |
204
|
|
|
{ |
205
|
|
|
if ($entity instanceof Part) { |
206
|
|
|
return $this->urlGenerator->generate('part_clone', ['id' => $entity->getID()]); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
throw new EntityNotSupported('The given entity is not supported yet!'); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Generates an URL to a page, where all parts are listed, which are contained in the given element. |
214
|
|
|
* |
215
|
|
|
* @param $entity mixed The entity for which the link should be generated. |
216
|
|
|
* @return string The URL to the page. |
217
|
|
|
* @throws EntityNotSupported If the method is not supported for the given Entity |
218
|
|
|
*/ |
219
|
|
|
public function listPartsURL($entity) : string |
220
|
|
|
{ |
221
|
|
|
if ($entity instanceof Category) { |
222
|
|
|
return $this->urlGenerator->generate('app_partlists_showcategory', ['id' => $entity->getID()]); |
223
|
|
|
} |
224
|
|
|
throw new EntityNotSupported('The given entity is not supported yet!'); |
225
|
|
|
|
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
public function deleteURL($entity) : string |
229
|
|
|
{ |
230
|
|
|
if ($entity instanceof AttachmentType) { |
231
|
|
|
return $this->urlGenerator->generate('attachment_type_delete', ['id' => $entity->getID()]); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
if ($entity instanceof Category) { |
235
|
|
|
return $this->urlGenerator->generate('category_delete', ['id' => $entity->getID()]); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
if ($entity instanceof Device) { |
239
|
|
|
return $this->urlGenerator->generate('device_delete', ['id' => $entity->getID()]); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
if ($entity instanceof Supplier) { |
243
|
|
|
return $this->urlGenerator->generate('supplier_delete', ['id' => $entity->getID()]); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
if ($entity instanceof Manufacturer) { |
247
|
|
|
return $this->urlGenerator->generate('manufacturer_new', ['id' => $entity->getID()]); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
if ($entity instanceof Storelocation) { |
251
|
|
|
return $this->urlGenerator->generate('store_location_new', ['id' => $entity->getID()]); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
if ($entity instanceof Footprint) { |
255
|
|
|
return $this->urlGenerator->generate('footprint_new', ['id' => $entity->getID()]); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
throw new EntityNotSupported('The given entity is not supported yet!'); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Generates an HTML link to the info page about the given entity. |
263
|
|
|
* |
264
|
|
|
* @param $entity mixed The entity for which the info link should be generated. |
265
|
|
|
* |
266
|
|
|
* @return string The HTML of the info page link |
267
|
|
|
* |
268
|
|
|
* @throws EntityNotSupported |
269
|
|
|
*/ |
270
|
|
|
public function infoHTML($entity): string |
271
|
|
|
{ |
272
|
|
|
$href = $this->infoURL($entity); |
273
|
|
|
|
274
|
|
|
if ($entity instanceof NamedDBElement) { |
|
|
|
|
275
|
|
|
return sprintf('<a href="%s">%s</a>', $href, $entity->getName()); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
throw new EntityNotSupported('The given entity is not supported yet!'); |
279
|
|
|
} |
280
|
|
|
} |
281
|
|
|
|