Completed
Push — master ( 32f96d...04ee7e )
by WEBEWEB
01:13
created

DataTablesController::optionsAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 1
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\Exception\BadDataTablesColumnException;
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\DataTablesExportHelper;
29
use WBW\Bundle\JQuery\DataTablesBundle\Helper\DataTablesWrapperHelper;
30
use WBW\Bundle\JQuery\DataTablesBundle\WBWJQueryDataTablesEvents;
31
use WBW\Library\Core\Network\HTTP\HTTPInterface;
32
33
/**
34
 * DataTables controller.
35
 *
36
 * @author webeweb <https://github.com/webeweb/>
37
 * @package WBW\Bundle\JQuery\DataTablesBundle\Controller
38
 */
39
class DataTablesController extends AbstractController {
40
41
    /**
42
     * Delete an existing entity.
43
     *
44
     * @param Request $request The request.
45
     * @param string $name The provider name.
46
     * @param string $id The entity id.
47
     * @return Response Returns the response.
48
     * @throws UnregisteredDataTablesProviderException Throws an unregistered provider exception.
49
     */
50
    public function deleteAction(Request $request, $name, $id) {
51
52
        $dtProvider = $this->getDataTablesProvider($name);
53
54
        try {
55
56
            $entity = $this->getDataTablesEntityById($dtProvider, $id);
57
58
            $this->dispatchDataTablesEvent(WBWJQueryDataTablesEvents::DATATABLES_PRE_DELETE, [$entity]);
59
60
            $em = $this->getDoctrine()->getManager();
61
            $em->remove($entity);
62
            $em->flush();
63
64
            $this->dispatchDataTablesEvent(WBWJQueryDataTablesEvents::DATATABLES_POST_DELETE, [$entity]);
65
66
            $output = $this->prepareActionResponse(200, "DataTablesController.deleteAction.success");
67
        } catch (Exception $ex) {
68
69
            $output = $this->handleDataTablesException($ex, "DataTablesController.deleteAction");
70
        }
71
72
        return $this->buildDataTablesResponse($request, $name, $output);
73
    }
74
75
    /**
76
     * Edit an existing entity.
77
     *
78
     * @param Request $request The request.
79
     * @param string $name The provider name
80
     * @param string $id The entity id.
81
     * @param string $data The data.
82
     * @param mixed $value The value
83
     * @return Response Returns the response.
84
     * @throws UnregisteredDataTablesProviderException Throws an unregistered provider exception.
85
     * @throws BadDataTablesEditorException Throws a bad editor exception.
86
     * @throws BadDataTablesColumnException Throws a bad column exception.
87
     */
88
    public function editAction(Request $request, $name, $id, $data, $value) {
89
90
        $dtProvider = $this->getDataTablesProvider($name);
91
92
        $dtEditor = $this->getDataTablesEditor($dtProvider);
93
94
        $dtColumn = $this->getDataTablesColumn($dtProvider, $data);
95
96
        try {
97
98
            $entity = $this->getDataTablesEntityById($dtProvider, $id);
99
100
            if (true === $request->isMethod(HTTPInterface::HTTP_METHOD_POST)) {
101
                $value = $request->request->get("value");
102
            }
103
104
            $this->dispatchDataTablesEvent(WBWJQueryDataTablesEvents::DATATABLES_PRE_EDIT, [$entity]);
105
106
            $dtEditor->editColumn($dtColumn, $entity, $value);
107
108
            $em = $this->getDoctrine()->getManager();
109
            $em->persist($entity);
110
            $em->flush();
111
112
            $this->dispatchDataTablesEvent(WBWJQueryDataTablesEvents::DATATABLES_POST_EDIT, [$entity]);
113
114
            $output = $this->prepareActionResponse(200, "DataTablesController.editAction.success");
115
        } catch (Exception $ex) {
116
117
            $output = $this->handleDataTablesException($ex, "DataTablesController.editAction");
118
        }
119
120
        return new JsonResponse($output);
121
    }
122
123
    /**
124
     * Export all entities.
125
     *
126
     * @param string $name The provider name.
127
     * @return Response Returns the response.
128
     * @throws UnregisteredDataTablesProviderException Throws an unregistered provider exception.
129
     * @throws BadDataTablesCSVExporterException Throws a bad CSV exporter exception.
130
     * @throws BadDataTablesRepositoryException Throws a bad repository exception.
131
     */
132
    public function exportAction(Request $request, $name) {
133
134
        $windows = DataTablesExportHelper::isWindows($request);
135
136
        $dtProvider = $this->getDataTablesProvider($name);
137
138
        $dtExporter = $this->getDataTablesCSVExporter($dtProvider);
139
140
        $repository = $this->getDataTablesRepository($dtProvider);
141
142
        $filename = (new DateTime())->format("Y.m.d-H.i.s") . "-" . $dtProvider->getName() . ".csv";
143
144
        $charset = true === $windows ? "iso-8859-1" : "utf-8";
145
146
        $response = new StreamedResponse();
147
        $response->headers->set("Content-Disposition", "attachment; filename=\"" . $filename . "\"");
148
        $response->headers->set("Content-Type", "text/csv; charset=" . $charset);
149
        $response->setCallback(function() use ($dtProvider, $repository, $dtExporter, $windows) {
150
            $this->exportDataTablesCallback($dtProvider, $repository, $dtExporter, $windows);
151
        });
152
        $response->setStatusCode(200);
153
154
        return $response;
155
    }
156
157
    /**
158
     * Lists all entities.
159
     *
160
     * @param Request $request The request.
161
     * @param string $name The provider name.
162
     * @return Response Returns the response.
163
     * @throws UnregisteredDataTablesProviderException Throws an unregistered provider exception.
164
     * @throws BadDataTablesRepositoryException Throws a bad repository exception.
165
     */
166
    public function indexAction(Request $request, $name) {
167
168
        if (false === $request->isXmlHttpRequest()) {
169
            return $this->renderAction($name);
170
        }
171
172
        $dtProvider = $this->getDataTablesProvider($name);
173
174
        $dtWrapper = $this->getDataTablesWrapper($dtProvider);
175
        DataTablesFactory::parseWrapper($dtWrapper, $request);
176
177
        $repository = $this->getDataTablesRepository($dtProvider);
178
179
        $dtWrapper->getResponse()->setRecordsFiltered($repository->dataTablesCountFiltered($dtWrapper));
180
        $dtWrapper->getResponse()->setRecordsTotal($repository->dataTablesCountTotal($dtWrapper));
181
182
        $entities = $repository->dataTablesFindAll($dtWrapper);
183
184
        $this->dispatchDataTablesEvent(WBWJQueryDataTablesEvents::DATATABLES_PRE_INDEX, $entities);
185
186
        foreach ($entities as $entity) {
187
188
            $rows = $dtWrapper->getResponse()->countRows();
189
190
            $dtWrapper->getResponse()->addRow();
191
192
            // Render each row.
193
            foreach (DataTablesEnumerator::enumRows() 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
        $this->dispatchDataTablesEvent(WBWJQueryDataTablesEvents::DATATABLES_POST_INDEX, $entities);
204
205
        return new JsonResponse($dtWrapper->getResponse());
206
    }
207
208
    /**
209
     * Options of a DataTables.
210
     *
211
     * @param string $name The provider name.
212
     * @return Response Returns a response.
213
     * @throws UnregisteredDataTablesProviderException Throws an unregistered provider exception.
214
     */
215
    public function optionsAction($name) {
216
217
        $dtProvider = $this->getDataTablesProvider($name);
218
219
        $dtWrapper = $this->getDataTablesWrapper($dtProvider);
220
221
        $dtOptions = DataTablesWrapperHelper::getOptions($dtWrapper);
222
223
        return new JsonResponse($dtOptions);
224
    }
225
226
    /**
227
     * Render a DataTables.
228
     *
229
     * @param string $name The provider name.
230
     * @return Response Returns the response.
231
     * @throws UnregisteredDataTablesProviderException Throws an unregistered provider exception.
232
     */
233
    public function renderAction($name) {
234
235
        $dtProvider = $this->getDataTablesProvider($name);
236
237
        $dtWrapper = $this->getDataTablesWrapper($dtProvider);
238
239
        $dtView = $dtProvider->getView();
240
        if (null === $dtProvider->getView()) {
241
            $dtView = "@WBWJQueryDataTables/DataTables/index.html.twig";
242
        }
243
244
        return $this->render($dtView, [
245
            "dtWrapper" => $dtWrapper,
246
        ]);
247
    }
248
249
    /**
250
     * Serialize an existing entity.
251
     *
252
     * @param string $name The provider name.
253
     * @param string $id The entity id.
254
     * @return Response Returns the response.
255
     * @throws BadDataTablesRepositoryException Throws a bad repository exception.
256
     * @throws UnregisteredDataTablesProviderException Throws an unregistered provider exception.
257
     */
258
    public function serializeAction($name, $id) {
259
260
        $dtProvider = $this->getDataTablesProvider($name);
261
262
        try {
263
264
            $entity = $this->getDataTablesEntityById($dtProvider, $id);
265
266
            $this->dispatchDataTablesEvent(WBWJQueryDataTablesEvents::DATATABLES_PRE_SHOW, [$entity]);
267
        } catch (EntityNotFoundException $ex) {
268
269
            $this->logInfo($ex->getMessage());
270
271
            $entity = [];
272
        }
273
274
        $serializer = $this->getDataTablesSerializer();
275
276
        return new Response($serializer->serialize($entity, "json"), 200, ["Content-type" => "application/json"]);
277
    }
278
279
    /**
280
     * Show an existing entity.
281
     *
282
     * @param string $name The provider name.
283
     * @param string $id The entity id.
284
     * @return Response Returns the response.
285
     * @throws BadDataTablesRepositoryException Throws a bad repository exception.
286
     * @throws UnregisteredDataTablesProviderException Throws an unregistered provider exception.
287
     */
288
    public function showAction($name, $id) {
289
        return $this->serializeAction($name, $id);
290
    }
291
}
292