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 Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException; |
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
16
|
|
|
use Symfony\Component\HttpFoundation\Response; |
17
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\API\DataTablesResponse; |
18
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Exception\BadDataTablesRepositoryException; |
19
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Exception\UnregisteredDataTablesProviderException; |
20
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Repository\DataTablesRepositoryInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* DataTables controller. |
24
|
|
|
* |
25
|
|
|
* @author webeweb <https://github.com/webeweb/> |
26
|
|
|
* @package WBW\Bundle\JQuery\DataTablesBundle\Controller |
27
|
|
|
*/ |
28
|
|
|
class DataTablesController extends AbstractDataTablesController { |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Build a response. |
32
|
|
|
* |
33
|
|
|
* @param Request $request The request. |
34
|
|
|
* @param string $name The provider name. |
35
|
|
|
* @param array $output The output. |
36
|
|
|
* @return Response Return the response. |
37
|
|
|
*/ |
38
|
|
|
protected function buildResponse(Request $request, $name, array $output) { |
39
|
|
|
|
40
|
|
|
// Determines if the request is an XML HTTP request. |
41
|
|
|
if (true === $request->isXmlHttpRequest()) { |
42
|
|
|
|
43
|
|
|
// Return the response. |
44
|
|
|
return new Response(json_encode($output)); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
// Notify the user. |
48
|
|
|
switch ($output["status"]) { |
49
|
|
|
case 200: |
50
|
|
|
$this->notifySuccess($output["notify"]); |
51
|
|
|
break; |
52
|
|
|
case 404: |
53
|
|
|
$this->notifyDanger($output["notify"]); |
54
|
|
|
break; |
55
|
|
|
case 500: |
56
|
|
|
$this->notifyWarning($output["notify"]); |
57
|
|
|
break; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// Return the response. |
61
|
|
|
return $this->redirectToRoute("jquery_datatables_index", ["name" => $name]); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Delete an existing entity. |
66
|
|
|
* |
67
|
|
|
* @param Request $request The request. |
68
|
|
|
* @param string $name The provider name. |
69
|
|
|
* @param string $id The entity id. |
70
|
|
|
* @throws UnregisteredDataTablesProviderException Throws an unregistered DataTables provider exception. |
71
|
|
|
* @throws BadDataTablesRepositoryException Throws a bad DataTables repository exception. |
72
|
|
|
*/ |
73
|
|
|
public function deleteAction(Request $request, $name, $id) { |
74
|
|
|
|
75
|
|
|
// Get the provider. |
76
|
|
|
$dtProvider = $this->getDataTablesProvider($name); |
77
|
|
|
|
78
|
|
|
// Get the entities manager. |
79
|
|
|
$em = $this->getDoctrine()->getManager(); |
80
|
|
|
|
81
|
|
|
// Get and check the entities repository. |
82
|
|
|
$repository = $em->getRepository($dtProvider->getEntity()); |
83
|
|
|
if (false === ($repository instanceOf DataTablesRepositoryInterface)) { |
84
|
|
|
throw new BadDataTablesRepositoryException($repository); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
// Initialize the output. |
88
|
|
|
$output = [ |
89
|
|
|
"status" => null, |
90
|
|
|
"notify" => null, |
91
|
|
|
]; |
92
|
|
|
|
93
|
|
|
// Get and check the entity. |
94
|
|
|
$entity = $repository->find($id); |
95
|
|
|
if (null === $entity) { |
96
|
|
|
|
97
|
|
|
// Set the output. |
98
|
|
|
$output["status"] = 404; |
99
|
|
|
$output["notify"] = $this->getNotification("DataTablesController.deleteAction.danger"); |
100
|
|
|
|
101
|
|
|
// Return the response. |
102
|
|
|
return $this->buildResponse($request, $name, $output); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
try { |
106
|
|
|
|
107
|
|
|
// Delete the entity. |
108
|
|
|
$em->remove($entity); |
109
|
|
|
$em->flush(); |
110
|
|
|
|
111
|
|
|
// Set the output. |
112
|
|
|
$output["status"] = 200; |
113
|
|
|
$output["notify"] = $this->getNotification("DataTablesController.deleteAction.success"); |
114
|
|
|
} catch (ForeignKeyConstraintViolationException $ex) { |
115
|
|
|
|
116
|
|
|
// Log a debug trace. |
117
|
|
|
$this->getLogger()->debug(sprintf("%s:%s %s", $ex->getErrorCode(), $ex->getSQLState(), $ex->getMessage())); |
118
|
|
|
|
119
|
|
|
// Set the output. |
120
|
|
|
$output["status"] = 500; |
121
|
|
|
$output["notify"] = $this->getNotification("DataTablesController.deleteAction.warning"); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
// Return the response. |
125
|
|
|
return $this->buildResponse($request, $name, $output); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Lists all entities. |
130
|
|
|
* |
131
|
|
|
* @param Request $request The request. |
132
|
|
|
* @param string $name The provider name. |
133
|
|
|
* @return Response Returns the response. |
134
|
|
|
* @throws UnregisteredDataTablesProviderException Throws an unregistered DataTables provider exception. |
135
|
|
|
* @throws BadDataTablesRepositoryException Throws a bad DataTables repository exception. |
136
|
|
|
*/ |
137
|
|
|
public function indexAction(Request $request, $name) { |
138
|
|
|
|
139
|
|
|
// Get the provider. |
140
|
|
|
$dtProvider = $this->getDataTablesProvider($name); |
141
|
|
|
|
142
|
|
|
// Get the wrapper. |
143
|
|
|
$dtWrapper = $this->getDataTablesWrapper($dtProvider); |
144
|
|
|
|
145
|
|
|
// Check if the request is an XML HTTP request. |
146
|
|
|
if (false === $request->isXmlHttpRequest()) { |
147
|
|
|
|
148
|
|
|
// Initialize the default view. |
149
|
|
|
$dtView = "@JQueryDataTables/DataTables/index.html.twig"; |
150
|
|
|
|
151
|
|
|
// Check the provider view. |
152
|
|
|
if (null !== $dtProvider->getView()) { |
153
|
|
|
$dtView = $dtProvider->getView(); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
// Return the response. |
157
|
|
|
return $this->render($dtView, [ |
158
|
|
|
"dtWrapper" => $dtWrapper, |
159
|
|
|
]); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
// Get the entities manager. |
163
|
|
|
$em = $this->getDoctrine()->getManager(); |
164
|
|
|
|
165
|
|
|
// Get and check the entities repository. |
166
|
|
|
$repository = $em->getRepository($dtProvider->getEntity()); |
167
|
|
|
if (false === ($repository instanceOf DataTablesRepositoryInterface)) { |
168
|
|
|
throw new BadDataTablesRepositoryException($repository); |
|
|
|
|
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
// Parse the request. |
172
|
|
|
$dtWrapper->parse($request); |
173
|
|
|
|
174
|
|
|
// |
175
|
|
|
$filtered = $repository->dataTablesCountFiltered($dtWrapper); |
176
|
|
|
$total = $repository->dataTablesCountTotal($dtWrapper); |
177
|
|
|
$entities = $repository->dataTablesFindAll($dtWrapper); |
178
|
|
|
|
179
|
|
|
// Set the response. |
180
|
|
|
$dtWrapper->getResponse()->setRecordsFiltered($filtered); |
181
|
|
|
$dtWrapper->getResponse()->setRecordsTotal($total); |
182
|
|
|
|
183
|
|
|
// Handle each entity. |
184
|
|
|
foreach ($entities as $entity) { |
185
|
|
|
|
186
|
|
|
// Count the rows. |
187
|
|
|
$rows = $dtWrapper->getResponse()->countRows(); |
188
|
|
|
|
189
|
|
|
// Create a row. |
190
|
|
|
$dtWrapper->getResponse()->addRow(); |
191
|
|
|
|
192
|
|
|
// Render each optional parameters. |
193
|
|
|
foreach (DataTablesResponse::dtRow() as $dtRow) { |
194
|
|
|
$dtWrapper->getResponse()->setRow($dtRow, $dtProvider->renderRow($dtRow, $entity, $rows)); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
// Render each column. |
198
|
|
|
foreach ($dtWrapper->getColumns() as $dtColumn) { |
199
|
|
|
$dtWrapper->getResponse()->setRow($dtColumn->getData(), $dtProvider->renderColumn($dtColumn, $entity)); |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
// Return the response. |
204
|
|
|
return new Response(json_encode($dtWrapper->getResponse())); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
} |
208
|
|
|
|
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: