fix folder name

This commit is contained in:
2021-03-18 13:37:13 +01:00
parent a2f6773f5a
commit eaa0ad925f
1578 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,441 @@
<?php
/*
* Chill is a software for social workers
*
* Copyright (C) 2014-2015, 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 Chill\PersonBundle\Privacy\PrivacyEvent;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Form\AccompanyingPeriodType;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Doctrine\Common\Collections\Criteria;
use Chill\PersonBundle\Security\Authorization\PersonVoter;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
/**
* Class AccompanyingPeriodController
*
* @package Chill\PersonBundle\Controller
*/
class AccompanyingPeriodController extends AbstractController
{
/**
* @var EventDispatcherInterface
*/
protected $eventDispatcher;
/**
* ReportController constructor.
*
* @param EventDispatcherInterface $eventDispatcher
*/
public function __construct(EventDispatcherInterface $eventDispatcher)
{
$this->eventDispatcher = $eventDispatcher;
}
/**
* @param $person_id
* @return Response
*/
public function listAction($person_id){
$person = $this->_getPerson($person_id);
$event = new PrivacyEvent($person, array(
'element_class' => AccompanyingPeriod::class,
'action' => 'list'
));
$this->eventDispatcher->dispatch(PrivacyEvent::PERSON_PRIVACY_EVENT, $event);
return $this->render('ChillPersonBundle:AccompanyingPeriod:list.html.twig',
array('accompanying_periods' => $person->getAccompanyingPeriodsOrdered(),
'person' => $person));
}
/**
* @param $person_id
* @param Request $request
* @return \Symfony\Component\HttpFoundation\RedirectResponse|Response
*/
public function createAction($person_id, Request $request)
{
$person = $this->_getPerson($person_id);
$this->denyAccessUnlessGranted(PersonVoter::UPDATE, $person,
'You are not allowed to update this person');
$accompanyingPeriod = new AccompanyingPeriod(new \DateTime());
$accompanyingPeriod->setClosingDate(new \DateTime());
$person->addAccompanyingPeriod(
$accompanyingPeriod);
$form = $this->createForm(
AccompanyingPeriodType::class,
$accompanyingPeriod,
[
'period_action' => 'create',
'center' => $person->getCenter()
]);
if ($request->getMethod() === 'POST') {
$form->handleRequest($request);
$errors = $this->_validatePerson($person);
$flashBag = $this->get('session')->getFlashBag();
if ($form->isValid(array('Default', 'closed'))
&& count($errors) === 0) {
$em = $this->getDoctrine()->getManager();
$em->persist($accompanyingPeriod);
$em->flush();
$flashBag->add('success',
$this->get('translator')->trans(
'A period has been created.'));
return $this->redirect($this->generateUrl('chill_person_accompanying_period_list',
array('person_id' => $person->getId())));
} else {
$flashBag->add('error', $this->get('translator')
->trans('Error! Period not created!'));
foreach($errors as $error) {
$flashBag->add('info', $error->getMessage());
}
}
}
return $this->render('ChillPersonBundle:AccompanyingPeriod:form.html.twig',
array(
'form' => $form->createView(),
'person' => $person,
'accompanying_period' => $accompanyingPeriod
)
);
}
/**
* @param $person_id
* @param $period_id
* @param Request $request
* @return \Symfony\Component\HttpFoundation\RedirectResponse|Response|\Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*/
public function updateAction($person_id, $period_id, Request $request){
$em = $this->getDoctrine()->getManager();
$accompanyingPeriod = $em->getRepository('ChillPersonBundle:AccompanyingPeriod')
->find($period_id);
if ($accompanyingPeriod === null) {
return $this->createNotFoundException("Period with id ".$period_id.
" is not found");
}
$person = $accompanyingPeriod->getPerson();
$this->denyAccessUnlessGranted(PersonVoter::UPDATE, $person,
'You are not allowed to update this person');
$form = $this->createForm(AccompanyingPeriodType::class,
$accompanyingPeriod, array('period_action' => 'update',
'center' => $person->getCenter()));
if ($request->getMethod() === 'POST') {
$form->handleRequest($request);
$errors = $this->_validatePerson($person);
$flashBag = $this->get('session')->getFlashBag();
if ($form->isValid(array('Default', 'closed'))
&& count($errors) === 0) {
$em->flush();
$flashBag->add('success',
$this->get('translator')->trans(
'An accompanying period has been updated.'));
return $this->redirect($this->generateUrl('chill_person_accompanying_period_list',
array('person_id' => $person->getId())));
} else {
$flashBag->add('error', $this->get('translator')
->trans('Error when updating the period'));
foreach($errors as $error) {
$flashBag->add('info', $error->getMessage());
}
}
}
return $this->render('ChillPersonBundle:AccompanyingPeriod:form.html.twig',
array(
'form' => $form->createView(),
'person' => $person,
'accompanying_period' => $accompanyingPeriod
) );
}
/**
* @param $person_id
* @param Request $request
* @return \Symfony\Component\HttpFoundation\RedirectResponse|Response
* @throws \Exception
*/
public function closeAction($person_id, Request $request)
{
$person = $this->_getPerson($person_id);
$this->denyAccessUnlessGranted(PersonVoter::UPDATE, $person,
'You are not allowed to update this person');
if ($person->isOpen() === false) {
$this->get('session')->getFlashBag()
->add('error', $this->get('translator')
->trans('Beware period is closed',
array('%name%' => $person->__toString())));
return $this->redirect(
$this->generateUrl('chill_person_accompanying_period_list', array(
'person_id' => $person->getId()
)));
}
$current = $person->getCurrentAccompanyingPeriod();
$form = $this->createForm(AccompanyingPeriodType::class, $current, array(
'period_action' => 'close',
'center' => $person->getCenter()
));
if ($request->getMethod() === 'POST') {
$form->handleRequest($request);
if ($form->isValid()){
$person->close($current);
$errors = $this->_validatePerson($person);
if (count($errors) === 0) {
$this->get('session')->getFlashBag()
->add('success', $this->get('translator')
->trans('An accompanying period has been closed.',
array('%name%' => $person->__toString())));
$this->getDoctrine()->getManager()->flush();
return $this->redirect(
$this->generateUrl('chill_person_accompanying_period_list', array(
'person_id' => $person->getId()
))
);
} else {
$this->get('session')->getFlashBag()
->add('error', $this->get('translator')
->trans('Error! Period not closed!'));
foreach ($errors as $error) {
$this->get('session')->getFlashBag()
->add('info', $error->getMessage());
}
}
} else { //if form is not valid
$this->get('session')->getFlashBag()
->add('error',
$this->get('translator')
->trans('Pediod closing form is not valid')
);
foreach ($form->getErrors() as $error) {
$this->get('session')->getFlashBag()
->add('info', $error->getMessage());
}
}
}
return $this->render('ChillPersonBundle:AccompanyingPeriod:form.html.twig',
array(
'form' => $form->createView(),
'person' => $person,
'accompanying_period' => $current
));
}
/**
* @param Person $person
* @return \Symfony\Component\Validator\ConstraintViolationListInterface
*/
private function _validatePerson(Person $person) {
$errors = $this->get('validator')->validate($person, null,
array('Default'));
$errors_accompanying_period = $this->get('validator')->validate($person, null,
array('accompanying_period_consistent'));
foreach($errors_accompanying_period as $error ) {
$errors->add($error);
}
return $errors;
}
/**
* @param $person_id
* @param Request $request
* @return \Symfony\Component\HttpFoundation\RedirectResponse|Response
*/
public function openAction($person_id, Request $request) {
$person = $this->_getPerson($person_id);
$this->denyAccessUnlessGranted(PersonVoter::UPDATE, $person,
'You are not allowed to update this person');
//in case the person is already open
if ($person->isOpen()) {
$this->get('session')->getFlashBag()
->add('error', $this->get('translator')
->trans('Error! Period %name% is not closed ; it can be open',
array('%name%' => $person->__toString())));
return $this->redirect(
$this->generateUrl('chill_person_accompanying_period_list', array(
'person_id' => $person->getId()
)));
}
$accompanyingPeriod = new AccompanyingPeriod(new \DateTime());
$form = $this->createForm(AccompanyingPeriodType::class,
$accompanyingPeriod, array('period_action' => 'open',
'center' => $person->getCenter()));
if ($request->getMethod() === 'POST') {
$form->handleRequest($request);
if ($form->isValid()) {
$person->open($accompanyingPeriod);
$errors = $this->_validatePerson($person);
if (count($errors) <= 0) {
$this->get('session')->getFlashBag()
->add('success', $this->get('translator')
->trans('An accompanying period has been opened.',
array('%name%' => $person->__toString())));
$this->getDoctrine()->getManager()->flush();
return $this->redirect(
$this->generateUrl('chill_person_accompanying_period_list', array(
'person_id' => $person->getId()
)));
} else {
$this->get('session')->getFlashBag()
->add('error', $this->get('translator')
->trans('Period not opened'));
foreach ($errors as $error) {
$this->get('session')->getFlashBag()
->add('info', $error->getMessage());
}
}
} else { // if errors in forms
$this->get('session')->getFlashBag()
->add('error', $this->get('translator')
->trans('Period not opened : form is invalid'));
}
}
return $this->render('ChillPersonBundle:AccompanyingPeriod:form.html.twig',
array('form' => $form->createView(),
'person' => $person,
'accompanying_period' => $accompanyingPeriod));
}
/**
* @param $person_id
* @param $period_id
* @param Request $request
* @return object|\Symfony\Component\HttpFoundation\RedirectResponse|Response
*/
public function reOpenAction($person_id, $period_id, Request $request)
{
$person = $this->_getPerson($person_id);
$criteria = Criteria::create();
$criteria->where($criteria->expr()->eq('id', $period_id));
/* @var $period AccompanyingPeriod */
$period = $person->getAccompanyingPeriods()
->matching($criteria)
->first();
if ($period === NULL) {
throw $this->createNotFoundException('period not found');
}
$confirm = $request->query->getBoolean('confirm', false);
if ($confirm === true && $period->canBeReOpened()) {
$period->reOpen();
$this->_validatePerson($person);
$this->getDoctrine()->getManager()->flush();
$this->addFlash('success', $this->get('translator')->trans(
'The period has been re-opened'));
return $this->redirectToRoute('chill_person_accompanying_period_list',
array('person_id' => $person->getId()));
} elseif ($confirm === false && $period->canBeReOpened()) {
return $this->render('ChillPersonBundle:AccompanyingPeriod:re_open.html.twig', array(
'period' => $period,
'person' => $person
));
} else {
return (new Response())
->setStatusCode(Response::HTTP_BAD_REQUEST)
->setContent("You cannot re-open this period");
}
}
/**
*
* @param int $id
* @return Person
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException if the person is not found
*/
private function _getPerson($id) {
$person = $this->getDoctrine()->getManager()
->getRepository('ChillPersonBundle:Person')->find($id);
if ($person === null) {
throw $this->createNotFoundException('Person not found');
}
$this->denyAccessUnlessGranted(PersonVoter::SEE, $person,
"You are not allowed to see this person");
return $person;
}
}

