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

View File

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

View File

@ -13,26 +13,7 @@
<div>
{% if minValidFrom is not null and maxValidFrom is not null %}
{{ 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 %}
{{ form_row(form.validFrom) }}
</div>
<ul class="record_actions sticky-form-buttons">

View File

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

View File

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