From e9aa0ba826244024426a0484a4e19a51008cbda2 Mon Sep 17 00:00:00 2001 From: Ambroise Maupate Date: Mon, 20 Nov 2023 14:58:21 +0100 Subject: [PATCH 1/2] fix: Fixed missing entityManager flush to remove custom-form answer. --- .../CustomFormAnswersController.php | 67 ++--- .../CustomFormFieldAttributesController.php | 14 +- .../CustomFormFieldsController.php | 234 +++++++++--------- .../CustomFormsUtilsController.php | 9 +- 4 files changed, 165 insertions(+), 159 deletions(-) diff --git a/lib/Rozier/src/Controllers/CustomForms/CustomFormAnswersController.php b/lib/Rozier/src/Controllers/CustomForms/CustomFormAnswersController.php index 092bf1fb..6a015479 100644 --- a/lib/Rozier/src/Controllers/CustomForms/CustomFormAnswersController.php +++ b/lib/Rozier/src/Controllers/CustomForms/CustomFormAnswersController.php @@ -14,6 +14,7 @@ use Symfony\Component\Validator\Constraints\NotBlank; use Symfony\Component\Validator\Constraints\NotNull; use Themes\Rozier\RozierApp; +use Twig\Error\RuntimeError; /** * @package Themes\Rozier\Controllers @@ -24,11 +25,12 @@ class CustomFormAnswersController extends RozierApp * List every node-types. * * @param Request $request - * @param int $customFormId + * @param int $customFormId * * @return Response + * @throws RuntimeError */ - public function listAction(Request $request, int $customFormId) + public function listAction(Request $request, int $customFormId): Response { $this->denyAccessUnlessGranted('ROLE_ACCESS_CUSTOMFORMS'); /* @@ -55,50 +57,49 @@ public function listAction(Request $request, int $customFormId) } /** - * Return an deletion form for requested node-type. + * Return a deletion form for requested node-type. * * @param Request $request * @param int $customFormAnswerId * * @return Response + * @throws RuntimeError */ - public function deleteAction(Request $request, int $customFormAnswerId) + public function deleteAction(Request $request, int $customFormAnswerId): Response { $this->denyAccessUnlessGranted('ROLE_ACCESS_CUSTOMFORMS_DELETE'); $customFormAnswer = $this->em()->find(CustomFormAnswer::class, $customFormAnswerId); - if (null !== $customFormAnswer) { - $this->assignation['customFormAnswer'] = $customFormAnswer; - - $form = $this->buildDeleteForm($customFormAnswer); - - $form->handleRequest($request); - - if ( - $form->isSubmitted() && - $form->isValid() && - $form->getData()['customFormAnswerId'] == $customFormAnswer->getId() - ) { - $this->em()->remove($customFormAnswer); - - $msg = $this->getTranslator()->trans('customFormAnswer.%id%.deleted', ['%id%' => $customFormAnswer->getId()]); - $this->publishConfirmMessage($request, $msg); - /* - * Redirect to update schema page - */ - return $this->redirectToRoute( - 'customFormAnswersHomePage', - ["customFormId" => $customFormAnswer->getCustomForm()->getId()] - ); - } - - $this->assignation['form'] = $form->createView(); + if (null === $customFormAnswer) { + throw new ResourceNotFoundException(); + } - return $this->render('@RoadizRozier/custom-form-answers/delete.html.twig', $this->assignation); + $this->assignation['customFormAnswer'] = $customFormAnswer; + $form = $this->buildDeleteForm($customFormAnswer); + $form->handleRequest($request); + + if ( + $form->isSubmitted() && + $form->isValid() + ) { + $this->em()->remove($customFormAnswer); + $this->em()->flush(); + + $msg = $this->getTranslator()->trans('customFormAnswer.%id%.deleted', ['%id%' => $customFormAnswer->getId()]); + $this->publishConfirmMessage($request, $msg); + /* + * Redirect to update schema page + */ + return $this->redirectToRoute( + 'customFormAnswersHomePage', + ["customFormId" => $customFormAnswer->getCustomForm()->getId()] + ); } - throw new ResourceNotFoundException(); + $this->assignation['form'] = $form->createView(); + + return $this->render('@RoadizRozier/custom-form-answers/delete.html.twig', $this->assignation); } /** @@ -106,7 +107,7 @@ public function deleteAction(Request $request, int $customFormAnswerId) * * @return FormInterface */ - private function buildDeleteForm(CustomFormAnswer $customFormAnswer) + private function buildDeleteForm(CustomFormAnswer $customFormAnswer): FormInterface { $builder = $this->createFormBuilder() ->add('customFormAnswerId', HiddenType::class, [ diff --git a/lib/Rozier/src/Controllers/CustomForms/CustomFormFieldAttributesController.php b/lib/Rozier/src/Controllers/CustomForms/CustomFormFieldAttributesController.php index 3562e29d..a6fac366 100644 --- a/lib/Rozier/src/Controllers/CustomForms/CustomFormFieldAttributesController.php +++ b/lib/Rozier/src/Controllers/CustomForms/CustomFormFieldAttributesController.php @@ -4,11 +4,12 @@ namespace Themes\Rozier\Controllers\CustomForms; -use Doctrine\Common\Collections\Collection; use RZ\Roadiz\CoreBundle\Entity\CustomFormAnswer; use RZ\Roadiz\CoreBundle\Entity\CustomFormFieldAttribute; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; use Themes\Rozier\RozierApp; +use Twig\Error\RuntimeError; /** * @package Themes\Rozier\Controllers @@ -19,11 +20,12 @@ class CustomFormFieldAttributesController extends RozierApp * List every node-types. * * @param Request $request - * @param int $customFormAnswerId + * @param int $customFormAnswerId * - * @return \Symfony\Component\HttpFoundation\Response + * @return Response + * @throws RuntimeError */ - public function listAction(Request $request, int $customFormAnswerId) + public function listAction(Request $request, int $customFormAnswerId): Response { $this->denyAccessUnlessGranted('ROLE_ACCESS_CUSTOMFORMS'); /* @@ -42,10 +44,10 @@ public function listAction(Request $request, int $customFormAnswerId) } /** - * @param Collection|array $answers + * @param iterable $answers * @return array */ - protected function getAnswersByGroups($answers) + protected function getAnswersByGroups(iterable $answers): array { $fieldsArray = []; diff --git a/lib/Rozier/src/Controllers/CustomForms/CustomFormFieldsController.php b/lib/Rozier/src/Controllers/CustomForms/CustomFormFieldsController.php index d0a203bd..0897579d 100644 --- a/lib/Rozier/src/Controllers/CustomForms/CustomFormFieldsController.php +++ b/lib/Rozier/src/Controllers/CustomForms/CustomFormFieldsController.php @@ -16,6 +16,7 @@ use Symfony\Component\Validator\Constraints\NotNull; use Themes\Rozier\Forms\CustomFormFieldType; use Themes\Rozier\RozierApp; +use Twig\Error\RuntimeError; /** * @package Themes\Rozier\Controllers @@ -25,12 +26,12 @@ class CustomFormFieldsController extends RozierApp /** * List every node-type-fields. * - * @param Request $request - * @param int $customFormId + * @param int $customFormId * * @return Response + * @throws RuntimeError */ - public function listAction(Request $request, int $customFormId) + public function listAction(int $customFormId): Response { $this->denyAccessUnlessGranted('ROLE_ACCESS_CUSTOMFORMS'); @@ -52,57 +53,58 @@ public function listAction(Request $request, int $customFormId) * Return an edition form for requested node-type. * * @param Request $request - * @param int $customFormFieldId + * @param int $customFormFieldId * * @return Response + * @throws RuntimeError */ - public function editAction(Request $request, int $customFormFieldId) + public function editAction(Request $request, int $customFormFieldId): Response { $this->denyAccessUnlessGranted('ROLE_ACCESS_CUSTOMFORMS'); /** @var CustomFormField|null $field */ $field = $this->em()->find(CustomFormField::class, $customFormFieldId); + if ($field === null) { + throw new ResourceNotFoundException(); + } - if ($field !== null) { - $this->assignation['customForm'] = $field->getCustomForm(); - $this->assignation['field'] = $field; - $form = $this->createForm(CustomFormFieldType::class, $field); - $form->handleRequest($request); - - if ($form->isSubmitted() && $form->isValid()) { - $this->em()->flush(); - - $msg = $this->getTranslator()->trans('customFormField.%name%.updated', ['%name%' => $field->getName()]); - $this->publishConfirmMessage($request, $msg); - - /* - * Redirect to update schema page - */ - return $this->redirectToRoute( - 'customFormFieldsListPage', - [ - 'customFormId' => $field->getCustomForm()->getId(), - ] - ); - } - - $this->assignation['form'] = $form->createView(); - - return $this->render('@RoadizRozier/custom-form-fields/edit.html.twig', $this->assignation); + $this->assignation['customForm'] = $field->getCustomForm(); + $this->assignation['field'] = $field; + $form = $this->createForm(CustomFormFieldType::class, $field); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $this->em()->flush(); + + $msg = $this->getTranslator()->trans('customFormField.%name%.updated', ['%name%' => $field->getName()]); + $this->publishConfirmMessage($request, $msg); + + /* + * Redirect to update schema page + */ + return $this->redirectToRoute( + 'customFormFieldsListPage', + [ + 'customFormId' => $field->getCustomForm()->getId(), + ] + ); } - throw new ResourceNotFoundException(); + $this->assignation['form'] = $form->createView(); + + return $this->render('@RoadizRozier/custom-form-fields/edit.html.twig', $this->assignation); } /** - * Return an creation form for requested node-type. + * Return a creation form for requested node-type. * * @param Request $request - * @param int $customFormId + * @param int $customFormId * * @return Response + * @throws RuntimeError */ - public function addAction(Request $request, int $customFormId) + public function addAction(Request $request, int $customFormId): Response { $this->denyAccessUnlessGranted('ROLE_ACCESS_CUSTOMFORMS'); @@ -110,90 +112,22 @@ public function addAction(Request $request, int $customFormId) $customForm = $this->em()->find(CustomForm::class, $customFormId); $field->setCustomForm($customForm); - if ( - $customForm !== null && - $field !== null - ) { - $this->assignation['customForm'] = $customForm; - $this->assignation['field'] = $field; - $form = $this->createForm(CustomFormFieldType::class, $field); - $form->handleRequest($request); - - if ($form->isSubmitted() && $form->isValid()) { - try { - $this->em()->persist($field); - $this->em()->flush(); - - $msg = $this->getTranslator()->trans( - 'customFormField.%name%.created', - ['%name%' => $field->getName()] - ); - $this->publishConfirmMessage($request, $msg); - - /* - * Redirect to update schema page - */ - return $this->redirectToRoute( - 'customFormFieldsListPage', - [ - 'customFormId' => $customFormId, - ] - ); - } catch (Exception $e) { - $msg = $e->getMessage(); - $this->publishErrorMessage($request, $msg); - /* - * Redirect to add page - */ - return $this->redirectToRoute( - 'customFormFieldsAddPage', - ['customFormId' => $customFormId] - ); - } - } - - $this->assignation['form'] = $form->createView(); - - return $this->render('@RoadizRozier/custom-form-fields/add.html.twig', $this->assignation); + if ($customForm === null) { + throw new ResourceNotFoundException(); } - throw new ResourceNotFoundException(); - } - - /** - * Return a deletion form for requested node. - * - * @param Request $request - * @param int $customFormFieldId - * - * @return Response - */ - public function deleteAction(Request $request, int $customFormFieldId) - { - $this->denyAccessUnlessGranted('ROLE_ACCESS_CUSTOMFORMS_DELETE'); - - $field = $this->em()->find(CustomFormField::class, $customFormFieldId); + $this->assignation['customForm'] = $customForm; + $this->assignation['field'] = $field; + $form = $this->createForm(CustomFormFieldType::class, $field); + $form->handleRequest($request); - if ($field !== null) { - $this->assignation['field'] = $field; - $form = $this->buildDeleteForm($field); - $form->handleRequest($request); - - if ( - $form->isSubmitted() && - $form->isValid() && - $form->getData()['customFormFieldId'] == $field->getId() - ) { - $customFormId = $field->getCustomForm()->getId(); - - $this->em()->remove($field); + if ($form->isSubmitted() && $form->isValid()) { + try { + $this->em()->persist($field); $this->em()->flush(); - /* - * Update Database - */ $msg = $this->getTranslator()->trans( - 'customFormField.%name%.deleted', + 'customFormField.%name%.created', ['%name%' => $field->getName()] ); $this->publishConfirmMessage($request, $msg); @@ -207,14 +141,80 @@ public function deleteAction(Request $request, int $customFormFieldId) 'customFormId' => $customFormId, ] ); + } catch (Exception $e) { + $msg = $e->getMessage(); + $this->publishErrorMessage($request, $msg); + /* + * Redirect to add page + */ + return $this->redirectToRoute( + 'customFormFieldsAddPage', + ['customFormId' => $customFormId] + ); } + } - $this->assignation['form'] = $form->createView(); + $this->assignation['form'] = $form->createView(); - return $this->render('@RoadizRozier/custom-form-fields/delete.html.twig', $this->assignation); + return $this->render('@RoadizRozier/custom-form-fields/add.html.twig', $this->assignation); + } + + /** + * Return a deletion form for requested node. + * + * @param Request $request + * @param int $customFormFieldId + * + * @return Response + * @throws RuntimeError + */ + public function deleteAction(Request $request, int $customFormFieldId): Response + { + $this->denyAccessUnlessGranted('ROLE_ACCESS_CUSTOMFORMS_DELETE'); + + $field = $this->em()->find(CustomFormField::class, $customFormFieldId); + + if ($field === null) { + throw new ResourceNotFoundException(); } - throw new ResourceNotFoundException(); + $this->assignation['field'] = $field; + $form = $this->buildDeleteForm($field); + $form->handleRequest($request); + + if ( + $form->isSubmitted() && + $form->isValid() && + $form->getData()['customFormFieldId'] == $field->getId() + ) { + $customFormId = $field->getCustomForm()->getId(); + + $this->em()->remove($field); + $this->em()->flush(); + + /* + * Update Database + */ + $msg = $this->getTranslator()->trans( + 'customFormField.%name%.deleted', + ['%name%' => $field->getName()] + ); + $this->publishConfirmMessage($request, $msg); + + /* + * Redirect to update schema page + */ + return $this->redirectToRoute( + 'customFormFieldsListPage', + [ + 'customFormId' => $customFormId, + ] + ); + } + + $this->assignation['form'] = $form->createView(); + + return $this->render('@RoadizRozier/custom-form-fields/delete.html.twig', $this->assignation); } /** @@ -222,7 +222,7 @@ public function deleteAction(Request $request, int $customFormFieldId) * * @return FormInterface */ - private function buildDeleteForm(CustomFormField $field) + private function buildDeleteForm(CustomFormField $field): FormInterface { $builder = $this->createFormBuilder() ->add('customFormFieldId', HiddenType::class, [ diff --git a/lib/Rozier/src/Controllers/CustomForms/CustomFormsUtilsController.php b/lib/Rozier/src/Controllers/CustomForms/CustomFormsUtilsController.php index fc014c56..ba24ba04 100644 --- a/lib/Rozier/src/Controllers/CustomForms/CustomFormsUtilsController.php +++ b/lib/Rozier/src/Controllers/CustomForms/CustomFormsUtilsController.php @@ -4,6 +4,7 @@ namespace Themes\Rozier\Controllers\CustomForms; +use PhpOffice\PhpSpreadsheet\Exception; use RZ\Roadiz\CoreBundle\Entity\CustomForm; use RZ\Roadiz\CoreBundle\Entity\CustomFormAnswer; use RZ\Roadiz\CoreBundle\CustomForm\CustomFormAnswerSerializer; @@ -32,11 +33,13 @@ public function __construct(CustomFormAnswerSerializer $customFormAnswerSerializ * Export all custom form's answer in a Xlsx file (.rzt). * * @param Request $request - * @param int $id + * @param int $id * * @return Response + * @throws Exception + * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception */ - public function exportAction(Request $request, int $id) + public function exportAction(Request $request, int $id): Response { /** @var CustomForm|null $customForm */ $customForm = $this->em()->find(CustomForm::class, $id); @@ -93,7 +96,7 @@ public function exportAction(Request $request, int $id) * * @return Response */ - public function duplicateAction(Request $request, int $id) + public function duplicateAction(Request $request, int $id): Response { $this->denyAccessUnlessGranted('ROLE_ACCESS_CUSTOMFORMS'); /** @var CustomForm|null $existingCustomForm */ From d7703fb4ef3758c0e980215f0d199e2d7307c9c3 Mon Sep 17 00:00:00 2001 From: Ambroise Maupate Date: Mon, 20 Nov 2023 14:59:21 +0100 Subject: [PATCH 2/2] chore: Bumped --- CHANGELOG.md | 7 +++++++ lib/RoadizCoreBundle/config/services.yaml | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e3e1ad67..9c7e7963 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## [v2.1.51](https://github.com/roadiz/core-bundle-dev-app/compare/v2.1.50...v2.1.51) (2023-11-20) + + +### Bug Fixes + +* Fixed missing entityManager flush to remove custom-form answer. ([e9aa0ba](https://github.com/roadiz/core-bundle-dev-app/commit/e9aa0ba826244024426a0484a4e19a51008cbda2)) + ## [v2.1.50](https://github.com/roadiz/core-bundle-dev-app/compare/v2.1.49...v2.1.50) (2023-11-16) diff --git a/lib/RoadizCoreBundle/config/services.yaml b/lib/RoadizCoreBundle/config/services.yaml index 84826e74..c41b3b48 100644 --- a/lib/RoadizCoreBundle/config/services.yaml +++ b/lib/RoadizCoreBundle/config/services.yaml @@ -1,6 +1,6 @@ --- parameters: - roadiz_core.cms_version: '2.1.50' + roadiz_core.cms_version: '2.1.51' roadiz_core.cms_version_prefix: 'main' env(APP_NAMESPACE): "roadiz" env(APP_VERSION): "0.1.0"