mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 23:23:51 +00:00
Household/composition add + fixes household composition editor
This commit is contained in:
@@ -0,0 +1,149 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Chill\PersonBundle\Controller;
|
||||
|
||||
use Chill\MainBundle\Pagination\PaginatorFactory;
|
||||
use Chill\PersonBundle\Entity\Household\Household;
|
||||
use Chill\PersonBundle\Entity\Household\HouseholdComposition;
|
||||
use Chill\PersonBundle\Form\HouseholdCompositionType;
|
||||
use Chill\PersonBundle\Repository\Household\HouseholdCompositionRepository;
|
||||
use Chill\PersonBundle\Security\Authorization\HouseholdVoter;
|
||||
use DateTimeImmutable;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\Form\FormFactoryInterface;
|
||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
|
||||
use Symfony\Component\Security\Core\Security;
|
||||
use Symfony\Component\Templating\EngineInterface;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
|
||||
class HouseholdCompositionController
|
||||
{
|
||||
private EngineInterface $engine;
|
||||
|
||||
private EntityManagerInterface $entityManager;
|
||||
|
||||
private FormFactoryInterface $formFactory;
|
||||
|
||||
private HouseholdCompositionRepository $householdCompositionRepository;
|
||||
|
||||
private PaginatorFactory $paginatorFactory;
|
||||
|
||||
private Security $security;
|
||||
|
||||
private TranslatorInterface $translator;
|
||||
|
||||
private UrlGeneratorInterface $urlGenerator;
|
||||
|
||||
public function __construct(
|
||||
Security $security,
|
||||
HouseholdCompositionRepository $householdCompositionRepository,
|
||||
PaginatorFactory $paginatorFactory,
|
||||
FormFactoryInterface $formFactory,
|
||||
EntityManagerInterface $entityManager,
|
||||
TranslatorInterface $translator,
|
||||
EngineInterface $engine,
|
||||
UrlGeneratorInterface $urlGenerator
|
||||
) {
|
||||
$this->security = $security;
|
||||
$this->householdCompositionRepository = $householdCompositionRepository;
|
||||
$this->paginatorFactory = $paginatorFactory;
|
||||
$this->formFactory = $formFactory;
|
||||
$this->entityManager = $entityManager;
|
||||
$this->translator = $translator;
|
||||
$this->engine = $engine;
|
||||
$this->urlGenerator = $urlGenerator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @Route("/{_locale}/person/household/{id}/composition/index", name="chill_person_household_composition_index")
|
||||
*/
|
||||
public function index(Household $household, Request $request): Response
|
||||
{
|
||||
if (!$this->security->isGranted(HouseholdVoter::SEE, $household)) {
|
||||
throw new AccessDeniedException('not allowed to edit an household');
|
||||
}
|
||||
|
||||
$count = $this->householdCompositionRepository->countByHousehold($household);
|
||||
$paginator = $this->paginatorFactory->create($count);
|
||||
$compositions = $this->householdCompositionRepository->findByHousehold(
|
||||
$household,
|
||||
['startDate' => 'DESC', 'id' => 'DESC'],
|
||||
$paginator->getItemsPerPage(),
|
||||
$paginator->getCurrentPageFirstItemNumber()
|
||||
);
|
||||
|
||||
if ($this->security->isGranted(HouseholdVoter::EDIT, $household)) {
|
||||
$isEdit = $request->query->has('edit');
|
||||
|
||||
if ($isEdit) {
|
||||
$householdCompositions = $household->getCompositions()->filter(static function (HouseholdComposition $composition) use ($request) {
|
||||
return $composition->getId() === $request->query->getInt('edit');
|
||||
});
|
||||
|
||||
if ($householdCompositions->count() !== 1) {
|
||||
throw new BadRequestHttpException('could not find the composition with this id associated to the household');
|
||||
}
|
||||
$householdComposition = $householdCompositions->first();
|
||||
} else {
|
||||
$householdComposition = (new HouseholdComposition())
|
||||
->setStartDate(new DateTimeImmutable());
|
||||
}
|
||||
$form = $this->formFactory->create(HouseholdCompositionType::class, $householdComposition);
|
||||
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
if (!$isEdit) {
|
||||
$this->entityManager->persist($householdComposition);
|
||||
$household->addComposition($householdComposition);
|
||||
}
|
||||
|
||||
$this->entityManager->flush();
|
||||
|
||||
$request->getSession()->getFlashBag()->add(
|
||||
'success',
|
||||
$this->translator->trans('household_composition.Composition added')
|
||||
);
|
||||
|
||||
return new RedirectResponse(
|
||||
$this->urlGenerator->generate('chill_person_household_composition_index', [
|
||||
'id' => $household->getId(),
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
if ($form->isSubmitted() && !$form->isValid()) {
|
||||
$request->getSession()->getFlashBag()->add(
|
||||
'warning',
|
||||
$this->translator->trans('This form contains errors')
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return new Response($this->engine->render(
|
||||
'@ChillPerson/HouseholdComposition/index.html.twig',
|
||||
[
|
||||
'household' => $household,
|
||||
'compositions' => $compositions,
|
||||
'form' => isset($form) ? $form->createView() : null,
|
||||
'isPosted' => isset($form) ? $form->isSubmitted() : false,
|
||||
'editId' => $request->query->getInt('edit', -1),
|
||||
]
|
||||
));
|
||||
}
|
||||
}
|
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Chill\PersonBundle\Controller;
|
||||
|
||||
use Chill\MainBundle\CRUD\Controller\ApiController;
|
||||
use Doctrine\ORM\QueryBuilder;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use UnexpectedValueException;
|
||||
|
||||
class HouseholdCompositionTypeApiController extends ApiController
|
||||
{
|
||||
/**
|
||||
* @param QueryBuilder $query
|
||||
*/
|
||||
protected function customizeQuery(string $action, Request $request, $query): void
|
||||
{
|
||||
switch ($action) {
|
||||
case '_index':
|
||||
$query->andWhere($query->expr()->eq('e.active', "'TRUE'"));
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new UnexpectedValueException('unexepcted action: ' . $action);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user