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 WBW\Bundle\BootstrapBundle\Controller\AbstractBootstrapController; |
19
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\API\DataTablesWrapper; |
20
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Exception\BadDataTablesCSVExporterException; |
21
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Exception\BadDataTablesRepositoryException; |
22
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Exception\UnregisteredDataTablesProviderException; |
23
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Manager\DataTablesManager; |
24
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Provider\DataTablesCSVExporterInterface; |
25
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Provider\DataTablesProviderInterface; |
26
|
|
|
use WBW\Bundle\JQuery\DataTablesBundle\Repository\DataTablesRepositoryInterface; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Abstract jQuery DataTables controller. |
30
|
|
|
* |
31
|
|
|
* @author webeweb <https://github.com/webeweb/> |
32
|
|
|
* @package WBW\Bundle\JQuery\DataTablesBundle\Controller |
33
|
|
|
* @abstract |
34
|
|
|
*/ |
35
|
|
|
abstract class AbstractDataTablesController extends AbstractBootstrapController { |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Build a DataTables response. |
39
|
|
|
* |
40
|
|
|
* @param Request $request The request. |
41
|
|
|
* @param string $name The provider name. |
42
|
|
|
* @param array $output The output. |
43
|
|
|
* @return Response Returns the DataTables response. |
44
|
|
|
*/ |
45
|
|
|
protected function buildDataTablesResponse(Request $request, $name, array $output) { |
46
|
|
|
|
47
|
|
|
// Determines if the request is an XML HTTP request. |
48
|
|
|
if (true === $request->isXmlHttpRequest()) { |
49
|
|
|
|
50
|
|
|
// Return the response. |
51
|
|
|
return new Response(json_encode($output)); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// Notify the user. |
55
|
|
|
switch ($output["status"]) { |
56
|
|
|
|
57
|
|
|
case 200: |
58
|
|
|
$this->notifySuccess($output["notify"]); |
59
|
|
|
break; |
60
|
|
|
|
61
|
|
|
case 404: |
62
|
|
|
$this->notifyDanger($output["notify"]); |
63
|
|
|
break; |
64
|
|
|
|
65
|
|
|
case 500: |
66
|
|
|
$this->notifyWarning($output["notify"]); |
67
|
|
|
break; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// Return the response. |
71
|
|
|
return $this->redirectToRoute("jquery_datatables_index", ["name" => $name]); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get a DataTables CSV exporter. |
76
|
|
|
* |
77
|
|
|
* @param string $name The provider name. |
78
|
|
|
* @return DataTablesCSVExporterInterface Returns the DataTables CSV exporter. |
79
|
|
|
* @throws UnregisteredDataTablesProviderException Throws an unregistered DataTables provider exception. |
80
|
|
|
* @throws BadDataTablesRepositoryException Throws a bad DataTables CSV exporter exception. |
81
|
|
|
*/ |
82
|
|
|
protected function getDataTablesCSVExporter($name) { |
83
|
|
|
|
84
|
|
|
// Get and check the DataTables provider. |
85
|
|
|
$dtProvider = $this->getDataTablesProvider($name); |
86
|
|
|
if (false === ($dtProvider instanceOf DataTablesCSVExporterInterface)) { |
87
|
|
|
throw new BadDataTablesCSVExporterException($dtProvider); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
// Return the DataTables CSV exporter. |
91
|
|
|
return $dtProvider; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get a DataTables entity by id. |
96
|
|
|
* |
97
|
|
|
* @param DataTablesProviderInterface $dtProvider The DataTables provider. |
98
|
|
|
* @param integer $id The entity id. |
99
|
|
|
* @return object Returns the DataTables entity. |
100
|
|
|
* @throws BadDataTablesRepositoryException Throws a bad DataTables repository exception. |
101
|
|
|
* @throws EntityNotFoundException Throws an Entity not found exception. |
102
|
|
|
*/ |
103
|
|
|
protected function getDataTablesEntityById(DataTablesProviderInterface $dtProvider, $id) { |
104
|
|
|
|
105
|
|
|
// Log a debug trace. |
106
|
|
|
$this->getLogger()->debug(sprintf("DataTables controller search for an entity [%s]", $id)); |
107
|
|
|
|
108
|
|
|
// Get the repository. |
109
|
|
|
$repository = $this->getDataTablesRepository($dtProvider); |
110
|
|
|
|
111
|
|
|
// Find and check the entity. |
112
|
|
|
$entity = $repository->find($id); |
113
|
|
|
if (null === $entity) { |
114
|
|
|
throw EntityNotFoundException::fromClassNameAndIdentifier($dtProvider->getEntity(), [$id]); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
// Return the entity. |
118
|
|
|
return $entity; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get the DataTables provider. |
123
|
|
|
* |
124
|
|
|
* @param string $name The provider name. |
125
|
|
|
* @return DataTablesProviderInterface Returns the DataTables provider. |
126
|
|
|
* @throws UnregisteredDataTablesProviderException Throws an unregistered DataTables provider exception. |
127
|
|
|
*/ |
128
|
|
|
protected function getDataTablesProvider($name) { |
129
|
|
|
|
130
|
|
|
// Log a debug trace. |
131
|
|
|
$this->getLogger()->debug(sprintf("DataTables controller search for a provider with name \"%s\"", $name)); |
132
|
|
|
|
133
|
|
|
// Get the DataTables provider. |
134
|
|
|
$dtProvider = $this->get(DataTablesManager::SERVICE_NAME)->getProvider($name); |
135
|
|
|
|
136
|
|
|
// Log a debug trace. |
137
|
|
|
$this->getLogger()->debug(sprintf("DataTables controller found a provider with name \"%s\"", $name)); |
138
|
|
|
|
139
|
|
|
// Return the DataTables provider. |
140
|
|
|
return $dtProvider; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Get a DataTables repository. |
145
|
|
|
* |
146
|
|
|
* @param DataTablesProviderInterface The DataTables provider. |
147
|
|
|
* @return EntityRepository Returns the DataTables repository. |
148
|
|
|
* @throws BadDataTablesRepositoryException Throws a bad DataTables repository exception. |
149
|
|
|
*/ |
150
|
|
|
protected function getDataTablesRepository(DataTablesProviderInterface $dtProvider) { |
151
|
|
|
|
152
|
|
|
// Log a debug trace. |
153
|
|
|
$this->getLogger()->debug(sprintf("DataTables controller search for a repository with name \"%s\"", $dtProvider->getName())); |
154
|
|
|
|
155
|
|
|
// Get the entities manager. |
156
|
|
|
$em = $this->getDoctrine()->getManager(); |
157
|
|
|
|
158
|
|
|
// Get and check the entities repository. |
159
|
|
|
$repository = $em->getRepository($dtProvider->getEntity()); |
160
|
|
|
if (false === ($repository instanceOf DataTablesRepositoryInterface)) { |
161
|
|
|
throw new BadDataTablesRepositoryException($repository); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
// Return the DataTables repository. |
165
|
|
|
return $repository; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Get a DataTables wrapper. |
170
|
|
|
* |
171
|
|
|
* @param DataTablesProviderInterface $dtProvider The DataTables provider. |
172
|
|
|
* @return DataTablesWrapper Returns the DataTables wrapper. |
173
|
|
|
*/ |
174
|
|
|
protected function getDataTablesWrapper(DataTablesProviderInterface $dtProvider) { |
175
|
|
|
|
176
|
|
|
// Initialize the URL. |
177
|
|
|
$url = $this->getRouter()->generate("jquery_datatables_index", ["name" => $dtProvider->getName()]); |
178
|
|
|
|
179
|
|
|
// Initialize the DataTables wrapper. |
180
|
|
|
$dtWrapper = new DataTablesWrapper($dtProvider->getMethod(), $url, $dtProvider->getName()); |
181
|
|
|
$dtWrapper->getMapping()->setPrefix($dtProvider->getPrefix()); |
182
|
|
|
|
183
|
|
|
// Handle each column. |
184
|
|
|
foreach ($dtProvider->getColumns() as $dtColumn) { |
185
|
|
|
|
186
|
|
|
// Log a debug trace. |
187
|
|
|
$this->getLogger()->debug(sprintf("DataTables provider add a column \"%s\"", $dtColumn->getData())); |
188
|
|
|
|
189
|
|
|
// Add. |
190
|
|
|
$dtWrapper->addColumn($dtColumn); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
// Return the DataTables wrapper. |
194
|
|
|
return $dtWrapper; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Get the notification. |
199
|
|
|
* |
200
|
|
|
* @param string $id The notification id. |
201
|
|
|
* @return string Returns the notification. |
202
|
|
|
*/ |
203
|
|
|
protected function getNotification($id) { |
204
|
|
|
return $this->getTranslator()->trans($id, [], "JQueryDataTablesBundle"); |
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: