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\ORM\EntityNotFoundException; |
16
|
|
|
use Exception; |
17
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
18
|
|
|
use Symfony\Component\HttpFoundation\Request; |
19
|
|
|
use Symfony\Component\HttpFoundation\Response; |
20
|
|
|
use Symfony\Component\HttpFoundation\StreamedResponse; |
21
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\API\DataTablesEnumerator; |
22
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Event\DataTablesEvents; |
23
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Exception\BadDataTablesCSVExporterException; |
24
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Exception\BadDataTablesEditorException; |
25
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Exception\BadDataTablesRepositoryException; |
26
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Exception\UnregisteredDataTablesProviderException; |
27
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Factory\DataTablesFactory; |
28
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Helper\DataTablesWrapperHelper; |
29
|
|
|
use WBW\Library\Core\Network\HTTP\HTTPInterface; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* DataTables controller. |
33
|
|
|
* |
34
|
|
|
* @author webeweb <https://github.com/webeweb/> |
35
|
|
|
* @package WBW\Bundle\JQuery\DataTablesBundle\Controller |
36
|
|
|
*/ |
37
|
|
|
class DataTablesController extends AbstractDataTablesController { |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Delete an existing entity. |
41
|
|
|
* |
42
|
|
|
* @param Request $request The request. |
43
|
|
|
* @param string $name The provider name. |
44
|
|
|
* @param string $id The entity id. |
45
|
|
|
* @throws UnregisteredDataTablesProviderException Throws an unregistered provider exception. |
46
|
|
|
* @throws BadDataTablesRepositoryException Throws a bad repository exception. |
47
|
|
|
*/ |
48
|
|
|
public function deleteAction(Request $request, $name, $id) { |
49
|
|
|
|
50
|
|
|
// Get the provider. |
51
|
|
|
$dtProvider = $this->getDataTablesProvider($name); |
52
|
|
|
|
53
|
|
|
try { |
54
|
|
|
|
55
|
|
|
// Get the entity. |
56
|
|
|
$entity = $this->getDataTablesEntityById($dtProvider, $id); |
57
|
|
|
|
58
|
|
|
// Dispatch an event. |
59
|
|
|
$this->dispatchDataTablesEvent(DataTablesEvents::DATATABLES_PRE_DELETE, [$entity]); |
60
|
|
|
|
61
|
|
|
// Get the entities manager and delete the entity. |
62
|
|
|
$em = $this->getDoctrine()->getManager(); |
63
|
|
|
$em->remove($entity); |
64
|
|
|
$em->flush(); |
65
|
|
|
|
66
|
|
|
// Dispatch an event. |
67
|
|
|
$this->dispatchDataTablesEvent(DataTablesEvents::DATATABLES_POST_DELETE, [$entity]); |
68
|
|
|
|
69
|
|
|
// Set the output. |
70
|
|
|
$output = $this->prepareActionResponse(200, "DataTablesController.deleteAction.success"); |
71
|
|
|
} catch (EntityNotFoundException $ex) { |
72
|
|
|
|
73
|
|
|
// Log a debug trace. |
74
|
|
|
$this->getLogger()->debug($ex->getMessage()); |
75
|
|
|
|
76
|
|
|
// Set the output. |
77
|
|
|
$output = $this->prepareActionResponse(404, "DataTablesController.deleteAction.danger"); |
78
|
|
|
} catch (Exception $ex) { |
79
|
|
|
|
80
|
|
|
// Log a debug trace. |
81
|
|
|
$this->getLogger()->debug(sprintf("%s", $ex->getMessage())); |
82
|
|
|
|
83
|
|
|
// Set the output. |
84
|
|
|
$output = $this->prepareActionResponse(500, "DataTablesController.deleteAction.warning"); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
// Return the response. |
88
|
|
|
return $this->buildDataTablesResponse($request, $name, $output); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Edit an existing entity. |
93
|
|
|
* |
94
|
|
|
* @param Request $request The request. |
95
|
|
|
* @param string $name The provider name |
96
|
|
|
* @param string $id The entity id. |
97
|
|
|
* @param string $data The data. |
98
|
|
|
* @param mixed $value The value |
99
|
|
|
* @return Response Returns the response. |
100
|
|
|
* @throws UnregisteredDataTablesProviderException Throws an unregistered provider exception. |
101
|
|
|
* @throws BadDataTablesEditorException Throws a bad editor exception. |
102
|
|
|
*/ |
103
|
|
|
public function editAction(Request $request, $name, $id, $data, $value) { |
104
|
|
|
|
105
|
|
|
// Get the provider. |
106
|
|
|
$dtProvider = $this->getDataTablesProvider($name); |
107
|
|
|
|
108
|
|
|
// Get the editor. |
109
|
|
|
$dtEditor = $this->getDataTablesEditor($dtProvider); |
110
|
|
|
|
111
|
|
|
// Get the column. |
112
|
|
|
$dtColumn = $this->getDataTablesColumn($dtProvider, $data); |
113
|
|
|
|
114
|
|
|
try { |
115
|
|
|
|
116
|
|
|
// Get the entity. |
117
|
|
|
$entity = $this->getDataTablesEntityById($dtProvider, $id); |
118
|
|
|
|
119
|
|
|
// Set the value. |
120
|
|
|
if (true === $request->isMethod(HTTPInterface::HTTP_METHOD_POST)) { |
121
|
|
|
$value = $request->request->get("value"); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
// Dispatch an event. |
125
|
|
|
$this->dispatchDataTablesEvent(DataTablesEvents::DATATABLES_PRE_EDIT, [$entity]); |
126
|
|
|
|
127
|
|
|
// Set the entity. |
128
|
|
|
$dtEditor->editColumn($dtColumn, $entity, $value); |
129
|
|
|
|
130
|
|
|
// Get the entities manager and delete the entity. |
131
|
|
|
$em = $this->getDoctrine()->getManager(); |
132
|
|
|
$em->persist($entity); |
133
|
|
|
$em->flush(); |
134
|
|
|
|
135
|
|
|
// Dispatch an event. |
136
|
|
|
$this->dispatchDataTablesEvent(DataTablesEvents::DATATABLES_POST_EDIT, [$entity]); |
137
|
|
|
|
138
|
|
|
// Set the output. |
139
|
|
|
$output = $this->prepareActionResponse(200, "DataTablesController.editAction.success"); |
140
|
|
|
} catch (EntityNotFoundException $ex) { |
141
|
|
|
|
142
|
|
|
// Log a debug trace. |
143
|
|
|
$this->getLogger()->debug($ex->getMessage()); |
144
|
|
|
|
145
|
|
|
// Set the output. |
146
|
|
|
$output = $this->prepareActionResponse(404, "DataTablesController.editAction.danger"); |
147
|
|
|
} catch (Exception $ex) { |
148
|
|
|
|
149
|
|
|
// Log a debug trace. |
150
|
|
|
$this->getLogger()->debug(sprintf("%s", $ex->getMessage())); |
151
|
|
|
|
152
|
|
|
// Set the output. |
153
|
|
|
$output = $this->prepareActionResponse(500, "DataTablesController.editAction.warning"); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
// Return the response. |
157
|
|
|
return new JsonResponse($output); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Export all entities. |
162
|
|
|
* |
163
|
|
|
* @param string $name The provider name. |
164
|
|
|
* @return Response Returns the response. |
165
|
|
|
* @throws UnregisteredDataTablesProviderException Throws an unregistered provider exception. |
166
|
|
|
* @throws BadDataTablesCSVExporterException Throws a bad CSV exporter exception. |
167
|
|
|
* @throws BadDataTablesRepositoryException Throws a bad repository exception. |
168
|
|
|
*/ |
169
|
|
|
public function exportAction($name) { |
170
|
|
|
|
171
|
|
|
// Get the provider. |
172
|
|
|
$dtProvider = $this->getDataTablesProvider($name); |
173
|
|
|
|
174
|
|
|
// Get the exporter. |
175
|
|
|
$dtExporter = $this->getDataTablesCSVExporter($dtProvider); |
176
|
|
|
|
177
|
|
|
// Get the entity repository. |
178
|
|
|
$repository = $this->getDataTablesRepository($dtProvider); |
179
|
|
|
|
180
|
|
|
// Initialize the filename. |
181
|
|
|
$filename = (new DateTime())->format("Y.m.d-H.i.s") . "-" . $dtProvider->getName() . ".csv"; |
182
|
|
|
|
183
|
|
|
// Initialize the response. |
184
|
|
|
$response = new StreamedResponse(); |
185
|
|
|
$response->headers->set("Content-Disposition", "attachment; filename=\"" . $filename . "\""); |
186
|
|
|
$response->headers->set("Content-Type", "text/csv; charset=utf-8"); |
187
|
|
|
$response->setCallback(function() use($dtProvider, $repository, $dtExporter) { |
188
|
|
|
$this->exportCallback($dtProvider, $repository, $dtExporter); |
189
|
|
|
}); |
190
|
|
|
$response->setStatusCode(200); |
191
|
|
|
|
192
|
|
|
// Return the response. |
193
|
|
|
return $response; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Lists all entities. |
198
|
|
|
* |
199
|
|
|
* @param Request $request The request. |
200
|
|
|
* @param string $name The provider name. |
201
|
|
|
* @return Response Returns the response. |
202
|
|
|
* @throws UnregisteredDataTablesProviderException Throws an unregistered provider exception. |
203
|
|
|
* @throws BadDataTablesRepositoryException Throws a bad repository exception. |
204
|
|
|
*/ |
205
|
|
|
public function indexAction(Request $request, $name) { |
206
|
|
|
|
207
|
|
|
// Check if the request is an XML HTTP request. |
208
|
|
|
if (false === $request->isXmlHttpRequest()) { |
209
|
|
|
return $this->renderAction($name); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
// Get the provider. |
213
|
|
|
$dtProvider = $this->getDataTablesProvider($name); |
214
|
|
|
|
215
|
|
|
// Get the wrapper. |
216
|
|
|
$dtWrapper = $this->getDataTablesWrapper($dtProvider); |
217
|
|
|
|
218
|
|
|
// Parse the request. |
219
|
|
|
DataTablesFactory::parseWrapper($dtWrapper, $request); |
220
|
|
|
|
221
|
|
|
// Get the entities repository. |
222
|
|
|
$repository = $this->getDataTablesRepository($dtProvider); |
223
|
|
|
|
224
|
|
|
// |
225
|
|
|
$filtered = $repository->dataTablesCountFiltered($dtWrapper); |
226
|
|
|
$total = $repository->dataTablesCountTotal($dtWrapper); |
227
|
|
|
$entities = $repository->dataTablesFindAll($dtWrapper); |
228
|
|
|
|
229
|
|
|
// Dispatch an event. |
230
|
|
|
$this->dispatchDataTablesEvent(DataTablesEvents::DATATABLES_PRE_INDEX, $entities); |
231
|
|
|
|
232
|
|
|
// Set the response. |
233
|
|
|
$dtWrapper->getResponse()->setRecordsFiltered($filtered); |
234
|
|
|
$dtWrapper->getResponse()->setRecordsTotal($total); |
235
|
|
|
|
236
|
|
|
// Handle each entity. |
237
|
|
|
foreach ($entities as $entity) { |
238
|
|
|
|
239
|
|
|
// Count the rows. |
240
|
|
|
$rows = $dtWrapper->getResponse()->countRows(); |
241
|
|
|
|
242
|
|
|
// Create a row. |
243
|
|
|
$dtWrapper->getResponse()->addRow(); |
244
|
|
|
|
245
|
|
|
// Render each row. |
246
|
|
|
foreach (DataTablesEnumerator::enumRows() as $dtRow) { |
247
|
|
|
$dtWrapper->getResponse()->setRow($dtRow, $dtProvider->renderRow($dtRow, $entity, $rows)); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
// Render each column. |
251
|
|
|
foreach ($dtWrapper->getColumns() as $dtColumn) { |
252
|
|
|
$dtWrapper->getResponse()->setRow($dtColumn->getData(), $dtProvider->renderColumn($dtColumn, $entity)); |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
// Dispatch an event. |
257
|
|
|
$this->dispatchDataTablesEvent(DataTablesEvents::DATATABLES_POST_INDEX, $entities); |
258
|
|
|
|
259
|
|
|
// Return the response. |
260
|
|
|
return new JsonResponse($dtWrapper->getResponse()); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Options of a DataTables. |
265
|
|
|
* |
266
|
|
|
* @param string $name The provider name. |
267
|
|
|
* @return Response Returns a response. |
268
|
|
|
* @throws UnregisteredDataTablesProviderException Throws an unregistered provider exception. |
269
|
|
|
*/ |
270
|
|
|
public function optionsAction($name) { |
271
|
|
|
|
272
|
|
|
// Get the provider. |
273
|
|
|
$dtProvider = $this->getDataTablesProvider($name); |
274
|
|
|
|
275
|
|
|
// Get the wrapper. |
276
|
|
|
$dtWrapper = $this->getDataTablesWrapper($dtProvider); |
277
|
|
|
|
278
|
|
|
// Get the options. |
279
|
|
|
$dtOptions = DataTablesWrapperHelper::getOptions($dtWrapper); |
280
|
|
|
|
281
|
|
|
// Return the response. |
282
|
|
|
return new JsonResponse($dtOptions); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Render a DataTables. |
287
|
|
|
* |
288
|
|
|
* @param string $name The provider name. |
289
|
|
|
* @return Response Returns the response. |
290
|
|
|
* @throws UnregisteredDataTablesProviderException Throws an unregistered provider exception. |
291
|
|
|
*/ |
292
|
|
|
public function renderAction($name) { |
293
|
|
|
|
294
|
|
|
// Get the provider. |
295
|
|
|
$dtProvider = $this->getDataTablesProvider($name); |
296
|
|
|
|
297
|
|
|
// Get the wrapper. |
298
|
|
|
$dtWrapper = $this->getDataTablesWrapper($dtProvider); |
299
|
|
|
|
300
|
|
|
// Get and check the provider view. |
301
|
|
|
$dtView = $dtProvider->getView(); |
302
|
|
|
if (null === $dtProvider->getView()) { |
303
|
|
|
$dtView = "@JQueryDataTables/DataTables/index.html.twig"; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
// Return the response. |
307
|
|
|
return $this->render($dtView, [ |
308
|
|
|
"dtWrapper" => $dtWrapper, |
309
|
|
|
]); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Show an existing entity. |
314
|
|
|
* |
315
|
|
|
* @param string $name The provider name. |
316
|
|
|
* @param string $id The entity id. |
317
|
|
|
* @return Response Returns the response. |
318
|
|
|
*/ |
319
|
|
|
public function showAction($name, $id) { |
320
|
|
|
|
321
|
|
|
// Get the provider. |
322
|
|
|
$dtProvider = $this->getDataTablesProvider($name); |
323
|
|
|
|
324
|
|
|
try { |
325
|
|
|
|
326
|
|
|
// Get the entity. |
327
|
|
|
$entity = $this->getDataTablesEntityById($dtProvider, $id); |
328
|
|
|
|
329
|
|
|
// Dispatch an event. |
330
|
|
|
$this->dispatchDataTablesEvent(DataTablesEvents::DATATABLES_PRE_SHOW, [$entity]); |
331
|
|
|
} catch (EntityNotFoundException $ex) { |
332
|
|
|
|
333
|
|
|
// Log a debug trace. |
334
|
|
|
$this->getLogger()->debug($ex->getMessage()); |
335
|
|
|
|
336
|
|
|
// Initialize the entity. |
337
|
|
|
$entity = []; |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
// Get the serializer. |
341
|
|
|
$serializer = $this->getDataTablesSerializer(); |
342
|
|
|
|
343
|
|
|
// Return the response. |
344
|
|
|
return new Response($serializer->serialize($entity, "json"), 200, ["Content-type" => "application/json"]); |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
} |
348
|
|
|
|