Completed
Push — master ( d1902c...e3e6b5 )
by WEBEWEB
01:26
created

AbstractDataTablesProvider::renderButtons()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 4
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 the DataTables buttons.
90
     *
91
     * @param DataTablesEntityInterface $entity The entity.
92
     * @param string $editRoute The edit route.
93
     * @param string $deleteRoute The delete route.
94
     * @param bool $enableDelete Enable delete ?
95
     * @return string Returns the DataTables buttons.
96
     * @throws InvalidArgumentException Throws an invalid argument exception if the entity is invalid.
97
     * @throws InvalidParameterException Throws an invalid parameter exception if a parameter is invalid.
98
     * @throws RouteNotFoundException Throws a route not found exception if the route doesn't exist.
99
     * @throws MissingMandatoryParametersException Throws a missing mandatory parameter exception if a mandatory exception is missing.
100
     * @deprecated since 3.4.0 use "WBW\Bundle\JQuery\DataTablesBundle\Provider\AbstractDataTablesProvider::renderRowButtons()" instead
101
     */
102
    protected function renderButtons($entity, $editRoute, $deleteRoute = null, $enableDelete = true) {
103
        if (null === $deleteRoute && true === $enableDelete) {
104
            $deleteRoute = "wbw_jquery_datatables_delete";
105
        }
106
        return $this->renderRowButtons($entity, $editRoute, $deleteRoute, null);
107
    }
108
109
    /**
110
     * Render a date.
111
     *
112
     * @param DateTime $date The date.
113
     * @param string $format The format.
114
     * @return string Returns the rendered date.
115
     */
116
    protected function renderDate(DateTime $date = null, $format = "Y-m-d") {
117
        return DateTimeRenderer::renderDateTime($date, $format);
118
    }
119
120
    /**
121
     * Render a date/time.
122
     *
123
     * @param DateTime $date The date/time.
124
     * @param string $format The format.
125
     * @return string Returns the rendered date/time.
126
     */
127
    protected function renderDateTime(DateTime $date = null, $format = DateTimeRenderer::DATETIME_FORMAT) {
128
        return DateTimeRenderer::renderDateTime($date, $format);
129
    }
130
131
    /**
132
     * Render a float.
133
     *
134
     * @param float $number The number.
135
     * @param int $decimals The decimals.
136
     * @param string $decPoint The decimal point.
137
     * @param string $thousandsSep The thousands separator.
138
     * @return string Returns the rendered number.
139
     */
140
    protected function renderFloat($number, $decimals = 2, $decPoint = ".", $thousandsSep = ",") {
141
        if (null === $number) {
142
            return "";
143
        }
144
        return number_format($number, $decimals, $decPoint, $thousandsSep);
145
    }
146
147
    /**
148
     * Render the DataTables row buttons.
149
     *
150
     * @param DataTablesEntityInterface $entity The entity.
151
     * @param string|null $editRoute The edit route.
152
     * @param string|null $deleteRoute The delete route.
153
     * @param string|null $showRoute The show route.
154
     * @return string Returns the DataTables row buttons.
155
     * @throws InvalidArgumentException Throws an invalid argument exception if the entity is invalid.
156
     * @throws InvalidParameterException Throws an invalid parameter exception if a parameter is invalid.
157
     * @throws RouteNotFoundException Throws a route not found exception if the route doesn't exist.
158
     * @throws MissingMandatoryParametersException Throws a missing mandatory parameter exception if a mandatory exception is missing.
159
     */
160
    protected function renderRowButtons($entity, $editRoute = null, $deleteRoute = null, $showRoute = null) {
161
162
        if (false === DataTablesEntityHelper::isCompatible($entity)) {
163
            throw new InvalidArgumentException(sprintf("The entity must implements DataTablesEntityInterface or declare a getId() method"));
164
        }
165
166
        $output = [];
167
168
        $titles   = [];
169
        $titles[] = $this->getTranslator()->trans("label.edit", [], "WBWJQueryDataTablesBundle");
170
        $titles[] = $this->getTranslator()->trans("label.delete", [], "WBWJQueryDataTablesBundle");
171
        $titles[] = $this->getTranslator()->trans("label.show", [], "WBWJQueryDataTablesBundle");
172
173
        $buttons   = [];
174
        $buttons[] = $this->getButtonTwigExtension()->bootstrapButtonDefaultFunction(["icon" => "fa:pen", "title" => $titles[0], "size" => "xs"]);
175
        $buttons[] = $this->getButtonTwigExtension()->bootstrapButtonDangerFunction(["icon" => "fa:trash", "title" => $titles[1], "size" => "xs"]);
176
        $buttons[] = $this->getButtonTwigExtension()->bootstrapButtonInfoFunction(["icon" => "fa:eye", "title" => $titles[2], "size" => "xs"]);
177
178
        $urls = [];
179
180
        if (null !== $editRoute) {
181
182
            $urls[]   = $this->getRouter()->generate($editRoute, ["id" => $entity->getId()]);
183
            $output[] = $this->getButtonTwigExtension()->bootstrapButtonLinkFilter($buttons[0], $urls[0]);
184
        }
185
186
        if (null !== $deleteRoute) {
187
188
            $args = "wbw_jquery_datatables_delete" === $deleteRoute ? ["name" => $this->getName()] : [];
189
190
            $urls[]   = $this->getRouter()->generate($deleteRoute, array_merge($args, ["id" => $entity->getId()]));
191
            $output[] = $this->getButtonTwigExtension()->bootstrapButtonLinkFilter($buttons[1], $urls[1]);
192
        }
193
194
        if (null !== $showRoute) {
195
196
            $urls[]   = $this->getRouter()->generate($showRoute, ["id" => $entity->getId()]);
197
            $output[] = $this->getButtonTwigExtension()->bootstrapButtonLinkFilter($buttons[2], $urls[2]);
198
        }
199
200
        return implode(" ", $output);
201
    }
202
203
    /**
204
     * Wrap a content.
205
     *
206
     * @param string|null $prefix The prefix
207
     * @param string $content The content.
208
     * @param string|null $suffix The suffix.
209
     * @return string Returns the wrapped content.
210
     */
211
    protected function wrapContent($prefix, $content, $suffix) {
212
213
        $output = [];
214
215
        if (null !== $prefix) {
216
            $output[] = $prefix;
217
        }
218
        $output[] = $content;
219
        if (null !== $suffix) {
220
            $output[] = $suffix;
221
        }
222
223
        return implode("", $output);
224
    }
225
}
226