Skip to content

Commit

Permalink
feat: Export custom-form answers into CSV file instead of Xlsx
Browse files Browse the repository at this point in the history
  • Loading branch information
ambroisemaupate committed Jul 11, 2024
1 parent a9af918 commit 5da1a44
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,25 @@ public function __construct(UrlGeneratorInterface $urlGenerator)

/**
* @param CustomFormAnswer $answer
*
* @return array
*/
public function toSimpleArray(CustomFormAnswer $answer): array
{
$answers = [];
$answers = [
'ip' => $answer->getIp(),
'submitted.date' => $answer->getSubmittedAt()
];
/** @var CustomFormFieldAttribute $answerAttr */
foreach ($answer->getAnswerFields() as $answerAttr) {
$field = $answerAttr->getCustomFormField();
if ($field->isDocuments()) {
$answers[$field->getName()] = implode(PHP_EOL, $answerAttr->getDocuments()->map(function (Document $document) {
$answers[$field->getLabel()] = implode(PHP_EOL, $answerAttr->getDocuments()->map(function (Document $document) {
return $this->urlGenerator->generate('documentsDownloadPage', [
'documentId' => $document->getId()
], UrlGeneratorInterface::ABSOLUTE_URL);
})->toArray());
} else {
$answers[$field->getName()] = $answerAttr->getValue();
$answers[$field->getLabel()] = $answerAttr->getValue();
}
}
return $answers;
Expand Down
2 changes: 1 addition & 1 deletion lib/RoadizCoreBundle/src/Entity/CustomForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ public function removeField(CustomFormField $field): CustomForm
}

/**
* @return Collection
* @return Collection<int, CustomFormAnswer>
*/
public function getCustomFormAnswers(): Collection
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,30 @@

namespace Themes\Rozier\Controllers\CustomForms;

use Doctrine\Persistence\ManagerRegistry;
use PhpOffice\PhpSpreadsheet\Exception;
use RZ\Roadiz\CoreBundle\Entity\CustomForm;
use RZ\Roadiz\CoreBundle\Entity\CustomFormAnswer;
use RZ\Roadiz\CoreBundle\CustomForm\CustomFormAnswerSerializer;
use RZ\Roadiz\CoreBundle\Xlsx\XlsxExporter;
use RZ\Roadiz\CoreBundle\Entity\CustomForm;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Themes\Rozier\RozierApp;

class CustomFormsUtilsController extends RozierApp
{
public function __construct(private readonly CustomFormAnswerSerializer $customFormAnswerSerializer)
{
public function __construct(
private readonly ManagerRegistry $managerRegistry,
private readonly TranslatorInterface $translator,
private readonly CustomFormAnswerSerializer $customFormAnswerSerializer,
private readonly SerializerInterface $serializer
) {
}

/**
* Export all custom form's answer in a Xlsx file (.rzt).
* Export all custom form's answers in a CSV file.
*
* @param Request $request
* @param int $id
Expand All @@ -32,45 +38,35 @@ public function __construct(private readonly CustomFormAnswerSerializer $customF
*/
public function exportAction(Request $request, int $id): Response
{
/** @var CustomForm|null $customForm */
$customForm = $this->em()->find(CustomForm::class, $id);
$customForm = $this->managerRegistry->getRepository(CustomForm::class)->find($id);
if (null === $customForm) {
throw $this->createNotFoundException();
}

$answers = $customForm->getCustomFormAnswers();

/**
* @var int $key
* @var CustomFormAnswer $answer
*/
$answersArray = [];
foreach ($answers as $key => $answer) {
$array = array_merge(
[$answer->getIp(), $answer->getSubmittedAt()],
$this->customFormAnswerSerializer->toSimpleArray($answer)
);
$answers[$key] = $array;
$answersArray[$key] = $this->customFormAnswerSerializer->toSimpleArray($answer);
}

$keys = ["ip", "submitted.date"];

$fields = $customForm->getFieldsLabels();
$keys = array_merge($keys, $fields);

$exporter = new XlsxExporter($this->getTranslator());
$xlsx = $exporter->exportXlsx($answers, $keys);

$response = new Response(
$xlsx,
Response::HTTP_OK,
[]
);

$keys = [
'ip',
'submitted.date',
...$fields
];

$response = new StreamedResponse(function () use ($answersArray, $keys) {
echo $this->serializer->serialize($answersArray, 'csv', [
'csv_headers' => $keys
]);
});
$response->headers->set('Content-Type', 'text/csv');
$response->headers->set(
'Content-Disposition',
$response->headers->makeDisposition(
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
$customForm->getName() . '.xlsx'
$customForm->getName() . '.csv'
)
);

Expand All @@ -84,15 +80,12 @@ public function exportAction(Request $request, int $id): Response
*
* @param Request $request
* @param int $id
*
* @return Response
*/
public function duplicateAction(Request $request, int $id): Response
{
$this->denyAccessUnlessGranted('ROLE_ACCESS_CUSTOMFORMS');
/** @var CustomForm|null $existingCustomForm */
$existingCustomForm = $this->em()->find(CustomForm::class, $id);

$existingCustomForm = $this->managerRegistry->getRepository(CustomForm::class)->find($id);
if (null === $existingCustomForm) {
throw $this->createNotFoundException();
}
Expand All @@ -101,7 +94,7 @@ public function duplicateAction(Request $request, int $id): Response
$newCustomForm = clone $existingCustomForm;
$newCustomForm->setCreatedAt(new \DateTime());
$newCustomForm->setUpdatedAt(new \DateTime());
$em = $this->em();
$em = $this->managerRegistry->getManager();

foreach ($newCustomForm->getFields() as $field) {
$em->persist($field);
Expand All @@ -110,7 +103,7 @@ public function duplicateAction(Request $request, int $id): Response
$em->persist($newCustomForm);
$em->flush();

$msg = $this->getTranslator()->trans("duplicated.custom.form.%name%", [
$msg = $this->translator->trans("duplicated.custom.form.%name%", [
'%name%' => $existingCustomForm->getDisplayName(),
]);

Expand All @@ -123,7 +116,7 @@ public function duplicateAction(Request $request, int $id): Response
} catch (\Exception $e) {
$this->publishErrorMessage(
$request,
$this->getTranslator()->trans("impossible.duplicate.custom.form.%name%", [
$this->translator->trans("impossible.duplicate.custom.form.%name%", [
'%name%' => $existingCustomForm->getDisplayName(),
]),
$newCustomForm
Expand Down

0 comments on commit 5da1a44

Please sign in to comment.