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 implements DataTablesProviderInterface { |
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 titles. |
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()]); |
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 implode(" ", $links); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Render a date. |
113
|
|
|
* |
114
|
|
|
* @param DateTime $date The date. |
115
|
|
|
* @param string $format The format. |
116
|
|
|
* @return string Returns the rendered date. |
117
|
|
|
*/ |
118
|
|
|
protected function renderDate(DateTime $date = null, $format = "Y-m-d") { |
119
|
|
|
return DateTimeRenderer::renderDateTime($date, $format); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Render a date/time. |
124
|
|
|
* |
125
|
|
|
* @param DateTime $date The date/time. |
126
|
|
|
* @param string $format The format. |
127
|
|
|
* @return string Returns the rendered date/time. |
128
|
|
|
*/ |
129
|
|
|
protected function renderDateTime(DateTime $date = null, $format = DateTimeRenderer::DATETIME_FORMAT) { |
130
|
|
|
return DateTimeRenderer::renderDateTime($date, $format); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Render a float. |
135
|
|
|
* |
136
|
|
|
* @param float $number The number. |
137
|
|
|
* @param int $decimals The decimals. |
138
|
|
|
* @param string $decPoint The decimal point. |
139
|
|
|
* @param string $thousandsSep The thousands separator. |
140
|
|
|
* @return string Returns the rendered number. |
141
|
|
|
*/ |
142
|
|
|
protected function renderFloat($number, $decimals = 2, $decPoint = ".", $thousandsSep = ",") { |
143
|
|
|
if (null === $number) { |
144
|
|
|
return ""; |
145
|
|
|
} |
146
|
|
|
return number_format($number, $decimals, $decPoint, $thousandsSep); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Wrap a content. |
151
|
|
|
* |
152
|
|
|
* @param string|null $prefix The prefix |
153
|
|
|
* @param string $content The content. |
154
|
|
|
* @param string|null $suffix The suffix. |
155
|
|
|
* @return string Returns the wrapped content. |
156
|
|
|
*/ |
157
|
|
|
protected function wrapContent($prefix, $content, $suffix) { |
158
|
|
|
|
159
|
|
|
$output = []; |
160
|
|
|
|
161
|
|
|
if (null !== $prefix) { |
162
|
|
|
$output[] = $prefix; |
163
|
|
|
} |
164
|
|
|
$output[] = $content; |
165
|
|
|
if (null !== $suffix) { |
166
|
|
|
$output[] = $suffix; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return implode("", $output); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|