View File

@@ -0,0 +1,55 @@
<?php
namespace Chill\PersonBundle\Controller;
use Chill\MainBundle\CRUD\Controller\CRUDController;
use Symfony\Component\HttpFoundation\Request;
use Chill\MainBundle\Pagination\PaginatorInterface;
/**
* Class AdminClosingMotiveController
* Controller for closing motives
*
* @package Chill\PersonBundle\Controller
*/
class AdminClosingMotiveController extends CRUDController
{
/**
* @param \Chill\MainBundle\CRUD\Controller\string|string $action
* @param Request $request
* @return object
*/
protected function createEntity($action, Request $request): object
{
$entity = parent::createEntity($action, $request);
if ($request->query->has('parent_id')) {
$parentId = $request->query->getInt('parent_id');
$parent = $this->getDoctrine()->getManager()
->getRepository($this->getEntityClass())
->find($parentId);
if (NULL === $parent) {
throw $this->createNotFoundException('parent id not found');
}
$entity->setParent($parent);
}
return $entity;
}
/**
* @param string $action
* @param \Doctrine\ORM\QueryBuilder|mixed $query
* @param Request $request
* @param PaginatorInterface $paginator
* @return \Doctrine\ORM\QueryBuilder|mixed
*/
protected function orderQuery(string $action, $query, Request $request, PaginatorInterface $paginator)
{
/** @var \Doctrine\ORM\QueryBuilder $query */
return $query->orderBy('e.ordering', 'ASC');
}
}

