1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the jquery-datatables-bundle package. |
5
|
|
|
* |
6
|
|
|
* (c) 2018 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\Controller; |
13
|
|
|
|
14
|
|
|
use DateTime; |
15
|
|
|
use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException; |
16
|
|
|
use Doctrine\ORM\EntityNotFoundException; |
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
18
|
|
|
use Symfony\Component\HttpFoundation\Response; |
19
|
|
|
use Symfony\Component\HttpFoundation\StreamedResponse; |
20
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\API\DataTablesResponse; |
21
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Exception\BadDataTablesRepositoryException; |
22
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Exception\UnregisteredDataTablesProviderException; |
23
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Helper\DataTablesWrapperHelper; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* DataTables controller. |
27
|
|
|
* |
28
|
|
|
* @author webeweb <https://github.com/webeweb/> |
29
|
|
|
* @package WBW\Bundle\JQuery\DataTablesBundle\Controller |
30
|
|
|
*/ |
31
|
|
|
class DataTablesController extends AbstractDataTablesController { |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Delete an existing entity. |
35
|
|
|
* |
36
|
|
|
* @param Request $request The request. |
37
|
|
|
* @param string $name The provider name. |
38
|
|
|
* @param string $id The entity id. |
39
|
|
|
* @throws UnregisteredDataTablesProviderException Throws an unregistered DataTables provider exception. |
40
|
|
|
* @throws BadDataTablesRepositoryException Throws a bad DataTables repository exception. |
41
|
|
|
*/ |
42
|
|
|
public function deleteAction(Request $request, $name, $id) { |
43
|
|
|
|
44
|
|
|
// Get the provider. |
45
|
|
|
$dtProvider = $this->getDataTablesProvider($name); |
46
|
|
|
|
47
|
|
|
// Initialize the output. |
48
|
|
|
$output = [ |
49
|
|
|
"status" => null, |
50
|
|
|
"notify" => null, |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
try { |
54
|
|
|
|
55
|
|
|
// Get the entity. |
56
|
|
|
$entity = $this->getDataTablesEntityById($dtProvider, $id); |
57
|
|
|
|
58
|
|
|
// Get the entities manager and delete the entity. |
59
|
|
|
$em = $this->getDoctrine()->getManager(); |
60
|
|
|
$em->remove($entity); |
61
|
|
|
$em->flush(); |
62
|
|
|
|
63
|
|
|
// Set the output. |
64
|
|
|
$output["status"] = 200; |
65
|
|
|
$output["notify"] = $this->getNotification("DataTablesController.deleteAction.success"); |
66
|
|
|
} catch (EntityNotFoundException $ex) { |
67
|
|
|
|
68
|
|
|
// Log a debug trace. |
69
|
|
|
$this->getLogger()->debug($ex->getMessage()); |
70
|
|
|
|
71
|
|
|
// Set the output. |
72
|
|
|
$output["status"] = 404; |
73
|
|
|
$output["notify"] = $this->getNotification("DataTablesController.deleteAction.danger"); |
74
|
|
|
} catch (ForeignKeyConstraintViolationException $ex) { |
75
|
|
|
|
76
|
|
|
// Log a debug trace. |
77
|
|
|
$this->getLogger()->debug(sprintf("%s:%s %s", $ex->getErrorCode(), $ex->getSQLState(), $ex->getMessage())); |
78
|
|
|
|
79
|
|
|
// Set the output. |
80
|
|
|
$output["status"] = 500; |
81
|
|
|
$output["notify"] = $this->getNotification("DataTablesController.deleteAction.warning"); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// Return the response. |
85
|
|
|
return $this->buildDataTablesResponse($request, $name, $output); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Export all entities. |
90
|
|
|
* |
91
|
|
|
* @param string $name The provider name. |
92
|
|
|
* @return Response Returns the response. |
93
|
|
|
* @throws UnregisteredDataTablesProviderException Throws an unregistered DataTables provider exception. |
94
|
|
|
* @throws BadDataTablesRepositoryException Throws a bad DataTables repository exception. |
95
|
|
|
*/ |
96
|
|
|
public function exportAction($name) { |
97
|
|
|
|
98
|
|
|
// Get the provider. |
99
|
|
|
$dtProvider = $this->getDataTablesCSVExporter($name); |
100
|
|
|
|
101
|
|
|
// Get the entities repository. |
102
|
|
|
$repository = $this->getDataTablesRepository($dtProvider); |
103
|
|
|
|
104
|
|
|
// Initialize the filename. |
105
|
|
|
$filename = (new DateTime())->format("Y.m.d-H.i.s") . "-" . $dtProvider->getName() . ".csv"; |
106
|
|
|
|
107
|
|
|
// Initialize the response. |
108
|
|
|
$response = new StreamedResponse(); |
109
|
|
|
$response->headers->set("Content-Disposition", "attachment; filename=\"" . $filename . "\""); |
110
|
|
|
$response->headers->set("Content-Type", "text/csv; charset=utf-8"); |
111
|
|
|
$response->setCallback(function() use($dtProvider, $repository) { |
112
|
|
|
$this->exportCallback($dtProvider, $repository); |
|
|
|
|
113
|
|
|
}); |
114
|
|
|
$response->setStatusCode(200); |
115
|
|
|
|
116
|
|
|
// Return the response. |
117
|
|
|
return $response; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Get an existing entity. |
122
|
|
|
* |
123
|
|
|
* @param Request $request The request. |
124
|
|
|
* @param string $name The provider name. |
125
|
|
|
* @param string $id The entity id. |
126
|
|
|
* @return Response Returns the response. |
127
|
|
|
*/ |
128
|
|
|
public function getAction(Request $request, $name, $id) { |
|
|
|
|
129
|
|
|
|
130
|
|
|
// Get the provider. |
131
|
|
|
$dtProvider = $this->getDataTablesProvider($name); |
132
|
|
|
|
133
|
|
|
// Initialize the entity. |
134
|
|
|
$entity = []; |
135
|
|
|
|
136
|
|
|
try { |
137
|
|
|
|
138
|
|
|
// Get the entity. |
139
|
|
|
$entity = $this->getDataTablesEntityById($dtProvider, $id); |
140
|
|
|
} catch (EntityNotFoundException $ex) { |
141
|
|
|
|
142
|
|
|
// Log a debug trace. |
143
|
|
|
$this->getLogger()->debug($ex->getMessage()); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
// Get the serializer. |
147
|
|
|
$serializer = $this->getDataTablesSerializer(); |
148
|
|
|
|
149
|
|
|
// Return the response. |
150
|
|
|
return new Response($serializer->serialize($entity, "json")); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Lists all entities. |
155
|
|
|
* |
156
|
|
|
* @param Request $request The request. |
157
|
|
|
* @param string $name The provider name. |
158
|
|
|
* @return Response Returns the response. |
159
|
|
|
* @throws UnregisteredDataTablesProviderException Throws an unregistered DataTables provider exception. |
160
|
|
|
* @throws BadDataTablesRepositoryException Throws a bad DataTables repository exception. |
161
|
|
|
*/ |
162
|
|
|
public function indexAction(Request $request, $name) { |
163
|
|
|
|
164
|
|
|
// Check if the request is an XML HTTP request. |
165
|
|
|
if (false === $request->isXmlHttpRequest()) { |
166
|
|
|
return $this->renderAction($name); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
// Get the provider. |
170
|
|
|
$dtProvider = $this->getDataTablesProvider($name); |
171
|
|
|
|
172
|
|
|
// Get the wrapper. |
173
|
|
|
$dtWrapper = $this->getDataTablesWrapper($dtProvider); |
174
|
|
|
|
175
|
|
|
// Parse the request. |
176
|
|
|
$dtWrapper->parse($request); |
177
|
|
|
|
178
|
|
|
// Get the entities repository. |
179
|
|
|
$repository = $this->getDataTablesRepository($dtProvider); |
180
|
|
|
|
181
|
|
|
// |
182
|
|
|
$filtered = $repository->dataTablesCountFiltered($dtWrapper); |
183
|
|
|
$total = $repository->dataTablesCountTotal($dtWrapper); |
184
|
|
|
$entities = $repository->dataTablesFindAll($dtWrapper); |
185
|
|
|
|
186
|
|
|
// Set the response. |
187
|
|
|
$dtWrapper->getResponse()->setRecordsFiltered($filtered); |
188
|
|
|
$dtWrapper->getResponse()->setRecordsTotal($total); |
189
|
|
|
|
190
|
|
|
// Handle each entity. |
191
|
|
|
foreach ($entities as $entity) { |
192
|
|
|
|
193
|
|
|
// Count the rows. |
194
|
|
|
$rows = $dtWrapper->getResponse()->countRows(); |
195
|
|
|
|
196
|
|
|
// Create a row. |
197
|
|
|
$dtWrapper->getResponse()->addRow(); |
198
|
|
|
|
199
|
|
|
// Render each optional parameter. |
200
|
|
|
foreach (DataTablesResponse::dtRow() as $dtRow) { |
201
|
|
|
$dtWrapper->getResponse()->setRow($dtRow, $dtProvider->renderRow($dtRow, $entity, $rows)); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
// Render each column. |
205
|
|
|
foreach ($dtWrapper->getColumns() as $dtColumn) { |
206
|
|
|
$dtWrapper->getResponse()->setRow($dtColumn->getData(), $dtProvider->renderColumn($dtColumn, $entity)); |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
// Return the response. |
211
|
|
|
return new Response(json_encode($dtWrapper->getResponse())); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Options of a DataTables. |
216
|
|
|
* |
217
|
|
|
* @param string $name The provider name. |
218
|
|
|
* @return Response Returns a response. |
219
|
|
|
* @throws UnregisteredDataTablesProviderException Throws an unregistered DataTables provider exception. |
220
|
|
|
*/ |
221
|
|
|
public function optionsAction($name) { |
222
|
|
|
|
223
|
|
|
// Get the provider. |
224
|
|
|
$dtProvider = $this->getDataTablesProvider($name); |
225
|
|
|
|
226
|
|
|
// Get the wrapper. |
227
|
|
|
$dtWrapper = $this->getDataTablesWrapper($dtProvider); |
228
|
|
|
|
229
|
|
|
// Get the options. |
230
|
|
|
$dtOptions = DataTablesWrapperHelper::getOptions($dtWrapper); |
231
|
|
|
|
232
|
|
|
// Return the response. |
233
|
|
|
return new Response(json_encode($dtOptions)); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Render a DataTables. |
238
|
|
|
* |
239
|
|
|
* @param string $name The provider name. |
240
|
|
|
* @return Response Returns the response. |
241
|
|
|
* @throws UnregisteredDataTablesProviderException Throws an unregistered DataTables provider exception. |
242
|
|
|
*/ |
243
|
|
|
public function renderAction($name) { |
244
|
|
|
|
245
|
|
|
// Get the provider. |
246
|
|
|
$dtProvider = $this->getDataTablesProvider($name); |
247
|
|
|
|
248
|
|
|
// Get the wrapper. |
249
|
|
|
$dtWrapper = $this->getDataTablesWrapper($dtProvider); |
250
|
|
|
|
251
|
|
|
// Get and check the provider view. |
252
|
|
|
$dtView = $dtProvider->getView(); |
253
|
|
|
if (null === $dtProvider->getView()) { |
254
|
|
|
$dtView = "@JQueryDataTables/DataTables/index.html.twig"; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
// Return the response. |
258
|
|
|
return $this->render($dtView, [ |
259
|
|
|
"dtWrapper" => $dtWrapper, |
260
|
|
|
]); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
} |
264
|
|
|
|
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: