1 | <?php |
||
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) { |
||
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) { |
||
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) { |
||
326 | |||
327 | } |
||
328 |