View File

@@ -0,0 +1,31 @@
<?php
namespace Chill\PersonBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
/**
* Class AdminController
*
* @package Chill\PersonBundle\Controller
*/
class AdminController extends AbstractController
{
/**
* @param $_locale
* @return \Symfony\Component\HttpFoundation\Response
*/
public function indexAction($_locale)
{
return $this->render('ChillPersonBundle:Admin:layout.html.twig', []);
}
/**
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
public function redirectToAdminIndexAction()
{
return $this->redirectToRoute('chill_main_admin_central');
}
}

View File

@@ -0,0 +1,15 @@
<?php
namespace Chill\PersonBundle\Controller;
use Chill\MainBundle\CRUD\Controller\CRUDController;
/**
* Class AdminMaritalStatusController
*
* @package Chill\PersonBundle\Controller
*/
class AdminMaritalStatusController extends CRUDController
{
// for minimal cases, nothing is required here !
}

View File

@@ -0,0 +1,316 @@
<?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\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
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;
/**
* Class PersonAddressController
* Controller for addresses associated with person
*
* @package Chill\PersonBundle\Controller
* @author Julien Fastré <julien.fastre@champs-libres.coop>
* @author Champs Libres <info@champs-libres.coop>
*/
class PersonAddressController extends AbstractController
{
public function listAction($person_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_SEE',
$person,
"You are not allowed to edit this person."
);
return $this->render('ChillPersonBundle:Address:list.html.twig', array(
'person' => $person
));
}
public function newAction($person_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 = new Address();
$form = $this->createCreateForm($person, $address);
return $this->render('ChillPersonBundle:Address:new.html.twig', array(
'person' => $person,
'form' => $form->createView()
));
}
public function createAction($person_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 = new Address();
$form = $this->createCreateForm($person, $address);
$form->handleRequest($request);
$person->addAddress($address);
if ($form->isSubmitted() && $form->isValid()) {
$validatePersonErrors = $this->validatePerson($person);
if (count($validatePersonErrors) !== 0) {
foreach ($validatePersonErrors as $error) {
$this->addFlash('error', $error->getMessage());
}
} elseif ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->flush();
$this->addFlash(
'success',
$this->get('translator')->trans('The new address was created successfully')
);
return $this->redirectToRoute('chill_person_address_list', array(
'person_id' => $person->getId()
));
} else {
$this->addFlash('error', $this->get('translator')
->trans('Error! Address not created!'));
}
}
return $this->render('ChillPersonBundle:Address:new.html.twig', array(
'person' => $person,
'form' => $form->createView()
));
}
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() and $form->isValid()) {
$validatePersonErrors = $this->validatePerson($person);
if (count($validatePersonErrors) !== 0) {
foreach ($validatePersonErrors as $error) {
$this->addFlash('error', $error->getMessage());
}
} elseif ($form->isValid()) {
$this->getDoctrine()->getManager()
->flush();
$this->addFlash('success', $this->get('translator')->trans(
"The address has been successfully updated"
));
return $this->redirectToRoute('chill_person_address_list', array(
'person_id' => $person->getId()
));
} else {
$this->addFlash('error', $this->get('translator')
->trans('Error when updating the period'));
}
}
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()
)),
'has_no_address' => true
));
$form->add('submit', SubmitType::class, array(
'label' => 'Submit'
));
return $form;
}
/**
*
* @param Person $person
* @param Address $address
* @return \Symfony\Component\Form\Form
*/
protected function createCreateForm(Person $person, Address $address)
{
$form = $this->createForm(AddressType::class, $address, array(
'method' => 'POST',
'action' => $this->generateUrl('chill_person_address_create', array(
'person_id' => $person->getId()
)),
'has_no_address' => true
));
$form->add('submit', SubmitType::class, 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();
}
/**
* @param Chill\PersonBundle\Entity\Person $person
* @return \Symfony\Component\Validator\ConstraintViolationListInterface
*/
private function validatePerson(Person $person)
{
$errors = $this->get('validator')
->validate($person, null, array('Default'));
$errors_addresses_consistent = $this->get('validator')
->validate($person, null, array('addresses_consistent'));
foreach($errors_addresses_consistent as $error) {
$errors->add($error);
}
return $errors;
}
}

