Skip to content

Commit 5ac9d72

Browse files
committed
Merge pull request #27 from josealbea/5.0
Working on PSR-7 compatibility for csv export
2 parents 369fdf3 + fda57af commit 5ac9d72

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"mouf/utils.common.paginable-interface" : "~1.0",
3030
"mouf/utils.common.conditioninterface" : "2.*",
3131
"twig/twig" : "1.*@stable",
32-
"php" : ">=5.4.0",
32+
"php" : ">=7.0",
3333
"mouf/javascript.historyjs" : "~1.8",
3434
"mouf/html.htmlelement" : ">=2.0-dev,<3.0",
3535
"mouf/utils.common.formatters" : "~3.0",

src/Mouf/Html/Widgets/EvoluGrid/EvoluGridResultSet.php

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
declare(strict_types=1);
3+
24
namespace Mouf\Html\Widgets\EvoluGrid;
35

46
use Mouf\Utils\Value\ValueUtils;
@@ -21,6 +23,8 @@
2123

2224
use Mouf\Utils\Action\ActionInterface;
2325
use Mouf\Utils\Common\SortableInterface;
26+
use Zend\Diactoros\Response;
27+
use Zend\Diactoros\Stream;
2428

2529
/**
2630
* This class represents the JSON result that can be sent to an evolugrid to display results.
@@ -196,8 +200,11 @@ public function run() {
196200
if ($this->sortOrder == null && isset($_GET['sort_order']) && !empty($_GET['sort_order'])) {
197201
$this->sortOrder = $_GET['sort_order'];
198202
}
203+
if ($this->format == null && isset($_GET['output']) && !empty($_GET['output'])) {
204+
$this->format = $_GET['output'];
205+
}
199206

200-
$this->output($this->format, $this->csvFilename);
207+
return $this->getResponse($this->format, $this->csvFilename);
201208
}
202209

203210
/**
@@ -208,7 +215,8 @@ public function run() {
208215
* @param string $filename
209216
* @throws \Exception
210217
*/
211-
public function output($format = null, $filename = "data.csv") {
218+
public function getResponse($format = null, $filename = "data.csv") : Response
219+
{
212220
if ($format == null) {
213221
$format = $this->format;
214222
if ($format == null) {
@@ -304,17 +312,24 @@ public function output($format = null, $filename = "data.csv") {
304312

305313
$jsonMessage['descriptor'] = $descriptor;
306314
$jsonMessage['additionnalData'] = $this->additionnalData;
307-
echo json_encode($jsonMessage);
315+
return new Response\JsonResponse($jsonMessage);
308316
} elseif ($format == self::FORMAT_CSV) {
309317

310-
header("Cache-Control: public");
311-
header("Content-Description: File Transfer");
312-
header("Content-Disposition: attachment; filename=$filename");
313-
header("Content-Type: mime/type");
314-
header("Content-Transfer-Encoding: binary");
315-
$fp = fopen("php://output", "w");
318+
$fp = fopen("php://temp", "w");
316319

317320
$this->outputCsv($fp);
321+
rewind($fp);
322+
$content = stream_get_contents($fp);
323+
324+
fclose($fp);
325+
326+
return new Response\HtmlResponse($content, 200, [
327+
"Cache-Control" => "public",
328+
"Content-Description" => "File Transfer",
329+
"Content-Disposition" => "attachment; filename=$filename",
330+
"Content-Type" => "mime/type",
331+
"Content-Transfer-Encoding" => "binary"
332+
]);
318333
} else {
319334
throw new \Exception(
320335
"The output format '" . $format . "' is not supported");
@@ -324,6 +339,7 @@ public function output($format = null, $filename = "data.csv") {
324339
public function saveCsv($filePath) {
325340
$fp = fopen($filePath, "w");
326341
$this->outputCsv($fp);
342+
fclose($fp);
327343
}
328344

329345
private function outputCsv($fp) {
@@ -380,7 +396,6 @@ function (EvoluColumnKeyInterface $elem) use ($row) {
380396
fputcsv($fp, $columns, ";");
381397
}
382398

383-
fclose($fp);
384399
}
385400

386401
/**
@@ -397,7 +412,7 @@ public function getUrl() {
397412
*
398413
* @return array<SplashRoute>
399414
*/
400-
public function getUrlsList() {
415+
public function getUrlsList($instanceName) {
401416
if ($this->url != null) {
402417
$instanceName = MoufManager::getMoufManager()->findInstanceName($this);
403418
$route = new SplashRoute($this->url, $instanceName, "run", null, "Ajax call by Evolugrid.");

0 commit comments

Comments
 (0)