1 | <?php |
||
50 | abstract class AbstractDataTablesController extends AbstractController { |
||
51 | |||
52 | /** |
||
53 | * Build a response. |
||
54 | * |
||
55 | * @param Request $request The request. |
||
56 | * @param string $name The provider name. |
||
57 | * @param ActionResponse $output The output. |
||
58 | * @return Response Returns the response. |
||
59 | */ |
||
60 | protected function buildDataTablesResponse(Request $request, $name, ActionResponse $output) { |
||
88 | |||
89 | /** |
||
90 | * Dispatch an event. |
||
91 | * |
||
92 | * @param string $eventName The event name. |
||
93 | * @param array $entities The entities. |
||
94 | * @return Event Returns the event in case of succes, null otherwise. |
||
95 | */ |
||
96 | protected function dispatchDataTablesEvent($eventName, array $entities) { |
||
110 | |||
111 | /** |
||
112 | * Export callback. |
||
113 | * |
||
114 | * @param DataTablesProviderInterface $dtProvider The provider. |
||
115 | * @param DataTablesRepositoryInterface $repository The repository. |
||
116 | * @param DataTablesCSVExporterInterface $dtExporter The exporter. |
||
117 | * @return void |
||
118 | */ |
||
119 | protected function exportCallback(DataTablesProviderInterface $dtProvider, DataTablesRepositoryInterface $repository, DataTablesCSVExporterInterface $dtExporter) { |
||
167 | |||
168 | /** |
||
169 | * Get a column. |
||
170 | * |
||
171 | * @param DataTablesProviderInterface $dtProvider The provider. |
||
172 | * @param string $data The data. |
||
173 | * @return DataTablesColumnInterface Returns the column. |
||
174 | * @throws BadDataTablesColumnException Throws a bad column exception. |
||
175 | */ |
||
176 | protected function getDataTablesColumn(DataTablesProviderInterface $dtProvider, $data) { |
||
195 | |||
196 | /** |
||
197 | * Get a CSV exporter. |
||
198 | * |
||
199 | * @param DataTablesProviderInterface $dtProvider The provider. |
||
200 | * @return DataTablesCSVExporterInterface Returns the CSV exporter. |
||
201 | * @throws UnregisteredDataTablesProviderException Throws an unregistered provider exception. |
||
202 | * @throws BadDataTablesCSVExporterException Throws a bad CSV exporter exception. |
||
203 | */ |
||
204 | protected function getDataTablesCSVExporter(DataTablesProviderInterface $dtProvider) { |
||
223 | |||
224 | /** |
||
225 | * Get an editor. |
||
226 | * |
||
227 | * @param DataTablesProviderInterface $dtProvider The provider. |
||
228 | * @return DataTablesEditorInterface Returns the editor. |
||
229 | * @throws UnregisteredDataTablesProviderException Throws an unregistered provider exception. |
||
230 | * @throws BadDataTablesEditorException Throws a bad editor exception. |
||
231 | */ |
||
232 | protected function getDataTablesEditor(DataTablesProviderInterface $dtProvider) { |
||
251 | |||
252 | /** |
||
253 | * Get an entity by id. |
||
254 | * |
||
255 | * @param DataTablesProviderInterface $dtProvider The provider. |
||
256 | * @param int $id The entity id. |
||
257 | * @return object Returns the entity. |
||
258 | * @throws BadDataTablesRepositoryException Throws a bad repository exception. |
||
259 | * @throws EntityNotFoundException Throws an Entity not found exception. |
||
260 | */ |
||
261 | protected function getDataTablesEntityById(DataTablesProviderInterface $dtProvider, $id) { |
||
281 | |||
282 | /** |
||
283 | * Get the manager. |
||
284 | * |
||
285 | * @return DataTablesManager Returns the manager. |
||
286 | */ |
||
287 | protected function getDataTablesManager() { |
||
290 | |||
291 | /** |
||
292 | * Get a notification. |
||
293 | * |
||
294 | * @param string $notificationId The notification id. |
||
295 | * @return string Returns the notification. |
||
296 | */ |
||
297 | protected function getDataTablesNotification($notificationId) { |
||
298 | return $this->getTranslator()->trans($notificationId, [], "JQueryDataTablesBundle"); |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * Get the provider. |
||
303 | * |
||
304 | * @param string $name The provider name. |
||
305 | * @return DataTablesProviderInterface Returns the provider. |
||
306 | * @throws UnregisteredDataTablesProviderException Throws an unregistered provider exception. |
||
307 | */ |
||
308 | protected function getDataTablesProvider($name) { |
||
309 | |||
310 | // Log a debug trace. |
||
311 | $this->getLogger()->debug(sprintf("DataTables controller search for a provider with name \"%s\"", $name)); |
||
312 | |||
313 | // Get the provider. |
||
314 | $dtProvider = $this->getDataTablesManager()->getProvider($name); |
||
315 | |||
316 | // Log a debug trace. |
||
317 | $this->getLogger()->debug(sprintf("DataTables controller found a provider with name \"%s\"", $name)); |
||
318 | |||
319 | // Return the provider. |
||
320 | return $dtProvider; |
||
321 | } |
||
322 | |||
323 | /** |
||
324 | * Get a repository. |
||
325 | * |
||
326 | * @param DataTablesProviderInterface $dtProvider The provider. |
||
327 | * @return EntityRepository Returns the repository. |
||
328 | * @throws BadDataTablesRepositoryException Throws a bad repository exception. |
||
329 | */ |
||
330 | protected function getDataTablesRepository(DataTablesProviderInterface $dtProvider) { |
||
331 | |||
332 | // Log a debug trace. |
||
333 | $this->getLogger()->debug(sprintf("DataTables controller search for a repository with name \"%s\"", $dtProvider->getName())); |
||
334 | |||
335 | // Get the entities manager. |
||
336 | $em = $this->getDoctrine()->getManager(); |
||
337 | |||
338 | // Get and check the entities repository. |
||
339 | $repository = $em->getRepository($dtProvider->getEntity()); |
||
340 | if (false === ($repository instanceOf DataTablesRepositoryInterface)) { |
||
341 | throw new BadDataTablesRepositoryException($repository); |
||
342 | } |
||
343 | |||
344 | // Return the repository. |
||
345 | return $repository; |
||
346 | } |
||
347 | |||
348 | /** |
||
349 | * Get a serializer. |
||
350 | * |
||
351 | * @return Serializer Returns the serializer. |
||
352 | */ |
||
353 | protected function getDataTablesSerializer() { |
||
354 | return new Serializer([new ObjectNormalizer()], [new JsonEncoder()]); |
||
355 | } |
||
356 | |||
357 | /** |
||
358 | * Get a wrapper. |
||
359 | * |
||
360 | * @param DataTablesProviderInterface $dtProvider The provider. |
||
361 | * @return DataTablesWrapperInterface Returns the wrapper. |
||
362 | */ |
||
363 | protected function getDataTablesWrapper(DataTablesProviderInterface $dtProvider) { |
||
364 | |||
365 | // Initialize the URL. |
||
366 | $url = $this->getRouter()->generate("jquery_datatables_index", ["name" => $dtProvider->getName()]); |
||
367 | |||
368 | // Initialize the wrapper. |
||
369 | $dtWrapper = DataTablesFactory::newWrapper($url, $dtProvider); |
||
370 | |||
371 | // Handle each column. |
||
372 | foreach ($dtProvider->getColumns() as $dtColumn) { |
||
373 | |||
374 | // Log a debug trace. |
||
375 | $this->getLogger()->debug(sprintf("DataTables provider add a column \"%s\"", $dtColumn->getData())); |
||
376 | |||
377 | // Add the column. |
||
378 | $dtWrapper->addColumn($dtColumn); |
||
379 | } |
||
380 | |||
381 | // Set the options. |
||
382 | if (null !== $dtProvider->getOptions()) { |
||
383 | $dtWrapper->setOptions($dtProvider->getOptions()); |
||
384 | } |
||
385 | |||
386 | // Return the wrapper. |
||
387 | return $dtWrapper; |
||
388 | } |
||
389 | |||
390 | /** |
||
391 | * Handle an exception. |
||
392 | * |
||
393 | * @param Exception $ex The exception. |
||
394 | * @param string $notificationBaseId The notification base id. |
||
395 | * @return ActionResponse Returns the action response. |
||
396 | */ |
||
397 | protected function handleDataTablesException(Exception $ex, $notificationBaseId) { |
||
410 | |||
411 | /** |
||
412 | * Prepare an action response. |
||
413 | * |
||
414 | * @param int $status The status. |
||
415 | * @param string $notificationId The notification id. |
||
416 | * @return ActionResponse Returns the action response. |
||
417 | */ |
||
418 | protected function prepareActionResponse($status, $notificationId) { |
||
428 | |||
429 | } |
||
430 |