Completed
Push — master ( e02664...9990f6 )
by WEBEWEB
01:20
created

renderActionButtonShow()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 2
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\Renderer\DateTimeRenderer;
24
use WBW\Bundle\CoreBundle\Service\RouterTrait;
25
use WBW\Bundle\CoreBundle\Service\TranslatorTrait;
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
30
/**
31
 * Abstract DataTables provider.
32
 *
33
 * @author webeweb <https://github.com/webeweb/>
34
 * @package WBW\Bundle\JQuery\DataTablesBundle\Provider
35
 */
36
abstract class AbstractDataTablesProvider implements DataTablesProviderInterface {
37
38
    use ButtonTwigExtensionTrait;
39
    use RouterTrait;
40
    use TranslatorTrait;
41
42
    /**
43
     * Constructor.
44
     *
45
     * @param RouterInterface $router The router.
46
     * @param TranslatorInterface $translator The translator.
47
     * @param ButtonTwigExtension $buttonTwigExtension The button Twig extension.
48
     */
49
    public function __construct(RouterInterface $router, TranslatorInterface $translator, ButtonTwigExtension $buttonTwigExtension) {
50
        $this->setButtonTwigExtension($buttonTwigExtension);
51
        $this->setRouter($router);
52
        $this->setTranslator($translator);
0 ignored issues
show
Documentation introduced by
$translator is of type object<Symfony\Component...on\TranslatorInterface>, but the function expects a null|object<WBW\Bundle\C...aseTranslatorInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
53
    }
54
55
    /**
56
     * {@inheritDoc}
57
     */
58
    public function getCSVExporter() {
59
        return null;
60
    }
61
62
    /**
63
     * {@inheritDoc}
64
     */
65
    public function getEditor() {
66
        return null;
67
    }
68
69
    /**
70
     * {@inheritDoc}
71
     */
72
    public function getMethod() {
73
        return null;
74
    }
75
76
    /**
77
     * {@inheritDoc}
78
     */
79
    public function getOptions() {
80
81
        $dtOptions = DataTablesFactory::newOptions();
82
        $dtOptions->addOption("responsive", true);
83
        $dtOptions->addOption("searchDelay", 1000);
84
85
        return $dtOptions;
86
    }
87
88
    /**
89
     * Render an action button "delete".
90
     *
91
     * @param DataTablesEntityInterface $entity The entity.
92
     * @param string $route The route.
93
     * @return string Returns the action button "delete".
94
     * @throws InvalidArgumentException Throws an invalid argument exception if the entity is invalid.
95
     * @throws InvalidParameterException Throws an invalid parameter exception if a parameter is invalid.
96
     * @throws RouteNotFoundException Throws a route not found exception if the route doesn't exist.
97
     * @throws MissingMandatoryParametersException Throws a missing mandatory parameter exception if a mandatory exception is missing.
98
     */
99
    protected function renderActionButtonDelete($entity, $route) {
100
101
        if (false === DataTablesEntityHelper::isCompatible($entity)) {
102
            throw new InvalidArgumentException(sprintf("The entity must implements DataTablesEntityInterface or declare a getId() method"));
103
        }
104
105
        $args = "wbw_jquery_datatables_delete" === $route ? ["name" => $this->getName()] : [];
106
107
        $title  = $this->getTranslator()->trans("label.delete", [], "WBWJQueryDataTablesBundle");
108
        $button = $this->getButtonTwigExtension()->bootstrapButtonDangerFunction(["icon" => "fa:trash", "title" => $title, "size" => "xs"]);
109
        $url    = $this->getRouter()->generate($route, array_merge($args, ["id" => $entity->getId()]));
110
111
        return $this->getButtonTwigExtension()->bootstrapButtonLinkFilter($button, $url);
112
    }
113
114
    /**
115
     * Render an action button "duplicate".
116
     *
117
     * @param DataTablesEntityInterface $entity The entity.
118
     * @param string $route The route.
119
     * @return string Returns the action button "duplicate".
120
     * @throws InvalidArgumentException Throws an invalid argument exception if the entity is invalid.
121
     * @throws InvalidParameterException Throws an invalid parameter exception if a parameter is invalid.
122
     * @throws RouteNotFoundException Throws a route not found exception if the route doesn't exist.
123
     * @throws MissingMandatoryParametersException Throws a missing mandatory parameter exception if a mandatory exception is missing.
124
     */
125
    protected function renderActionButtonDuplicate($entity, $route) {
126
127
        if (false === DataTablesEntityHelper::isCompatible($entity)) {
128
            throw new InvalidArgumentException(sprintf("The entity must implements DataTablesEntityInterface or declare a getId() method"));
129
        }
130
131
        $title  = $this->getTranslator()->trans("label.duplicate", [], "WBWJQueryDataTablesBundle");
132
        $button = $this->getButtonTwigExtension()->bootstrapButtonDefaultFunction(["icon" => "fa:copy", "title" => $title, "size" => "xs"]);
133
        $url    = $this->getRouter()->generate($route, ["id" => $entity->getId()]);
134
135
        return $this->getButtonTwigExtension()->bootstrapButtonLinkFilter($button, $url);
136
    }
137
138
    /**
139
     * Render an action button "edit".
140
     *
141
     * @param DataTablesEntityInterface $entity The entity.
142
     * @param string $route The route.
143
     * @return string Returns the action button "edit".
144
     * @throws InvalidArgumentException Throws an invalid argument exception if the entity is invalid.
145
     * @throws InvalidParameterException Throws an invalid parameter exception if a parameter is invalid.
146
     * @throws RouteNotFoundException Throws a route not found exception if the route doesn't exist.
147
     * @throws MissingMandatoryParametersException Throws a missing mandatory parameter exception if a mandatory exception is missing.
148
     */
149
    protected function renderActionButtonEdit($entity, $route) {
150
151
        if (false === DataTablesEntityHelper::isCompatible($entity)) {
152
            throw new InvalidArgumentException(sprintf("The entity must implements DataTablesEntityInterface or declare a getId() method"));
153
        }
154
155
        $title  = $this->getTranslator()->trans("label.edit", [], "WBWJQueryDataTablesBundle");
156
        $button = $this->getButtonTwigExtension()->bootstrapButtonDefaultFunction(["icon" => "fa:pen", "title" => $title, "size" => "xs"]);
157
        $url    = $this->getRouter()->generate($route, ["id" => $entity->getId()]);
158
159
        return $this->getButtonTwigExtension()->bootstrapButtonLinkFilter($button, $url);
160
    }
161
162
    /**
163
     * Render an action button "show".
164
     *
165
     * @param DataTablesEntityInterface $entity The entity.
166
     * @param string $route The route.
167
     * @return string Returns the action button "show".
168
     * @throws InvalidArgumentException Throws an invalid argument exception if the entity is invalid.
169
     * @throws InvalidParameterException Throws an invalid parameter exception if a parameter is invalid.
170
     * @throws RouteNotFoundException Throws a route not found exception if the route doesn't exist.
171
     * @throws MissingMandatoryParametersException Throws a missing mandatory parameter exception if a mandatory exception is missing.
172
     */
173
    protected function renderActionButtonShow($entity, $route) {
174
175
        if (false === DataTablesEntityHelper::isCompatible($entity)) {
176
            throw new InvalidArgumentException(sprintf("The entity must implements DataTablesEntityInterface or declare a getId() method"));
177
        }
178
179
        $title  = $this->getTranslator()->trans("label.show", [], "WBWJQueryDataTablesBundle");
180
        $button = $this->getButtonTwigExtension()->bootstrapButtonInfoFunction(["icon" => "fa:eye", "title" => $title, "size" => "xs"]);
181
        $url    = $this->getRouter()->generate($route, ["id" => $entity->getId()]);
182
183
        return $this->getButtonTwigExtension()->bootstrapButtonLinkFilter($button, $url);
184
    }
185
186
    /**
187
     * Render the DataTables buttons.
188
     *
189
     * @param DataTablesEntityInterface $entity The entity.
190
     * @param string $editRoute The edit route.
191
     * @param string $deleteRoute The delete route.
192
     * @param bool $enableDelete Enable delete ?
193
     * @return string Returns the DataTables buttons.
194
     * @throws InvalidArgumentException Throws an invalid argument exception if the entity is invalid.
195
     * @throws InvalidParameterException Throws an invalid parameter exception if a parameter is invalid.
196
     * @throws RouteNotFoundException Throws a route not found exception if the route doesn't exist.
197
     * @throws MissingMandatoryParametersException Throws a missing mandatory parameter exception if a mandatory exception is missing.
198
     * @deprecated since 3.4.0 use "WBW\Bundle\JQuery\DataTablesBundle\Provider\AbstractDataTablesProvider::renderRowButtons()" instead
199
     */
200
    protected function renderButtons($entity, $editRoute, $deleteRoute = null, $enableDelete = true) {
201
        if (null === $deleteRoute && true === $enableDelete) {
202
            $deleteRoute = "wbw_jquery_datatables_delete";
203
        }
204
        return $this->renderRowButtons($entity, $editRoute, $deleteRoute, null);
205
    }
206
207
    /**
208
     * Render a date.
209
     *
210
     * @param DateTime $date The date.
211
     * @param string $format The format.
212
     * @return string Returns the rendered date.
213
     */
214
    protected function renderDate(DateTime $date = null, $format = "Y-m-d") {
215
        return DateTimeRenderer::renderDateTime($date, $format);
216
    }
217
218
    /**
219
     * Render a date/time.
220
     *
221
     * @param DateTime $date The date/time.
222
     * @param string $format The format.
223
     * @return string Returns the rendered date/time.
224
     */
225
    protected function renderDateTime(DateTime $date = null, $format = DateTimeRenderer::DATETIME_FORMAT) {
226
        return DateTimeRenderer::renderDateTime($date, $format);
227
    }
228
229
    /**
230
     * Render a float.
231
     *
232
     * @param float $number The number.
233
     * @param int $decimals The decimals.
234
     * @param string $decPoint The decimal point.
235
     * @param string $thousandsSep The thousands separator.
236
     * @return string Returns the rendered number.
237
     */
238
    protected function renderFloat($number, $decimals = 2, $decPoint = ".", $thousandsSep = ",") {
239
        if (null === $number) {
240
            return "";
241
        }
242
        return number_format($number, $decimals, $decPoint, $thousandsSep);
243
    }
244
245
    /**
246
     * Render the DataTables row buttons.
247
     *
248
     * @param DataTablesEntityInterface $entity The entity.
249
     * @param string|null $editRoute The edit route.
250
     * @param string|null $deleteRoute The delete route.
251
     * @param string|null $showRoute The show route.
252
     * @return string Returns the DataTables row buttons.
253
     * @throws InvalidArgumentException Throws an invalid argument exception if the entity is invalid.
254
     * @throws InvalidParameterException Throws an invalid parameter exception if a parameter is invalid.
255
     * @throws RouteNotFoundException Throws a route not found exception if the route doesn't exist.
256
     * @throws MissingMandatoryParametersException Throws a missing mandatory parameter exception if a mandatory exception is missing.
257
     */
258
    protected function renderRowButtons($entity, $editRoute = null, $deleteRoute = null, $showRoute = null) {
259
260
        $anchors = [];
261
262
        if (null !== $editRoute) {
263
            $anchors[] = $this->renderActionButtonEdit($entity, $editRoute);
264
        }
265
266
        if (null !== $deleteRoute) {
267
            $anchors[] = $this->renderActionButtonDelete($entity, $deleteRoute);
268
        }
269
270
        if (null !== $showRoute) {
271
            $anchors[] = $this->renderActionButtonShow($entity, $showRoute);
272
        }
273
274
        return implode(" ", $anchors);
275
    }
276
277
    /**
278
     * Wrap a content.
279
     *
280
     * @param string|null $prefix The prefix
281
     * @param string $content The content.
282
     * @param string|null $suffix The suffix.
283
     * @return string Returns the wrapped content.
284
     */
285
    protected function wrapContent($prefix, $content, $suffix) {
286
287
        $output = [];
288
289
        if (null !== $prefix) {
290
            $output[] = $prefix;
291
        }
292
        $output[] = $content;
293
        if (null !== $suffix) {
294
            $output[] = $suffix;
295
        }
296
297
        return implode("", $output);
298
    }
299
}
300