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\ORM\EntityNotFoundException; |
15
|
|
|
use Doctrine\ORM\EntityRepository; |
16
|
|
|
use Symfony\Component\HttpFoundation\Request; |
17
|
|
|
use Symfony\Component\HttpFoundation\Response; |
18
|
|
|
use Symfony\Component\Serializer\Encoder\JsonEncoder; |
19
|
|
|
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; |
20
|
|
|
use Symfony\Component\Serializer\Serializer; |
21
|
|
|
use WBW\Bundle\BootstrapBundle\Controller\AbstractBootstrapController; |
22
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\API\DataTablesWrapper; |
23
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Exception\BadDataTablesCSVExporterException; |
24
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Exception\BadDataTablesRepositoryException; |
25
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Exception\UnregisteredDataTablesProviderException; |
26
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Manager\DataTablesManager; |
27
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Provider\DataTablesCSVExporterInterface; |
28
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Provider\DataTablesProviderInterface; |
29
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Repository\DataTablesRepositoryInterface; |
30
|
|
|
use WBW\Library\Core\Utility\Argument\IntegerUtility; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Abstract jQuery DataTables controller. |
34
|
|
|
* |
35
|
|
|
* @author webeweb <https://github.com/webeweb/> |
36
|
|
|
* @package WBW\Bundle\JQuery\DataTablesBundle\Controller |
37
|
|
|
* @abstract |
38
|
|
|
*/ |
39
|
|
|
abstract class AbstractDataTablesController extends AbstractBootstrapController { |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Build a DataTables response. |
43
|
|
|
* |
44
|
|
|
* @param Request $request The request. |
45
|
|
|
* @param string $name The provider name. |
46
|
|
|
* @param array $output The output. |
47
|
|
|
* @return Response Returns the DataTables response. |
48
|
|
|
*/ |
49
|
|
|
protected function buildDataTablesResponse(Request $request, $name, array $output) { |
50
|
|
|
|
51
|
|
|
// Determines if the request is an XML HTTP request. |
52
|
|
|
if (true === $request->isXmlHttpRequest()) { |
53
|
|
|
|
54
|
|
|
// Return the response. |
55
|
|
|
return new Response(json_encode($output)); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// Notify the user. |
59
|
|
|
switch ($output["status"]) { |
60
|
|
|
|
61
|
|
|
case 200: |
62
|
|
|
$this->notifySuccess($output["notify"]); |
63
|
|
|
break; |
64
|
|
|
|
65
|
|
|
case 404: |
66
|
|
|
$this->notifyDanger($output["notify"]); |
67
|
|
|
break; |
68
|
|
|
|
69
|
|
|
case 500: |
70
|
|
|
$this->notifyWarning($output["notify"]); |
71
|
|
|
break; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// Return the response. |
75
|
|
|
return $this->redirectToRoute("jquery_datatables_index", ["name" => $name]); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Export callback. |
80
|
|
|
* |
81
|
|
|
* @param DataTablesProviderInterface $dtProvider The DataTables provider. |
82
|
|
|
* @param DataTablesRepositoryInterface $repository The DataTables repository. |
83
|
|
|
* @return void |
84
|
|
|
*/ |
85
|
|
|
protected function exportCallback(DataTablesProviderInterface $dtProvider, DataTablesRepositoryInterface $repository) { |
86
|
|
|
|
87
|
|
|
// Get the entities manager. |
88
|
|
|
$em = $this->getDoctrine()->getManager(); |
89
|
|
|
|
90
|
|
|
// Open the file. |
91
|
|
|
$stream = fopen("php://output", "w+"); |
92
|
|
|
|
93
|
|
|
// Export the columns. |
94
|
|
|
fputcsv($stream, $dtProvider->exportColumns(), ";"); |
|
|
|
|
95
|
|
|
|
96
|
|
|
// Paginates. |
97
|
|
|
$total = $repository->dataTablesCountExported($dtProvider); |
98
|
|
|
$pages = IntegerUtility::getPagesCount($total, DataTablesRepositoryInterface::REPOSITORY_LIMIT); |
99
|
|
|
|
100
|
|
|
// Handle each page. |
101
|
|
|
for ($i = 0; $i < $pages; ++$i) { |
102
|
|
|
|
103
|
|
|
// Get the offset and limit. |
104
|
|
|
list($offset, $limit) = IntegerUtility::getLinesLimit($i, DataTablesRepositoryInterface::REPOSITORY_LIMIT, $total); |
105
|
|
|
|
106
|
|
|
// Get the export query with offset and limit. |
107
|
|
|
$result = $repository->dataTablesExportAll($dtProvider) |
108
|
|
|
->setFirstResult($offset) // Use offset |
109
|
|
|
->setMaxResults($limit) // Use limit |
110
|
|
|
->getQuery() |
111
|
|
|
->iterate(); |
112
|
|
|
|
113
|
|
|
// Handle each entity. |
114
|
|
|
while (false !== ($row = $result->next())) { |
115
|
|
|
|
116
|
|
|
// Export the entity. |
117
|
|
|
fputcsv($stream, $dtProvider->exportRow($row[0]), ";"); |
|
|
|
|
118
|
|
|
|
119
|
|
|
// Detach the entity to avoid memory consumption. |
120
|
|
|
$em->detach($row[0]); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
// Close the file. |
124
|
|
|
fclose($stream); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Get a DataTables CSV exporter. |
129
|
|
|
* |
130
|
|
|
* @param string $name The provider name. |
131
|
|
|
* @return DataTablesProviderInterface Returns the DataTables CSV exporter. |
132
|
|
|
* @throws UnregisteredDataTablesProviderException Throws an unregistered DataTables provider exception. |
133
|
|
|
* @throws BadDataTablesCSVExporterException Throws a bad DataTables CSV exporter exception. |
134
|
|
|
*/ |
135
|
|
|
protected function getDataTablesCSVExporter($name) { |
136
|
|
|
|
137
|
|
|
// Get the DataTables provider. |
138
|
|
|
$dtProvider = $this->getDataTablesProvider($name); |
139
|
|
|
|
140
|
|
|
// Log a debug trace. |
141
|
|
|
$this->getLogger()->debug(sprintf("DataTables controller search for a CSV exporter with name \"%s\"", $name)); |
142
|
|
|
|
143
|
|
|
// Check the DataTables CSV exporter. |
144
|
|
|
if (false === ($dtProvider instanceOf DataTablesCSVExporterInterface)) { |
145
|
|
|
throw new BadDataTablesCSVExporterException($dtProvider); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
// Log a debug trace. |
149
|
|
|
$this->getLogger()->debug(sprintf("DataTables controller found a CSV exporter with name \"%s\"", $name)); |
150
|
|
|
|
151
|
|
|
// Return the DataTables provider. |
152
|
|
|
return $dtProvider; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Get a DataTables entity by id. |
157
|
|
|
* |
158
|
|
|
* @param DataTablesProviderInterface $dtProvider The DataTables provider. |
159
|
|
|
* @param integer $id The entity id. |
160
|
|
|
* @return object Returns the DataTables entity. |
161
|
|
|
* @throws BadDataTablesRepositoryException Throws a bad DataTables repository exception. |
162
|
|
|
* @throws EntityNotFoundException Throws an Entity not found exception. |
163
|
|
|
*/ |
164
|
|
|
protected function getDataTablesEntityById(DataTablesProviderInterface $dtProvider, $id) { |
165
|
|
|
|
166
|
|
|
// Get the repository. |
167
|
|
|
$repository = $this->getDataTablesRepository($dtProvider); |
168
|
|
|
|
169
|
|
|
// Log a debug trace. |
170
|
|
|
$this->getLogger()->debug(sprintf("DataTables controller search for an entity [%s]", $id)); |
171
|
|
|
|
172
|
|
|
// Find and check the entity. |
173
|
|
|
$entity = $repository->find($id); |
174
|
|
|
if (null === $entity) { |
175
|
|
|
throw EntityNotFoundException::fromClassNameAndIdentifier($dtProvider->getEntity(), [$id]); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
// Log a debug trace. |
179
|
|
|
$this->getLogger()->debug(sprintf("DataTables controller found an entity [%s]", $id)); |
180
|
|
|
|
181
|
|
|
// Return the entity. |
182
|
|
|
return $entity; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Get the DataTables provider. |
187
|
|
|
* |
188
|
|
|
* @param string $name The provider name. |
189
|
|
|
* @return DataTablesProviderInterface Returns the DataTables provider. |
190
|
|
|
* @throws UnregisteredDataTablesProviderException Throws an unregistered DataTables provider exception. |
191
|
|
|
*/ |
192
|
|
|
protected function getDataTablesProvider($name) { |
193
|
|
|
|
194
|
|
|
// Log a debug trace. |
195
|
|
|
$this->getLogger()->debug(sprintf("DataTables controller search for a provider with name \"%s\"", $name)); |
196
|
|
|
|
197
|
|
|
// Get the DataTables provider. |
198
|
|
|
$dtProvider = $this->get(DataTablesManager::SERVICE_NAME)->getProvider($name); |
199
|
|
|
|
200
|
|
|
// Log a debug trace. |
201
|
|
|
$this->getLogger()->debug(sprintf("DataTables controller found a provider with name \"%s\"", $name)); |
202
|
|
|
|
203
|
|
|
// Return the DataTables provider. |
204
|
|
|
return $dtProvider; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Get a DataTables repository. |
209
|
|
|
* |
210
|
|
|
* @param DataTablesProviderInterface The DataTables provider. |
211
|
|
|
* @return EntityRepository Returns the DataTables repository. |
212
|
|
|
* @throws BadDataTablesRepositoryException Throws a bad DataTables repository exception. |
213
|
|
|
*/ |
214
|
|
|
protected function getDataTablesRepository(DataTablesProviderInterface $dtProvider) { |
215
|
|
|
|
216
|
|
|
// Log a debug trace. |
217
|
|
|
$this->getLogger()->debug(sprintf("DataTables controller search for a repository with name \"%s\"", $dtProvider->getName())); |
218
|
|
|
|
219
|
|
|
// Get the entities manager. |
220
|
|
|
$em = $this->getDoctrine()->getManager(); |
221
|
|
|
|
222
|
|
|
// Get and check the entities repository. |
223
|
|
|
$repository = $em->getRepository($dtProvider->getEntity()); |
224
|
|
|
if (false === ($repository instanceOf DataTablesRepositoryInterface)) { |
225
|
|
|
throw new BadDataTablesRepositoryException($repository); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
// Return the DataTables repository. |
229
|
|
|
return $repository; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Get a DataTables serializer. |
234
|
|
|
* |
235
|
|
|
* @return Serializer Returns the DataTables serializer. |
236
|
|
|
*/ |
237
|
|
|
protected function getDataTablesSerializer() { |
238
|
|
|
return new Serializer([new ObjectNormalizer()], [new JsonEncoder()]); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Get a DataTables wrapper. |
243
|
|
|
* |
244
|
|
|
* @param DataTablesProviderInterface $dtProvider The DataTables provider. |
245
|
|
|
* @return DataTablesWrapper Returns the DataTables wrapper. |
246
|
|
|
*/ |
247
|
|
|
protected function getDataTablesWrapper(DataTablesProviderInterface $dtProvider) { |
248
|
|
|
|
249
|
|
|
// Initialize the URL. |
250
|
|
|
$url = $this->getRouter()->generate("jquery_datatables_index", ["name" => $dtProvider->getName()]); |
251
|
|
|
|
252
|
|
|
// Initialize the DataTables wrapper. |
253
|
|
|
$dtWrapper = new DataTablesWrapper($dtProvider->getMethod(), $url, $dtProvider->getName()); |
254
|
|
|
$dtWrapper->getMapping()->setPrefix($dtProvider->getPrefix()); |
255
|
|
|
|
256
|
|
|
// Handle each column. |
257
|
|
|
foreach ($dtProvider->getColumns() as $dtColumn) { |
258
|
|
|
|
259
|
|
|
// Log a debug trace. |
260
|
|
|
$this->getLogger()->debug(sprintf("DataTables provider add a column \"%s\"", $dtColumn->getData())); |
261
|
|
|
|
262
|
|
|
// Add. |
263
|
|
|
$dtWrapper->addColumn($dtColumn); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
// Return the DataTables wrapper. |
267
|
|
|
return $dtWrapper; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Get the notification. |
272
|
|
|
* |
273
|
|
|
* @param string $id The notification id. |
274
|
|
|
* @return string Returns the notification. |
275
|
|
|
*/ |
276
|
|
|
protected function getNotification($id) { |
277
|
|
|
return $this->getTranslator()->trans($id, [], "JQueryDataTablesBundle"); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
} |
281
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.