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