Created
June 18, 2018 05:26
-
-
Save lwillems/4b7cd312f6e56dae0a9dc0b82188be94 to your computer and use it in GitHub Desktop.
Generic version for csv export (adapted from https://knpuniversity.com/screencast/easyadminbundle/csv-export)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Controller; | |
use App\Service\CsvExporter; | |
use EasyCorp\Bundle\EasyAdminBundle\Controller\AdminController as BaseAdminController; | |
class AdminController extends BaseAdminController | |
{ | |
/** | |
* @var CsvExporter | |
*/ | |
private $csvExporter; | |
public function __construct(CsvExporter $csvExporter) | |
{ | |
$this->csvExporter = $csvExporter; | |
} | |
public function exportAction() | |
{ | |
$sortDirection = $this->request->query->get('sortDirection'); | |
if (empty($sortDirection) || !in_array(strtoupper($sortDirection), ['ASC', 'DESC'])) { | |
$sortDirection = 'DESC'; | |
} | |
$queryBuilder = $this->createListQueryBuilder( | |
$this->entity['class'], | |
$sortDirection, | |
$this->request->query->get('sortField'), | |
$this->entity['list']['dql_filter'] | |
); | |
return $this->csvExporter->getResponseFromQueryBuilder( | |
$queryBuilder, | |
sprintf( | |
'export_%s_%s.csv', | |
$this->entity['label'], | |
date('d-m-Y') | |
) | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Service; | |
use Doctrine\Common\Collections\ArrayCollection; | |
use Doctrine\ORM\QueryBuilder; | |
use Symfony\Component\HttpFoundation\StreamedResponse; | |
use Symfony\Component\PropertyAccess\PropertyAccess; | |
use Symfony\Component\Serializer\SerializerInterface; | |
class CsvExporter | |
{ | |
/** | |
* @var SerializerInterface | |
*/ | |
private $serializer; | |
public function __construct(SerializerInterface $serializer) | |
{ | |
$this->serializer = $serializer; | |
} | |
public function getResponseFromQueryBuilder(QueryBuilder $queryBuilder, $filename) | |
{ | |
$response = new StreamedResponse(); | |
$entities = new ArrayCollection($queryBuilder->getQuery()->getResult()); | |
$first = $entities->first(); | |
$columns = $this->serializer->normalize( | |
$first, | |
null, | |
[ | |
'groups' => [ | |
'export' | |
] | |
] | |
); | |
if (sizeof($columns) == 0) { | |
throw new \LogicException( | |
sprintf( | |
'There is no export data available for this entity %s', | |
get_class($first) | |
) | |
); | |
} | |
$propertyAccessor = PropertyAccess::createPropertyAccessor(); | |
$response->setCallback(function () use ($entities, $columns, $propertyAccessor) { | |
$handle = fopen('php://output', 'w+'); | |
// Add header | |
fputcsv($handle, array_keys($columns)); | |
while ($entity = $entities->current()) { | |
$values = []; | |
foreach ($columns as $key => $column) { | |
$values[] = $propertyAccessor->getValue($entity, $key); | |
} | |
fputcsv($handle, $values); | |
$entities->next(); | |
} | |
fclose($handle); | |
}); | |
$response->headers->set('Content-Type', 'text/csv; charset=utf-8'); | |
$response->headers->set('Content-Disposition', 'attachment; filename="' . $filename . '"'); | |
return $response; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment