Skip to content

Commit

Permalink
Ref #215: show bank selector in donation form when payment type is check
Browse files Browse the repository at this point in the history
  • Loading branch information
LucileDT committed Dec 13, 2020
1 parent 629a710 commit 6790fa2
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 2 deletions.
83 changes: 83 additions & 0 deletions public/javascript/Donation/common.js
Original file line number Diff line number Diff line change
@@ -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');
}
}
34 changes: 32 additions & 2 deletions src/Form/DonationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Form;

use App\Entity\Bank;
use App\Entity\Donation;
use App\Entity\People;
use App\Entity\Payment;
Expand All @@ -13,22 +14,39 @@
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
->add('amount', MoneyType::class, [
'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
Expand All @@ -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)
Expand Down
27 changes: 27 additions & 0 deletions src/FormDataObject/UpdateDonationFDO.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -23,6 +24,8 @@ class UpdateDonationFDO

private $comment;

private $bank;

public function __construct(Donation $donation = null)
{
if ($donation !== null)
Expand All @@ -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();
}
}

Expand Down Expand Up @@ -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;
}
}
4 changes: 4 additions & 0 deletions templates/Donation/edit.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
{% endblock %}

{% block javascript %}
<script src="{{ asset('javascript/Donation/common.js') }}"></script>
<script src="{{ asset('javascript/Donation/edit.js') }}"></script>
{% endblock %}

Expand Down Expand Up @@ -81,6 +82,9 @@
<div class="field-error-spot">
{{ form_errors(form.payment_type) }}
</div>
<div id="payment-bank" class="form-group d-none">
{{ form_row(form.bank)}}
</div>
</div>
<div class="col-12 mt-3">
<label for="donation_cashed_date">{% trans %}Paiment encaissé le{% endtrans %}</label>
Expand Down
4 changes: 4 additions & 0 deletions templates/Donation/new.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
{% endblock %}

{% block javascript %}
<script src="{{ asset('javascript/Donation/common.js') }}"></script>
<script src="{{ asset('javascript/Donation/edit.js') }}"></script>
{% endblock %}

Expand Down Expand Up @@ -62,6 +63,9 @@
<div class="field-error-spot">
{{ form_errors(form.payment_type) }}
</div>
<div id="payment-bank" class="form-group d-none">
{{ form_row(form.bank)}}
</div>
</div>
<div class="col-12 mt-3">
<label for="donation_cashed_date">{% trans %}Paiment encaissé le{% endtrans %}</label>
Expand Down

0 comments on commit 6790fa2

Please sign in to comment.