Skip to content

Commit

Permalink
Add windows client support into exportAction()
Browse files Browse the repository at this point in the history
  • Loading branch information
webeweb committed Dec 19, 2018
1 parent 30a498a commit df05786
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
10 changes: 6 additions & 4 deletions Controller/AbstractDataTablesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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);
Expand All @@ -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]);
Expand All @@ -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.
Expand Down
18 changes: 13 additions & 5 deletions Controller/DataTablesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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);
Expand All @@ -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);

Expand Down

0 comments on commit df05786

Please sign in to comment.