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