|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the jquery-datatables-bundle package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2022 WEBEWEB |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace WBW\Bundle\JQuery\DataTablesBundle\Provider; |
|
13
|
|
|
|
|
14
|
|
|
use InvalidArgumentException; |
|
15
|
|
|
use Symfony\Component\Routing\Exception\InvalidParameterException; |
|
16
|
|
|
use Symfony\Component\Routing\Exception\MissingMandatoryParametersException; |
|
17
|
|
|
use Symfony\Component\Routing\Exception\RouteNotFoundException; |
|
18
|
|
|
use Symfony\Component\Routing\RouterInterface; |
|
19
|
|
|
use WBW\Bundle\BootstrapBundle\Twig\Extension\CSS\ButtonTwigExtension; |
|
20
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Helper\DataTablesEntityHelper; |
|
21
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\WBWJQueryDataTablesBundle; |
|
22
|
|
|
use WBW\Library\Types\Helper\ArrayHelper; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* DataTables buttons renderer trait. |
|
26
|
|
|
* |
|
27
|
|
|
* @author webeweb <https://github.com/webeweb> |
|
28
|
|
|
* @package WBW\Bundle\JQuery\DataTablesBundle\Provider |
|
29
|
|
|
* @internal |
|
30
|
|
|
*/ |
|
31
|
|
|
trait DataTablesButtonsRendererTrait { |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Get the button Twig extension. |
|
35
|
|
|
* |
|
36
|
|
|
* @return ButtonTwigExtension|null Returns the button Twig extension. |
|
37
|
|
|
*/ |
|
38
|
|
|
abstract public function getButtonTwigExtension(): ?ButtonTwigExtension; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Get the provider name. |
|
42
|
|
|
* |
|
43
|
|
|
* @return string Returns the provider name. |
|
44
|
|
|
*/ |
|
45
|
|
|
abstract public function getName(): string; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Get the router. |
|
49
|
|
|
* |
|
50
|
|
|
* @return RouterInterface|null Returns the router. |
|
51
|
|
|
*/ |
|
52
|
|
|
abstract public function getRouter(): ?RouterInterface; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Render an action button. |
|
56
|
|
|
* |
|
57
|
|
|
* @param object $entity The entity. |
|
58
|
|
|
* @param string $route The route. |
|
59
|
|
|
* @param array<string,mixed> $options The options. |
|
60
|
|
|
* @return string Returns the action button. |
|
61
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if the entity is invalid. |
|
62
|
|
|
* @throws InvalidParameterException Throws an invalid parameter exception if a parameter is invalid. |
|
63
|
|
|
* @throws RouteNotFoundException Throws a route not found exception if the route doesn't exist. |
|
64
|
|
|
* @throws MissingMandatoryParametersException Throws a missing mandatory parameter exception if a mandatory parameter is missing. |
|
65
|
|
|
* @internal |
|
66
|
|
|
*/ |
|
67
|
|
|
private function renderActionButton($entity, string $route, array $options): string { |
|
68
|
|
|
|
|
69
|
|
|
DataTablesEntityHelper::isCompatible($entity, true); |
|
70
|
|
|
|
|
71
|
|
|
$_label = ArrayHelper::get($options, "label"); |
|
72
|
|
|
$_domain = ArrayHelper::get($options, "translation_domain"); |
|
73
|
|
|
$_icon = ArrayHelper::get($options, "icon"); |
|
74
|
|
|
$_params = ArrayHelper::get($options, "route", []); |
|
75
|
|
|
$_target = ArrayHelper::get($options, "target"); |
|
76
|
|
|
|
|
77
|
|
|
$method = sprintf("bootstrapButton%sFunction", ArrayHelper::get($options, "type", "Default")); |
|
78
|
|
|
|
|
79
|
|
|
$title = $this->translate($_label, [], $_domain); |
|
80
|
|
|
$button = $this->getButtonTwigExtension()->$method(["icon" => $_icon, "title" => $title, "size" => "xs"]); |
|
81
|
|
|
$href = $this->getRouter()->generate($route, array_merge($_params, ["id" => $entity->getId()])); |
|
82
|
|
|
|
|
83
|
|
|
return $this->getButtonTwigExtension()->bootstrapButtonLinkFilter($button, $href, $_target); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Render an action button "comment". |
|
88
|
|
|
* |
|
89
|
|
|
* @param object $entity The entity. |
|
90
|
|
|
* @param string $route The route. |
|
91
|
|
|
* @param string|null $comment The comment. |
|
92
|
|
|
* @return string Returns the action button "comment". |
|
93
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if the entity is invalid. |
|
94
|
|
|
* @throws InvalidParameterException Throws an invalid parameter exception if a parameter is invalid. |
|
95
|
|
|
* @throws RouteNotFoundException Throws a route not found exception if the route doesn't exist. |
|
96
|
|
|
* @throws MissingMandatoryParametersException Throws a missing mandatory parameter exception if a mandatory parameter is missing. |
|
97
|
|
|
*/ |
|
98
|
|
|
protected function renderActionButtonComment($entity, string $route, ?string $comment): string { |
|
99
|
|
|
|
|
100
|
|
|
return $this->renderActionButton($entity, $route, [ |
|
101
|
|
|
"type" => "Default", |
|
102
|
|
|
"icon" => 0 < mb_strlen($comment) ? "fa:comment" : "fa:comment-slash", |
|
|
|
|
|
|
103
|
|
|
"label" => "label.comment", |
|
104
|
|
|
"translation_domain" => WBWJQueryDataTablesBundle::getTranslationDomain(), |
|
105
|
|
|
]); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Render an action button "delete". |
|
110
|
|
|
* |
|
111
|
|
|
* @param object $entity The entity. |
|
112
|
|
|
* @param string $route The route. |
|
113
|
|
|
* @return string Returns the action button "delete". |
|
114
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if the entity is invalid. |
|
115
|
|
|
* @throws InvalidParameterException Throws an invalid parameter exception if a parameter is invalid. |
|
116
|
|
|
* @throws RouteNotFoundException Throws a route not found exception if the route doesn't exist. |
|
117
|
|
|
* @throws MissingMandatoryParametersException Throws a missing mandatory parameter exception if a mandatory parameter is missing. |
|
118
|
|
|
*/ |
|
119
|
|
|
protected function renderActionButtonDelete($entity, string $route): string { |
|
120
|
|
|
|
|
121
|
|
|
$args = "wbw_jquery_datatables_delete" === $route ? ["name" => $this->getName()] : []; |
|
122
|
|
|
|
|
123
|
|
|
return $this->renderActionButton($entity, $route, [ |
|
124
|
|
|
"type" => "Danger", |
|
125
|
|
|
"icon" => "fa:trash", |
|
126
|
|
|
"label" => "label.delete", |
|
127
|
|
|
"translation_domain" => WBWJQueryDataTablesBundle::getTranslationDomain(), |
|
128
|
|
|
"route" => $args, |
|
129
|
|
|
]); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Render an action button "duplicate". |
|
134
|
|
|
* |
|
135
|
|
|
* @param object $entity The entity. |
|
136
|
|
|
* @param string $route The route. |
|
137
|
|
|
* @return string Returns the action button "duplicate". |
|
138
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if the entity is invalid. |
|
139
|
|
|
* @throws InvalidParameterException Throws an invalid parameter exception if a parameter is invalid. |
|
140
|
|
|
* @throws RouteNotFoundException Throws a route not found exception if the route doesn't exist. |
|
141
|
|
|
* @throws MissingMandatoryParametersException Throws a missing mandatory parameter exception if a mandatory parameter is missing. |
|
142
|
|
|
*/ |
|
143
|
|
|
protected function renderActionButtonDuplicate($entity, string $route): string { |
|
144
|
|
|
|
|
145
|
|
|
return $this->renderActionButton($entity, $route, [ |
|
146
|
|
|
"type" => "Primary", |
|
147
|
|
|
"icon" => "fa:copy", |
|
148
|
|
|
"label" => "label.duplicate", |
|
149
|
|
|
"translation_domain" => WBWJQueryDataTablesBundle::getTranslationDomain(), |
|
150
|
|
|
]); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Render an action button "edit". |
|
155
|
|
|
* |
|
156
|
|
|
* @param object $entity The entity. |
|
157
|
|
|
* @param string $route The route. |
|
158
|
|
|
* @return string Returns the action button "edit". |
|
159
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if the entity is invalid. |
|
160
|
|
|
* @throws InvalidParameterException Throws an invalid parameter exception if a parameter is invalid. |
|
161
|
|
|
* @throws RouteNotFoundException Throws a route not found exception if the route doesn't exist. |
|
162
|
|
|
* @throws MissingMandatoryParametersException Throws a missing mandatory parameter exception if a mandatory parameter is missing. |
|
163
|
|
|
*/ |
|
164
|
|
|
protected function renderActionButtonEdit($entity, string $route): string { |
|
165
|
|
|
|
|
166
|
|
|
return $this->renderActionButton($entity, $route, [ |
|
167
|
|
|
"type" => "Default", |
|
168
|
|
|
"icon" => "fa:pen", |
|
169
|
|
|
"label" => "label.edit", |
|
170
|
|
|
"translation_domain" => WBWJQueryDataTablesBundle::getTranslationDomain(), |
|
171
|
|
|
]); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Render an action button "new". |
|
176
|
|
|
* |
|
177
|
|
|
* @param object|null $entity The entity. |
|
178
|
|
|
* @param string $route The route. |
|
179
|
|
|
* @return string Returns the action button "new". |
|
180
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if the entity is invalid. |
|
181
|
|
|
* @throws InvalidParameterException Throws an invalid parameter exception if a parameter is invalid. |
|
182
|
|
|
* @throws RouteNotFoundException Throws a route not found exception if the route doesn't exist. |
|
183
|
|
|
* @throws MissingMandatoryParametersException Throws a missing mandatory parameter exception if a mandatory parameter is missing. |
|
184
|
|
|
*/ |
|
185
|
|
|
protected function renderActionButtonNew($entity, string $route): string { |
|
186
|
|
|
|
|
187
|
|
|
$args = []; |
|
188
|
|
|
if (null !== $entity && DataTablesEntityHelper::isCompatible($entity, true)) { |
|
189
|
|
|
$args = ["id" => $entity->getId()]; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
$title = $this->translate("label.new", [], WBWJQueryDataTablesBundle::getTranslationDomain()); |
|
193
|
|
|
$button = $this->getButtonTwigExtension()->bootstrapButtonPrimaryFunction(["icon" => "fa:plus", "title" => $title, "size" => "xs"]); |
|
194
|
|
|
$href = $this->getRouter()->generate($route, $args); |
|
195
|
|
|
|
|
196
|
|
|
return $this->getButtonTwigExtension()->bootstrapButtonLinkFilter($button, $href); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Render an action button "PDF". |
|
201
|
|
|
* |
|
202
|
|
|
* @param object $entity The entity. |
|
203
|
|
|
* @param string $route The route. |
|
204
|
|
|
* @return string Returns the action button "PDF". |
|
205
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if the entity is invalid. |
|
206
|
|
|
* @throws InvalidParameterException Throws an invalid parameter exception if a parameter is invalid. |
|
207
|
|
|
* @throws RouteNotFoundException Throws a route not found exception if the route doesn't exist. |
|
208
|
|
|
* @throws MissingMandatoryParametersException Throws a missing mandatory parameter exception if a mandatory parameter is missing. |
|
209
|
|
|
*/ |
|
210
|
|
|
protected function renderActionButtonPdf($entity, string $route): string { |
|
211
|
|
|
|
|
212
|
|
|
return $this->renderActionButton($entity, $route, [ |
|
213
|
|
|
"type" => "Danger", |
|
214
|
|
|
"icon" => "fa:file-pdf", |
|
215
|
|
|
"label" => "PDF", |
|
216
|
|
|
"translation_domain" => WBWJQueryDataTablesBundle::getTranslationDomain(), |
|
217
|
|
|
"target" => "_blank", |
|
218
|
|
|
]); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* Render an action button "show". |
|
223
|
|
|
* |
|
224
|
|
|
* @param object $entity The entity. |
|
225
|
|
|
* @param string $route The route. |
|
226
|
|
|
* @return string Returns the action button "show". |
|
227
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if the entity is invalid. |
|
228
|
|
|
* @throws InvalidParameterException Throws an invalid parameter exception if a parameter is invalid. |
|
229
|
|
|
* @throws RouteNotFoundException Throws a route not found exception if the route doesn't exist. |
|
230
|
|
|
* @throws MissingMandatoryParametersException Throws a missing mandatory parameter exception if a mandatory parameter is missing. |
|
231
|
|
|
*/ |
|
232
|
|
|
protected function renderActionButtonShow($entity, string $route): string { |
|
233
|
|
|
|
|
234
|
|
|
return $this->renderActionButton($entity, $route, [ |
|
235
|
|
|
"type" => "Info", |
|
236
|
|
|
"icon" => "fa:eye", |
|
237
|
|
|
"label" => "label.show", |
|
238
|
|
|
"translation_domain" => WBWJQueryDataTablesBundle::getTranslationDomain(), |
|
239
|
|
|
]); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* Render an action button "switch". |
|
244
|
|
|
* |
|
245
|
|
|
* @param object $entity The entity. |
|
246
|
|
|
* @param string $route The route. |
|
247
|
|
|
* @param bool|null $enabled Enabled ? |
|
248
|
|
|
* @return string Returns the action button "switch". |
|
249
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if the entity is invalid. |
|
250
|
|
|
* @throws InvalidParameterException Throws an invalid parameter exception if a parameter is invalid. |
|
251
|
|
|
* @throws RouteNotFoundException Throws a route not found exception if the route doesn't exist. |
|
252
|
|
|
* @throws MissingMandatoryParametersException Throws a missing mandatory parameter exception if a mandatory parameter is missing. |
|
253
|
|
|
*/ |
|
254
|
|
|
protected function renderActionButtonSwitch($entity, string $route, ?bool $enabled): string { |
|
255
|
|
|
|
|
256
|
|
|
return $this->renderActionButton($entity, $route, [ |
|
257
|
|
|
"type" => true === $enabled ? "Success" : "Danger", |
|
258
|
|
|
"icon" => true === $enabled ? "fa:toggle-on" : "fa:toggle-off", |
|
259
|
|
|
"label" => true === $enabled ? "label.disable" : "label.enable", |
|
260
|
|
|
"translation_domain" => WBWJQueryDataTablesBundle::getTranslationDomain(), |
|
261
|
|
|
]); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* Translate. |
|
266
|
|
|
* |
|
267
|
|
|
* @param string|null $id The id. |
|
268
|
|
|
* @param array $parameters Teh parameters. |
|
269
|
|
|
* @param string|null $domain The domain. |
|
270
|
|
|
* @param string|null $locale The locale. |
|
271
|
|
|
* @return string Returns the translated id in case of success, id otherwise. |
|
272
|
|
|
*/ |
|
273
|
|
|
abstract protected function translate(?string $id, array $parameters = [], string $domain = null, string $locale = null): string; |
|
274
|
|
|
} |
|
275
|
|
|
|