Skip to content

Commit

Permalink
Merge tag v2.3.19 into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
roadiz-ci committed Jul 12, 2024
1 parent a5af958 commit 3b7a121
Show file tree
Hide file tree
Showing 73 changed files with 274 additions and 571 deletions.
3 changes: 2 additions & 1 deletion src/Controllers/CacheController.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function deleteDoctrineCache(Request $request): Response
*/
return $this->redirectToRoute('adminHomePage');
}

$this->prepareBaseAssignation();
$this->assignation['form'] = $form->createView();

return $this->render('@RoadizRozier/cache/deleteDoctrine.html.twig', $this->assignation);
Expand Down Expand Up @@ -77,6 +77,7 @@ public function deleteAssetsCache(Request $request): Response
return $this->redirectToRoute('adminHomePage');
}

$this->prepareBaseAssignation();
$this->assignation['form'] = $form->createView();

return $this->render('@RoadizRozier/cache/deleteAssets.html.twig', $this->assignation);
Expand Down
71 changes: 32 additions & 39 deletions src/Controllers/CustomForms/CustomFormsUtilsController.php
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
5 changes: 1 addition & 4 deletions src/Controllers/Nodes/ExportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ public function __construct(private readonly NodeSourceXlsxSerializer $xlsxSeria
*/
public function exportAllXlsxAction(Request $request, int $translationId, ?int $parentNodeId = null): Response
{
/*
* Get translation
*/
$translation = $this->em()
->find(Translation::class, $translationId);

Expand Down Expand Up @@ -69,7 +66,7 @@ public function exportAllXlsxAction(Request $request, int $translationId, ?int $
->findBy($criteria, $order);

$this->xlsxSerializer->setOnlyTexts(true);
$this->xlsxSerializer->addUrls($request, $this->getSettingsBag()->get('force_locale'));
$this->xlsxSerializer->addUrls();
$xlsx = $this->xlsxSerializer->serialize($sources);

$response = new Response(
Expand Down
66 changes: 19 additions & 47 deletions src/Controllers/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Themes\Rozier\Controllers;

use DateTime;
use IteratorAggregate;
use PhpOffice\PhpSpreadsheet\Exception;
use RZ\Roadiz\Core\AbstractEntities\AbstractField;
use RZ\Roadiz\CoreBundle\Entity\Node;
Expand All @@ -18,7 +17,7 @@
use RZ\Roadiz\CoreBundle\Form\NodeStatesType;
use RZ\Roadiz\CoreBundle\Form\NodeTypesType;
use RZ\Roadiz\CoreBundle\Form\SeparatorType;
use RZ\Roadiz\CoreBundle\Xlsx\XlsxExporter;
use RZ\Roadiz\CoreBundle\Xlsx\NodeSourceXlsxSerializer;
use Symfony\Component\Form\ClickableInterface;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
Expand All @@ -44,6 +43,10 @@ class SearchController extends RozierApp
protected bool $pagination = true;
protected ?int $itemPerPage = null;

public function __construct(protected readonly NodeSourceXlsxSerializer $xlsxSerializer)
{
}

/**
* @param mixed $var
* @return bool
Expand Down Expand Up @@ -265,7 +268,6 @@ public function searchNodeSourceAction(Request $request, int $nodetypeId): Respo
$this->extendForm($builder, $nodetype);
$this->addButtons($builder, true);

/** @var Form $form */
$form = $builder->getForm();
$form->handleRequest($request);

Expand All @@ -277,7 +279,7 @@ public function searchNodeSourceAction(Request $request, int $nodetypeId): Respo
return $response;
}

if (null !== $response = $this->handleNodeForm($form, $nodetype)) {
if (null !== $response = $this->handleNodeForm($request, $form, $nodetype)) {
return $response;
}

Expand Down Expand Up @@ -314,11 +316,11 @@ protected function buildNodeTypeForm(?int $nodetypeId = null): FormBuilderInterf

/**
* @param FormBuilderInterface $builder
* @param bool $exportXlsx
* @param bool $export
*
* @return FormBuilderInterface
*/
protected function addButtons(FormBuilderInterface $builder, bool $exportXlsx = false): FormBuilderInterface
protected function addButtons(FormBuilderInterface $builder, bool $export = false): FormBuilderInterface
{
$builder->add('search', SubmitType::class, [
'label' => 'search.a.node',
Expand All @@ -327,7 +329,7 @@ protected function addButtons(FormBuilderInterface $builder, bool $exportXlsx =
],
]);

if ($exportXlsx) {
if ($export) {
$builder->add('export', SubmitType::class, [
'label' => 'export.all.nodesSource',
'attr' => [
Expand Down Expand Up @@ -363,14 +365,15 @@ protected function handleNodeTypeForm(FormInterface $nodeTypeForm): ?RedirectRes
}

/**
* @param Request $request
* @param FormInterface $form
* @param NodeType $nodetype
*
* @return null|Response
* @throws Exception
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
*/
protected function handleNodeForm(FormInterface $form, NodeType $nodetype): ?Response
protected function handleNodeForm(Request $request, FormInterface $form, NodeType $nodetype): ?Response
{
if ($form->isSubmitted() && $form->isValid()) {
$data = [];
Expand Down Expand Up @@ -416,8 +419,13 @@ protected function handleNodeForm(FormInterface $form, NodeType $nodetype): ?Res
*/
$button = $form->get('export');
if ($button instanceof ClickableInterface && $button->isClicked()) {
$filename = 'search-' . $nodetype->getName() . '-' . date("YmdHis") . '.xlsx';
$this->xlsxSerializer->setOnlyTexts(true);
$this->xlsxSerializer->addUrls();
$xlsx = $this->xlsxSerializer->serialize($entities);

$response = new Response(
$this->getXlsxResults($nodetype, $entities),
$xlsx,
Response::HTTP_OK,
[]
);
Expand All @@ -426,10 +434,11 @@ protected function handleNodeForm(FormInterface $form, NodeType $nodetype): ?Res
'Content-Disposition',
$response->headers->makeDisposition(
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
'search.xlsx'
$filename
)
);

$response->prepare($request);
return $response;
}

Expand All @@ -441,43 +450,6 @@ protected function handleNodeForm(FormInterface $form, NodeType $nodetype): ?Res
return null;
}

/**
* @param NodeType $nodetype
* @param array|IteratorAggregate $entities
*
* @return string
* @throws Exception
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
*/
protected function getXlsxResults(NodeType $nodetype, iterable $entities): string
{
$fields = $nodetype->getFields();
$keys = [];
$answers = [];
$keys[] = "title";
/** @var NodeTypeField $field */
foreach ($fields as $field) {
if (!$field->isVirtual() && !$field->isCollection()) {
$keys[] = $field->getName();
}
}
foreach ($entities as $idx => $nodesSource) {
$array = [];
foreach ($keys as $key) {
$getter = 'get' . str_replace('_', '', ucwords($key));
$tmp = $nodesSource->$getter();
if (is_array($tmp)) {
$tmp = implode(',', $tmp);
}
$array[] = $tmp;
}
$answers[$idx] = $array;
}

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

/**
* @param string $prefix
* @return FormBuilderInterface
Expand Down
25 changes: 6 additions & 19 deletions src/Explorer/ConfigurableExplorerItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,14 @@

final class ConfigurableExplorerItem extends AbstractExplorerItem
{
private PersistableInterface $entity;
private array $configuration;
private RendererInterface $renderer;
private DocumentUrlGeneratorInterface $documentUrlGenerator;
private UrlGeneratorInterface $urlGenerator;
private ?EmbedFinderFactory $embedFinderFactory;

public function __construct(
PersistableInterface $entity,
array &$configuration,
RendererInterface $renderer,
DocumentUrlGeneratorInterface $documentUrlGenerator,
UrlGeneratorInterface $urlGenerator,
?EmbedFinderFactory $embedFinderFactory = null
private readonly PersistableInterface $entity,
private readonly array &$configuration,
private readonly RendererInterface $renderer,
private readonly DocumentUrlGeneratorInterface $documentUrlGenerator,
private readonly UrlGeneratorInterface $urlGenerator,
private readonly ?EmbedFinderFactory $embedFinderFactory = null
) {
$this->entity = $entity;
$this->configuration = $configuration;
$this->renderer = $renderer;
$this->documentUrlGenerator = $documentUrlGenerator;
$this->urlGenerator = $urlGenerator;
$this->embedFinderFactory = $embedFinderFactory;
}

/**
Expand Down
11 changes: 4 additions & 7 deletions src/Explorer/FolderExplorerItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@

final class FolderExplorerItem extends AbstractExplorerItem
{
private Folder $folder;
private UrlGeneratorInterface $urlGenerator;

public function __construct(Folder $folder, UrlGeneratorInterface $urlGenerator)
{
$this->folder = $folder;
$this->urlGenerator = $urlGenerator;
public function __construct(
private readonly Folder $folder,
private readonly UrlGeneratorInterface $urlGenerator
) {
}

/**
Expand Down
Loading

0 comments on commit 3b7a121

Please sign in to comment.