fixes on address valid from edit for household

This commit is contained in:
Julien Fastré 2022-04-14 00:02:52 +02:00
parent 405694a0b4
commit 86ec020f80
6 changed files with 67 additions and 58 deletions

View File

@ -0,0 +1,29 @@
<?php
namespace Chill\MainBundle\Form\Type;
use Chill\MainBundle\Entity\Address;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class AddressDateType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add(
'validFrom',
ChillDateType::class,
[
'required' => true,
]
);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefault('data_class', Address::class);
}
}

View File

@ -12,14 +12,17 @@ declare(strict_types=1);
namespace Chill\PersonBundle\Controller; namespace Chill\PersonBundle\Controller;
use Chill\MainBundle\Entity\Address; use Chill\MainBundle\Entity\Address;
use Chill\MainBundle\Form\Type\AddressDateType;
use Chill\MainBundle\Form\Type\AddressType; use Chill\MainBundle\Form\Type\AddressType;
use Chill\PersonBundle\Entity\Household\Household; use Chill\PersonBundle\Entity\Household\Household;
use Chill\PersonBundle\Form\HouseholdType; use Chill\PersonBundle\Form\HouseholdType;
use Chill\PersonBundle\Repository\Household\PositionRepository; use Chill\PersonBundle\Repository\Household\PositionRepository;
use Chill\PersonBundle\Security\Authorization\AccompanyingPeriodVoter; use Chill\PersonBundle\Security\Authorization\AccompanyingPeriodVoter;
use Chill\PersonBundle\Security\Authorization\HouseholdVoter;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Security; use Symfony\Component\Security\Core\Security;
@ -197,41 +200,35 @@ class HouseholdController extends AbstractController
*/ */
public function addressValidFromEdit(Request $request, Household $household) public function addressValidFromEdit(Request $request, Household $household)
{ {
// TODO ACL $this->denyAccessUnlessGranted(HouseholdVoter::EDIT, $household);
$address_id = $request->query->get('address_id'); if (!$request->query->has('address_id')) {
$address = $this->getDoctrine()->getManager() throw new BadRequestException('parameter address_id is missing');
->getRepository(Address::class) }
->find($address_id);
$form = $this->createForm(AddressType::class, $address, []); $address_id = $request->query->getInt('address_id');
$form->handleRequest($request);
$addresses = $household->getAddressesOrdered(); // loop over adresses of the household, to be sure that the household is associated
// to the edited address
foreach ($addresses as $a) { $address = null;
if ($a->getId() === $address->getId()) { foreach ($household->getAddresses() as $householdAddress) {
$currentValueIndex = array_search($a, $addresses, true); if ($address_id === $householdAddress->getId()) {
$address = $householdAddress;
} }
} }
if (count($addresses) > 1 && 0 < $currentValueIndex) { if (null === $address) {
$previousAddress = $addresses[$currentValueIndex - 1]; throw new BadRequestException('The edited address does not belongs to the household');
$minValidFrom = $previousAddress->getValidTo();
} else {
$minValidFrom = null;
} }
if (count($addresses) > 1 && count($addresses) > $currentValueIndex + 1) { $form = $this->createForm(AddressDateType::class, $address, []);
$nextAddress = $addresses[$currentValueIndex + 1];
$maxValidFrom = $nextAddress->getValidFrom(); $form->handleRequest($request);
} else {
$maxValidFrom = null;
}
if ($form->isSubmitted() && $form->isValid()) { if ($form->isSubmitted() && $form->isValid()) {
$this->getDoctrine()->getManager()->persist($address); $household->makeAddressConsistent();
$this->getDoctrine()->getManager()->flush(); $this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('chill_person_household_addresses', [ return $this->redirectToRoute('chill_person_household_addresses', [
@ -245,8 +242,6 @@ class HouseholdController extends AbstractController
'household' => $household, 'household' => $household,
'address' => $address, 'address' => $address,
'form' => $form->createView(), 'form' => $form->createView(),
'minValidFrom' => $minValidFrom,
'maxValidFrom' => $maxValidFrom,
] ]
); );
} }

View File

@ -151,6 +151,9 @@ class Household
return $this->addresses; return $this->addresses;
} }
/**
* @return array|Address[]
*/
public function getAddressesOrdered(): array public function getAddressesOrdered(): array
{ {
$addresses = $this->getAddresses()->toArray(); $addresses = $this->getAddresses()->toArray();

View File

@ -13,26 +13,7 @@
<div> <div>
{% if minValidFrom is not null and maxValidFrom is not null %} {{ form_row(form.validFrom) }}
{{ form_row(form.validFrom, {'attr': {'min': minValidFrom|date('Y-m-d'), 'max': maxValidFrom|date('Y-m-d') }}) }}
{% else %}
{% if minValidFrom is not null %}
{{ form_row(form.validFrom, {'attr': {'min': minValidFrom|date('Y-m-d')}}) }}
{% elseif maxValidFrom is not null %}
{{ form_row(form.validFrom, {'attr': {'max': maxValidFrom|date('Y-m-d')}}) }}
{% else %}
{{ form_row(form.validFrom) }}
{% endif %}
{% endif %}
</div>
<div hidden>
{% if address.isNoAddress %}
{{ form_row(form.street, {'value': ' '})}}
{{ form_row(form.streetNumber, {'value': ' '})}}
{{ form_row(form.postCode)}}
{% else %}
{{ form_rest(form) }}
{% endif %}
</div> </div>
<ul class="record_actions sticky-form-buttons"> <ul class="record_actions sticky-form-buttons">

View File

@ -62,16 +62,17 @@
'extended_infos': true, 'extended_infos': true,
'has_no_address': true 'has_no_address': true
}) }} }) }}
<ul class="record_actions"> {% if is_granted('CHILL_PERSON_HOUSEHOLD_EDIT', household) %}
<li> <ul class="record_actions">
<a href="{{ path('chill_person_household_address_valid_from_edit', { 'household_id': household.id, 'address_id' : address.id } ) }}" <li>
class="btn btn-edit">{{ "edit address valid from"|trans }} <a href="{{ path('chill_person_household_address_valid_from_edit', { 'household_id': household.id, 'address_id' : address.id } ) }}"
</a> class="btn btn-edit">{{ "edit address valid from"|trans }}
<a href="{{ path('chill_person_household_address_edit', { 'household_id': household.id, 'address_id' : address.id } ) }}" </a>
class="btn btn-edit"> <a href="{{ path('chill_person_household_address_edit', { 'household_id': household.id, 'address_id' : address.id } ) }}"
</a> class="btn btn-edit"></a>
</li> </li>
</ul> </ul>
{% endif %}
</div> </div>
<div class="date"> <div class="date">

View File

@ -498,8 +498,8 @@ Concerns household n°%id%: Concerne le ménage n°%id%
Composition: Composition Composition: Composition
Budget: Budget Budget: Budget
The composition has been successfully removed.: La composition a été supprimée. The composition has been successfully removed.: La composition a été supprimée.
edit address valid from: Changer la date du déménagement edit address valid from: Modifier la date du déménagement
Edit household address valid from: Changer la date du déménagement Edit household address valid from: Modifier la date du déménagement
# accompanying course work # accompanying course work