Skip to content

Commit 4fc33a9

Browse files
authored
Merge pull request #40 from ThibBal/5.2
[fix] Fix export CSV
2 parents 30201ce + 433b2aa commit 4fc33a9

File tree

1 file changed

+10
-54
lines changed

1 file changed

+10
-54
lines changed

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

Lines changed: 10 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -318,18 +318,6 @@ public function getResponse($format = null, $filename = 'data.csv') : Response
318318
$columnArr['escapeHTML'] = $column->isEscapeHTML();
319319

320320
$columnsArr[] = $columnArr;
321-
322-
if (($column instanceof EvoluColumnFormatterInterface) && ($column->getFormatter() != null)) {
323-
foreach ($resultData as $key => $row) {
324-
$formatter = $column->getFormatter();
325-
$resultData[$key][$column->getKey()] = $formatter->format($row[$column->getKey()]);
326-
}
327-
}
328-
if ($column instanceof EvoluColumnRowFormatterInterface) {
329-
foreach ($resultData as $key => $row) {
330-
$resultData[$key] = $column->formatRow($row);
331-
}
332-
}
333321
}
334322
}
335323

@@ -372,57 +360,25 @@ public function saveCsv($filePath)
372360

373361
private function outputCsv($fp)
374362
{
375-
foreach ($this->columns as $key => $column) {
376-
/* @var $column EvoluGridColumn */
377-
if (!($column instanceof EvoluColumnKeyInterface) || !$column->isExported()) {
378-
unset($this->columns[$key]);
379-
}
380-
}
381-
382-
// Let's only keep the columns that are tied to keys (we cannot render JS in CSV exports)
383-
$keyColumns = array_filter($this->columns, function (EvoluColumnInterface $column) {
384-
return $column instanceof EvoluColumnKeyInterface;
363+
$columns = array_filter($this->columns, function (EvoluColumnInterface $column) {
364+
return $column->isExported();
385365
});
386366

387367
// TODO: enable autoBuildColumns on CSV
388368
$columnsTitles = array_map(
389-
function (EvoluColumnKeyInterface $column) {
390-
return iconv('UTF-8', $this->csvEncoding, $column->getTitle());
391-
}, $keyColumns);
369+
function (EvoluColumnInterface $column) {
370+
return iconv('UTF-8', $this->csvEncoding, (string) $column->getTitle());
371+
}, $columns);
392372
fputcsv($fp, $columnsTitles, ';');
393373

394374
$resultArray = ValueUtils::val($this->results);
395375

396376
foreach ($resultArray as $row) {
397-
$columns = array_map(
398-
function (EvoluColumnKeyInterface $elem) use ($row) {
399-
if (($elem instanceof EvoluColumnFormatterInterface) && ($elem->getFormatter() != null)) {
400-
$formatter = $elem->getFormatter();
401-
$row[$elem->getKey()] = $formatter->format($row[$elem->getKey()]);
402-
}
403-
if ($elem instanceof EvoluColumnRowFormatterInterface) {
404-
if (is_array($row)) {
405-
$row = $elem->formatRow($row);
406-
}
407-
}
408-
if (is_object($row)) {
409-
$key = $elem->getKey();
410-
if (property_exists($row, $key)) {
411-
return ($row->$key == '') ? ' '
412-
: iconv('UTF-8', $this->csvEncoding, strip_tags((string) $row->$key));
413-
} else {
414-
return ' ';
415-
}
416-
} else {
417-
if (isset($row[$elem->getKey()])) {
418-
return ($row[$elem->getKey()] == '') ? ' '
419-
: iconv('UTF-8', $this->csvEncoding, strip_tags((string) $row[$elem->getKey()]));
420-
} else {
421-
return ' ';
422-
}
423-
}
424-
}, $keyColumns);
425-
fputcsv($fp, $columns, ';');
377+
$results = array_map(
378+
function (EvoluColumnInterface $column) use ($row) {
379+
return iconv('UTF-8', $this->csvEncoding, (string) $column->render($row));
380+
}, $columns);
381+
fputcsv($fp, $results, ';');
426382
}
427383
}
428384

0 commit comments

Comments
 (0)