src/Service/CsvExporter.php line 75

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use App\Repository\OrderItemRepository;
  4. use App\Repository\OrderRepository;
  5. use Doctrine\ORM\QueryBuilder;
  6. use EasyCorp\Bundle\EasyAdminBundle\Collection\FieldCollection;
  7. use Goodby\CSV\Export\Standard\Exporter;
  8. use Goodby\CSV\Export\Standard\ExporterConfig;
  9. use Symfony\Component\HttpFoundation\HeaderUtils;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\HttpFoundation\StreamedResponse;
  12. class CsvExporter
  13. {
  14.     private $orderItemRepository;
  15.     private $orderRepository;
  16.     public function __construct(OrderRepository $orderRepositoryOrderItemRepository $orderItemRepository)
  17.     {
  18.         $this->orderItemRepository $orderItemRepository;
  19.         $this->orderRepository $orderRepository;
  20.     }
  21.     public function createResponseFromQueryBuilder(QueryBuilder $queryBuilderFieldCollection $fieldsstring $filename): Response
  22.     {
  23.         $result $queryBuilder->getQuery()->getArrayResult();
  24.         // Convert DateTime objects into strings
  25.         $data = [];
  26.         foreach ($result as $index => $row) {
  27.             $tempArr = [];
  28.             foreach ($row as $columnKey => $columnValue) {
  29.                 $data[$index][$columnKey] = $columnValue instanceof \DateTimeInterface
  30.                     $columnValue->format('Y-m-d')
  31.                     : $columnValue;
  32.             }
  33.         }
  34.         // Humanize headers based on column labels in EA
  35.         if (isset($data[0])) {
  36.             $headers = [];
  37.             $properties array_keys($data[0]);
  38.             foreach ($properties as $property) {
  39.                 $headers[$property] = ucfirst($property);
  40.                 foreach ($fields as $field) {
  41.                     // Override property name if a custom label was set
  42.                     if ($property === $field->getProperty() && $field->getLabel()) {
  43.                         $headers[$property] = $field->getLabel();
  44.                         // And stop iterating
  45.                        
  46.                         break;
  47.                     }
  48.                 }
  49.             }
  50.             // Add headers to the final data array
  51.             array_unshift($data$headers);
  52.         }
  53.         $response = new StreamedResponse(function () use ($data) {
  54.             $config = new ExporterConfig();
  55.             $exporter = new Exporter($config);
  56.             $exporter->export('php://output'$data);
  57.         });
  58.         $dispositionHeader $response->headers->makeDisposition(
  59.             HeaderUtils::DISPOSITION_ATTACHMENT,
  60.             $filename
  61.         );
  62.         $response->headers->set('Content-Disposition'$dispositionHeader);
  63.         $response->headers->set('Content-Type''text/csv; charset=utf-8');
  64.         return $response;
  65.     }
  66.     public function createOrderResponseFromQueryBuilder(QueryBuilder $queryBuilderFieldCollection $fieldsstring $filename): Response
  67.     {
  68.         $result $queryBuilder->getQuery()->getResult();
  69.         $data = [];
  70.         foreach ($result as $record) {
  71.             $orderItems $record->getOrderItems();
  72.             
  73.             $index 0;
  74.             foreach ($orderItems as $orderItem) {
  75.                 if ($index == 0){
  76.                     $data[] = [
  77.                         'id' => $record->getOrderId(),
  78.                         'orderDate' => $record->getDate()->format('Y-m-d'),
  79.                         'hcp' => $record->getHcp(),
  80.                         'patient' => $record->getCustomer()->getName(),
  81.                         'productName' => $orderItem->getName(),
  82.                         'quantity'  => $orderItem->getQuantity(),
  83.                         'productPrice' => $orderItem->getPriceString(),
  84.                         'shippingCost' => $record->getShippingCostString(),
  85.                         'totalPrice' => $record->getTotalString(),
  86.                         'paymentStatus' => $record->getPaymentStatus(),
  87.                     ];
  88.                 } else {
  89.                     $data[] = [
  90.                         'id' => '',
  91.                         'orderDate' => '',
  92.                         'hcp' => '',
  93.                         'patient' => '',
  94.                         'productName' => $orderItem->getName(),
  95.                         'quantity'  =>  $orderItem->getQuantity(),
  96.                         'productPrice' => $orderItem->getPriceString(),
  97.                         'shippingCost' => '',
  98.                         'totalPrice' => '',
  99.                         'paymentStatus' => '',
  100.                     ];
  101.                 }
  102.                 $index++;
  103.             }
  104.         }
  105.         // Humanize headers based on column labels in EA
  106.         if (isset($data[0])) {
  107.             $headers = [];
  108.             $properties array_keys($data[0]);
  109.             foreach ($properties as $property) {
  110.                 $headers[$property] = ucfirst($property);
  111.                 foreach ($fields as $field) {
  112.                     // Override property name if a custom label was set
  113.                     if ($property === $field->getProperty() && $field->getLabel()) {
  114.                         $headers[$property] = $field->getLabel();
  115.                         // And stop iterating
  116.                        
  117.                         break;
  118.                     }
  119.                 }
  120.             }
  121.             // Add headers to the final data array
  122.             array_unshift($data$headers);
  123.         }
  124.         $response = new StreamedResponse(function () use ($data) {
  125.             $config = new ExporterConfig();
  126.             $exporter = new Exporter($config);
  127.             $exporter->export('php://output'$data);
  128.         });
  129.         $dispositionHeader $response->headers->makeDisposition(
  130.             HeaderUtils::DISPOSITION_ATTACHMENT,
  131.             $filename
  132.         );
  133.         $response->headers->set('Content-Disposition'$dispositionHeader);
  134.         $response->headers->set('Content-Type''text/csv; charset=utf-8');
  135.         return $response;
  136.     }
  137. }