From 6790fa2272638ee403b5a304db9d9a70c6c6e904 Mon Sep 17 00:00:00 2001 From: LucileDT Date: Sun, 13 Dec 2020 20:25:19 +0100 Subject: [PATCH] Ref #215: show bank selector in donation form when payment type is check --- public/javascript/Donation/common.js | 83 ++++++++++++++++++++++++ src/Form/DonationType.php | 34 +++++++++- src/FormDataObject/UpdateDonationFDO.php | 27 ++++++++ templates/Donation/edit.html.twig | 4 ++ templates/Donation/new.html.twig | 4 ++ 5 files changed, 150 insertions(+), 2 deletions(-) create mode 100644 public/javascript/Donation/common.js diff --git a/public/javascript/Donation/common.js b/public/javascript/Donation/common.js new file mode 100644 index 00000000..2cc298c0 --- /dev/null +++ b/public/javascript/Donation/common.js @@ -0,0 +1,83 @@ +/////////////////////////////////// +// -- Document ready listener -- // +/////////////////////////////////// +$(document).ready(function() { + // Show bank select if payment type needs it + let isBankNeeded = $("option:selected", '#donation_payment_type').data('is-bank-needed') === undefined ? false : true; + console.debug(isBankNeeded); + if (isBankNeeded) + { + removeDisplayNone('payment-bank'); + } + else + { + addDisplayNone('payment-bank'); + } + + // Show/hide bank select picker depending on payment type + $('#donation_payment_type').change(function(event) { + let isBankNeeded = $("option:selected", this).data('is-bank-needed') === undefined ? false : true; + if (isBankNeeded) + { + removeDisplayNone('payment-bank'); + } + else + { + addDisplayNone('payment-bank'); + } + }); +}); + +////////////////////////////////// +// -- Functions declarations -- // +////////////////////////////////// + +/** + * Add or remove the d-none class to an element + * Depending on if it has it or not + * + * @param {string} elementId The DOM element's id property + */ +function toggleDisplayNone(elementId) +{ + let element = $('#' + elementId); + + if (element.hasClass('d-none')) + { + element.removeClass('d-none'); + } + else + { + element.addClass('d-none'); + } +} + +/** + * Add the d-none class to an element if it doesn't have it already. + * + * @param {string} elementId The DOM element's id property + */ +function addDisplayNone(elementId) +{ + let element = $('#' + elementId); + + if (!element.hasClass('d-none')) + { + element.addClass('d-none'); + } +} + +/** + * Remove the d-none class to an element if it already has it. + * + * @param {string} elementId The DOM element's id property + */ +function removeDisplayNone(elementId) +{ + let element = $('#' + elementId); + + if (element.hasClass('d-none')) + { + element.removeClass('d-none'); + } +} \ No newline at end of file diff --git a/src/Form/DonationType.php b/src/Form/DonationType.php index 98f1d26d..7cccd3a1 100644 --- a/src/Form/DonationType.php +++ b/src/Form/DonationType.php @@ -2,6 +2,7 @@ namespace App\Form; +use App\Entity\Bank; use App\Entity\Donation; use App\Entity\People; use App\Entity\Payment; @@ -13,9 +14,16 @@ use Symfony\Component\Form\Extension\Core\Type\TextareaType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; +use Symfony\Contracts\Translation\TranslatorInterface; class DonationType extends AbstractType { + public $translator; + + public function __construct(TranslatorInterface $translator) { + $this->translator = $translator; + } + public function buildForm(FormBuilderInterface $builder, array $options) { $builder @@ -23,12 +31,22 @@ public function buildForm(FormBuilderInterface $builder, array $options) 'attr' => ['placeholder' => '15,50'], ]) ->add('payment_type', EntityType::class, [ - // looks for choices from this entity - 'class' => PaymentType::class, + 'label' => $this->translator->trans('Via'), // uses the label property as the visible option string 'choice_label' => 'label', + // looks for choices from this entity + 'class' => PaymentType::class, 'multiple' => false, 'expanded' => false, + 'attr' => [ + 'autocomplete' => 'off', + ], + 'choice_attr' => function(PaymentType $paymentType) + { + return [ + 'data-is-bank-needed' => $paymentType->isBankneeded(), + ]; + }, ]) ->add('donator', EntityType::class, [ // looks for choices from this entity @@ -54,6 +72,18 @@ public function buildForm(FormBuilderInterface $builder, array $options) 'required' => false, ]) ; + $builder->add('bank', EntityType::class, [ + 'class' => Bank::class, + 'choice_label' => 'name', + 'multiple' => false, + 'expanded' => false, + 'required' => false, + 'label' => $this->translator->trans('Banque'), + 'attr' => [ + 'data-toggle' => 'select2', + ], + 'placeholder' => $this->translator->trans('Sélectionnez une banque'), + ]); } public function configureOptions(OptionsResolver $resolver) diff --git a/src/FormDataObject/UpdateDonationFDO.php b/src/FormDataObject/UpdateDonationFDO.php index 4e50e21a..b7b4a4ca 100644 --- a/src/FormDataObject/UpdateDonationFDO.php +++ b/src/FormDataObject/UpdateDonationFDO.php @@ -3,6 +3,7 @@ namespace App\FormDataObject; use Symfony\Component\Validator\Constraints as Assert; +use \App\Entity\Bank; use \App\Entity\Donation; use \App\Entity\Payment; @@ -23,6 +24,8 @@ class UpdateDonationFDO private $comment; + private $bank; + public function __construct(Donation $donation = null) { if ($donation !== null) @@ -35,6 +38,7 @@ public function __construct(Donation $donation = null) $this->paymentType = $payment->getType(); $this->cashedDate = $payment->getDateCashed(); $this->comment = $payment->getComment(); + $this->bank = $payment->getBank(); } } @@ -103,4 +107,27 @@ function setComment($comment): self $this->comment = $comment; return $this; } + + /** + * Return donation payment bank + * + * @return Bank + */ + function getBank(): ?Bank + { + return $this->bank; + } + + /** + * Set donation payment bank + * + * @param Bank $bank + * @return \self + */ + function setBank(?Bank $bank): self + { + $this->bank = $bank; + + return $this; + } } diff --git a/templates/Donation/edit.html.twig b/templates/Donation/edit.html.twig index a9026529..e3a66c25 100644 --- a/templates/Donation/edit.html.twig +++ b/templates/Donation/edit.html.twig @@ -22,6 +22,7 @@ {% endblock %} {% block javascript %} + {% endblock %} @@ -81,6 +82,9 @@
{{ form_errors(form.payment_type) }}
+
+ {{ form_row(form.bank)}} +
diff --git a/templates/Donation/new.html.twig b/templates/Donation/new.html.twig index 6dccca9c..f0b1b2f9 100644 --- a/templates/Donation/new.html.twig +++ b/templates/Donation/new.html.twig @@ -15,6 +15,7 @@ {% endblock %} {% block javascript %} + {% endblock %} @@ -62,6 +63,9 @@
{{ form_errors(form.payment_type) }}
+
+ {{ form_row(form.bank)}} +