allow to update existing address

This commit is contained in:
Julien Fastré 2016-03-10 21:44:53 +01:00
parent afe6ace331
commit 91ed954be8
5 changed files with 202 additions and 0 deletions

View File

@ -0,0 +1,151 @@
<?php
/*
* Chill is a software for social workers
*
* Copyright (C) 2016, Champs Libres Cooperative SCRLFS,
* <http://www.champs-libres.coop>, <info@champs-libres.coop>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Chill\PersonBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Chill\PersonBundle\Entity\Person;
use Chill\MainBundle\Form\Type\AddressType;
use Chill\MainBundle\Entity\Address;
use Doctrine\Common\Collections\Criteria;
use Symfony\Component\HttpFoundation\Request;
/**
* Controller for addresses associated with person
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
* @author Champs Libres <info@champs-libres.coop>
*/
class PersonAddressController extends Controller
{
public function editAction($person_id, $address_id)
{
$person = $this->getDoctrine()->getManager()
->getRepository('ChillPersonBundle:Person')
->find($person_id);
if ($person === NULL) {
throw $this->createNotFoundException("Person with id $person_id not"
. " found ");
}
$this->denyAccessUnlessGranted('CHILL_PERSON_UPDATE', $person,
"You are not allowed to edit this person.");
$address = $this->findAddressById($person, $address_id);
$form = $this->createEditForm($person, $address);
return $this->render('ChillPersonBundle:Address:edit.html.twig', array(
'person' => $person,
'address' => $address,
'form' => $form->createView()
));
}
public function updateAction($person_id, $address_id, Request $request)
{
$person = $this->getDoctrine()->getManager()
->getRepository('ChillPersonBundle:Person')
->find($person_id);
if ($person === NULL) {
throw $this->createNotFoundException("Person with id $person_id not"
. " found ");
}
$this->denyAccessUnlessGranted('CHILL_PERSON_UPDATE', $person,
"You are not allowed to edit this person.");
$address = $this->findAddressById($person, $address_id);
$form = $this->createEditForm($person, $address);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->getDoctrine()->getManager()
->flush();
$this->addFlash('success', $this->get('translator')->trans(
"The address has been successfully updated"));
return $this->redirectToRoute('chill_person_view', array(
'person_id' => $person->getId()
));
}
return $this->render('ChillPersonBundle:Address:edit.html.twig', array(
'person' => $person,
'address' => $address,
'form' => $form->createView()
));
}
/**
*
* @param Person $person
* @param Address $address
* @return \Symfony\Component\Form\Form
*/
protected function createEditForm(Person $person, Address $address)
{
$form = $this->createForm(AddressType::class, $address, array(
'method' => 'POST',
'action' => $this->generateUrl('chill_person_address_update', array(
'person_id' => $person->getId(),
'address_id' => $address->getId()
))
));
$form->add('submit', 'submit', array(
'label' => 'Submit'
));
return $form;
}
/**
*
* @param Person $person
* @param int $address_id
* @return Address
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException if the address id does not exists or is not associated with given person
*/
protected function findAddressById(Person $person, $address_id)
{
// filtering address
$criteria = Criteria::create()
->where(Criteria::expr()->eq('id', $address_id))
->setMaxResults(1);
$addresses = $person->getAddresses()->matching($criteria);
if (count($addresses) === 0) {
throw $this->createNotFoundException("Address with id $address_id "
. "matching person $person_id not found ");
}
return $addresses->first();
}
}

View File

@ -70,6 +70,14 @@ chill_person_accompanying_period_open:
path: /{_locale}/person/{person_id}/accompanying-period/open path: /{_locale}/person/{person_id}/accompanying-period/open
defaults: { _controller: ChillPersonBundle:AccompanyingPeriod:open } defaults: { _controller: ChillPersonBundle:AccompanyingPeriod:open }
chill_person_address_edit:
path: /{_locale}/person/{person_id}/address/{address_id}/edit
defaults: { _controller: ChillPersonBundle:PersonAddress:edit }
chill_person_address_update:
path: /{_locale}/person/{person_id}/address/{address_id}/update
defaults: { _controller: ChillPersonBundle:PersonAddress:update }
chill_person_export: chill_person_export:
path: /{_locale}/person/export/ path: /{_locale}/person/export/
defaults: { _controller: ChillPersonBundle:Person:export } defaults: { _controller: ChillPersonBundle:Person:export }

View File

@ -70,8 +70,12 @@ Reset: 'Remise à zéro'
'Person details': 'Détails de la personne' 'Person details': 'Détails de la personne'
'Update details for %name%': 'Modifier détails de %name%' 'Update details for %name%': 'Modifier détails de %name%'
Accompanying period list: Périodes d'accompagnement Accompanying period list: Périodes d'accompagnement
#address
Since %date%: Depuis le %date% Since %date%: Depuis le %date%
No address given: Pas d'adresse renseignée No address given: Pas d'adresse renseignée
The address has been successfully updated: L'adresse a été mise à jour avec succès
Update address for %name%: Mettre à jour une adresse pour %name%
#timeline #timeline
'An accompanying period is opened for %person% on %date%': Une période d'accompagnement a été ouverte le %date% pour %person% 'An accompanying period is opened for %person% on %date%': Une période d'accompagnement a été ouverte le %date% pour %person%

View File

@ -0,0 +1,36 @@
{#
* Copyright (C) 2014, Champs Libres Cooperative SCRLFS, <http://www.champs-libres.coop>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
#}
{% extends "ChillPersonBundle::layout.html.twig" %}
{% set activeRouteKey = '' %}
{% block title %}{{ 'Update address for %name%'|trans({ '%name%': person.firstName|capitalize ~ ' ' ~ person.lastName } )|capitalize }}{% endblock %}
{% block personcontent %}
{{ form_start(form) }}
{{ form_row(form.streetAddress1) }}
{{ form_row(form.streetAddress2) }}
{{ form_row(form.postCode) }}
{{ form_row(form.validFrom) }}
{{ form_row(form.submit, { 'attr' : { 'class': 'sc-button bt-edit' } } ) }}
{{ form_end(form) }}
{% endblock personcontent %}

View File

@ -173,6 +173,9 @@ This view should receive those arguments:
<dd> <dd>
{%- if person.lastAddress is not empty -%} {%- if person.lastAddress is not empty -%}
{{ address._render(person.lastAddress) }} {{ address._render(person.lastAddress) }}
<a href="{{ path('chill_person_address_edit', { 'person_id': person.id, 'address_id' : person.lastAddress.id } ) }}">
{{ 'Edit'|trans }}
</a>
{%- else -%} {%- else -%}
<span class=".chill-no-data-statement">{{ 'No address given'|trans }}</span> <span class=".chill-no-data-statement">{{ 'No address given'|trans }}</span>
{%- endif -%} {%- endif -%}