11<?php
2+ declare (strict_types=1 );
3+
24namespace Mouf \Html \Widgets \EvoluGrid ;
35
46use Mouf \Utils \Value \ValueUtils ;
2123
2224use Mouf \Utils \Action \ActionInterface ;
2325use 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