View File

@@ -0,0 +1,416 @@
<?php
/*
* Chill is a software for social workers
*
* Copyright (C) 2015, 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 Chill\PersonBundle\Privacy\PrivacyEvent;
use Psr\Log\LoggerInterface;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Form\PersonType;
use Chill\PersonBundle\Form\CreationPersonType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Role\Role;
use Chill\PersonBundle\Security\Authorization\PersonVoter;
use Chill\PersonBundle\Search\SimilarPersonMatcher;
use Symfony\Component\Translation\TranslatorInterface;
use Chill\MainBundle\Search\SearchProvider;
use Chill\PersonBundle\Repository\PersonRepository;
use Chill\PersonBundle\Config\ConfigPersonAltNamesHelper;
/**
* Class PersonController
*
* @package Chill\PersonBundle\Controller
*/
class PersonController extends AbstractController
{
/**
*
* @var SimilarPersonMatcher
*/
protected $similarPersonMatcher;
/**
*
* @var TranslatorInterface
*/
protected $translator;
/**
* @var EventDispatcherInterface
*/
protected $eventDispatcher;
/**
*
* @var PersonRepository;
*/
protected $personRepository;
/**
*
* @var ConfigPersonAltNamesHelper
*/
protected $configPersonAltNameHelper;
/**
* @var \Psr\Log\LoggerInterface
*/
private $logger;
public function __construct(
SimilarPersonMatcher $similarPersonMatcher,
TranslatorInterface $translator,
EventDispatcherInterface $eventDispatcher,
PersonRepository $personRepository,
ConfigPersonAltNamesHelper $configPersonAltNameHelper,
LoggerInterface $logger
) {
$this->similarPersonMatcher = $similarPersonMatcher;
$this->translator = $translator;
$this->eventDispatcher = $eventDispatcher;
$this->configPersonAltNameHelper = $configPersonAltNameHelper;
$this->personRepository = $personRepository;
$this->logger = $logger;
}
public function getCFGroup()
{
$cFGroup = null;
$em = $this->getDoctrine()->getManager();
$cFDefaultGroup = $em->getRepository("ChillCustomFieldsBundle:CustomFieldsDefaultGroup")
->findOneByEntity("Chill\PersonBundle\Entity\Person");
if($cFDefaultGroup) {
$cFGroup = $cFDefaultGroup->getCustomFieldsGroup();
}
return $cFGroup;
}
public function viewAction($person_id)
{
$person = $this->_getPerson($person_id);
if ($person === null) {
throw $this->createNotFoundException("Person with id $person_id not"
. " found on this server");
}
$this->denyAccessUnlessGranted('CHILL_PERSON_SEE', $person,
"You are not allowed to see this person.");
$event = new PrivacyEvent($person);
$this->eventDispatcher->dispatch(PrivacyEvent::PERSON_PRIVACY_EVENT, $event);
return $this->render('ChillPersonBundle:Person:view.html.twig',
array(
"person" => $person,
"cFGroup" => $this->getCFGroup(),
"alt_names" => $this->configPersonAltNameHelper->getChoices(),
));
}
public function editAction($person_id)
{
$person = $this->_getPerson($person_id);
if ($person === null) {
return $this->createNotFoundException();
}
$this->denyAccessUnlessGranted('CHILL_PERSON_UPDATE', $person,
'You are not allowed to edit this person');
$form = $this->createForm(PersonType::class, $person,
array(
"action" => $this->generateUrl('chill_person_general_update',
array("person_id" => $person_id)),
"cFGroup" => $this->getCFGroup()
)
);
return $this->render('ChillPersonBundle:Person:edit.html.twig',
array('person' => $person, 'form' => $form->createView()));
}
public function updateAction($person_id, Request $request)
{
$person = $this->_getPerson($person_id);
if ($person === null) {
return $this->createNotFoundException();
}
$this->denyAccessUnlessGranted('CHILL_PERSON_UPDATE', $person,
'You are not allowed to edit this person');
$form = $this->createForm(PersonType::class, $person,
array("cFGroup" => $this->getCFGroup()));
if ($request->getMethod() === 'POST') {
$form->handleRequest($request);
if ( ! $form->isValid() ) {
$this->get('session')
->getFlashBag()->add('error', $this->translator
->trans('This form contains errors'));
return $this->render('ChillPersonBundle:Person:edit.html.twig',
array('person' => $person,
'form' => $form->createView()));
}
$this->get('session')->getFlashBag()
->add('success',
$this->get('translator')
->trans('The person data has been updated')
);
$em = $this->getDoctrine()->getManager();
$em->flush();
$url = $this->generateUrl('chill_person_view', array(
'person_id' => $person->getId()
));
return $this->redirect($url);
}
}
public function newAction()
{
// this is a dummy default center.
$defaultCenter = $this->get('security.token_storage')
->getToken()
->getUser()
->getGroupCenters()[0]
->getCenter();
$person = (new Person(new \DateTime('now')))
->setCenter($defaultCenter);
$form = $this->createForm(
CreationPersonType::class,
$person,
array(
'action' => $this->generateUrl('chill_person_review'),
'form_status' => CreationPersonType::FORM_NOT_REVIEWED
));
return $this->_renderNewForm($form);
}
private function _renderNewForm($form)
{
return $this->render('ChillPersonBundle:Person:create.html.twig',
array(
'form' => $form->createView()
));
}
/**
*
* @param type $form
* @return \Chill\PersonBundle\Entity\Person
*/
private function _bindCreationForm($form)
{
/**
* @var Person
*/
$person = $form->getData();
$periods = $person->getAccompanyingPeriodsOrdered();
$period = $periods[0];
$period->setOpeningDate($form['creation_date']->getData());
// $person = new Person($form['creation_date']->getData());
//
// $person->setFirstName($form['firstName']->getData())
// ->setLastName($form['lastName']->getData())
// ->setGender($form['gender']->getData())
// ->setBirthdate($form['birthdate']->getData())
// ->setCenter($form['center']->getData())
// ;
return $person;
}
/**
*
* @param \Chill\PersonBundle\Entity\Person $person
* @return \Symfony\Component\Validator\ConstraintViolationListInterface
*/
private function _validatePersonAndAccompanyingPeriod(Person $person)
{
$errors = $this->get('validator')
->validate($person, null, array('creation'));
//validate accompanying periods
$periods = $person->getAccompanyingPeriods();
foreach ($periods as $period) {
$period_errors = $this->get('validator')
->validate($period);
//group errors :
foreach($period_errors as $error) {
$errors->add($error);
}
}
return $errors;
}
public function reviewAction(Request $request)
{
if ($request->getMethod() !== 'POST') {
$r = new Response("You must send something to review the creation of a new Person");
$r->setStatusCode(400);
return $r;
}
$form = $this->createForm(
//CreationPersonType::NAME,
CreationPersonType::class,
new Person(),
array(
'action' => $this->generateUrl('chill_person_create'),
'form_status' => CreationPersonType::FORM_BEING_REVIEWED
));
$form->handleRequest($request);
$person = $this->_bindCreationForm($form);
$errors = $this->_validatePersonAndAccompanyingPeriod($person);
$this->logger->info(sprintf('Person created with %d errors ', count($errors)));
if ($errors->count() > 0) {
$this->logger->info('The created person has errors');
$flashBag = $this->get('session')->getFlashBag();
$translator = $this->get('translator');
$flashBag->add('error', $translator->trans('The person data are not valid'));
foreach($errors as $error) {
$flashBag->add('info', $error->getMessage());
}
$form = $this->createForm(
CreationPersonType::NAME,
$person,
array(
'action' => $this->generateUrl('chill_person_review'),
'form_status' => CreationPersonType::FORM_NOT_REVIEWED
));
$form->handleRequest($request);
return $this->_renderNewForm($form);
} else {
$this->logger->info('Person created without errors');
}
$alternatePersons = $this->similarPersonMatcher
->matchPerson($person);
if (count($alternatePersons) === 0) {
return $this->forward('ChillPersonBundle:Person:create');
}
$this->get('session')->getFlashBag()->add('info',
$this->get('translator')->trans(
'%nb% person with similar name. Please verify that this is a new person',
array('%nb%' => count($alternatePersons)))
);
return $this->render('ChillPersonBundle:Person:create_review.html.twig',
array(
'person' => $person,
'alternatePersons' => $alternatePersons,
'firstName' => $form['firstName']->getData(),
'lastName' => $form['lastName']->getData(),
'birthdate' => $form['birthdate']->getData(),
'gender' => $form['gender']->getData(),
'creation_date' => $form['creation_date']->getData(),
'form' => $form->createView()));
}
public function createAction(Request $request)
{
if ($request->getMethod() !== 'POST') {
$r = new Response('You must send something to create a person !');
$r->setStatusCode(400);
return $r;
}
$form = $this->createForm(CreationPersonType::class, null, array(
'form_status' => CreationPersonType::FORM_REVIEWED
));
$form->handleRequest($request);
$person = $this->_bindCreationForm($form);
$errors = $this->_validatePersonAndAccompanyingPeriod($person);
$this->denyAccessUnlessGranted('CHILL_PERSON_CREATE', $person,
'You are not allowed to create this person');
if ($errors->count() === 0) {
$em = $this->getDoctrine()->getManager();
$em->persist($person);
$em->flush();
return $this->redirect($this->generateUrl('chill_person_general_edit',
array('person_id' => $person->getId())));
} else {
$text = "this should not happen if you reviewed your submission\n";
foreach ($errors as $error) {
$text .= $error->getMessage()."\n";
}
$r = new Response($text);
$r->setStatusCode(400);
return $r;
}
}
/**
* easy getting a person by his id
* @return \Chill\PersonBundle\Entity\Person
*/
private function _getPerson($id)
{
$person = $this->personRepository->find($id);
return $person;
}
}

