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\NamedDBElement; |
35
|
|
|
use App\Entity\Part; |
36
|
|
|
use App\Exceptions\EntityNotSupported; |
37
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
38
|
|
|
|
39
|
|
|
class EntityURLGenerator |
40
|
|
|
{ |
41
|
|
|
/** |
42
|
|
|
* @var UrlGeneratorInterface |
43
|
|
|
*/ |
44
|
|
|
protected $urlGenerator; |
45
|
|
|
|
46
|
|
|
public function __construct(UrlGeneratorInterface $urlGenerator) |
47
|
|
|
{ |
48
|
|
|
$this->urlGenerator = $urlGenerator; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Generates an URL to the page using the given page type and element. |
53
|
|
|
* For the given types, the [type]URL() functions are called (e.g. infoURL()). |
54
|
|
|
* Not all entity class and $type combinations are supported. |
55
|
|
|
* |
56
|
|
|
* @param $entity mixed The element for which the page should be generated. |
57
|
|
|
* @param string $type The page type. Currently supported: 'info', 'edit', 'create', 'clone', 'list'/'list_parts' |
58
|
|
|
* @return string The link to the desired page. |
59
|
|
|
* @throws EntityNotSupported Thrown if the entity is not supported for the given type. |
60
|
|
|
* @throws \InvalidArgumentException Thrown if the givent type is not existing. |
61
|
|
|
*/ |
62
|
|
|
public function getURL($entity, string $type) |
63
|
|
|
{ |
64
|
|
|
switch ($type) { |
65
|
|
|
case 'info': |
66
|
|
|
return $this->infoURL($entity); |
67
|
|
|
case 'edit': |
68
|
|
|
return $this->editURL($entity); |
69
|
|
|
case 'create': |
70
|
|
|
return $this->createURL($entity); |
71
|
|
|
case 'clone': |
72
|
|
|
return $this->cloneURL($entity); |
73
|
|
|
case 'list': |
74
|
|
|
case 'list_parts': |
75
|
|
|
return $this->listPartsURL($entity); |
76
|
|
|
case 'delete': |
77
|
|
|
return $this->deleteURL($entity); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
throw new \InvalidArgumentException('Method is not supported!'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Generates an URL to a page, where info about this entity can be viewed. |
85
|
|
|
* |
86
|
|
|
* @param $entity mixed The entity for which the info should be generated. |
87
|
|
|
* @return string The URL to the info page |
88
|
|
|
* @throws EntityNotSupported If the method is not supported for the given Entity |
89
|
|
|
*/ |
90
|
|
|
public function infoURL($entity): string |
91
|
|
|
{ |
92
|
|
|
if ($entity instanceof Part) { |
93
|
|
|
return $this->urlGenerator->generate('part_info', ['id' => $entity->getID()]); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
//Otherwise throw an error |
97
|
|
|
throw new EntityNotSupported('The given entity is not supported yet!'); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Generates an URL to a page, where this entity can be edited. |
102
|
|
|
* |
103
|
|
|
* @param $entity mixed The entity for which the edit link should be generated. |
104
|
|
|
* @return string The URL to the edit page. |
105
|
|
|
* @throws EntityNotSupported If the method is not supported for the given Entity |
106
|
|
|
*/ |
107
|
|
|
public function editURL($entity): string |
108
|
|
|
{ |
109
|
|
|
if ($entity instanceof Part) { |
110
|
|
|
return $this->urlGenerator->generate('part_edit', ['id' => $entity->getID()]); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
if($entity instanceof AttachmentType) { |
114
|
|
|
return $this->urlGenerator->generate('attachment_type_edit', ['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 a entity of this type can be created. |
123
|
|
|
* |
124
|
|
|
* @param $entity mixed The entity for which the link should be generated. |
125
|
|
|
* @return string The URL to the page. |
126
|
|
|
* @throws EntityNotSupported If the method is not supported for the given Entity |
127
|
|
|
*/ |
128
|
|
|
public function createURL($entity): string |
129
|
|
|
{ |
130
|
|
|
if ($entity instanceof Part) { |
131
|
|
|
return $this->urlGenerator->generate('part_new'); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if($entity instanceof AttachmentType) { |
135
|
|
|
return $this->urlGenerator->generate('attachment_type_new'); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
throw new EntityNotSupported('The given entity is not supported yet!'); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Generates an URL to a page, where a new entity can be created, that has the same informations as the |
143
|
|
|
* given entity (element cloning) |
144
|
|
|
* |
145
|
|
|
* @param $entity mixed The entity for which the link should be generated. |
146
|
|
|
* @return string The URL to the page. |
147
|
|
|
* @throws EntityNotSupported If the method is not supported for the given Entity |
148
|
|
|
*/ |
149
|
|
|
public function cloneURL($entity): string |
150
|
|
|
{ |
151
|
|
|
if ($entity instanceof Part) { |
152
|
|
|
return $this->urlGenerator->generate('part_clone', ['id' => $entity->getID()]); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
throw new EntityNotSupported('The given entity is not supported yet!'); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Generates an URL to a page, where all parts are listed, which are contained in the given element. |
160
|
|
|
* |
161
|
|
|
* @param $entity mixed The entity for which the link should be generated. |
162
|
|
|
* @return string The URL to the page. |
163
|
|
|
* @throws EntityNotSupported If the method is not supported for the given Entity |
164
|
|
|
*/ |
165
|
|
|
public function listPartsURL($entity) : string |
166
|
|
|
{ |
167
|
|
|
if ($entity instanceof Category) { |
168
|
|
|
return $this->urlGenerator->generate('app_partlists_showcategory', ['id' => $entity->getID()]); |
169
|
|
|
} |
170
|
|
|
throw new EntityNotSupported('The given entity is not supported yet!'); |
171
|
|
|
|
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public function deleteURL($entity) : string |
175
|
|
|
{ |
176
|
|
|
if($entity instanceof AttachmentType) { |
177
|
|
|
return $this->urlGenerator->generate('attachment_type_delete', ['id' => $entity->getID()]); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
throw new EntityNotSupported('The given entity is not supported yet!'); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Generates an HTML link to the info page about the given entity. |
185
|
|
|
* |
186
|
|
|
* @param $entity mixed The entity for which the info link should be generated. |
187
|
|
|
* |
188
|
|
|
* @return string The HTML of the info page link |
189
|
|
|
* |
190
|
|
|
* @throws EntityNotSupported |
191
|
|
|
*/ |
192
|
|
|
public function infoHTML($entity): string |
193
|
|
|
{ |
194
|
|
|
$href = $this->infoURL($entity); |
195
|
|
|
|
196
|
|
|
if ($entity instanceof NamedDBElement) { |
|
|
|
|
197
|
|
|
return sprintf('<a href="%s">%s</a>', $href, $entity->getName()); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
throw new EntityNotSupported('The given entity is not supported yet!'); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|