chill-bundles/src/Bundle/ChillPersonBundle/Controller/ReassignAccompanyingPeriodController.php

222 lines
7.7 KiB
PHP

<?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\Entity\User;
use Chill\MainBundle\Pagination\PaginatorFactory;
use Chill\MainBundle\Repository\UserRepository;
use Chill\MainBundle\Templating\Entity\UserRender;
use Chill\PersonBundle\Repository\AccompanyingPeriodACLAwareRepositoryInterface;
use Chill\PersonBundle\Repository\AccompanyingPeriodRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\CallbackTransformer;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Templating\EngineInterface;
use function is_int;
class ReassignAccompanyingPeriodController extends AbstractController
{
private AccompanyingPeriodACLAwareRepositoryInterface $accompanyingPeriodACLAwareRepository;
private AccompanyingPeriodRepository $courseRepository;
private EntityManagerInterface $em;
private EngineInterface $engine;
private FormFactoryInterface $formFactory;
private PaginatorFactory $paginatorFactory;
private Security $security;
private UserRender $userRender;
private UserRepository $userRepository;
public function __construct(
AccompanyingPeriodACLAwareRepositoryInterface $accompanyingPeriodACLAwareRepository,
UserRepository $userRepository,
AccompanyingPeriodRepository $courseRepository,
EngineInterface $engine,
FormFactoryInterface $formFactory,
PaginatorFactory $paginatorFactory,
Security $security,
UserRender $userRender,
EntityManagerInterface $em
) {
$this->accompanyingPeriodACLAwareRepository = $accompanyingPeriodACLAwareRepository;
$this->engine = $engine;
$this->formFactory = $formFactory;
$this->paginatorFactory = $paginatorFactory;
$this->security = $security;
$this->userRepository = $userRepository;
$this->userRender = $userRender;
$this->courseRepository = $courseRepository;
$this->em = $em;
}
/**
* @Route("/{_locale}/person/accompanying-periods/reassign", name="chill_course_list_reassign")
*/
public function listAction(Request $request): Response
{
if (!$this->security->isGranted('ROLE_USER') || !$this->security->getUser() instanceof User) {
throw new AccessDeniedException();
}
$form = $this->buildFilterForm();
$form->handleRequest($request);
$userFrom = $form['user']->getData();
$total = $this->accompanyingPeriodACLAwareRepository->countByUserOpenedAccompanyingPeriod($userFrom);
$paginator = $this->paginatorFactory->create($total);
$periods = $this->accompanyingPeriodACLAwareRepository
->findByUserOpenedAccompanyingPeriod(
$userFrom,
['openingDate' => 'ASC'],
$paginator->getItemsPerPage(),
$paginator->getCurrentPageFirstItemNumber()
);
$periodIds = [];
foreach ($periods as $period) {
$periodIds[] = $period->getId();
}
// Create an array of period id's to pass into assignForm hiddenfield
$assignForm = $this->buildReassignForm($periodIds, $userFrom);
$assignForm->handleRequest($request);
if ($assignForm->isSubmitted()) {
if ($assignForm->isSubmitted()) {
$assignPeriodIds = json_decode($assignForm->get('periods')->getData(), true);
$userTo = $assignForm->get('userTo')->getData();
$userFrom = $assignForm->get('userFrom')->getData();
foreach ($assignPeriodIds as $periodId) {
$period = $this->courseRepository->find($periodId);
if ($period->getUser() === $userFrom) {
$period->setUser($userTo);
}
}
$this->em->flush();
// redirect to the first page
return $this->redirectToRoute('chill_course_list_reassign', [
'form' => ['user' => $userFrom->getId()],
]);
}
}
return new Response(
$this->engine->render('@ChillPerson/AccompanyingPeriod/reassign_list.html.twig', [
'paginator' => $paginator,
'periods' => $periods,
'form' => $form->createView(),
'assignForm' => $assignForm->createView(),
])
);
}
private function buildFilterForm(): FormInterface
{
$data = [
'user' => null,
];
$builder = $this->formFactory->createBuilder(FormType::class, $data, [
'method' => 'get', 'csrf_protection' => false, ]);
$builder
->add('user', EntityType::class, [
'class' => User::class, // pickUserType or PickDyamicUserType
'choices' => $this->userRepository->findByActive(['username' => 'ASC']),
'choice_label' => function (User $u) {
return $this->userRender->renderString($u, []);
},
'multiple' => false,
'label' => 'User',
'required' => false,
]);
return $builder->getForm();
}
private function buildReassignForm(array $periodIds, ?User $userFrom): FormInterface
{
$defaultData = [
'userFrom' => $userFrom,
'periods' => json_encode($periodIds),
'assignTo' => null,
];
$builder = $this->formFactory->createBuilder(FormType::class, $defaultData);
$builder
->add('periods', HiddenType::class)
->add('userFrom', HiddenType::class)
->add('userTo', EntityType::class, [
'class' => User::class, // PickUserType
'choices' => $this->userRepository->findByActive(['username' => 'ASC']),
'choice_label' => function (User $u) {
return $this->userRender->renderString($u, []);
},
'placeholder' => 'Choose a user to reassign to',
'multiple' => false,
'label' => 'User',
'required' => true,
// add a constraint: userFrom is not equal to userTo
//'constraints' => NotEqualToh
]);
$builder->get('userFrom')->addModelTransformer(new CallbackTransformer(
static function (?User $user) {
if (null === $user) {
return '';
}
return $user->getId();
},
function (?string $id) {
if (null === $id) {
return null;
}
if (!is_int((int) $id)) {
throw new TransformationFailedException('the user id is not a numeric');
}
return $this->userRepository->find((int) $id);
}
));
return $builder->getForm();
}
}