View File

@@ -0,0 +1,109 @@
<?php
/*
* Copyright (C) 2015 Champs-Libres Coopérative <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 Chill\PersonBundle\Privacy\PrivacyEvent;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Chill\MainBundle\Timeline\TimelineBuilder;
use Chill\MainBundle\Pagination\PaginatorFactory;
use Chill\PersonBundle\Security\Authorization\PersonVoter;
/**
* Class TimelinePersonController
*
* @package Chill\PersonBundle\Controller
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
class TimelinePersonController extends AbstractController
{
/**
* @var EventDispatcherInterface
*/
protected $eventDispatcher;
/**
*
* @var TimelineBuilder
*/
protected $timelineBuilder;
/**
*
* @var PaginatorFactory
*/
protected $paginatorFactory;
/**
* TimelinePersonController constructor.
*
* @param EventDispatcherInterface $eventDispatcher
*/
public function __construct(
EventDispatcherInterface $eventDispatcher,
TimelineBuilder $timelineBuilder,
PaginatorFactory $paginatorFactory
) {
$this->eventDispatcher = $eventDispatcher;
$this->timelineBuilder = $timelineBuilder;
$this->paginatorFactory = $paginatorFactory;
}
public function personAction(Request $request, $person_id)
{
$person = $this->getDoctrine()
->getRepository('ChillPersonBundle:Person')
->find($person_id);
if ($person === NULL) {
throw $this->createNotFoundException();
}
$this->denyAccessUnlessGranted(PersonVoter::SEE, $person);
$nbItems = $this->timelineBuilder->countItems('person',
[ 'person' => $person ]
);
$paginator = $this->paginatorFactory->create($nbItems);
$event = new PrivacyEvent($person, array('action' => 'timeline'));
$this->eventDispatcher->dispatch(PrivacyEvent::PERSON_PRIVACY_EVENT, $event);
return $this->render('ChillPersonBundle:Timeline:index.html.twig', array
(
'timeline' => $this->timelineBuilder->getTimelineHTML(
'person',
array('person' => $person),
$paginator->getCurrentPage()->getFirstItemNumber(),
$paginator->getItemsPerPage()
),
'person' => $person,
'nb_items' => $nbItems,
'paginator' => $paginator
)
);
}
}