From f75704f77c75a55550c1fd0d85d8d9e6e30c9379 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Sun, 5 Jan 2025 22:00:07 +0100 Subject: [PATCH] Allow users to save a new label profiles directly from the label generator dialog This fixes issue #806 --- src/Controller/LabelController.php | 26 +++++++++++++++++++++++- src/Form/LabelSystem/LabelDialogType.php | 16 +++++++++++++++ templates/label_system/dialog.html.twig | 11 ++++++++++ translations/messages.en.xlf | 24 ++++++++++++++++++++++ 4 files changed, 76 insertions(+), 1 deletion(-) diff --git a/src/Controller/LabelController.php b/src/Controller/LabelController.php index 09bec781..b3d9e62f 100644 --- a/src/Controller/LabelController.php +++ b/src/Controller/LabelController.php @@ -108,8 +108,31 @@ public function generator(Request $request, ?LabelProfile $profile = null): Resp $pdf_data = null; $filename = 'invalid.pdf'; - //Generate PDF either when the form is submitted and valid, or the form was not submit yet, and generate is set if (($form->isSubmitted() && $form->isValid()) || ($generate && !$form->isSubmitted() && $profile instanceof LabelProfile)) { + + //Check if the label should be saved as profile + if ($form->get('save_profile')->isClicked() && $this->isGranted('@labels.create_profiles')) { + //Retrieve the profile name from the form + $new_name = $form->get('save_profile_name')->getData(); + //ensure that the name is not empty + if ($new_name === '' || $new_name === null) { + $form->get('save_profile_name')->addError(new FormError($this->translator->trans('label_generator.profile_name_empty'))); + goto render; + } + + $profile = new LabelProfile(); + $profile->setName($form->get('save_profile_name')->getData()); + $profile->setOptions($form_options); + $this->em->persist($profile); + $this->em->flush(); + $this->addFlash('success', 'label_generator.profile_saved'); + + return $this->redirectToRoute('label_dialog_profile', [ + 'profile' => $profile->getID(), + 'target_id' => (string) $form->get('target_id')->getData() + ]); + } + $target_id = (string) $form->get('target_id')->getData(); $targets = $this->findObjects($form_options->getSupportedElement(), $target_id); if ($targets !== []) { @@ -132,6 +155,7 @@ public function generator(Request $request, ?LabelProfile $profile = null): Resp } } + render: return $this->render('label_system/dialog.html.twig', [ 'form' => $form, 'pdf_data' => $pdf_data, diff --git a/src/Form/LabelSystem/LabelDialogType.php b/src/Form/LabelSystem/LabelDialogType.php index 33c79797..f2710b19 100644 --- a/src/Form/LabelSystem/LabelDialogType.php +++ b/src/Form/LabelSystem/LabelDialogType.php @@ -71,6 +71,22 @@ public function buildForm(FormBuilderInterface $builder, array $options): void 'label' => false, 'disabled' => !$this->security->isGranted('@labels.edit_options') || $options['disable_options'], ]); + + $builder->add('save_profile_name', TextType::class, [ + 'required' => false, + 'attr' =>[ + 'placeholder' => 'label_generator.save_profile_name', + ] + ]); + + $builder->add('save_profile', SubmitType::class, [ + 'label' => 'label_generator.save_profile', + 'disabled' => !$this->security->isGranted('@labels.create_profiles'), + 'attr' => [ + 'class' => 'btn btn-outline-success' + ] + ]); + $builder->add('update', SubmitType::class, [ 'label' => 'label_generator.update', ]); diff --git a/templates/label_system/dialog.html.twig b/templates/label_system/dialog.html.twig index b763a467..50db99e7 100644 --- a/templates/label_system/dialog.html.twig +++ b/templates/label_system/dialog.html.twig @@ -99,6 +99,17 @@ {% endif %} + +
+
+
+ {{ form_widget(form.save_profile_name) }} + {{ form_widget(form.save_profile) }} +
+ {{ form_errors(form.save_profile_name) }} +
+
+ diff --git a/translations/messages.en.xlf b/translations/messages.en.xlf index a3eea465..6e4c47cb 100644 --- a/translations/messages.en.xlf +++ b/translations/messages.en.xlf @@ -12299,5 +12299,29 @@ Please note, that you can not impersonate a disabled user. If you try you will g Edit profiles + + + label_generator.profile_name_empty + Profile name must not be empty! + + + + + label_generator.save_profile_name + Profile name + + + + + label_generator.save_profile + Save as new profile + + + + + label_generator.profile_saved + Profile saved! + +