diff --git a/Controller/AbstractDataTablesController.php b/Controller/AbstractDataTablesController.php index 5f1f05d1..df79c294 100644 --- a/Controller/AbstractDataTablesController.php +++ b/Controller/AbstractDataTablesController.php @@ -27,6 +27,7 @@ use WBW\Bundle\JQuery\DataTablesBundle\Exception\BadDataTablesEditorException; use WBW\Bundle\JQuery\DataTablesBundle\Exception\BadDataTablesRepositoryException; use WBW\Bundle\JQuery\DataTablesBundle\Exception\UnregisteredDataTablesProviderException; +use WBW\Bundle\JQuery\DataTablesBundle\Helper\DataTablesExportHelper; use WBW\Bundle\JQuery\DataTablesBundle\Manager\DataTablesManager; use WBW\Bundle\JQuery\DataTablesBundle\Provider\DataTablesCSVExporterInterface; use WBW\Bundle\JQuery\DataTablesBundle\Provider\DataTablesEditorInterface; @@ -87,9 +88,10 @@ protected function buildDataTablesResponse(Request $request, $name, ActionRespon * @param DataTablesProviderInterface $dtProvider The provider. * @param DataTablesRepositoryInterface $repository The repository. * @param DataTablesCSVExporterInterface $dtExporter The exporter. + * @param bool $windows Windows ? * @return void */ - protected function exportCallback(DataTablesProviderInterface $dtProvider, DataTablesRepositoryInterface $repository, DataTablesCSVExporterInterface $dtExporter) { + protected function exportCallback(DataTablesProviderInterface $dtProvider, DataTablesRepositoryInterface $repository, DataTablesCSVExporterInterface $dtExporter, $windows) { // Get the entities manager. $em = $this->getDoctrine()->getManager(); @@ -98,7 +100,7 @@ protected function exportCallback(DataTablesProviderInterface $dtProvider, DataT $stream = fopen("php://output", "w+"); // Export the columns. - fputcsv($stream, $dtExporter->exportColumns(), ";"); + fputcsv($stream, DataTablesExportHelper::convert($dtExporter->exportColumns(), $windows), ";"); // Paginates. $total = $repository->dataTablesCountExported($dtProvider); @@ -121,7 +123,7 @@ protected function exportCallback(DataTablesProviderInterface $dtProvider, DataT while (false !== ($row = $result->next())) { // Export the entity. - fputcsv($stream, $dtExporter->exportRow($row[0]), ";"); + fputcsv($stream, DataTablesExportHelper::convert($dtExporter->exportRow($row[0]), $windows), ";"); // Detach the entity to avoid memory consumption. $em->detach($row[0]); @@ -134,7 +136,7 @@ protected function exportCallback(DataTablesProviderInterface $dtProvider, DataT /** * Get a column. * - * @param DataTableProviderInterface $dtProvider The provider. + * @param DataTablesProviderInterface $dtProvider The provider. * @param string $data The data. * @return DataTablesColumn Returns the column. * @throws BadDataTablesColumnException Throws a bad column exception. diff --git a/Controller/DataTablesController.php b/Controller/DataTablesController.php index 7b777ddc..b6fc003b 100644 --- a/Controller/DataTablesController.php +++ b/Controller/DataTablesController.php @@ -12,8 +12,8 @@ namespace WBW\Bundle\JQuery\DataTablesBundle\Controller; use DateTime; -use Exception; use Doctrine\ORM\EntityNotFoundException; +use Exception; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -23,6 +23,7 @@ use WBW\Bundle\JQuery\DataTablesBundle\Exception\BadDataTablesEditorException; use WBW\Bundle\JQuery\DataTablesBundle\Exception\BadDataTablesRepositoryException; use WBW\Bundle\JQuery\DataTablesBundle\Exception\UnregisteredDataTablesProviderException; +use WBW\Bundle\JQuery\DataTablesBundle\Helper\DataTablesExportHelper; use WBW\Bundle\JQuery\DataTablesBundle\Helper\DataTablesWrapperHelper; use WBW\Library\Core\Network\HTTP\HTTPInterface; @@ -146,13 +147,17 @@ public function editAction(Request $request, $name, $id, $data, $value) { /** * Export all entities. * + * @param Request $request The request. * @param string $name The provider name. * @return Response Returns the response. * @throws UnregisteredDataTablesProviderException Throws an unregistered provider exception. * @throws BadDataTablesCSVExporterException Throws a bad CSV exporter exception. * @throws BadDataTablesRepositoryException Throws a bad repository exception. */ - public function exportAction($name) { + public function exportAction(Request $request, $name) { + + // Determines the client OS. + $windows = DataTablesExportHelper::isWindows($request); // Get the provider. $dtProvider = $this->getDataTablesProvider($name); @@ -166,12 +171,15 @@ public function exportAction($name) { // Initialize the filename. $filename = (new DateTime())->format("Y.m.d-H.i.s") . "-" . $dtProvider->getName() . ".csv"; + // Initialize the charset. + $charset = true === $windows ? "iso-8859-1" : "utf-8"; + // Initialize the response. $response = new StreamedResponse(); $response->headers->set("Content-Disposition", "attachment; filename=\"" . $filename . "\""); - $response->headers->set("Content-Type", "text/csv; charset=utf-8"); - $response->setCallback(function() use($dtProvider, $repository, $dtExporter) { - $this->exportCallback($dtProvider, $repository, $dtExporter); + $response->headers->set("Content-Type", "text/csv; charset=" . $charset); + $response->setCallback(function() use($dtProvider, $repository, $dtExporter, $windows) { + $this->exportCallback($dtProvider, $repository, $dtExporter, $windows); }); $response->setStatusCode(200);