Completed
Push — master ( 15d87d...7defa9 )
by WEBEWEB
14:36
created

AbstractDataTablesProvider::getOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 Symfony\Component\Routing\RouterInterface;
16
use Symfony\Component\Translation\TranslatorInterface;
17
use WBW\Bundle\BootstrapBundle\Twig\Extension\CSS\ButtonTwigExtension;
18
use WBW\Bundle\BootstrapBundle\Twig\Extension\CSS\ButtonTwigExtensionTrait;
19
use WBW\Bundle\CoreBundle\Renderer\DateTimeRenderer;
20
use WBW\Bundle\CoreBundle\Service\RouterTrait;
21
use WBW\Bundle\CoreBundle\Service\TranslatorTrait;
22
use WBW\Bundle\JQuery\DataTablesBundle\Factory\DataTablesFactory;
23
24
/**
25
 * Abstract DataTables provider.
26
 *
27
 * @author webeweb <https://github.com/webeweb/>
28
 * @package WBW\Bundle\JQuery\DataTablesBundle\Provider
29
 */
30
abstract class AbstractDataTablesProvider {
31
32
    use ButtonTwigExtensionTrait;
33
    use RouterTrait;
34
    use TranslatorTrait;
35
36
    /**
37
     * Constructor.
38
     *
39
     * @param RouterInterface $router The router.
40
     * @param TranslatorInterface $translator The translator.
41
     * @param ButtonTwigExtension $buttonTwigExtension The button Twig extension.
42
     */
43
    public function __construct(RouterInterface $router, TranslatorInterface $translator, ButtonTwigExtension $buttonTwigExtension) {
44
        $this->setButtonTwigExtension($buttonTwigExtension);
45
        $this->setRouter($router);
46
        $this->setTranslator($translator);
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function getMethod() {
53
        return null;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function getOptions() {
60
61
        // Initialize the options.
62
        $dtOptions = DataTablesFactory::newOptions();
63
        $dtOptions->addOption("responsive", true);
64
        $dtOptions->addOption("searchDelay", 1000);
65
66
        // Return the options.
67
        return $dtOptions;
68
    }
69
70
    /**
71
     * Render the DataTables buttons.
72
     *
73
     * @param mixed $entity The entity.
74
     * @param string $editRoute The edit route.
75
     * @param string $deleteRoute The delete route.
76
     * @param bool $enableDelete Enable delete ?
77
     * @return string Returns the DataTables buttons.
78
     */
79
    protected function renderButtons($entity, $editRoute, $deleteRoute = null, $enableDelete = true) {
80
81
        // Initialize the translations.
82
        $titles   = [];
83
        $titles[] = $this->getTranslator()->trans("label.edit", [], "CoreBundle");
84
        $titles[] = $this->getTranslator()->trans("label.delete", [], "CoreBundle");
85
86
        // Initialize the actions.
87
        $actions   = [];
88
        $actions[] = $this->getButtonTwigExtension()->bootstrapButtonDefaultFunction(["icon" => "pencil", "title" => $titles[0], "size" => "xs"]);
89
        $actions[] = $this->getButtonTwigExtension()->bootstrapButtonDangerFunction(["icon" => "trash", "title" => $titles[1], "size" => "xs"]);
90
91
        // Initialize the routes.
92
        $routes   = [];
93
        $routes[] = $this->getRouter()->generate($editRoute, ["id" => $entity->getId()]);
94
        $routes[] = $this->getRouter()->generate("jquery_datatables_delete", ["name" => $this->getName(), "id" => $entity->getId()]);
0 ignored issues
show
Bug introduced by
The method getName() does not seem to exist on object<WBW\Bundle\JQuery...ractDataTablesProvider>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
95
96
        // Check the delete route and use it if provided.
97
        if (null !== $deleteRoute) {
98
            $routes[1] = $this->getRouter()->generate($deleteRoute, ["id" => $entity->getId()]);
99
        }
100
101
        // Initialize the links.
102
        $links   = [];
103
        $links[] = $this->getButtonTwigExtension()->bootstrapButtonLinkFilter($actions[0], $routes[0]);
104
        if (true === $enableDelete) {
105
            $links[] = $this->getButtonTwigExtension()->bootstrapButtonLinkFilter($actions[1], $routes[1]);
106
        }
107
108
        // Return the row buttons.
109
        return implode(" ", $links);
110
    }
111
112
    /**
113
     * Render a date.
114
     *
115
     * @param DateTime $date The date.
116
     * @param string $format The format.
117
     * @return string Returns the rendered date.
118
     */
119
    protected function renderDate(DateTime $date = null, $format = "Y-m-d") {
120
        return DateTimeRenderer::renderDateTime($date, $format);
121
    }
122
123
    /**
124
     * Render a date/time.
125
     *
126
     * @param DateTime $date The date/time.
127
     * @param string $format The format.
128
     * @return string Returns the rendered date/time.
129
     */
130
    protected function renderDateTime(DateTime $date = null, $format = DateTimeRenderer::DATETIME_FORMAT) {
131
        return DateTimeRenderer::renderDateTime($date, $format);
132
    }
133
134
    /**
135
     * Render a float.
136
     *
137
     * @param float $number The number.
138
     * @param int $decimals The decimals.
139
     * @param string $decPoint The decimal point.
140
     * @param string $thousandsSep The thousands separator.
141
     * @return string Returns the rendered number.
142
     */
143
    protected function renderFloat($number, $decimals = 2, $decPoint = ".", $thousandsSep = ",") {
144
        if (null === $number) {
145
            return "";
146
        }
147
        return number_format($number, $decimals, $decPoint, $thousandsSep);
148
    }
149
}
150