1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the jquery-datatables-bundle package. |
5
|
|
|
* |
6
|
|
|
* (c) 2019 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 DateTime; |
15
|
|
|
use InvalidArgumentException; |
16
|
|
|
use Symfony\Component\Routing\Exception\InvalidParameterException; |
17
|
|
|
use Symfony\Component\Routing\Exception\MissingMandatoryParametersException; |
18
|
|
|
use Symfony\Component\Routing\Exception\RouteNotFoundException; |
19
|
|
|
use Symfony\Component\Routing\RouterInterface; |
20
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
21
|
|
|
use WBW\Bundle\BootstrapBundle\Twig\Extension\CSS\ButtonTwigExtension; |
22
|
|
|
use WBW\Bundle\BootstrapBundle\Twig\Extension\CSS\ButtonTwigExtensionTrait; |
23
|
|
|
use WBW\Bundle\CoreBundle\Service\RouterTrait; |
24
|
|
|
use WBW\Bundle\CoreBundle\Service\TranslatorTrait; |
25
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\API\DataTablesResponseInterface; |
26
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Entity\DataTablesEntityInterface; |
27
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Factory\DataTablesFactory; |
28
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Helper\DataTablesEntityHelper; |
29
|
|
|
use WBW\Library\Core\Renderer\DateTimeRenderer; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Abstract DataTables provider. |
33
|
|
|
* |
34
|
|
|
* @author webeweb <https://github.com/webeweb/> |
35
|
|
|
* @package WBW\Bundle\JQuery\DataTablesBundle\Provider |
36
|
|
|
*/ |
37
|
|
|
abstract class AbstractDataTablesProvider implements DataTablesProviderInterface { |
38
|
|
|
|
39
|
|
|
use ButtonTwigExtensionTrait; |
40
|
|
|
use RouterTrait; |
41
|
|
|
use TranslatorTrait; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Constructor. |
45
|
|
|
* |
46
|
|
|
* @param RouterInterface $router The router. |
47
|
|
|
* @param TranslatorInterface $translator The translator. |
48
|
|
|
* @param ButtonTwigExtension $buttonTwigExtension The button Twig extension. |
49
|
|
|
*/ |
50
|
|
|
public function __construct(RouterInterface $router, TranslatorInterface $translator, ButtonTwigExtension $buttonTwigExtension) { |
51
|
|
|
$this->setButtonTwigExtension($buttonTwigExtension); |
52
|
|
|
$this->setRouter($router); |
53
|
|
|
$this->setTranslator($translator); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritDoc} |
58
|
|
|
*/ |
59
|
|
|
public function getCSVExporter() { |
60
|
|
|
return null; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritDoc} |
65
|
|
|
*/ |
66
|
|
|
public function getEditor() { |
67
|
|
|
return null; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritDoc} |
72
|
|
|
*/ |
73
|
|
|
public function getMethod() { |
74
|
|
|
return null; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritDoc} |
79
|
|
|
*/ |
80
|
|
|
public function getOptions() { |
81
|
|
|
|
82
|
|
|
$dtOptions = DataTablesFactory::newOptions(); |
83
|
|
|
$dtOptions->addOption("responsive", true); |
84
|
|
|
$dtOptions->addOption("searchDelay", 1000); |
85
|
|
|
|
86
|
|
|
return $dtOptions; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Render an action button "delete". |
91
|
|
|
* |
92
|
|
|
* @param DataTablesEntityInterface $entity The entity. |
93
|
|
|
* @param string $route The route. |
94
|
|
|
* @return string Returns the action button "delete". |
95
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if the entity is invalid. |
96
|
|
|
* @throws InvalidParameterException Throws an invalid parameter exception if a parameter is invalid. |
97
|
|
|
* @throws RouteNotFoundException Throws a route not found exception if the route doesn't exist. |
98
|
|
|
* @throws MissingMandatoryParametersException Throws a missing mandatory parameter exception if a mandatory exception is missing. |
99
|
|
|
*/ |
100
|
|
|
protected function renderActionButtonDelete($entity, $route) { |
101
|
|
|
|
102
|
|
|
if (false === DataTablesEntityHelper::isCompatible($entity)) { |
103
|
|
|
throw new InvalidArgumentException(sprintf("The entity must implements DataTablesEntityInterface or declare a getId() method")); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$args = "wbw_jquery_datatables_delete" === $route ? ["name" => $this->getName()] : []; |
107
|
|
|
|
108
|
|
|
$title = $this->getTranslator()->trans("label.delete", [], "WBWJQueryDataTablesBundle"); |
109
|
|
|
$button = $this->getButtonTwigExtension()->bootstrapButtonDangerFunction(["icon" => "fa:trash", "title" => $title, "size" => "xs"]); |
110
|
|
|
$url = $this->getRouter()->generate($route, array_merge($args, ["id" => $entity->getId()])); |
111
|
|
|
|
112
|
|
|
return $this->getButtonTwigExtension()->bootstrapButtonLinkFilter($button, $url); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Render an action button "duplicate". |
117
|
|
|
* |
118
|
|
|
* @param DataTablesEntityInterface $entity The entity. |
119
|
|
|
* @param string $route The route. |
120
|
|
|
* @return string Returns the action button "duplicate". |
121
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if the entity is invalid. |
122
|
|
|
* @throws InvalidParameterException Throws an invalid parameter exception if a parameter is invalid. |
123
|
|
|
* @throws RouteNotFoundException Throws a route not found exception if the route doesn't exist. |
124
|
|
|
* @throws MissingMandatoryParametersException Throws a missing mandatory parameter exception if a mandatory exception is missing. |
125
|
|
|
*/ |
126
|
|
|
protected function renderActionButtonDuplicate($entity, $route) { |
127
|
|
|
|
128
|
|
|
if (false === DataTablesEntityHelper::isCompatible($entity)) { |
129
|
|
|
throw new InvalidArgumentException(sprintf("The entity must implements DataTablesEntityInterface or declare a getId() method")); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$title = $this->getTranslator()->trans("label.duplicate", [], "WBWJQueryDataTablesBundle"); |
133
|
|
|
$button = $this->getButtonTwigExtension()->bootstrapButtonDefaultFunction(["icon" => "fa:copy", "title" => $title, "size" => "xs"]); |
134
|
|
|
$url = $this->getRouter()->generate($route, ["id" => $entity->getId()]); |
135
|
|
|
|
136
|
|
|
return $this->getButtonTwigExtension()->bootstrapButtonLinkFilter($button, $url); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Render an action button "edit". |
141
|
|
|
* |
142
|
|
|
* @param DataTablesEntityInterface $entity The entity. |
143
|
|
|
* @param string $route The route. |
144
|
|
|
* @return string Returns the action button "edit". |
145
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if the entity is invalid. |
146
|
|
|
* @throws InvalidParameterException Throws an invalid parameter exception if a parameter is invalid. |
147
|
|
|
* @throws RouteNotFoundException Throws a route not found exception if the route doesn't exist. |
148
|
|
|
* @throws MissingMandatoryParametersException Throws a missing mandatory parameter exception if a mandatory exception is missing. |
149
|
|
|
*/ |
150
|
|
|
protected function renderActionButtonEdit($entity, $route) { |
151
|
|
|
|
152
|
|
|
if (false === DataTablesEntityHelper::isCompatible($entity)) { |
153
|
|
|
throw new InvalidArgumentException(sprintf("The entity must implements DataTablesEntityInterface or declare a getId() method")); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
$title = $this->getTranslator()->trans("label.edit", [], "WBWJQueryDataTablesBundle"); |
157
|
|
|
$button = $this->getButtonTwigExtension()->bootstrapButtonDefaultFunction(["icon" => "fa:pen", "title" => $title, "size" => "xs"]); |
158
|
|
|
$url = $this->getRouter()->generate($route, ["id" => $entity->getId()]); |
159
|
|
|
|
160
|
|
|
return $this->getButtonTwigExtension()->bootstrapButtonLinkFilter($button, $url); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Render an action button "show". |
165
|
|
|
* |
166
|
|
|
* @param DataTablesEntityInterface $entity The entity. |
167
|
|
|
* @param string $route The route. |
168
|
|
|
* @return string Returns the action button "show". |
169
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if the entity is invalid. |
170
|
|
|
* @throws InvalidParameterException Throws an invalid parameter exception if a parameter is invalid. |
171
|
|
|
* @throws RouteNotFoundException Throws a route not found exception if the route doesn't exist. |
172
|
|
|
* @throws MissingMandatoryParametersException Throws a missing mandatory parameter exception if a mandatory exception is missing. |
173
|
|
|
*/ |
174
|
|
|
protected function renderActionButtonShow($entity, $route) { |
175
|
|
|
|
176
|
|
|
if (false === DataTablesEntityHelper::isCompatible($entity)) { |
177
|
|
|
throw new InvalidArgumentException(sprintf("The entity must implements DataTablesEntityInterface or declare a getId() method")); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
$title = $this->getTranslator()->trans("label.show", [], "WBWJQueryDataTablesBundle"); |
181
|
|
|
$button = $this->getButtonTwigExtension()->bootstrapButtonInfoFunction(["icon" => "fa:eye", "title" => $title, "size" => "xs"]); |
182
|
|
|
$url = $this->getRouter()->generate($route, ["id" => $entity->getId()]); |
183
|
|
|
|
184
|
|
|
return $this->getButtonTwigExtension()->bootstrapButtonLinkFilter($button, $url); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Render the DataTables buttons. |
189
|
|
|
* |
190
|
|
|
* @param DataTablesEntityInterface $entity The entity. |
191
|
|
|
* @param string $editRoute The edit route. |
192
|
|
|
* @param string $deleteRoute The delete route. |
193
|
|
|
* @param bool $enableDelete Enable delete ? |
194
|
|
|
* @return string Returns the DataTables buttons. |
195
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if the entity is invalid. |
196
|
|
|
* @throws InvalidParameterException Throws an invalid parameter exception if a parameter is invalid. |
197
|
|
|
* @throws RouteNotFoundException Throws a route not found exception if the route doesn't exist. |
198
|
|
|
* @throws MissingMandatoryParametersException Throws a missing mandatory parameter exception if a mandatory exception is missing. |
199
|
|
|
* @deprecated since 3.4.0 use {@see WBW\Bundle\JQuery\DataTablesBundle\Provider\AbstractDataTablesProvider::renderRowButtons()} instead |
200
|
|
|
*/ |
201
|
|
|
protected function renderButtons($entity, $editRoute, $deleteRoute = null, $enableDelete = true) { |
202
|
|
|
if (null === $deleteRoute && true === $enableDelete) { |
203
|
|
|
$deleteRoute = "wbw_jquery_datatables_delete"; |
204
|
|
|
} |
205
|
|
|
return $this->renderRowButtons($entity, $editRoute, $deleteRoute, null); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Render a date. |
210
|
|
|
* |
211
|
|
|
* @param DateTime $date The date. |
212
|
|
|
* @param string $format The format. |
213
|
|
|
* @return string Returns the rendered date. |
214
|
|
|
*/ |
215
|
|
|
protected function renderDate(DateTime $date = null, $format = "Y-m-d") { |
216
|
|
|
return DateTimeRenderer::renderDateTime($date, $format); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Render a date/time. |
221
|
|
|
* |
222
|
|
|
* @param DateTime $date The date/time. |
223
|
|
|
* @param string $format The format. |
224
|
|
|
* @return string Returns the rendered date/time. |
225
|
|
|
*/ |
226
|
|
|
protected function renderDateTime(DateTime $date = null, $format = DateTimeRenderer::DATETIME_FORMAT) { |
227
|
|
|
return DateTimeRenderer::renderDateTime($date, $format); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Render a float. |
232
|
|
|
* |
233
|
|
|
* @param float $number The number. |
234
|
|
|
* @param int $decimals The decimals. |
235
|
|
|
* @param string $decPoint The decimal point. |
236
|
|
|
* @param string $thousandsSep The thousands separator. |
237
|
|
|
* @return string Returns the rendered number. |
238
|
|
|
*/ |
239
|
|
|
protected function renderFloat($number, $decimals = 2, $decPoint = ".", $thousandsSep = ",") { |
240
|
|
|
if (null === $number) { |
241
|
|
|
return ""; |
242
|
|
|
} |
243
|
|
|
return number_format($number, $decimals, $decPoint, $thousandsSep); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* {@inheritdoc} |
248
|
|
|
*/ |
249
|
|
|
public function renderRow($dtRow, $entity, $rowNumber) { |
250
|
|
|
|
251
|
|
|
if (false === DataTablesEntityHelper::isCompatible($entity)) { |
252
|
|
|
throw new InvalidArgumentException(sprintf("The entity must implements DataTablesEntityInterface or declare a getId() method")); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
switch ($dtRow) { |
256
|
|
|
|
257
|
|
|
case DataTablesResponseInterface::DATATABLES_ROW_ATTR: |
258
|
|
|
return ["data-id" => $entity->getId()]; |
259
|
|
|
|
260
|
|
|
case DataTablesResponseInterface::DATATABLES_ROW_DATA: |
261
|
|
|
return ["pkey" => $entity->getId()]; |
262
|
|
|
|
263
|
|
|
case DataTablesResponseInterface::DATATABLES_ROW_ID: |
264
|
|
|
return implode("-", [$this->getName(), $entity->getId()]); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
return null; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Render the DataTables row buttons. |
272
|
|
|
* |
273
|
|
|
* @param DataTablesEntityInterface $entity The entity. |
274
|
|
|
* @param string|null $editRoute The edit route. |
275
|
|
|
* @param string|null $deleteRoute The delete route. |
276
|
|
|
* @param string|null $showRoute The show route. |
277
|
|
|
* @return string Returns the DataTables row buttons. |
278
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if the entity is invalid. |
279
|
|
|
* @throws InvalidParameterException Throws an invalid parameter exception if a parameter is invalid. |
280
|
|
|
* @throws RouteNotFoundException Throws a route not found exception if the route doesn't exist. |
281
|
|
|
* @throws MissingMandatoryParametersException Throws a missing mandatory parameter exception if a mandatory exception is missing. |
282
|
|
|
*/ |
283
|
|
|
protected function renderRowButtons($entity, $editRoute = null, $deleteRoute = null, $showRoute = null) { |
284
|
|
|
|
285
|
|
|
$anchors = []; |
286
|
|
|
|
287
|
|
|
if (null !== $showRoute) { |
288
|
|
|
$anchors[] = $this->renderActionButtonShow($entity, $showRoute); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
if (null !== $editRoute) { |
292
|
|
|
$anchors[] = $this->renderActionButtonEdit($entity, $editRoute); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
if (null !== $deleteRoute) { |
296
|
|
|
$anchors[] = $this->renderActionButtonDelete($entity, $deleteRoute); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
return implode(" ", $anchors); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Wrap a content. |
304
|
|
|
* |
305
|
|
|
* @param string|null $prefix The prefix |
306
|
|
|
* @param string $content The content. |
307
|
|
|
* @param string|null $suffix The suffix. |
308
|
|
|
* @return string Returns the wrapped content. |
309
|
|
|
*/ |
310
|
|
|
protected function wrapContent($prefix, $content, $suffix) { |
311
|
|
|
|
312
|
|
|
$output = []; |
313
|
|
|
|
314
|
|
|
if (null !== $prefix) { |
315
|
|
|
$output[] = $prefix; |
316
|
|
|
} |
317
|
|
|
$output[] = $content; |
318
|
|
|
if (null !== $suffix) { |
319
|
|
|
$output[] = $suffix; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
return implode("", $output); |
323
|
|
|
} |
324
|
|
|
} |
325
|
|
|
|