Skip to content

Commit

Permalink
Allow users to save a new label profiles directly from the label gene…
Browse files Browse the repository at this point in the history
…rator dialog

This fixes issue #806
  • Loading branch information
jbtronics committed Jan 5, 2025
1 parent 9d09543 commit f75704f
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/Controller/LabelController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {

Check failure on line 114 in src/Controller/LabelController.php

View workflow job for this annotation

GitHub Actions / Static analysis

Call to an undefined method Symfony\Component\Form\FormInterface::isClicked().
//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 !== []) {
Expand All @@ -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,
Expand Down
16 changes: 16 additions & 0 deletions src/Form/LabelSystem/LabelDialogType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
]);
Expand Down
11 changes: 11 additions & 0 deletions templates/label_system/dialog.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,17 @@
</div>
</div>
{% endif %}

<div class="form-group row">
<div class="offset-sm-3 col-sm-9">
<div class="input-group">
{{ form_widget(form.save_profile_name) }}
{{ form_widget(form.save_profile) }}
</div>
{{ form_errors(form.save_profile_name) }}
</div>
</div>

</div>
</div>

Expand Down
24 changes: 24 additions & 0 deletions translations/messages.en.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -12299,5 +12299,29 @@ Please note, that you can not impersonate a disabled user. If you try you will g
<target>Edit profiles</target>
</segment>
</unit>
<unit id="JzfeFN6" name="label_generator.profile_name_empty">
<segment>
<source>label_generator.profile_name_empty</source>
<target>Profile name must not be empty!</target>
</segment>
</unit>
<unit id="0TP6The" name="label_generator.save_profile_name">
<segment>
<source>label_generator.save_profile_name</source>
<target>Profile name</target>
</segment>
</unit>
<unit id="OhUW6es" name="label_generator.save_profile">
<segment>
<source>label_generator.save_profile</source>
<target>Save as new profile</target>
</segment>
</unit>
<unit id="sqc4h7S" name="label_generator.profile_saved">
<segment>
<source>label_generator.profile_saved</source>
<target>Profile saved!</target>
</segment>
</unit>
</file>
</xliff>

0 comments on commit f75704f

Please sign in to comment.